:root {
    color-scheme: light;
    --navy: #074b82;
    --blue: #0f66a8;
    --blue-soft: #eaf3fb;
    --orange: #f47a20;
    --orange-dark: #d95f0c;
    --ink: #25344a;
    --muted: #6c7b8f;
    --line: #dce4ed;
    --surface: #ffffff;
    --canvas: #f4f7fb;
    --success: #16845b;
    --danger: #c53f47;
    --warning: #c27609;
    --radius: 6px;
    --shadow: 0 5px 18px rgba(30, 54, 82, .08);
    font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { font-size: 16px; background: var(--canvas); }
body { margin: 0; min-height: 100vh; background: var(--canvas); color: var(--ink); line-height: 1.45; }
a { color: var(--blue); text-decoration: none; }
button, input, select, textarea { font: inherit; }
button, .button { cursor: pointer; }

.topbar {
    min-height: 64px;
    padding: 0 22px;
    display: flex;
    align-items: center;
    gap: 26px;
    color: white;
    background: linear-gradient(90deg, var(--navy), var(--blue));
    box-shadow: 0 2px 8px rgba(0, 43, 80, .18);
    position: sticky;
    top: 0;
    z-index: 60;
}
.brand { color: white; display: flex; align-items: center; gap: 10px; font-weight: 720; white-space: nowrap; }
.brand-mark { display: inline-grid; place-items: center; width: 34px; height: 34px; background: #ffca28; color: var(--navy); border-radius: 7px; font-weight: 850; letter-spacing: -.05em; }
.main-nav { display: flex; align-self: stretch; align-items: stretch; flex: 1; gap: 2px; }
.nav-group { position: relative; display: flex; align-items: stretch; }
.nav-link, .account-button { border: 0; background: transparent; color: rgba(255,255,255,.88); padding: 0 15px; display: flex; align-items: center; gap: 7px; font-weight: 620; font-size: .91rem; }
.nav-link:hover, .nav-link.active, .account-button:hover { color: #fff; background: rgba(255,255,255,.1); }
.nav-link.active { box-shadow: inset 0 -3px 0 var(--orange); }
.nav-dropdown { display: none; position: absolute; min-width: 220px; top: calc(100% - 1px); left: 0; background: #fff; color: var(--ink); box-shadow: var(--shadow); border: 1px solid var(--line); border-radius: 0 0 6px 6px; padding: 8px 0; z-index: 80; }
.nav-group:hover > .nav-dropdown, .nav-group:focus-within > .nav-dropdown { display: block; }
.nav-dropdown a, .nav-dropdown button { display: block; width: 100%; text-align: left; padding: 11px 16px; color: var(--ink); border: 0; background: #fff; font-size: .9rem; }
.nav-dropdown a:hover, .nav-dropdown button:hover { background: var(--blue-soft); color: var(--blue); }
.account-menu { margin-left: auto; }
.account-button { padding-right: 0; }
.avatar { width: 32px; height: 32px; display: inline-grid; place-items: center; border-radius: 50%; background: var(--orange); color: #fff; }
.account-dropdown { right: 0; left: auto; }
.account-dropdown form { margin: 0; }
.mobile-menu { display: none; margin-left: auto; border: 1px solid rgba(255,255,255,.3); color: white; background: transparent; border-radius: 4px; padding: 6px 10px; }

.app-main { width: min(1480px, calc(100% - 48px)); margin: 0 auto; padding: 30px 0 70px; }
.app-main-full { width: calc(100% - 48px); }
.page-header { min-height: 64px; margin: -30px calc(50% - 50vw) 26px; padding: 18px max(24px, calc((100vw - 1480px) / 2)); background: #fff; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.app-main-full .page-header { padding-left: 24px; padding-right: 24px; }
.page-header h1 { margin: 0; font-size: 1.45rem; font-weight: 620; }
.page-header p { margin: 3px 0 0; color: var(--muted); font-size: .88rem; }
.page-actions { display: flex; gap: 10px; align-items: center; }

.button, button.button { display: inline-flex; align-items: center; justify-content: center; min-height: 42px; border: 1px solid var(--line); border-radius: 5px; padding: 8px 16px; background: #fff; color: var(--ink); font-weight: 650; font-size: .9rem; }
.button:hover { border-color: #b9c7d5; background: #f9fbfd; }
.button-primary { color: white; border-color: var(--orange); background: var(--orange); }
.button-primary:hover { color: white; border-color: var(--orange-dark); background: var(--orange-dark); }
.button-danger { color: var(--danger); }
.button-small { min-height: 34px; padding: 5px 10px; font-size: .82rem; }
.button-icon { min-width: 42px; padding-inline: 10px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 1px 2px rgba(27, 45, 68, .02); }
.card-header { display: flex; justify-content: space-between; gap: 16px; align-items: center; padding: 20px 22px 14px; }
.card-header h2, .card h2 { font-size: 1.08rem; font-weight: 630; margin: 0; }
.card-body { padding: 20px 22px; }
.card + .card { margin-top: 20px; }

.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; margin-bottom: 20px; }
.metric { padding: 20px; position: relative; overflow: hidden; }
.metric::before { content: ''; width: 4px; position: absolute; inset: 0 auto 0 0; background: var(--blue); }
.metric.orange::before { background: var(--orange); }
.metric.green::before { background: var(--success); }
.metric.red::before { background: var(--danger); }
.metric-label { color: var(--muted); font-size: .84rem; font-weight: 650; }
.metric-value { margin-top: 8px; font-size: 1.65rem; font-weight: 690; letter-spacing: -.025em; }
.metric-meta { color: var(--muted); margin-top: 4px; font-size: .78rem; }
.dashboard-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(320px, .8fr); gap: 20px; align-items: start; }
.dashboard-stack { display: grid; gap: 20px; }
.chart-wrap { padding: 4px 4px 0; }
.chart { height: 300px; display: flex; gap: 12px; align-items: stretch; padding: 18px 8px 0; border-bottom: 1px solid var(--line); background: repeating-linear-gradient(to top, transparent 0, transparent 59px, #edf1f5 60px); }
.chart-month { flex: 1; min-width: 34px; display: flex; flex-direction: column; justify-content: end; align-items: stretch; gap: 6px; text-align: center; position: relative; }
.chart-bars { height: 235px; display: flex; gap: 4px; align-items: end; justify-content: center; }
.chart-bar { width: min(28px, 42%); min-height: 2px; background: var(--blue); border-radius: 3px 3px 0 0; position: relative; }
.chart-bar.expense { background: var(--orange); }
.chart-bar:hover::after { content: attr(data-value); position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); white-space: nowrap; background: var(--ink); color: white; padding: 4px 7px; border-radius: 4px; font-size: .72rem; z-index: 5; }
.chart-label { height: 32px; font-size: .74rem; color: var(--muted); white-space: nowrap; }
.legend { display: flex; gap: 18px; color: var(--muted); font-size: .78rem; }
.legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; background: var(--blue); }
.legend i.expense { background: var(--orange); }

.toolbar { padding: 16px 18px; display: flex; align-items: center; flex-wrap: wrap; gap: 10px; border-bottom: 1px solid var(--line); }
.toolbar .search { min-width: min(360px, 100%); flex: 1; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; min-width: 720px; }
th, td { text-align: left; border-bottom: 1px solid var(--line); padding: 13px 16px; vertical-align: middle; }
th { color: var(--muted); font-size: .76rem; text-transform: uppercase; letter-spacing: .035em; background: #fafcfe; }
td { font-size: .9rem; }
tr:hover td { background: #fbfdff; }
.numeric { text-align: right; font-variant-numeric: tabular-nums; }
.actions { display: flex; justify-content: flex-end; gap: 6px; }
.badge { display: inline-flex; border-radius: 999px; padding: 4px 9px; background: #edf2f7; color: #5d6d80; font-size: .75rem; font-weight: 680; }
.badge-paid, .badge-accepted { background: #e7f6ef; color: #14734f; }
.badge-sent { background: #e6f1fb; color: #17659b; }
.badge-overdue, .badge-rejected, .badge-cancelled { background: #fcebee; color: #a92d3b; }
.badge-draft { background: #eef1f5; color: #667486; }

.form-card { padding: 24px; }
.form-section { padding: 0 0 25px; margin-bottom: 25px; border-bottom: 1px solid var(--line); }
.form-section:last-child { border-bottom: 0; margin-bottom: 0; padding-bottom: 0; }
.form-section h2 { margin: 0 0 18px; font-size: 1.05rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px 20px; }
.form-grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-wide { grid-column: 1 / -1; }
label { font-size: .82rem; font-weight: 650; color: #43536a; }
input, select, textarea { width: 100%; min-height: 42px; padding: 9px 11px; border: 1px solid #cfd9e4; border-radius: 4px; color: var(--ink); background: #fff; outline: none; }
textarea { min-height: 105px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(15,102,168,.1); }
input[type="checkbox"], input[type="radio"] { width: auto; min-height: auto; accent-color: var(--orange); }
.choice { display: flex; gap: 9px; align-items: flex-start; font-size: .9rem; }
.help { color: var(--muted); font-size: .77rem; font-weight: 400; }
.error-text { color: var(--danger); font-size: .8rem; }
.form-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 22px; }

.split-view { display: grid; grid-template-columns: minmax(0, 1fr) 390px; gap: 22px; align-items: start; }
.sticky-preview { position: sticky; top: 86px; }
.document-editor { padding: 24px; }
.line-items { display: grid; gap: 10px; }
.line-item { display: grid; grid-template-columns: 34px minmax(220px, 2.2fr) 92px 80px 120px 90px 44px; gap: 8px; align-items: start; padding: 12px 0; border-bottom: 1px solid var(--line); }
.drag-handle { color: #a0adbb; padding-top: 11px; cursor: grab; }
.line-item textarea { min-height: 72px; margin-top: 7px; }
.line-item .remove-line { color: var(--danger); border: 0; background: transparent; height: 42px; font-size: 1.15rem; }
.line-total { padding-top: 11px; text-align: right; font-weight: 650; }
.totals { width: min(420px, 100%); margin: 20px 0 0 auto; }
.total-row { display: flex; justify-content: space-between; padding: 7px 0; }
.total-row.grand { border-top: 2px solid var(--ink); margin-top: 5px; padding-top: 11px; font-weight: 750; font-size: 1.08rem; }
.paper { width: 100%; aspect-ratio: 210 / 297; background: #fff; border: 1px solid #cfd8e2; box-shadow: 0 8px 30px rgba(30,50,70,.12); padding: 9%; font-family: Arial, sans-serif; color: #202938; font-size: 8px; display: flex; flex-direction: column; }
.paper-logo { min-height: 60px; display: flex; justify-content: flex-end; align-items: start; font-size: 18px; font-weight: 750; }
.paper-logo img { max-width: 48%; max-height: 58px; object-fit: contain; }
.paper-sender { font-size: 5.5px; text-decoration: underline; margin: 15px 0 8px; }
.paper-address { min-height: 73px; }
.paper-info { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.paper h3 { font-size: 14px; margin: 11px 0 8px; }
.paper-table { width: 100%; min-width: 0; font-size: 6.5px; }
.paper-table th, .paper-table td { padding: 4px 2px; }
.paper-table th { border-bottom: 2px solid var(--document-accent, #f47b20); }
.paper-layout-frame .paper-table td { border: 1px solid #dce4ed; }
.paper-layout-compact .paper-address { min-height: 90px !important; }
.paper-footer { margin-top: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; border-top: 1px solid #ccd4dc; padding-top: 7px; font-size: 5.3px; }

.settings-layout { display: grid; grid-template-columns: 230px minmax(0, 880px); gap: 24px; align-items: start; }
.settings-nav { position: sticky; top: 86px; overflow: hidden; }
.settings-nav a { display: block; padding: 13px 15px; color: var(--ink); border-left: 3px solid transparent; }
.settings-nav a:hover, .settings-nav a.active { color: var(--orange-dark); background: #fff7f1; border-left-color: var(--orange); }
.settings-content { min-width: 0; }
.color-row { display: flex; align-items: center; gap: 10px; }
.color-row input[type="color"] { width: 52px; padding: 3px; }
.logo-preview { max-width: 260px; max-height: 90px; object-fit: contain; display: block; margin-bottom: 14px; }

.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 30px 18px; background: linear-gradient(145deg, #edf4fa 0%, #f8fafc 48%, #e8f1f8 100%); }
.auth-card { width: min(440px, 100%); background: #fff; border: 1px solid var(--line); border-radius: 9px; box-shadow: 0 18px 55px rgba(21,55,82,.14); padding: 34px; }
.auth-logo { display: flex; align-items: center; gap: 12px; color: var(--ink); font-size: 1.3rem; font-weight: 750; margin-bottom: 30px; }
.auth-card h1 { margin: 0 0 8px; font-size: 1.55rem; }
.auth-card > p { color: var(--muted); margin: 0 0 22px; }
.auth-card .field { margin-bottom: 15px; }
.auth-card .button { width: 100%; margin-top: 7px; }
.auth-links { display: flex; justify-content: space-between; gap: 12px; margin-top: 18px; font-size: .84rem; }
.setup-card { width: min(860px, 100%); }
.setup-progress { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 28px; }
.setup-step { padding: 9px; background: #edf2f7; color: var(--muted); border-radius: 4px; text-align: center; font-size: .8rem; font-weight: 650; }
.setup-step.active { background: var(--blue); color: #fff; }

.filter-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.filter-bar .field { min-width: 160px; }
.quick-filters { display: flex; gap: 6px; flex-wrap: wrap; }
.quick-filters a { padding: 7px 10px; background: #f1f5f9; border-radius: 4px; font-size: .8rem; color: var(--ink); }
.quick-filters a.active { background: var(--blue); color: white; }
.summary-table td:first-child { font-weight: 620; }
.summary-table tr:last-child td { border-top: 2px solid var(--ink); font-weight: 760; }
.empty-state { padding: 54px 20px; text-align: center; color: var(--muted); }
.empty-state h3 { color: var(--ink); margin: 9px 0 5px; }
.empty-icon { width: 44px; height: 44px; margin: auto; display: grid; place-items: center; border-radius: 50%; background: var(--blue-soft); color: var(--blue); font-size: 1.3rem; }
.toast { padding: 13px 16px; margin-bottom: 18px; border-radius: 5px; border: 1px solid; background: #fff; }
.toast-success { border-color: #b8dfcf; color: #146644; background: #effaf5; }
.toast-error { border-color: #f0bdc3; color: #992f3b; background: #fff3f4; }
.toast-info { border-color: #bcd7ec; color: #195d8b; background: #f0f7fc; }
.pagination { display: flex; justify-content: center; gap: 5px; padding: 18px; }
.pagination a { min-width: 36px; height: 36px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 4px; background: #fff; }
.pagination a.active { background: var(--blue); color: white; border-color: var(--blue); }
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--line); margin-bottom: 22px; }
.tabs a { padding: 12px 18px; color: var(--muted); border-bottom: 3px solid transparent; }
.tabs a.active { color: var(--orange-dark); border-bottom-color: var(--orange); }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.muted { color: var(--muted); }
.text-right { text-align: right; }
.mt-0 { margin-top: 0 !important; }

@media (max-width: 1100px) {
    .account-name { display: none; }
    .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .dashboard-grid, .split-view { grid-template-columns: 1fr; }
    .sticky-preview { position: static; max-width: 520px; margin-inline: auto; }
    .line-item { grid-template-columns: 28px minmax(180px, 1fr) 80px 70px 110px 80px 38px; }
}

@media (max-width: 820px) {
    .topbar { min-height: 58px; padding: 0 14px; }
    .mobile-menu { display: block; }
    .main-nav { display: none; position: absolute; top: 58px; left: 0; right: 0; background: var(--navy); padding: 8px; flex-direction: column; box-shadow: var(--shadow); }
    .main-nav.open { display: flex; }
    .nav-group { display: block; }
    .nav-link { min-height: 44px; width: 100%; }
    .nav-dropdown { position: static; box-shadow: none; border-radius: 4px; }
    .account-menu { margin-left: 0; }
    .account-button { padding: 0; }
    .app-main, .app-main-full { width: calc(100% - 24px); padding-top: 20px; }
    .page-header { margin-top: -20px; padding: 15px 12px; align-items: flex-start; }
    .page-header h1 { font-size: 1.22rem; }
    .page-actions { flex-wrap: wrap; justify-content: flex-end; }
    .settings-layout { grid-template-columns: 1fr; }
    .settings-nav { position: static; display: flex; overflow-x: auto; }
    .settings-nav a { white-space: nowrap; border-left: 0; border-bottom: 3px solid transparent; }
    .settings-nav a.active { border-bottom-color: var(--orange); }
    .form-grid, .form-grid.cols-3, .two-col { grid-template-columns: 1fr; }
    .field-wide { grid-column: auto; }
    .line-items { overflow-x: auto; }
    .line-item { min-width: 760px; }
    .chart { overflow-x: auto; }
    .chart-month { min-width: 52px; }
}

@media (max-width: 560px) {
    .brand > span:last-child { display: none; }
    .metric-grid { grid-template-columns: 1fr; }
    .page-header { flex-direction: column; }
    .page-actions { width: 100%; justify-content: stretch; }
    .page-actions .button { flex: 1; }
    .card-header, .card-body, .form-card, .document-editor { padding-left: 16px; padding-right: 16px; }
    .toolbar { align-items: stretch; }
    .toolbar > * { width: 100%; }
    .filter-bar .field { width: 100%; }
    .auth-card { padding: 25px 20px; }
}

@media print {
    .topbar, .page-header, .no-print, .toast { display: none !important; }
    body, .app-main, .app-main-full { background: #fff; width: 100%; margin: 0; padding: 0; }
    .paper { border: 0; box-shadow: none; width: 210mm; height: 297mm; padding: 18mm; }
}
