:root {
    --canvas: #070707;
    --surface: #0d0d0d;
    --surface-raised: #131313;
    --surface-hover: #1a1a1a;
    --line: #292929;
    --line-strong: #454545;
    --text: #f7f7f2;
    --text-soft: #c7c7bf;
    --text-muted: #92928b;
    --accent: #fdfd96;
    --accent-readable: #fdfd96;
    --accent-hover: #ffffbd;
    --accent-soft: rgba(253, 253, 150, .1);
    --accent-border: rgba(253, 253, 150, .38);
    --accent-text: #111108;
    --danger: #e26868;
    --danger-soft: rgba(226, 104, 104, .12);
    --danger-border: rgba(226, 104, 104, .42);
    --success: #71c69c;
    --success-soft: rgba(113, 198, 156, .1);
    --success-border: rgba(113, 198, 156, .38);
    --warning: #e9bd6a;
    --warning-soft: rgba(233, 189, 106, .1);
    --backdrop: transparent;
    --modal-backdrop: rgba(0, 0, 0, .8);
    --sidebar: #030303;
    --input: #090909;
    --radius-panel: 14px;
    --radius-control: 8px;
    font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { color: var(--text); }
button, input, select, textarea { font: inherit; }
button { color: inherit; }

.app {
    display: none;
    width: 100%;
    height: 100%;
    align-items: center;
    justify-content: center;
    padding: 3vh 2.5vw;
    background: transparent;
}
.app.is-open { display: flex; }
.workspace {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-columns: 228px minmax(0, 1fr);
    width: min(1500px, 95vw);
    height: min(900px, 94vh);
    overflow: hidden;
    border: 1px solid var(--line-strong);
    border-radius: 16px;
    background: var(--canvas);
    box-shadow: 0 28px 80px rgba(0, 0, 0, .62);
    opacity: 1;
    visibility: visible;
    transform: none;
}
.sidebar { display: flex; min-width: 0; flex-direction: column; padding: 22px 16px; border-right: 1px solid var(--line); background: var(--sidebar); }
.brand { display: flex; align-items: center; gap: 12px; min-height: 50px; padding: 0 7px 18px; }
.brand-mark { display: grid; width: 48px; height: 48px; flex: 0 0 48px; place-items: center; border: 1px solid var(--accent-border); border-radius: 13px; background: var(--accent); color: var(--accent-text); font-size: 22px; font-weight: 900; box-shadow: inset 0 0 0 3px rgba(0, 0, 0, .09); }
.brand-copy { min-width: 0; }
.brand-copy strong, .brand-copy span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand-copy strong { font-size: 15px; letter-spacing: -.01em; }
.brand-copy span { margin-top: 3px; color: var(--text-muted); font-size: 11px; }
.navigation { display: grid; gap: 5px; margin-top: 10px; }
.nav-button { display: flex; width: 100%; min-height: 42px; align-items: center; justify-content: space-between; padding: 0 12px; border: 1px solid transparent; border-radius: var(--radius-control); background: transparent; color: var(--text-soft); text-align: left; cursor: pointer; transition: background 140ms ease, color 140ms ease, border-color 140ms ease; }
.nav-button:hover { background: var(--surface); color: var(--text); }
.nav-button.is-active { border-color: var(--accent-border); background: var(--accent-soft); color: var(--accent-readable); }
.nav-count { min-width: 24px; padding: 2px 6px; border-radius: 999px; background: var(--surface-raised); color: var(--text-muted); font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 10px; text-align: center; }
.nav-button.is-active .nav-count { background: var(--accent); color: var(--accent-text); }
.sidebar-footer { display: grid; gap: 3px; margin-top: auto; padding: 18px 8px 2px; border-top: 1px solid var(--line); }
.sidebar-footer strong, .sidebar-footer span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer strong { font-size: 14px; }
.sidebar-footer span:last-child { color: var(--text-muted); font-size: 12px; }
.muted { color: var(--text-muted); font-size: 11px; }

.content-column { display: flex; min-width: 0; min-height: 0; overflow: hidden; flex-direction: column; }
.topbar { display: flex; min-height: 88px; align-items: center; justify-content: space-between; gap: 16px; padding: 18px 28px; border-bottom: 1px solid var(--line); }
.kicker { margin: 0 0 3px; color: var(--text-muted); font-size: 12px; }
h1 { margin: 0; font-size: 24px; line-height: 1.2; letter-spacing: -.025em; }
h2, h3, p { margin-top: 0; }
h2 { margin-bottom: 5px; font-size: 17px; letter-spacing: -.015em; }
h3 { margin-bottom: 4px; font-size: 14px; }
.topbar-actions, .toolbar, .toolbar-group, .action-row, .pagination { display: flex; align-items: center; gap: 8px; }
.content { flex: 1; min-height: 0; overflow: auto; padding: 24px 28px 42px; overscroll-behavior: contain; scrollbar-color: var(--line-strong) transparent; scrollbar-width: thin; }
.content::-webkit-scrollbar { width: 8px; height: 8px; }
.content::-webkit-scrollbar-thumb { border: 2px solid transparent; border-radius: 999px; background: var(--line-strong); background-clip: padding-box; }
.stack { display: grid; gap: 18px; }

.button { display: inline-flex; min-height: 40px; align-items: center; justify-content: center; padding: 0 14px; border: 1px solid var(--line-strong); border-radius: var(--radius-control); outline: none; background: var(--surface-raised); color: var(--text); cursor: pointer; text-decoration: none; white-space: nowrap; transition: transform 110ms ease, border-color 140ms ease, background 140ms ease; }
.button:hover { border-color: #696969; background: var(--surface-hover); }
.button:active { transform: translateY(1px); }
.button:focus-visible, .nav-button:focus-visible, .table-action:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.button.primary { border-color: var(--accent); background: var(--accent); color: var(--accent-text); font-weight: 700; }
.button.primary:hover { background: var(--accent-hover); }
.button.danger { border-color: var(--danger-border); background: var(--danger-soft); color: #ffb7b7; }
.button.ghost { background: transparent; }
.button.compact { min-height: 36px; padding: 0 12px; font-size: 12px; }
.button:disabled { cursor: not-allowed; opacity: .45; transform: none; }

.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius-panel); background: var(--surface); }
.metric { min-width: 0; padding: 20px; border-right: 1px solid var(--line); }
.metric:last-child { border-right: 0; }
.metric-label { display: block; margin-bottom: 14px; color: var(--text-muted); font-size: 11px; }
.metric-value { display: block; overflow: hidden; font-family: ui-monospace, "Cascadia Mono", monospace; font-size: clamp(20px, 2vw, 30px); font-weight: 650; letter-spacing: -.05em; text-overflow: ellipsis; white-space: nowrap; }
.metric-meta { display: block; margin-top: 8px; color: var(--text-muted); font-size: 11px; }
.section-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; }
.section-heading p { margin: 4px 0 0; color: var(--text-muted); font-size: 12px; }

.panel { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius-panel); background: var(--surface); }
.panel-header { display: flex; min-height: 64px; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.panel-header h2, .panel-header p { margin: 0; }
.panel-header p { margin-top: 3px; color: var(--text-muted); font-size: 11px; }
.toolbar { flex-wrap: wrap; justify-content: space-between; padding: 0 0 4px; }
.toolbar-group { flex-wrap: wrap; }
.search-wrap { position: relative; width: min(330px, 42vw); }
.search-wrap input { padding-left: 12px; }

input, select, textarea { width: 100%; min-height: 40px; padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: var(--radius-control); outline: none; background: var(--input); color: var(--text); }
select { min-width: 150px; appearance: none; cursor: pointer; }
select option { background: var(--surface-raised); color: var(--text); }
input::placeholder, textarea::placeholder { color: var(--text-muted); }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { min-height: 94px; resize: vertical; }
.field { display: grid; gap: 7px; }
.field label { color: var(--text-soft); font-size: 12px; font-weight: 600; }
.field small { color: var(--text-muted); font-size: 11px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.form-grid .span-2 { grid-column: 1 / -1; }
.form-shell { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(250px, .6fr); gap: 18px; align-items: start; }
.form-panel { padding: 20px; }
.form-summary { position: sticky; top: 0; padding: 20px; }
.summary-line { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; color: var(--text-soft); font-size: 12px; }
.summary-line + .summary-line { border-top: 1px solid var(--line); }
.summary-line strong { color: var(--text); }
.summary-total { margin-top: 8px; padding-top: 15px; font-size: 14px; }
.summary-total strong { font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 20px; }

.invoice-table { min-width: 850px; }
.invoice-row { display: grid; grid-template-columns: 150px minmax(150px, 1.2fr) minmax(150px, 1.4fr) 130px 120px 92px; align-items: center; gap: 14px; min-height: 66px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.invoice-row:last-child { border-bottom: 0; }
.invoice-row.header { min-height: 42px; background: var(--surface-raised); color: var(--text-muted); font-size: 10px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; }
.invoice-row:not(.header):hover { background: var(--surface-hover); }
.invoice-cell { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.invoice-id { font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 12px; }
.invoice-party strong, .invoice-party span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.invoice-party strong { font-size: 13px; }
.invoice-party span { margin-top: 3px; color: var(--text-muted); font-size: 11px; }
.invoice-amount { font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 12px; font-weight: 700; }
.status { display: inline-flex; min-height: 25px; align-items: center; padding: 0 8px; border: 1px solid var(--line-strong); border-radius: 999px; background: var(--surface-raised); color: var(--text-soft); font-size: 10px; font-weight: 700; }
.status.paid, .status.refunded { border-color: var(--success-border); background: var(--success-soft); color: var(--success); }
.status.overdue, .status.rejected, .status.cancelled, .status.void { border-color: var(--danger-border); background: var(--danger-soft); color: #ffaaaa; }
.status.pending, .status.partially_paid { border-color: var(--accent-border); background: var(--accent-soft); color: var(--accent-readable); }
.table-action { min-height: 32px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: var(--radius-control); background: transparent; color: var(--text-soft); font-size: 11px; cursor: pointer; }
.table-action:hover { border-color: var(--accent-border); color: var(--accent); }
.table-scroll { overflow-x: auto; }
.pagination { justify-content: space-between; min-height: 58px; padding: 10px 16px; border-top: 1px solid var(--line); color: var(--text-muted); font-size: 11px; }

.empty-state, .error-state { display: grid; min-height: 290px; place-items: center; padding: 40px; text-align: center; }
.state-copy { max-width: 360px; }
.state-mark { display: grid; width: 52px; height: 52px; margin: 0 auto 16px; place-items: center; border: 1px solid var(--line-strong); border-radius: 14px; background: var(--surface-raised); color: var(--accent-readable); font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 18px; }
.state-copy p { margin: 7px 0 0; color: var(--text-muted); font-size: 12px; line-height: 1.6; }
.skeleton-list { display: grid; }
.skeleton-row { height: 66px; border-bottom: 1px solid var(--line); background: linear-gradient(90deg, transparent, rgba(255,255,255,.035), transparent); background-size: 220% 100%; }

.modal-backdrop { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; background: var(--modal-backdrop); }
.modal { width: min(640px, 94vw); max-height: min(760px, 90vh); overflow: auto; border: 1px solid var(--line-strong); border-radius: var(--radius-panel); background: var(--canvas); box-shadow: 0 26px 80px rgba(0,0,0,.68); }
.modal.small { width: min(460px, 94vw); }
.modal-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; padding: 20px; border-bottom: 1px solid var(--line); }
.modal-header h2, .modal-header p { margin: 0; }
.modal-header p { margin-top: 5px; color: var(--text-muted); font-size: 11px; }
.modal-body { padding: 20px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius-control); background: var(--line); }
.detail-item { min-height: 74px; padding: 13px; background: var(--surface); }
.detail-item span, .detail-item strong { display: block; overflow-wrap: anywhere; }
.detail-item span { color: var(--text-muted); font-size: 10px; }
.detail-item strong { margin-top: 7px; font-size: 13px; }
.detail-item.full { grid-column: 1 / -1; }
.payment-total { padding: 18px; border: 1px solid var(--accent-border); border-radius: var(--radius-control); background: var(--accent-soft); }
.payment-total span, .payment-total strong { display: block; }
.payment-total span { color: var(--text-soft); font-size: 11px; }
.payment-total strong { margin-top: 6px; color: var(--accent-readable); font-family: ui-monospace, "Cascadia Mono", monospace; font-size: 28px; }

.toast-root { position: fixed; right: 24px; bottom: 24px; display: grid; width: min(380px, calc(100vw - 48px)); gap: 8px; }
.toast { padding: 13px 15px; border: 1px solid var(--line-strong); border-radius: var(--radius-control); background: var(--surface-raised); box-shadow: 0 12px 30px rgba(0,0,0,.35); color: var(--text); font-size: 12px; }
.toast.success { border-color: var(--success-border); }
.toast.error { border-color: var(--danger-border); }

@media (prefers-reduced-motion: no-preference) {
    .app.animations .modal { animation: modal-in 150ms cubic-bezier(.16,1,.3,1); }
    .app.animations .skeleton-row { animation: shimmer 1.4s linear infinite; }
    .app.animations .toast { animation: toast-in 160ms cubic-bezier(.16,1,.3,1); }
}
@keyframes modal-in { from { opacity: 0; transform: translateY(8px) scale(.99); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes shimmer { to { background-position: -220% 0; } }

@media (max-width: 980px) {
    .app { padding: 0; }
    .workspace { width: 100vw; height: 100vh; border: 0; border-radius: 0; grid-template-columns: 190px minmax(0, 1fr); }
    .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .metric:nth-child(2) { border-right: 0; }
    .metric:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
    .form-shell { grid-template-columns: 1fr; }
    .form-summary { position: static; }
}
@media (max-width: 680px) {
    .workspace { display: flex; flex-direction: column; }
    .sidebar { flex: 0 0 auto; padding: 10px 12px; border-right: 0; border-bottom: 1px solid var(--line); }
    .brand, .sidebar-footer { display: none; }
    .navigation { display: flex; margin: 0; overflow-x: auto; }
    .nav-button { min-width: max-content; }
    .nav-count { display: none; }
    .topbar { min-height: 72px; padding: 14px 16px; }
    .topbar-actions .ghost { display: none; }
    .content { padding: 18px 16px 34px; }
    .metrics { grid-template-columns: 1fr; }
    .metric { border-right: 0; border-bottom: 1px solid var(--line); }
    .metric:last-child { border-bottom: 0; }
    .form-grid, .detail-grid { grid-template-columns: 1fr; }
    .form-grid .span-2, .detail-item.full { grid-column: auto; }
    .toolbar, .toolbar-group, .search-wrap { width: 100%; }
    .toolbar-group select { flex: 1; min-width: 120px; }
    .search-wrap { max-width: none; }
}

[data-theme="light"] {
    --canvas: #f2f2ee; --surface: #fafaf7; --surface-raised: #e9e9e4; --surface-hover: #e4e4de;
    --line: #d0d0c8; --line-strong: #aaa99f; --text: #151511; --text-soft: #45453f; --text-muted: #686861;
    --sidebar: #e6e6e0; --input: #fdfdfa; --backdrop: transparent; --modal-backdrop: rgba(12,12,10,.65);
}

@media (prefers-reduced-transparency: reduce) { .app { background: transparent; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; } }
