:root {
    --bg: #f0f4f8;
    --surface: #ffffff;
    --surface-soft: #f7f9fb;
    --text: #1a2332;
    --muted: #64748b;
    --line: #e2e8f0;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #eff6ff;
    --accent: #7c3aed;
    --danger: #dc2626;
    --success: #16a34a;
    --warning: #d97706;
    --info: #0ea5e9;
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 4px 16px rgba(0,0,0,0.04);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.08);
    --radius: 10px;
    --sidebar-bg: #0f172a;
    --sidebar-text: #cbd5e1;
    --sidebar-active: rgba(255,255,255,0.08);
    --sidebar-hover: rgba(255,255,255,0.05);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 14px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
}

a {
    color: inherit;
    text-decoration: none;
}

button,
input,
select,
textarea {
    font: inherit;
}

/* ── Layout ── */
.app-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
}

/* ── Mobile Header ── */
.mobile-header {
    display: none;
}

/* ── Sidebar ── */
.sidebar {
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    padding: 24px 14px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    border-right: 1px solid rgba(255,255,255,0.06);
}

.sidebar::-webkit-scrollbar {
    width: 4px;
}

.sidebar::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.1);
    border-radius: 4px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 10px 24px;
    font-size: 17px;
    font-weight: 800;
    color: #fff;
    letter-spacing: -0.3px;
}

.brand-mark {
    display: grid;
    width: 38px;
    height: 38px;
    place-items: center;
    border-radius: 10px;
    background: var(--primary);
    color: #fff;
    font-size: 15px;
    font-weight: 800;
}

.nav-section {
    margin-top: 8px;
}

.nav-label {
    color: #475569;
    font-size: 11px;
    font-weight: 700;
    padding: 14px 10px 8px;
    text-transform: uppercase;
    letter-spacing: 0.8px;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 42px;
    padding: 8px 12px;
    border-radius: 8px;
    color: var(--sidebar-text);
    margin: 2px 0;
    font-weight: 500;
    transition: background 0.15s, color 0.15s;
}

.nav-link svg {
    width: 18px;
    height: 18px;
    opacity: 0.7;
}

.nav-link:hover {
    background: var(--sidebar-hover);
    color: #e2e8f0;
}

.nav-link.active {
    background: var(--primary);
    color: #fff;
    font-weight: 600;
}

.nav-link.active svg {
    opacity: 1;
}

.nav-group {
    margin: 2px 0;
}

.nav-parent {
    width: 100%;
    border: 0;
    background: transparent;
    cursor: pointer;
    text-align: left;
}

.nav-parent .nav-caret {
    margin-left: auto;
    width: 16px;
    height: 16px;
    color: #475569;
    transition: transform 0.2s;
}

.nav-group.open .nav-caret {
    color: #94a3b8;
    transform: rotate(180deg);
}

.nav-submenu {
    display: grid;
    gap: 2px;
    margin: 4px 0 8px 18px;
    padding-left: 12px;
    border-left: 1px solid rgba(255,255,255,0.08);
}

.nav-group:not(.open) .nav-submenu {
    display: none;
}

.nav-sublink {
    display: flex;
    align-items: center;
    gap: 9px;
    min-height: 36px;
    padding: 6px 10px;
    border-radius: 6px;
    color: #94a3b8;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.15s, color 0.15s;
}

.nav-sublink svg {
    width: 16px;
    height: 16px;
    flex: 0 0 auto;
    opacity: 0.6;
}

.nav-sublink:hover {
    background: var(--sidebar-hover);
    color: #e2e8f0;
}

.nav-sublink.active {
    background: rgba(37,99,235,0.15);
    color: #93c5fd;
    font-weight: 600;
}

.nav-sublink.active svg {
    opacity: 1;
}

/* ── Main content ── */
.main {
    min-width: 0;
    padding: 28px 32px;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
}

.page-title {
    margin: 0;
    font-size: 22px;
    font-weight: 800;
    line-height: 1.3;
    letter-spacing: -0.3px;
}

.page-subtitle {
    color: var(--muted);
    margin-top: 2px;
    font-size: 13px;
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 8px;
}

.avatar {
    display: grid;
    width: 36px;
    height: 36px;
    place-items: center;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    font-weight: 800;
    font-size: 14px;
}

/* ── Grid ── */
.grid {
    display: grid;
    gap: 16px;
}

.grid-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

.grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

/* ── Card ── */
.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.metric {
    padding: 20px;
}

.metric-label {
    color: var(--muted);
    font-weight: 600;
    font-size: 13px;
}

.metric-value {
    margin-top: 8px;
    font-size: 28px;
    font-weight: 800;
    letter-spacing: -0.5px;
}

.metric-note {
    margin-top: 4px;
    color: var(--muted);
    font-size: 13px;
}

.panel {
    padding: 20px;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.panel-title {
    font-size: 16px;
    font-weight: 700;
}

/* ── Toolbar ── */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.search-form {
    display: flex;
    gap: 10px;
    flex: 1;
    min-width: 240px;
}

/* ── Form ── */
.field,
.form-control {
    width: 100%;
}

.form-control {
    min-height: 42px;
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 8px;
    color: var(--text);
    padding: 10px 12px;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.form-control:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

textarea.form-control {
    min-height: 104px;
    resize: vertical;
}

label {
    display: block;
    margin-bottom: 6px;
    color: #374151;
    font-weight: 600;
    font-size: 13px;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}

.form-span {
    grid-column: 1 / -1;
}

/* ── Button ── */
.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 40px;
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 8px 16px;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    font-weight: 600;
    white-space: nowrap;
    transition: background 0.15s, box-shadow 0.15s;
}

.button svg {
    width: 16px;
    height: 16px;
}

.button:hover {
    background: var(--primary-dark);
    box-shadow: 0 2px 8px rgba(37,99,235,0.25);
}

.button.secondary {
    background: #ffffff;
    color: var(--text);
    border-color: var(--line);
}

.button.secondary:hover {
    background: var(--surface-soft);
    border-color: #cbd5e1;
}

.button.danger {
    background: #fff;
    color: var(--danger);
    border-color: #fecaca;
}

.button.danger:hover {
    background: #fef2f2;
}

.button.icon {
    width: 38px;
    padding: 0;
}

/* ── Table ── */
.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
}

table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
}

th,
td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: middle;
}

th {
    background: var(--surface-soft);
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

tr:last-child td {
    border-bottom: 0;
}

tbody tr:hover td {
    background: #f8fafc;
}

.actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
}

/* ── Badge ── */
.badge {
    display: inline-flex;
    align-items: center;
    min-height: 24px;
    border-radius: 6px;
    padding: 3px 10px;
    background: var(--surface-soft);
    color: var(--primary-dark);
    font-weight: 600;
    font-size: 12px;
}

.badge.info,
.badge.confirmed {
    background: #eff6ff;
    color: var(--primary);
}

.badge.warning {
    background: #fffbeb;
    color: var(--warning);
}

.badge.success {
    background: #f0fdf4;
    color: var(--success);
}

.badge.danger {
    background: #fef2f2;
    color: var(--danger);
}

.badge.pending,
.badge.open {
    background: #fffbeb;
    color: var(--warning);
}

.badge.progress,
.badge.in-progress,
.badge.partial {
    background: #f5f3ff;
    color: var(--accent);
}

.badge.completed,
.badge.paid,
.badge.active {
    background: #f0fdf4;
    color: var(--success);
}

.badge.cancelled,
.badge.inactive,
.badge.overdue,
.badge.out {
    background: #fef2f2;
    color: var(--danger);
}

.badge.low,
.badge.material {
    background: #fffbeb;
    color: var(--warning);
}

.badge.product {
    background: #eff6ff;
    color: var(--primary);
}

/* ── Status rows ── */
.status-row td:first-child {
    box-shadow: inset 3px 0 0 transparent;
}

.status-row.status-pending td,
.status-row.status-open td {
    background: #fffdf7;
}

.status-row.status-confirmed td {
    background: #f8faff;
}

.status-row.status-in-progress td,
.status-row.status-partial td {
    background: #faf8ff;
}

.status-row.status-completed td,
.status-row.status-paid td,
.status-row.status-active td,
.status-row.status-ok td {
    background: #f8fdf9;
}

.status-row.status-cancelled td,
.status-row.status-inactive td,
.status-row.status-overdue td,
.status-row.status-out td {
    background: #fff8f7;
}

.status-row.status-low td {
    background: #fffdf7;
}

.status-row.status-pending td:first-child,
.status-row.status-open td:first-child,
.status-row.status-low td:first-child {
    box-shadow: inset 3px 0 0 var(--warning);
}

.status-row.status-confirmed td:first-child {
    box-shadow: inset 3px 0 0 var(--primary);
}

.status-row.status-in-progress td:first-child,
.status-row.status-partial td:first-child {
    box-shadow: inset 3px 0 0 var(--accent);
}

.status-row.status-completed td:first-child,
.status-row.status-paid td:first-child,
.status-row.status-active td:first-child,
.status-row.status-ok td:first-child {
    box-shadow: inset 3px 0 0 var(--success);
}

.status-row.status-cancelled td:first-child,
.status-row.status-inactive td:first-child,
.status-row.status-overdue td:first-child,
.status-row.status-out td:first-child {
    box-shadow: inset 3px 0 0 var(--danger);
}

/* ── Alert ── */
.alert {
    border-radius: var(--radius);
    padding: 12px 16px;
    margin-bottom: 16px;
    border: 1px solid var(--line);
    font-weight: 500;
}

.alert.success {
    background: #f0fdf4;
    color: #166534;
    border-color: #bbf7d0;
}

.alert.error {
    background: #fef2f2;
    color: var(--danger);
    border-color: #fecaca;
}

/* ── Pagination ── */
.pagination {
    margin-top: 16px;
}

.pagination-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.pagination-summary {
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
}

.pagination-links {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap;
}

.page-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    min-height: 36px;
    padding: 6px 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: var(--muted);
    font-weight: 600;
    transition: all 0.15s;
}

.page-link:hover {
    background: var(--primary-light);
    color: var(--primary);
    border-color: #bfdbfe;
}

.page-link.active {
    border-color: var(--primary);
    background: var(--primary);
    color: #fff;
}

.page-link.disabled {
    background: var(--surface-soft);
    color: #cbd5e1;
    cursor: not-allowed;
}

/* ── Chart ── */
.chart {
    display: flex;
    align-items: end;
    gap: 10px;
    min-height: 230px;
    padding-top: 12px;
}

.bar {
    flex: 1;
    min-width: 28px;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: end;
    gap: 8px;
}

.bar-fill {
    width: 100%;
    min-height: 4px;
    border-radius: 6px 6px 2px 2px;
    background: var(--primary);
}

.bar-label {
    color: var(--muted);
    font-size: 12px;
}

/* ── Auth ── */
.auth-shell {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background: var(--sidebar-bg);
}

.auth-card {
    width: min(100%, 420px);
    padding: 32px;
    border-radius: 16px;
}

.auth-card .brand {
    color: var(--text);
    padding: 0 0 24px;
}

/* ── Invoice ── */
.payment-methods {
    display: flex;
    gap: 16px;
    margin-bottom: 12px;
}

.payment-methods > div {
    flex: 1;
}

.print-heading {
    display: flex;
    justify-content: space-between;
    gap: 16px;
}

.invoice-total {
    font-size: 24px;
    font-weight: 850;
}

/* ── Hamburger ── */
.hamburger {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 40px;
    height: 40px;
    padding: 8px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.15s;
}

.hamburger:hover {
    background: rgba(0,0,0,0.04);
}

.hamburger span {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--text);
    border-radius: 2px;
    transition: transform 0.25s, opacity 0.2s;
}

.hamburger.active span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}

.hamburger.active span:nth-child(2) {
    opacity: 0;
}

.hamburger.active span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

/* ── Sidebar overlay ── */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 998;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.sidebar-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.mobile-only {
    display: none;
}

/* ── Responsive ── */
@media (max-width: 1100px) {
    .grid-4 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 820px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    /* Mobile header */
    .mobile-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 10px 16px;
        background: #fff;
        border-bottom: 1px solid var(--line);
        position: sticky;
        top: 0;
        z-index: 999;
    }

    .mobile-header .brand {
        padding: 0;
        font-size: 16px;
        color: var(--text);
    }

    .mobile-header .brand-mark {
        width: 34px;
        height: 34px;
        font-size: 14px;
    }

    .hamburger {
        display: flex;
    }

    /* Sidebar slide from right */
    .sidebar {
        position: fixed;
        top: 0;
        right: 0;
        width: 280px;
        height: 100vh;
        z-index: 999;
        transform: translateX(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        padding-top: 20px;
        box-shadow: none;
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: -8px 0 30px rgba(0,0,0,0.2);
    }

    .sidebar-overlay {
        display: block;
    }

    /* Main content */
    .main {
        padding: 16px 12px;
        min-width: 0;
        overflow-x: hidden;
    }

    /* Prevent overflow on all containers */
    .card,
    .panel,
    .main-body,
    section {
        min-width: 0;
    }

    /* Table scroll - key fix */
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin-left: -12px;
        margin-right: -12px;
        border-radius: 0;
        border-left: 0;
        border-right: 0;
    }

    table {
        min-width: 580px;
    }

    th, td {
        padding: 10px 12px;
        font-size: 13px;
    }

    /* Topbar */
    .topbar {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .page-title {
        font-size: 18px;
    }

    /* Toolbar & search */
    .toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .search-form {
        flex-direction: column;
        align-items: stretch;
        min-width: 0;
    }

    .search-form input[style*="max-width"],
    .search-form select[style*="width: auto"] {
        max-width: none !important;
        width: 100% !important;
    }

    /* Grids stack on mobile */
    .grid-4 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .grid-2,
    .form-grid {
        grid-template-columns: 1fr;
    }

    /* Panel header with actions */
    .panel-header {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .panel-header .actions {
        flex-wrap: wrap;
    }

    .panel-header .actions .button {
        flex: 1;
        min-width: 0;
    }

    /* Actions */
    .actions {
        justify-content: flex-start;
        flex-wrap: wrap;
    }

    /* User menu */
    .user-menu {
        flex-wrap: wrap;
        gap: 8px;
    }

    .user-menu .button,
    .user-menu form,
    .user-menu form .button {
        flex: 1;
        min-width: 120px;
    }

    /* Invoice form - payment section */
    .payment-methods {
        flex-direction: column;
        gap: 10px;
    }

    /* Print heading */
    .print-heading {
        flex-direction: column;
        gap: 12px;
    }

    .print-heading > div[style*="text-align:right"] {
        text-align: left !important;
    }

    .invoice-total {
        font-size: 20px;
    }

    /* Metric cards */
    .metric {
        padding: 14px;
    }

    .metric-value {
        font-size: 22px;
    }

    .panel {
        padding: 16px;
    }

    .card .table-wrap,
    .panel .table-wrap {
        margin-left: -16px;
        margin-right: -16px;
    }

    /* Chart scroll */
    .chart {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        min-height: 200px;
        padding-bottom: 4px;
    }

    .bar {
        min-width: 32px;
    }

    .bar-label {
        font-size: 10px;
    }

    /* Pagination */
    .pagination-nav {
        align-items: stretch;
        flex-direction: column;
    }

    .pagination-links {
        display: flex;
        flex-wrap: wrap;
        gap: 4px;
    }

    .page-link {
        min-width: 32px;
        min-height: 32px;
        padding: 4px 8px;
        font-size: 12px;
    }

    /* Badge */
    .badge {
        font-size: 11px;
        padding: 2px 8px;
    }

    /* Button on mobile */
    .button {
        font-size: 13px;
        padding: 8px 12px;
        min-height: 38px;
    }
}

@media (max-width: 520px) {
    /* Stack metric grid to 1 col */
    .grid-4 {
        grid-template-columns: 1fr;
    }

    .user-menu .button,
    .user-menu form,
    .user-menu form .button {
        width: 100%;
        min-width: 0;
    }

    /* Full width action buttons */
    .toolbar .button,
    .toolbar a.button {
        width: 100%;
        justify-content: center;
    }
}

@media print {
    body {
        background: #fff;
    }

    .sidebar,
    .topbar,
    .mobile-header,
    .no-print {
        display: none !important;
    }

    .app-shell {
        display: block;
    }

    .main {
        padding: 0;
    }

    .card {
        border: 0;
        box-shadow: none;
    }
}
