/* ============================================================
   APS ERP — Bootstrap 5 + Custom Styles (Responsive)
   ============================================================ */

/* ----- CSS Variables ----- */
:root {
    --sidebar-width: 240px;
    --sidebar-bg: #FFF9F0;
    --sidebar-hover: #F5E6D3;
    --sidebar-active: #FFE8D6;
    --primary: #e94560;
    --primary-hover: #c73650;
    --bg: #fff;
    --card-bg: #fff;
    --text: #2d3436;
    --text-muted: #636e72;
    --border: #dfe6e9;
    --success: #00b894;
    --warning: #fdcb6e;
    --danger: #d63031;
}

/* ----- Reset & Base ----- */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
                 "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    display: flex;
    min-height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ----- Sidebar ----- */
.sidebar {
    width: var(--sidebar-width);
    min-height: 100vh;
    background: var(--sidebar-bg);
    color: var(--text);
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 1000;
    transition: transform 0.3s ease;
    border-right: 1px solid var(--border);
}

.sidebar-brand {
    padding: 20px;
    border-bottom: 1px solid var(--border);
}
.sidebar-brand h2 { font-size: 1.2rem; margin: 0; color: var(--text); }
.sidebar-brand small { color: var(--text-muted); font-size: 0.75rem; }

.sidebar-nav {
    list-style: none;
    padding: 10px 0;
    flex: 1;
    overflow-y: auto;
}
.sidebar-nav li { margin: 2px 0; }
.sidebar-nav a {
    display: block;
    padding: 10px 20px;
    color: var(--text);
    font-size: 0.9rem;
    transition: all 0.2s;
    border-radius: 0;
}
.sidebar-nav a:hover, .sidebar-nav a.active {
    background: var(--sidebar-active);
    color: var(--text);
    text-decoration: none;
    font-weight: 500;
}
.sidebar-nav a .nav-icon { margin-right: 8px; }

.sidebar-section-title {
    padding: 15px 20px 5px;
    font-size: 0.7rem;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 1px;
}

.sidebar-footer {
    padding: 15px 20px;
    border-top: 1px solid var(--border);
    font-size: 0.85rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.sidebar-footer a { color: var(--text-muted); font-size: 0.8rem; }

/* ----- Main Content ----- */
.content {
    margin-left: var(--sidebar-width);
    flex: 1;
    padding: 30px;
    min-height: 100vh;
}

/* ----- Mobile sidebar toggle ----- */
.sidebar-toggle {
    display: none;
    position: fixed;
    top: 10px;
    right: 10px;
    z-index: 1100;
    background: var(--primary);
    color: #fff;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    font-size: 1.2rem;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 999;
}

/* ----- Page Header ----- */
.page-header {
    margin-bottom: 25px;
}
.page-header h1 { font-size: 1.5rem; margin-bottom: 5px; }
.page-header p { color: var(--text-muted); font-size: 0.9rem; }

/* ----- Cards ----- */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.card h3 { font-size: 1rem; margin-bottom: 15px; color: var(--text); }

/* ----- KPI Grid ----- */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 25px;
}
.kpi-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
}
.kpi-label { color: var(--text-muted); font-size: 0.8rem; margin-bottom: 5px; }
.kpi-value { font-size: 1.8rem; font-weight: 700; color: var(--text); }

/* ----- Charts Grid ----- */
.charts-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    margin-bottom: 25px;
}
.chart-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
}
.chart-full { grid-column: 1 / -1; }
.chart-card h3 { font-size: 0.9rem; margin-bottom: 15px; }
.chart-card canvas { max-height: 300px; }

/* ----- Tables ----- */
.table-container {
    overflow-x: auto;
}
@media (max-width: 768px) {
    .table-container {
        background:
            linear-gradient(to right, var(--card-bg) 30%, rgba(255,255,255,0)),
            linear-gradient(to right, rgba(255,255,255,0), var(--card-bg) 70%) 100% 0,
            radial-gradient(farthest-side at 0% 50%, rgba(0,0,0,0.10), rgba(0,0,0,0)),
            radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,0.10), rgba(0,0,0,0)) 100% 0;
        background-repeat: no-repeat;
        background-size: 30px 100%, 30px 100%, 10px 100%, 10px 100%;
        background-attachment: local, local, scroll, scroll;
    }
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}
thead th {
    background: #f8f9fa;
    padding: 10px 12px;
    text-align: left;
    font-weight: 600;
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
}
tbody td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
}
tbody tr:hover { background: #f8f9fa; }

/* ----- Buttons ----- */
.btn {
    display: inline-block;
    padding: 8px 16px;
    border-radius: 8px;
    border: 1px solid transparent;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: #f1f2f6; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #dfe6e9; }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-warning { background: var(--warning); color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 0.8rem; }
.btn-block { display: block; width: 100%; }

/* ----- Forms ----- */
.form-group { margin-bottom: 16px; }
.form-group label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
    font-size: 0.85rem;
    color: var(--text);
}
.form-group input:not([type="checkbox"]):not([type="radio"]),
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
    transition: border-color 0.2s;
}
.form-group input:not([type="checkbox"]):not([type="radio"]):focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(233, 69, 96, 0.1);
}
.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}
.form-row-3 {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 16px;
}

/* ----- Flash Messages ----- */
.flash-messages { margin-bottom: 20px; }
.flash {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 8px;
    font-size: 0.9rem;
}
.flash-error { background: #ffeaa7; color: #6b5900; border-left: 4px solid var(--danger); }
.flash-success { background: #d5f5e3; color: #1e7e34; border-left: 4px solid var(--success); }
.flash-info { background: #d6eaf8; color: #21618c; border-left: 4px solid #2980b9; }
.flash-warning { background: #fdebd0; color: #7d6608; border-left: 4px solid var(--warning); }

/* ----- Badges ----- */
.badge {
    display: inline-block;
    padding: 3px 8px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
}
.badge-primary { background: #e8eaf6; color: #3f51b5; }
.badge-success { background: #d5f5e3; color: #1e7e34; }
.badge-danger { background: #fadbd8; color: #c0392b; }
.badge-warning { background: #fdebd0; color: #b7950b; }
.badge-info { background: #d6eaf8; color: #21618c; }

/* ----- Login Page ----- */
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    min-height: 100vh;
    min-height: 100dvh;
    background: #FFF9F0;
    margin-left: 0;
    padding: 20px;
    box-sizing: border-box;
}
.login-card {
    background: #fff;
    border: 1px solid var(--border, #e0e0e0);
    border-radius: 16px;
    padding: 40px;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}
.login-card h1 { text-align: center; margin-bottom: 5px; font-size: 1.5rem; }
.login-card > p { text-align: center; color: var(--text-muted); margin-bottom: 30px; }

@media (max-width: 768px) {
    .login-container {
        align-items: flex-start;
        padding: 12px;
    }
    .login-card {
        border-radius: 16px;
        border: 1px solid var(--border, #e0e0e0);
        box-shadow: 0 2px 12px rgba(0,0,0,0.06);
        padding: 32px 24px 24px;
        max-width: 100%;
        min-height: calc(100vh - 24px);
        min-height: calc(100dvh - 24px);
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
    .login-card h1 { font-size: 1.75rem; }
}

.login-divider {
    display: flex;
    align-items: center;
    margin: 24px 0;
    color: var(--text-muted);
    font-size: 0.85rem;
}
.login-divider::before, .login-divider::after {
    content: '';
    flex: 1;
    border-top: 1px solid var(--border);
}
.login-divider span { padding: 0 12px; }

.wecom-section {
    text-align: center;
    padding: 20px;
    background: #f5f6fa;
    border-radius: 12px;
    border: 1px dashed var(--border);
}
.wecom-section.disabled {
    opacity: 0.5;
    pointer-events: none;
}
.wecom-section img {
    width: 40px;
    height: 40px;
    margin-bottom: 10px;
}
.wecom-section p { color: var(--text-muted); font-size: 0.85rem; margin: 0; }

/* ----- Search Bar ----- */
.search-bar {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}
.search-bar input {
    flex: 1;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
}
.search-bar input:focus { outline: none; border-color: var(--primary); }

/* ----- Toolbar ----- */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 10px;
}
.filter-group {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}
@media (max-width: 768px) {
    .filter-group {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 4px;
        scrollbar-width: none;
        background:
            linear-gradient(to right, var(--card-bg) 30%, rgba(255,255,255,0)),
            linear-gradient(to right, rgba(255,255,255,0), var(--card-bg) 70%) 100% 0,
            radial-gradient(farthest-side at 0% 50%, rgba(0,0,0,0.12), rgba(0,0,0,0)),
            radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,0.12), rgba(0,0,0,0)) 100% 0;
        background-repeat: no-repeat;
        background-size: 30px 100%, 30px 100%, 12px 100%, 12px 100%;
        background-attachment: local, local, scroll, scroll;
    }
    .filter-group::-webkit-scrollbar { display: none; }
    .filter-group > .btn,
    .filter-group > a.btn {
        flex-shrink: 0;
        white-space: nowrap;
    }
}

/* ----- Pagination ----- */
.pagination {
    display: flex;
    gap: 5px;
    margin-top: 20px;
    justify-content: center;
}
.pagination a, .pagination span {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.85rem;
}
.pagination a:hover { background: var(--bg); text-decoration: none; }
.pagination .current { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ----- Pagination Toolbar (macros/pagination.html) ----- */
.pagination-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
    margin-bottom: 12px;
    font-size: 0.85rem;
}
@media (max-width: 768px) {
    .pagination-toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .pagination-toolbar > select,
    .pagination-toolbar > button {
        display: none;
    }
    .pagination-toolbar > form {
        width: 100%;
        gap: 6px;
    }
    .pagination-toolbar > form input[type="text"] {
        flex: 1;
        min-width: 0;
        padding: 10px 12px;
    }
    .pagination-toolbar > form button[type="submit"] {
        flex-shrink: 0;
        padding: 10px 14px;
    }
    .pagination-toolbar > span[style*="flex:1"] {
        display: none;
    }
    .pagination-toolbar .btn-sm {
        padding: 12px 14px;
        font-size: 1rem;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
}

/* ----- Approval Steps ----- */
.approval-steps {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}
.approval-step {
    flex: 1;
    min-width: 150px;
    padding: 15px;
    border: 2px solid var(--border);
    border-radius: 10px;
    text-align: center;
}
.approval-step.done { border-color: var(--success); background: #d5f5e3; }
.approval-step.current { border-color: var(--primary); background: #fce4ec; }
.approval-step.pending { opacity: 0.5; }
.approval-step .step-label { font-size: 0.75rem; color: var(--text-muted); }
.approval-step .step-user { font-weight: 600; }

/* ----- Section ----- */
.section { margin-bottom: 30px; }
.section h2 { font-size: 1.2rem; margin-bottom: 15px; }

.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.mt-3 { margin-top: 16px; }
.mb-3 { margin-bottom: 16px; }
.mr-2 { margin-right: 8px; }

/* ----- Responsive ----- */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        padding-bottom: 0;
        box-sizing: border-box;
    }
    .sidebar.open {
        transform: translateX(0);
        bottom: 60px;
    }
    .sidebar-nav-spacer {
        height: 120px;
        list-style: none;
    }
    .sidebar-toggle { display: block; }
    .sidebar-overlay.active { display: block; }
    .content {
        margin-left: 0;
        margin: 12px;
        padding: 20px 15px;
        padding-bottom: 80px;
        max-width: calc(100vw - 24px);
        overflow-x: hidden;
        border-radius: 16px;
        background: var(--card-bg);
        min-height: calc(100vh - 24px);
    }
    body {
        max-width: 100vw;
        overflow-x: hidden;
    }
    body.sidebar-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
    }
    .form-row, .form-row-3 {
        grid-template-columns: 1fr;
        min-width: 0;
        overflow: hidden;
    }
    .charts-grid {
        grid-template-columns: 1fr;
    }
    .kpi-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    .kpi-card {
        padding: 18px 16px;
    }
    .kpi-value {
        font-size: 1.6rem;
    }
    .approval-steps { flex-direction: column; }

    /* ----- Mobile Card Tables ----- */
    table.card-mode,
    table.card-mode thead,
    table.card-mode tbody,
    table.card-mode th,
    table.card-mode td,
    table.card-mode tr {
        display: block;
    }
    table.card-mode thead { display: none; }
    table.card-mode tbody tr {
        margin-bottom: 12px;
        border: 1px solid var(--border);
        border-radius: 8px;
        padding: 10px 12px;
        background: var(--card-bg);
        box-shadow: 0 1px 2px rgba(0,0,0,0.04);
    }
    table.card-mode tbody tr:hover { background: var(--card-bg); }
    table.card-mode tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 7px 0;
        border-bottom: 1px solid #f0f0f0;
        font-size: 0.85rem;
    }
    table.card-mode tbody td:last-child { border-bottom: none; }
    table.card-mode tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted);
        font-size: 0.8rem;
        flex-shrink: 0;
        margin-right: 12px;
        max-width: 40%;
    }
    table.card-mode tbody td .cell-content {
        text-align: right;
        flex: 1;
    }

    /* Card table action columns — full width buttons */
    table.card-mode td.actions-cell {
        justify-content: flex-end;
        gap: 8px;
        padding-top: 10px;
    }
    table.card-mode td.actions-cell::before { display: none; }
    table.card-mode td.actions-cell .btn-sm {
        min-width: 44px;
        min-height: 44px;
        padding: 12px 14px;
        font-size: 0.95rem;
    }

    /* ----- Toolbar & Search ----- */
    .toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .toolbar > * { width: 100%; }
    .search-bar {
        flex-direction: column;
    }
    .search-bar input { width: 100%; }

    /* ----- Page Header ----- */
    .page-header h1 { font-size: 1.25rem; }

    /* ----- Larger touch targets ----- */
    .btn {
        padding: 12px 20px;
        font-size: 1rem;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        line-height: 1.2;
    }
    .btn-sm {
        padding: 12px 16px;
        font-size: 1rem;
        min-height: 44px;
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        line-height: 1.2;
    }
    .pagination a, .pagination span {
        padding: 10px 14px;
        font-size: 0.9rem;
        min-width: 40px;
        text-align: center;
    }

    /* Touch-friendly checkboxes */
    input[type="checkbox"] {
        width: 22px;
        height: 22px;
        min-width: 22px;
        accent-color: var(--primary);
    }
    label:has(input[type="checkbox"]) {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        cursor: pointer;
    }

    /* ----- Modal → bottom sheet ----- */
    .modal-dialog {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-width: 100%;
        border-radius: 16px 16px 0 0;
        max-height: 85vh;
        margin: 0;
    }
    .modal-overlay.active { align-items: flex-end; }

    /* ----- Filter Collapse on Mobile ----- */
    .filter-group.mobile-truncated > .btn:not(.filter-toggle-btn),
    .filter-group.mobile-truncated > a.btn:not(.filter-toggle-btn) {
        display: none;
    }
    .filter-group.mobile-truncated > .btn.filter-visible,
    .filter-group.mobile-truncated > a.btn.filter-visible {
        display: inline-flex;
    }
    .filter-toggle-btn {
        flex-shrink: 0;
        white-space: nowrap;
        padding: 10px 14px;
        font-size: 0.9rem;
        min-height: 40px;
        background: #f1f2f6;
        border: 1px solid var(--border);
        border-radius: 8px;
        color: var(--text);
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 4px;
    }
    .search-bar.mobile-collapsed {
        display: none;
    }
    .search-bar.mobile-expanded {
        display: flex;
        flex-direction: column;
    }
    .search-toggle-btn {
        flex-shrink: 0;
        white-space: nowrap;
        padding: 10px 14px;
        font-size: 0.9rem;
        min-height: 40px;
        background: #f1f2f6;
        border: 1px solid var(--border);
        border-radius: 8px;
        color: var(--text);
        cursor: pointer;
        display: inline-flex;
        align-items: center;
        gap: 4px;
        width: 100%;
        justify-content: center;
        margin-bottom: 8px;
    }
    .filter-group.mobile-expanded > .btn,
    .filter-group.mobile-expanded > a.btn {
        display: inline-flex !important;
    }
    .filter-group.mobile-expanded .filter-toggle-btn {
        background: var(--primary);
        color: #fff;
        border-color: var(--primary);
    }

    /* ----- Acceptance Form Pages ----- */
    .form-group input:not([type="checkbox"]):not([type="radio"]),
    .form-group select,
    .form-group textarea {
        width: 100% !important;
        min-width: 0;
        box-sizing: border-box;
    }
    .form-group input[type="date"],
    input[type="date"] {
        width: 100% !important;
    }

    /* Placement / Influencer rebate form tables */
    .table-container table:not(.card-mode) {
        min-width: 640px;
    }
    .table-container table:not(.card-mode) input[type="number"],
    .table-container table:not(.card-mode) input[type="text"] {
        width: 100% !important;
        min-width: 80px;
    }
    .table-container table:not(.card-mode) select {
        width: 100% !important;
        min-width: 100px;
    }

    /* Rebate type selector bar */
    #rebate-type-select {
        width: 100% !important;
        max-width: 100%;
    }

    /* Group cards in influencer rebate form */
    .group-card {
        padding: 10px !important;
    }
    .group-card .group-records-table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .group-card .group-records-table thead th {
        white-space: nowrap;
        font-size: 0.78rem;
        padding: 6px 4px;
    }
    .group-card .group-records-table tbody td {
        font-size: 0.82rem;
        padding: 6px 4px;
    }
    .group-card .group-records-table input {
        font-size: 0.82rem;
        min-width: 80px;
    }

    /* AI recognition dual-panel: stack vertically */
    .recognition-row td > div {
        flex-direction: column !important;
        gap: 8px !important;
    }
    .recognition-row td > div > div {
        flex: 1 1 auto !important;
        width: 100% !important;
    }
    .rec-text-paste,
    .rec-image-paste {
        min-height: 80px;
    }

    /* Summary footer */
    #total-tax-num {
        font-size: 1.2rem !important;
    }

    /* Button rows & flex containers in forms: wrap on mobile */
    .card form > div[style*="display:flex"],
    .card form > div[style*="display: flex"] {
        flex-wrap: wrap !important;
    }
    /* Preserve nowrap for group card header (title + remove button) */
    .group-card > div[style*="display:flex"] {
        flex-wrap: wrap !important;
    }

    /* Invoice section on mobile */
    #invoice-section .form-row {
        flex-direction: column;
    }
    #invoice-section select,
    #invoice-section input:not([type="checkbox"]) {
        width: 100% !important;
    }

    /* Edit form table → stacked */
    .card form > table:not(.card-mode):not(.group-records-table):not(.expense-items-table):not(.freelancer-expense-table):not(.timesheet-entry-table) {
        width: 100%;
    }
    .card form > table:not(.card-mode):not(.group-records-table):not(.expense-items-table):not(.freelancer-expense-table):not(.timesheet-entry-table) td {
        display: block;
        width: 100% !important;
        padding: 4px 0;
    }
    .card form > table:not(.card-mode):not(.group-records-table):not(.expense-items-table):not(.freelancer-expense-table):not(.timesheet-entry-table) tr {
        display: block;
        margin-bottom: 8px;
        border-bottom: 1px solid var(--border);
        padding-bottom: 8px;
    }
    .card form > table:not(.card-mode):not(.group-records-table):not(.expense-items-table):not(.freelancer-expense-table):not(.timesheet-entry-table) td:first-child {
        color: var(--text-muted);
        font-size: 0.8rem;
        font-weight: 600;
        margin-bottom: 2px;
    }
}

/* ----- Extra-Small Phones (iPhone SE, etc.) ----- */
@media (max-width: 375px) {
    .content { padding: 12px 10px 80px; }
    .kpi-grid { grid-template-columns: 1fr; }
    .page-header h1 { font-size: 1.1rem; }
    .card { padding: 14px; }
    table.card-mode tbody td { font-size: 0.8rem; }
}

/* ----- Small Phones ----- */
@media (max-width: 480px) {
    .kpi-grid { grid-template-columns: repeat(2, 1fr); }
    .kpi-value { font-size: 1.4rem; }

    /* Stack page-header actions */
    .page-header { flex-direction: column; align-items: flex-start; gap: 8px; }
}

/* ----- Responsive Grid Helpers ----- */
.responsive-grid-2 {
    grid-template-columns: 1fr 1fr;
}
.responsive-grid-3 {
    grid-template-columns: 1fr 1fr 1fr;
}
.approval-action-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

@media (max-width: 768px) {
    .responsive-grid-2,
    .responsive-grid-3 {
        grid-template-columns: 1fr;
    }
    .approval-action-row {
        flex-direction: column;
        align-items: stretch;
    }
    .approval-action-row .form-group {
        margin-bottom: 4px;
    }
    .approval-action-row .btn {
        width: 100%;
    }
    /* Timeline items stack vertically on mobile */
    .approval-timeline > div {
        flex-wrap: wrap;
    }
    .approval-timeline > div > div:last-child {
        width: 100%;
        margin-top: 4px;
        margin-left: 44px;
    }
}

/* ----- Responsive Grid Helpers ----- */
.reminder-grid {
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}
.project-summary-grid {
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
}
.quick-link-btn {
    min-width: 100px;
}

@media (max-width: 768px) {
    .reminder-grid {
        grid-template-columns: 1fr;
    }
    .project-summary-grid {
        grid-template-columns: 1fr;
    }
    .quick-link-btn {
        min-width: auto;
        flex: 1 1 calc(50% - 8px);
        text-align: center;
        padding: 14px 12px;
        font-size: 1rem;
        min-height: 48px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 6px;
    }
}

@media (max-width: 375px) {
    .reminder-grid {
        grid-template-columns: 1fr;
    }
    .project-summary-grid {
        grid-template-columns: 1fr;
    }
}

/* ----- Mobile Bottom Tab Bar ----- */
.mobile-tabbar {
    display: none;
}
@media (max-width: 768px) {
    .mobile-tabbar {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 60px;
        background: var(--card-bg);
        border-top: 1px solid var(--border);
        z-index: 1100;
        box-shadow: 0 -2px 8px rgba(0,0,0,0.08);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    .mobile-tabbar a {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: var(--text-muted);
        text-decoration: none;
        font-size: 0.65rem;
        gap: 2px;
        -webkit-tap-highlight-color: transparent;
    }
    .mobile-tabbar a .tab-icon {
        font-size: 1.3rem;
        line-height: 1;
    }
    .mobile-tabbar a.active {
        color: var(--primary);
    }
}

/* ----- Payroll: resigning/joining row highlights ----- */
.row-resigning {
    background: #fff5f5 !important;
    border-left: 3px solid #e94560;
}
.row-resigning:hover {
    background: #ffe8e8 !important;
}
.row-joining {
    background: #f0f7ff !important;
    border-left: 3px solid #1a73e8;
}
.row-joining:hover {
    background: #e3f0ff !important;
}
.row-imported {
    background: #fffbeb !important;
    border-left: 3px solid #f59e0b;
}
.row-imported:hover {
    background: #fef3c7 !important;
}

/* ----- Modal Dialog ----- */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 9000;
    align-items: center;
    justify-content: center;
}
.modal-overlay.active { display: flex; }
.modal-dialog {
    background: var(--card-bg);
    border-radius: 10px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
    width: 480px;
    max-width: 95vw;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 1.05rem; }
.modal-close {
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    color: var(--text-muted);
    padding: 0 4px;
}
.modal-close:hover { color: var(--text); }
.modal-body {
    padding: 16px 20px;
    overflow-y: auto;
    flex: 1;
}
.modal-body .search-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.9rem;
    margin-bottom: 12px;
}
.modal-body .employee-list { max-height: 280px; overflow-y: auto; }
.modal-body .employee-item {
    padding: 10px 12px;
    cursor: pointer;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: background 0.15s;
}
.modal-body .employee-item:hover { background: var(--bg); }
.modal-body .employee-item.selected { background: #eef2ff; border: 1px solid #a5b4fc; }
.modal-body .employee-item .emp-info { flex: 1; }
.modal-body .employee-item .emp-name { font-weight: 600; }
.modal-body .employee-item .emp-meta { font-size: 0.8rem; color: var(--text-muted); }
.modal-body .no-results {
    padding: 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
}
.manager-trigger {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--card-bg);
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text);
    transition: border-color 0.2s;
}
.manager-trigger:hover { border-color: var(--primary); }
.manager-trigger .placeholder { color: var(--text-muted); }
.manager-trigger .arrow { margin-left: auto; color: var(--text-muted); font-size: 0.7rem; }

/* ----- Modal Bottom Sheet (mobile) ----- */
@media (max-width: 768px) {
    [style*="position:fixed"][style*="align-items:center"] {
        align-items: flex-end !important;
    }
    [style*="position:fixed"][style*="align-items:center"] > .card,
    [style*="position:fixed"][style*="align-items:center"] > div > .card {
        border-radius: 16px 16px 0 0 !important;
        max-width: 100% !important;
        width: 100% !important;
        margin: 0 !important;
        max-height: 85vh;
        overflow-y: auto;
    }
}

/* ----- Detail Key-Value Table (mobile-friendly) ----- */
.detail-table {
    width: 100%;
    border-collapse: collapse;
}
.detail-table td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
.detail-table td:first-child {
    font-weight: 600;
    color: var(--text-muted);
    white-space: nowrap;
    width: 150px;
}
@media (max-width: 768px) {
    .detail-table,
    .detail-table tbody,
    .detail-table tr,
    .detail-table td {
        display: block;
    }
    .detail-table tr {
        padding: 8px 0;
        border-bottom: 1px solid var(--border);
    }
    .detail-table td {
        border-bottom: none;
        padding: 3px 0;
    }
    .detail-table td:first-child {
        width: auto;
        font-size: 0.75rem;
        margin-bottom: 2px;
    }
}

/* ----- Form Mobile Enhancements ----- */
@media (max-width: 768px) {
    .form-group input:not([type="checkbox"]):not([type="radio"]),
    .form-group select,
    .form-group textarea {
        padding: 12px 14px;
        font-size: 16px;
    }
    .form-actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
    .form-actions .btn {
        width: 100%;
        text-align: center;
    }

    /* Prevent iOS auto-zoom on all text inputs */
    input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="button"]):not([type="color"]):not([type="range"]),
    select,
    textarea {
        font-size: 16px;
    }
}

/* ----- Responsive Tabs ----- */
.tab-nav {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--border);
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.tab-nav a,
.tab-nav button {
    padding: 10px 16px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    text-decoration: none;
    white-space: nowrap;
}
.tab-nav a.active,
.tab-nav button.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 600;
}
@media (max-width: 768px) {
    .tab-nav {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    .tab-nav::-webkit-scrollbar { display: none; }
    .tab-nav a,
    .tab-nav button {
        flex-shrink: 0;
    }
}
