/**
 * mobile-fix.css - iPhone 響應式修正
 * 解決表格溢出、Grid 固定列數、圖表並排等問題
 * 2026-03-25
 */

/* ===== 1. 表格水平滾動容器 ===== */
.table-wrapper {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 16px;
}

/* 表格基礎修正 */
table {
    min-width: 100%;
}

/* 表格容器（支援滾動提示） */
.table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
}

/* 手機滾動提示漸層（右側陰影提示可滾動） */
@media (max-width: 768px) {
    .table-container::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to left, var(--bg-card, rgba(255,255,255,0.9)), transparent);
        pointer-events: none;
        opacity: 0.8;
    }

    /* 滾動到底時隱藏提示 */
    .table-container.scrolled-end::after {
        opacity: 0;
    }
}

/* 已存在的表格自動添加滾動 */
.card table,
.modal-body table,
.detail-section table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* ===== 2. 小螢幕 Grid 自動調整 ===== */
@media (max-width: 480px) {
    /* 員工選擇格：3欄 → 2欄 */
    .employee-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* 工時選擇：5欄 → 3欄 */
    .work-type-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }

    /* 表單類型：3欄 → 1欄 */
    .form-type-selector {
        grid-template-columns: 1fr !important;
    }

    /* 圖表：2欄 → 1欄 */
    .chart-grid {
        grid-template-columns: 1fr !important;
    }

    /* 統計卡片：自動換行 */
    .summary-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* 篩選列：堆疊排列 */
    .filter-bar {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .filter-group {
        width: 100% !important;
    }

    .filter-group select,
    .filter-group input {
        flex: 1 !important;
        min-width: 0 !important;
    }

    /* 頭部資訊堆疊 */
    .header {
        flex-direction: column !important;
        gap: 12px !important;
        text-align: center !important;
    }

    .header-info {
        flex-direction: column !important;
        gap: 8px !important;
    }

    /* 容器 padding 減少 */
    .container {
        padding: 12px !important;
    }

    /* 卡片 padding 減少 */
    .card {
        padding: 16px !important;
    }

    /* 字體縮小 */
    .header h1 {
        font-size: 1.2rem !important;
    }

    .summary-card .value {
        font-size: 18px !important;
    }
}

/* ===== 3. 超小螢幕（iPhone SE 等） ===== */
@media (max-width: 375px) {
    .employee-grid {
        grid-template-columns: 1fr !important;
    }

    .work-type-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .summary-grid {
        grid-template-columns: 1fr !important;
    }

    .form-type-btn {
        padding: 12px 8px !important;
    }

    .form-type-icon {
        font-size: 1.5rem !important;
    }
}

/* ===== 4. 模態框修正 ===== */
@media (max-width: 480px) {
    .modal {
        width: 95% !important;
        max-height: 90vh !important;
        margin: 10px !important;
    }

    .modal-header {
        padding: 15px !important;
    }

    .modal-header h2 {
        font-size: 1.1rem !important;
    }

    .modal-body {
        padding: 15px !important;
    }

    /* 明細表格可滾動 */
    .attendance-table {
        font-size: 0.8rem !important;
    }

    .attendance-table th,
    .attendance-table td {
        padding: 6px 8px !important;
    }
}

/* ===== 5. Tab 標籤滾動 ===== */
.tabs {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
}

.tabs::-webkit-scrollbar {
    display: none; /* Chrome/Safari */
}

.tab {
    white-space: nowrap !important;
    flex-shrink: 0 !important;
}

/* ===== 6. 按鈕堆疊 ===== */
@media (max-width: 480px) {
    .button-group,
    .action-buttons {
        flex-direction: column !important;
        gap: 8px !important;
    }

    .button-group button,
    .action-buttons button {
        width: 100% !important;
    }
}

/* ===== 7. 防止水平溢出 ===== */
html, body {
    overflow-x: hidden;
    max-width: 100vw;
}

.content,
.container,
.main-content {
    max-width: 100%;
    overflow-x: hidden;
}

/* 長文字換行 */
td, th, .card, .detail-value {
    word-break: break-word;
}

/* ===== 8. iOS 輸入框放大修正 ===== */
@media (max-width: 480px) {
    input[type="text"],
    input[type="number"],
    input[type="date"],
    input[type="tel"],
    select,
    textarea {
        font-size: 16px !important; /* 防止 iOS 自動放大 */
    }
}

/* ===== 9. 數據表格響應式 ===== */
@media (max-width: 600px) {
    /* 將表格轉為卡片式顯示 */
    .responsive-table {
        display: block;
    }

    .responsive-table thead {
        display: none;
    }

    .responsive-table tbody,
    .responsive-table tr {
        display: block;
    }

    .responsive-table tr {
        margin-bottom: 12px;
        background: white;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        padding: 12px;
    }

    .responsive-table td {
        display: flex;
        justify-content: space-between;
        padding: 8px 0;
        border: none;
        border-bottom: 1px solid #eee;
    }

    .responsive-table td:last-child {
        border-bottom: none;
    }

    .responsive-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #666;
    }
}

/* ===== 10. 圖表容器響應式 ===== */
.chart-box {
    min-height: 200px;
}

@media (max-width: 480px) {
    .chart-box {
        padding: 12px !important;
    }

    .chart-box h3 {
        font-size: 0.9rem !important;
    }
}

/* ===== 11. 日期選擇器修正 ===== */
@media (max-width: 480px) {
    input[type="date"],
    input[type="month"] {
        min-height: 44px; /* iOS 觸控友好 */
    }
}

/* ===== 12. 登入框響應式 ===== */
@media (max-width: 480px) {
    .login-box {
        width: 90% !important;
        max-width: none !important;
        padding: 25px !important;
    }
}

/* ===== 13. 管理後台快速導航 ===== */
.admin-nav {
    position: relative;
    display: inline-block;
}

.admin-nav-btn {
    background: rgba(255,255,255,0.15);
    border: 1px solid rgba(255,255,255,0.3);
    color: white;
    padding: 8px 14px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s;
}

.admin-nav-btn:hover {
    background: rgba(255,255,255,0.25);
}

.admin-nav-btn::after {
    content: '▼';
    font-size: 0.7rem;
    opacity: 0.7;
}

.admin-nav-menu {
    background: white;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    opacity: 0;
    visibility: hidden;
    transition: all 0.25s ease;
    /* 桌面：下拉選單 */
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 8px;
    border-radius: 12px;
    min-width: 200px;
    max-height: 80vh;
    z-index: 10001;
    transform: translateY(-10px);
}

/* 手機：從底部彈出 */
@media (max-width: 768px) {
    .admin-nav-menu {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        margin: 0 !important;
        border-radius: 20px 20px 0 0 !important;
        min-width: 100% !important;
        width: 100% !important;
        max-height: 70vh !important;
        z-index: 99999 !important;
        transform: translateY(100%) !important;
        padding-bottom: env(safe-area-inset-bottom, 20px) !important;
    }
}

/* iOS 拖曳提示條 */
@media (max-width: 480px) {
    .admin-nav-menu::before {
        content: '';
        display: block;
        width: 36px;
        height: 5px;
        background: #d0d0d0;
        border-radius: 3px;
        margin: 10px auto 6px;
    }
}

.admin-nav-menu.show {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) !important;
}

.admin-nav-menu a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    color: #333;
    text-decoration: none;
    font-size: 0.9rem;
    transition: background 0.15s;
    border-bottom: 1px solid #f0f0f0;
}

.admin-nav-menu a:first-child {
    border-radius: 12px 12px 0 0;
}

.admin-nav-menu a:last-child {
    border-bottom: none;
    border-radius: 0 0 12px 12px;
}

.admin-nav-menu a:hover {
    background: #f5f5f5;
}

.admin-nav-menu a.active {
    background: linear-gradient(135deg, #667eea22, #764ba222);
    color: #667eea;
    font-weight: 600;
}

.admin-nav-menu .nav-icon {
    font-size: 1.1rem;
    width: 24px;
    text-align: center;
}

.admin-nav-menu .nav-divider {
    height: 1px;
    background: #e0e0e0;
    margin: 4px 0;
}

.admin-nav-menu .nav-category {
    padding: 10px 20px 6px;
    font-size: 0.75rem;
    color: #999;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* 點擊外部關閉選單 */
.admin-nav-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999;
    display: none;
}

.admin-nav-overlay.show {
    display: block;
}

/* 小螢幕：選單額外樣式 */
@media (max-width: 768px) {
    .admin-nav-menu a {
        touch-action: manipulation;
        padding: 14px 20px;
        font-size: 0.95rem;
    }

    .admin-nav-menu .nav-icon {
        font-size: 1.2rem;
    }

    .admin-nav-overlay {
        z-index: 99998 !important;
    }

    .admin-nav-overlay.show {
        background: rgba(0,0,0,0.5);
    }

    html.nav-open {
        overflow-y: hidden;
        overscroll-behavior: none;
    }

    html.nav-open body {
        overflow-y: hidden;
        position: relative;
    }
}

/* ===== 14. 設備特定 UI 調整 ===== */

/* 手機：隱藏重整按鈕（有下滑重整） */
@media (max-width: 768px) {
    .refresh-btn-desktop,
    .back-btn[title="重新整理"],
    button[onclick="location.reload()"] {
        display: none !important;
    }
}

/* 桌面：顯示重整按鈕 */
@media (min-width: 769px) {
    .refresh-btn-mobile {
        display: none !important;
    }
}

/* 手機：觸控優化（更大的點擊區域） */
@media (max-width: 768px) {
    .back-btn,
    .admin-nav-btn,
    .tab,
    button {
        min-height: 44px; /* iOS 建議最小觸控尺寸 */
        min-width: 44px;
    }

    /* 表格行更大點擊區域 */
    tr {
        min-height: 48px;
    }

    td, th {
        padding: 12px 10px !important;
    }
}

/* 手機：簡化 header 顯示 */
@media (max-width: 480px) {
    .user-badge {
        display: none !important;
    }

    .header h1 {
        font-size: 1.1rem !important;
    }

    /* 導航按鈕文字簡化 */
    .admin-nav-btn {
        padding: 8px 10px !important;
        font-size: 1.2rem !important;
    }

    /* 隱藏「選單」文字，只留圖標 */
    .admin-nav-btn::before {
        content: '';
    }
}

/* 觸控設備：移除 hover 效果（防止點擊後殘留） */
@media (hover: none) {
    .back-btn:hover,
    .admin-nav-btn:hover,
    .tab:hover,
    tr:hover {
        background: inherit;
    }
}

/* 桌面：顯示完整資訊 */
@media (min-width: 769px) {
    .mobile-only {
        display: none !important;
    }
}

/* 手機：隱藏桌面專用元素 */
@media (max-width: 768px) {
    .desktop-only {
        display: none !important;
    }
}

/* ===== 15. 表格固定第一欄（P0） ===== */
@media (max-width: 768px) {
    .table-sticky-first {
        position: relative;
    }

    .table-sticky-first thead th:first-child,
    .table-sticky-first tbody td:first-child {
        position: sticky;
        left: 0;
        background: white;
        z-index: 10;
        min-width: 70px;
        max-width: 100px;
        box-shadow: 2px 0 4px rgba(0,0,0,0.1);
    }

    .table-sticky-first thead th:first-child {
        background: #f8f9fa;
        z-index: 11;
    }

    /* 深色主題支援（自動偵測 CSS 變數） */
    .table-sticky-first thead th:first-child,
    .table-sticky-first tbody td:first-child {
        background: var(--bg-card, white);
    }
    .table-sticky-first thead th:first-child {
        background: var(--bg-primary, #f8f9fa);
    }

    /* 數字欄位不換行（維持人眼可讀性）*/
    .table-sticky-first td {
        white-space: nowrap;
    }

    /* 表格最小寬度，強制橫向滾動而非壓縮 */
    .table-sticky-first {
        min-width: 500px;
    }
}

/* ===== 16. 隱藏次要欄位（P1） ===== */
@media (max-width: 600px) {
    .col-hide-mobile {
        display: none !important;
    }
}

/* ===== 17. 卡片式轉換（P2 - 小手機） ===== */
@media (max-width: 480px) {
    .card-table thead {
        display: none;
    }

    .card-table tbody {
        display: block;
    }

    .card-table tr {
        display: block;
        margin-bottom: 12px;
        padding: 12px;
        background: white;
        border-radius: 8px;
        border: 1px solid #e0e0e0;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }

    .card-table td {
        display: flex;
        justify-content: space-between;
        padding: 8px 0;
        border: none;
        border-bottom: 1px solid #eee;
    }

    .card-table td:last-child {
        border-bottom: none;
    }

    .card-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #666;
        margin-right: 10px;
    }

    /* 卡片表格的第一欄不需要固定 */
    .card-table thead th:first-child,
    .card-table tbody td:first-child {
        position: static;
        box-shadow: none;
    }
}

/* ===== 18. 橫向模式優化 ===== */
@media (orientation: landscape) and (max-height: 500px) {
    /* 縮小 header */
    .header {
        padding: 8px 15px !important;
        min-height: auto !important;
    }

    .header h1 {
        font-size: 1rem !important;
    }

    /* 縮小表格字體和間距 */
    table {
        font-size: 0.8rem !important;
    }

    th, td {
        padding: 4px 6px !important;
    }

    /* 縮小統計卡片 */
    .summary-grid {
        gap: 8px !important;
    }

    .summary-card {
        padding: 10px !important;
    }

    .summary-card .value,
    .summary-value {
        font-size: 1.2rem !important;
    }

    /* 縮小篩選列 */
    .filter-bar {
        padding: 10px !important;
        gap: 10px !important;
    }

    /* 隱藏重整按鈕文字（只留圖標） */
    .refresh-btn {
        padding: 8px !important;
    }

    /* 小螢幕橫向隱藏更多欄位 */
    .col-landscape-hide {
        display: none !important;
    }
}

/* 大螢幕橫向（844px+）：全欄顯示 */
@media (orientation: landscape) and (min-width: 813px) and (max-height: 500px) {
    table {
        font-size: 0.85rem !important;
    }

    th, td {
        padding: 6px 8px !important;
    }

    /* 顯示所有欄位 */
    .col-landscape-hide {
        display: table-cell !important;
    }
}

/* ===== 19. 照片網格手機適配 ===== */
@media (max-width: 480px) {
    .photo-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }

    .photo-card img {
        height: 100px !important;
    }
}

@media (max-width: 375px) {
    .photo-grid {
        grid-template-columns: 1fr !important;
    }

    .photo-card img {
        height: 150px !important;
    }
}

/* ===== 20. 超小螢幕（390px iPhone 12/13/14）優化 ===== */
@media (max-width: 390px) {
    /* 篩選列緊湊排列 */
    .filter-bar {
        padding: 12px !important;
        gap: 10px !important;
    }

    .filter-group {
        min-width: 100% !important;
    }

    /* 表單網格單欄 */
    .form-grid {
        grid-template-columns: 1fr !important;
    }

    /* Modal 更緊湊 */
    .modal {
        width: 95% !important;
        margin: 5px auto !important;
    }

    .modal-body {
        padding: 16px !important;
    }

    .modal-footer {
        padding: 12px 16px !important;
        gap: 8px !important;
    }

    /* Badge 縮小 */
    .status-badge,
    .category-badge,
    .company-badge,
    .project-count,
    .remote-badge {
        font-size: 0.7rem !important;
        padding: 2px 6px !important;
    }

    /* 表格字體縮小 */
    table {
        font-size: 0.8rem !important;
    }

    th, td {
        padding: 8px 6px !important;
    }

    /* 按鈕縮小 */
    .btn {
        padding: 8px 12px !important;
        font-size: 0.8rem !important;
    }

    /* 統計卡片緊湊 */
    .summary-grid {
        gap: 10px !important;
    }

    .summary-card {
        padding: 14px !important;
    }

    .summary-value {
        font-size: 1.4rem !important;
    }

    .summary-label {
        font-size: 0.75rem !important;
    }

    /* Header 緊湊 */
    .header h1 {
        font-size: 1rem !important;
    }

    /* 輸入框防止 iOS 自動放大 */
    input[type="text"],
    input[type="number"],
    input[type="date"],
    input[type="tel"],
    input[type="month"],
    select,
    textarea {
        font-size: 16px !important;
    }
}
