:root {
    color-scheme: dark;
    --bg: #090b10;
    --bg-elevated: #10141c;
    --surface: #151a24;
    --surface-hover: #1b2230;
    --sidebar: #0c1018;
    --line: rgba(255, 255, 255, 0.08);
    --line-strong: rgba(255, 255, 255, 0.14);
    --text: #eef1f6;
    --text-soft: #c5ccd8;
    --muted: #8b95a7;
    --accent: #6ea8fe;
    --accent-strong: #8bbcff;
    --accent-soft: rgba(110, 168, 254, 0.14);
    --ok: #3dd68c;
    --ok-soft: rgba(61, 214, 140, 0.14);
    --warn: #e6c15a;
    --warn-soft: rgba(230, 193, 90, 0.14);
    --err: #ff6b7a;
    --err-soft: rgba(255, 107, 122, 0.14);
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
    --radius: 12px;
    --radius-sm: 8px;
    --nav-w: 236px;
    --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
    --mono: ui-monospace, "SF Mono", "IBM Plex Mono", monospace;
}

html[data-theme="light"] {
    color-scheme: light;
    --bg: #eef2f7;
    --bg-elevated: #ffffff;
    --surface: #ffffff;
    --surface-hover: #f7f9fc;
    --sidebar: #10141c;
    --line: rgba(16, 20, 28, 0.1);
    --line-strong: rgba(16, 20, 28, 0.16);
    --text: #12151c;
    --text-soft: #3a4254;
    --muted: #667085;
    --accent: #3b7dff;
    --accent-strong: #2f6fed;
    --accent-soft: rgba(59, 125, 255, 0.12);
    --ok: #1f9d63;
    --ok-soft: rgba(31, 157, 99, 0.12);
    --warn: #b5810a;
    --warn-soft: rgba(181, 129, 10, 0.12);
    --err: #d6455d;
    --err-soft: rgba(214, 69, 93, 0.12);
    --shadow: 0 16px 40px rgba(16, 20, 28, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    -webkit-font-smoothing: antialiased;
    font-family: var(--sans);
    background: var(--bg);
    color: var(--text);
    letter-spacing: -0.02em;
    font-size: 15px;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-strong); }

.shell {
    display: grid;
    grid-template-columns: var(--nav-w) 1fr;
    min-height: 100vh;
}

.sidebar {
    background: var(--sidebar);
    border-right: 1px solid var(--line);
    padding: 22px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    position: sticky;
    top: 0;
    height: 100vh;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #fff;
    font-weight: 700;
    font-size: 1.15rem;
    letter-spacing: -0.03em;
    padding: 6px 8px 14px;
}
.brand-mark {
    width: 32px;
    height: 32px;
    border-radius: 9px;
    background: linear-gradient(180deg, #8bbcff, #3b7dff);
    display: grid;
    place-items: center;
    font-size: 0.85rem;
    font-weight: 800;
    color: #fff;
    box-shadow: 0 8px 20px rgba(59, 125, 255, 0.35);
}
.brand-sub {
    margin: 0 8px 18px;
    color: #8b95a7;
    font-size: 0.78rem;
    font-weight: 500;
}

.sidebar nav { display: flex; flex-direction: column; gap: 4px; }
.sidebar nav a {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #c5ccd8;
    padding: 10px 12px;
    border-radius: 12px;
    font-size: 0.94rem;
    font-weight: 500;
}
.sidebar nav a svg { width: 18px; height: 18px; opacity: 0.85; flex: none; }
.sidebar nav a.is-active,
.sidebar nav a:hover {
    background: rgba(255, 255, 255, 0.06);
    color: #fff;
}
.sidebar nav a.is-active {
    background: var(--accent-soft);
    color: #fff;
    box-shadow: inset 0 0 0 1px rgba(110, 168, 254, 0.25);
}

.sidebar-foot {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-top: 16px;
}
.theme-toggle,
.logout {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    border: 0;
    background: transparent;
    color: #8b95a7;
    padding: 10px 12px;
    border-radius: 12px;
    cursor: pointer;
    font: inherit;
    text-align: left;
}
.theme-toggle:hover,
.logout:hover { background: rgba(255,255,255,0.06); color: #fff; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 14px 24px;
    border-bottom: 1px solid var(--line);
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(18px);
    position: sticky;
    top: 0;
    z-index: 5;
}
.topbar .company-switch { margin-right: auto; }

.company-switch label {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}
.company-switch select {
    min-width: 220px;
    font-weight: 600;
    text-transform: none;
    letter-spacing: 0;
}

.user-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 999px;
    background: var(--surface);
    border: 1px solid var(--line);
    color: var(--text-soft);
    font-size: 0.9rem;
    font-weight: 600;
}

.content { padding: 28px 32px 48px; max-width: 1120px; }

.page-head, .panel-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    margin-bottom: 22px;
}
h1 { font-size: 1.7rem; margin: 0 0 4px; letter-spacing: -0.04em; font-weight: 700; }
h2 { font-size: 1.02rem; margin: 0; font-weight: 650; }
.muted, .lede { color: var(--muted); margin: 0; }

input, textarea, select {
    font: inherit;
    padding: 11px 12px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text);
    width: 100%;
    outline: none;
}
input:focus, textarea:focus, select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--accent-soft);
}

.btn, button.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 14px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-weight: 600;
    font-size: 0.92rem;
    cursor: pointer;
    width: auto;
    text-decoration: none;
    transition: 0.15s ease;
}
.btn:hover { background: var(--surface-hover); color: var(--text); }
.btn-primary {
    background: var(--accent);
    border-color: transparent;
    color: #081018;
}
.btn-primary:hover { background: var(--accent-strong); color: #081018; }
.btn-ghost { background: transparent; }
.btn-danger { background: var(--err-soft); color: var(--err); border-color: transparent; }

.flash {
    padding: 12px 14px;
    border-radius: 12px;
    margin-bottom: 16px;
    border: 1px solid var(--line);
    background: var(--surface);
}
.flash-success { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.flash-error { background: var(--err-soft); color: var(--err); border-color: transparent; }
.flash-info, .flash-warning { background: var(--warn-soft); color: var(--warn); border-color: transparent; }

.stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    margin: 8px 0 24px;
}
.stats article, .panel, .card, fieldset {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px;
}
.stats article {
    background: linear-gradient(180deg, var(--surface) 0%, var(--bg-elevated) 100%);
}
.stats span, .stats small { color: var(--muted); font-size: 0.82rem; font-weight: 600; }
.stats strong {
    display: block;
    font-size: 1.55rem;
    margin: 10px 0 6px;
    letter-spacing: -0.04em;
    font-variant-numeric: tabular-nums;
}

.table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
}
.table th, .table td {
    text-align: left;
    padding: 13px 14px;
    border-bottom: 1px solid var(--line);
}
.table th {
    color: var(--muted);
    font-size: 0.75rem;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    font-weight: 700;
    background: var(--bg-elevated);
}
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: var(--surface-hover); }
.panel .table { border: 0; background: transparent; }
.table .num, .totals {
    text-align: right;
    font-variant-numeric: tabular-nums;
    font-family: var(--mono);
    font-size: 0.92rem;
}

.badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    background: var(--accent-soft);
    color: var(--accent-strong);
}
.badge-paid { background: var(--ok-soft); color: var(--ok); }
.badge-sent { background: var(--accent-soft); color: var(--accent-strong); }
.badge-open { background: var(--warn-soft); color: var(--warn); }
.badge-cancelled, .badge-draft { background: var(--surface-hover); color: var(--muted); }

.form-grid, .stack { display: grid; gap: 16px; }
.row-2, .row-3 { display: grid; gap: 12px; }
.row-2 { grid-template-columns: 1fr 1fr; }
.row-3 { grid-template-columns: 1fr 1fr 1fr; }
label { display: grid; gap: 6px; font-size: 0.88rem; color: var(--text-soft); font-weight: 600; }
label small { color: var(--muted); font-weight: 500; }
legend { font-weight: 700; padding: 0 4px; }
fieldset { display: grid; gap: 12px; }

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.filters a {
    padding: 7px 12px;
    border-radius: 999px;
    font-size: 0.85rem;
    font-weight: 600;
    background: var(--surface);
    color: var(--muted);
    border: 1px solid var(--line);
}
.filters a.is-active { background: var(--accent-soft); color: var(--accent-strong); border-color: transparent; }
.action-bar, .form-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 16px 0; }
.search { display: flex; gap: 8px; margin-bottom: 16px; }
.search input { max-width: 320px; }
.empty { color: var(--muted); padding: 28px 0; }

.auth-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background:
        radial-gradient(900px 420px at 50% -10%, rgba(110, 168, 254, 0.18), transparent 50%),
        var(--bg);
}
.auth-card {
    width: min(440px, 100%);
    background: var(--surface);
    border: 1px solid var(--line);
    padding: 32px;
    border-radius: 24px;
    box-shadow: var(--shadow);
}
.auth-card .brand { color: var(--text); padding: 0 0 8px; }
.auth-card h1 { margin-bottom: 8px; }
.check-list { padding-left: 18px; color: var(--muted); }
.check-list .ok { color: var(--ok); }
.check-list .fail { color: var(--err); }

.nav-toggle {
    width: 34px;
    height: 34px;
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--text);
    border-radius: 10px;
    cursor: pointer;
    font-size: 16px;
}
html.nav-collapsed .shell { grid-template-columns: 76px 1fr; }
html.nav-collapsed .brand-text,
html.nav-collapsed .brand-sub,
html.nav-collapsed .sidebar nav a span,
html.nav-collapsed .theme-toggle,
html.nav-collapsed .logout { display: none; }
html.nav-collapsed .sidebar { padding: 16px 10px; align-items: center; }
html.nav-collapsed .sidebar nav a { justify-content: center; padding: 10px; }
html.nav-collapsed .brand { padding: 6px 0 12px; }

.line-list { display: grid; gap: 12px; }
.line-row {
    display: grid;
    grid-template-columns: minmax(0, 2.2fr) 90px 120px 110px 80px 110px 40px;
    gap: 10px;
    align-items: end;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--bg-elevated);
}
.line-copy { display: grid; gap: 8px; }
.line-net {
    display: grid;
    gap: 6px;
    font-size: 0.78rem;
    color: var(--muted);
}
.line-net strong {
    color: var(--text);
    font-variant-numeric: tabular-nums;
    font-size: 0.95rem;
    line-height: 1.4;
    padding-bottom: 8px;
}
.invoice-totals {
    margin: 16px 0 0 auto;
    width: min(340px, 100%);
    display: grid;
    gap: 8px;
    padding: 14px 16px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--bg-elevated);
}
.invoice-totals-row {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    color: var(--muted);
    font-size: 0.92rem;
}
.invoice-totals-row strong {
    color: var(--text);
    font-variant-numeric: tabular-nums;
}
.invoice-totals-row.is-grand {
    margin-top: 4px;
    padding-top: 10px;
    border-top: 1px solid var(--line);
    color: var(--text);
    font-size: 1.05rem;
    font-weight: 700;
}
.line-details {
    color: var(--muted);
    font-size: 0.88rem;
    margin-top: 4px;
    white-space: pre-wrap;
}
.link-btn {
    border: 0;
    background: none;
    color: var(--accent-strong);
    font: inherit;
    font-size: 0.82rem;
    font-weight: 600;
    padding: 0;
    text-align: left;
    cursor: pointer;
    width: auto;
}
.check {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 10px 12px;
    align-items: start;
    font-weight: 600;
}
.check input { margin-top: 4px; width: auto; }
.check small { display: block; margin-top: 4px; font-weight: 500; color: var(--muted); }
.invoice-sheet .invoice-totals { margin-top: 18px; }
.invoice-board {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 16px;
    align-items: start;
}
.invoice-box {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px;
    min-height: 180px;
}
.invoice-box header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 12px;
}
.invoice-box h2 { font-size: 1.05rem; margin: 0; }
.invoice-box .count {
    min-width: 28px;
    height: 28px;
    border-radius: 999px;
    display: grid;
    place-items: center;
    font-size: 0.8rem;
    font-weight: 700;
    background: var(--surface-hover);
    color: var(--muted);
}
.invoice-box-draft { border-color: rgba(148, 163, 184, 0.4); }
.invoice-box-open { border-color: rgba(230, 193, 90, 0.35); }
.invoice-box-paid { border-color: rgba(61, 214, 140, 0.35); }
.invoice-box-cancelled { border-color: rgba(232, 93, 93, 0.28); }
.invoice-box-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.invoice-box-list li {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: center;
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--bg-elevated);
}
.invoice-box-list a {
    display: grid;
    gap: 2px;
    color: inherit;
    min-width: 0;
}
.invoice-box-list a span, .invoice-box-list a small {
    color: var(--muted);
    font-size: 0.82rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.pay-form { margin: 0; }
.btn-icon {
    width: 34px;
    height: 34px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--ok-soft);
    color: var(--ok);
    display: grid;
    place-items: center;
    cursor: pointer;
}
.btn-icon:hover { filter: brightness(1.08); }
@media (max-width: 1100px) {
    .invoice-board { grid-template-columns: 1fr; }
    .line-row { grid-template-columns: 1fr 1fr 1fr; }
    .line-copy { grid-column: 1 / -1; }
}
@media (max-width: 900px) {
    .shell { grid-template-columns: 1fr; }
    .sidebar { height: auto; position: relative; flex-direction: row; flex-wrap: wrap; }
    .sidebar nav { flex-direction: row; flex-wrap: wrap; }
    .sidebar-foot { margin: 0; flex-direction: row; padding: 0; }
    .stats, .row-2, .row-3 { grid-template-columns: 1fr; }
    .content { padding: 18px; }
    .topbar { padding: 14px 16px; }
    html.nav-collapsed .shell { grid-template-columns: 1fr; }
}
