:root {
  --bg: #f4f2ed;
  --panel: #fffdf8;
  --text: #1d2622;
  --muted: #6e7772;
  --line: #dedbd2;
  --accent: #0d6b52;
  --accent-soft: #dceee7;
  --income: #167c5a;
  --expense: #c8503f;
  --debt: #8c5a23;
  --shadow: 0 12px 35px rgba(31, 43, 37, .07);
  --radius: 18px;
}

[data-theme="dark"] {
  --bg: #131816;
  --panel: #1c2420;
  --text: #ecf2ed;
  --muted: #a7b0aa;
  --line: #35403a;
  --accent: #70c8a7;
  --accent-soft: #243d34;
  --income: #65c69f;
  --expense: #f08370;
  --debt: #e0aa69;
  --shadow: 0 12px 35px rgba(0, 0, 0, .2);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); font-family: Inter, "PingFang SC", "Microsoft YaHei", sans-serif; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.topbar, main { width: min(1180px, calc(100% - 32px)); margin-inline: auto; }
.topbar { display: flex; justify-content: space-between; align-items: flex-end; gap: 24px; padding: 42px 0 26px; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 7px; font-size: clamp(2rem, 5vw, 3.4rem); letter-spacing: -.05em; line-height: 1; }
h2 { margin-bottom: 0; font-size: 1.18rem; }
h3 { margin: 26px 0 12px; font-size: 1rem; }
.subtitle, .quiet { color: var(--muted); }
.subtitle { margin-bottom: 0; }
.local-badge { display: inline-block; margin: 12px 0 0; border-radius: 99px; padding: 6px 10px; color: var(--accent); background: var(--accent-soft); font-size: .75rem; font-weight: 700; }
.eyebrow { margin-bottom: 7px; color: var(--accent); font-size: .68rem; font-weight: 800; letter-spacing: .16em; }
.header-actions, .backup-actions, .filters { display: flex; flex-wrap: wrap; gap: 8px; }
.button, .icon-button { border: 1px solid transparent; border-radius: 12px; padding: 10px 14px; color: var(--text); background: transparent; font-weight: 700; }
.button.primary { color: #fff; background: var(--accent); }
[data-theme="dark"] .button.primary { color: #102019; }
.button.secondary { border-color: var(--line); background: var(--panel); }
.button.ghost { border-color: var(--line); }
.panel { border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; background: var(--panel); box-shadow: var(--shadow); }
.summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 14px; }
.summary-card { position: relative; overflow: hidden; min-height: 112px; border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; background: var(--panel); }
.summary-card::after { content: ""; position: absolute; right: -25px; bottom: -35px; width: 90px; height: 90px; border-radius: 50%; background: currentColor; opacity: .08; }
.summary-card span { display: block; margin-bottom: 16px; color: var(--muted); font-size: .85rem; }
.summary-card strong { font-size: clamp(1.35rem, 2.4vw, 1.9rem); letter-spacing: -.03em; }
.income-card { color: var(--income); } .expense-card { color: var(--expense); } .debt-card { color: var(--debt); }
.layout-main { display: grid; grid-template-columns: 1.25fr .75fr; gap: 14px; }
.insights-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 14px 0; }
.insights-grid.single-insight { grid-template-columns: 1fr; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 20px; }
.panel-heading p { margin-bottom: 5px; }
.type-switch { display: grid; grid-template-columns: repeat(4, 1fr); padding: 4px; margin-bottom: 18px; border-radius: 14px; background: var(--bg); }
.type-button { border: 0; border-radius: 10px; padding: 9px; color: var(--muted); background: transparent; font-weight: 750; }
.type-button.active { color: var(--text); background: var(--panel); box-shadow: 0 3px 12px rgba(0,0,0,.08); }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 13px; }
label { display: grid; gap: 7px; color: var(--muted); font-size: .8rem; font-weight: 650; }
input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 11px; padding: 11px 12px; outline: none; color: var(--text); background: var(--bg); }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
.full { grid-column: 1 / -1; }
.form-hint { margin: 14px 0; color: var(--muted); font-size: .78rem; line-height: 1.6; }
.submit-button { width: 100%; padding-block: 12px; }
.text-button { border: 0; color: var(--accent); background: transparent; font-weight: 700; }
.account-list { display: grid; gap: 8px; }
.account-card { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 11px; border-bottom: 1px solid var(--line); padding: 11px 2px; }
.account-card:last-child { border-bottom: 0; }
.account-icon { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; color: var(--accent); background: var(--accent-soft); font-size: .76rem; font-weight: 800; }
.account-name small { display: block; margin-top: 3px; color: var(--muted); }
.account-value { font-variant-numeric: tabular-nums; font-weight: 780; }
.account-card.liability .account-icon { color: var(--debt); background: color-mix(in srgb, var(--debt) 12%, transparent); }
.bar-chart { display: grid; gap: 14px; min-height: 170px; align-content: start; }
.bar-row { display: grid; grid-template-columns: 82px 1fr 80px; align-items: center; gap: 9px; font-size: .82rem; }
.bar-track { height: 9px; overflow: hidden; border-radius: 9px; background: var(--bg); }
.bar-fill { height: 100%; border-radius: inherit; background: var(--expense); }
.bar-value { text-align: right; font-variant-numeric: tabular-nums; }
.trend-chart { display: flex; align-items: flex-end; justify-content: space-around; gap: 10px; min-height: 190px; padding-top: 10px; }
.trend-group { display: grid; grid-template-rows: 145px auto; gap: 8px; min-width: 36px; flex: 1; text-align: center; color: var(--muted); font-size: .72rem; }
.trend-bars { display: flex; align-items: flex-end; justify-content: center; gap: 4px; height: 145px; }
.trend-bar { width: min(16px, 42%); min-height: 2px; border-radius: 5px 5px 2px 2px; }
.trend-income { background: var(--income); } .trend-expense { background: var(--expense); }
.ledger-panel { margin-bottom: 40px; }
.ledger-heading { align-items: flex-end; }
.filters input, .filters select { width: auto; min-width: 140px; padding-block: 9px; }
.filters input[type="search"] { min-width: 200px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .84rem; }
th { padding: 10px; color: var(--muted); font-size: .72rem; text-align: left; }
td { border-top: 1px solid var(--line); padding: 13px 10px; vertical-align: middle; }
.number { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.amount-expense { color: var(--expense); } .amount-income { color: var(--income); }
.type-pill { display: inline-block; border-radius: 99px; padding: 4px 8px; background: var(--bg); font-size: .72rem; font-weight: 750; }
.row-actions { display: flex; justify-content: flex-end; gap: 3px; }
.row-actions button { border: 0; padding: 6px; color: var(--muted); background: transparent; }
.empty-state { padding: 34px; color: var(--muted); text-align: center; }
.hidden { display: none !important; }
dialog { width: min(760px, calc(100% - 24px)); max-height: 90vh; border: 1px solid var(--line); border-radius: 20px; padding: 0; color: var(--text); background: var(--panel); box-shadow: 0 24px 90px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(11, 18, 14, .62); backdrop-filter: blur(3px); }
.dialog-shell { padding: 24px; }
.dialog-heading { display: flex; justify-content: space-between; align-items: start; }
.icon-button { padding: 0 8px; font-size: 1.8rem; font-weight: 400; }
.callout { border-radius: 12px; padding: 12px 14px; color: var(--muted); background: var(--accent-soft); font-size: .82rem; line-height: 1.55; }
.settings-accounts { display: grid; gap: 8px; }
.settings-account { display: grid; grid-template-columns: 1fr 170px; align-items: end; gap: 12px; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dialog-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 26px; }
.toast { position: fixed; z-index: 10; right: 22px; bottom: 22px; transform: translateY(20px); border-radius: 12px; padding: 12px 16px; color: #fff; background: #1f332b; opacity: 0; pointer-events: none; transition: .2s ease; box-shadow: var(--shadow); }
.toast.show { transform: translateY(0); opacity: 1; }

@media (max-width: 850px) {
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .layout-main, .insights-grid { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
  .topbar, main { width: min(100% - 20px, 1180px); }
  .topbar { align-items: flex-start; padding-top: 25px; }
  .header-actions { justify-content: flex-end; }
  .header-actions .button { padding: 8px 9px; font-size: .76rem; }
  .summary-card { min-height: 95px; padding: 15px; }
  .summary-card span { margin-bottom: 11px; }
  .panel { padding: 17px; }
  .form-grid, .settings-grid { grid-template-columns: 1fr; }
  .settings-account { grid-template-columns: 1fr 125px; }
  .ledger-heading { align-items: stretch; flex-direction: column; }
  .filters { display: grid; grid-template-columns: 1fr 1fr; }
  .filters input, .filters select, .filters input[type="search"] { width: 100%; min-width: 0; }
  .filters input[type="search"] { grid-column: 1 / -1; }
  th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5) { display: none; }
  .bar-row { grid-template-columns: 70px 1fr 72px; }
}

/* V2 additions */
.status-line { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.status-line .local-badge { margin: 0; }
.status-badge { display: inline-flex; align-items: center; gap: 5px; border: 1px solid var(--line); border-radius: 99px; padding: 5px 9px; color: var(--muted); background: var(--panel); font-size: .72rem; font-weight: 700; }
.status-badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--income); }
.status-badge.offline::before { background: var(--expense); }
.update-banner { position: sticky; z-index: 5; top: 10px; display: flex; align-items: center; justify-content: center; gap: 12px; width: min(760px, calc(100% - 24px)); margin: 0 auto 12px; border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px; background: var(--panel); box-shadow: var(--shadow); }
.update-banner p { margin: 0; }
.quick-templates { display: flex; flex-wrap: wrap; gap: 8px; margin: -4px 0 16px; }
.template-chip { border: 1px solid var(--line); border-radius: 99px; padding: 7px 11px; color: var(--text); background: var(--panel); font-size: .78rem; font-weight: 700; }
.template-chip:hover { border-color: var(--accent); color: var(--accent); }
.type-switch { grid-template-columns: repeat(5, 1fr); }
.single-grid { display: grid; grid-template-columns: 1fr; gap: 13px; }
.full-button { width: 100%; }
.filter-date { min-width: 137px !important; }
.filter-total { margin: 12px 0 0; color: var(--muted); font-size: .8rem; }
.mobile-only { display: none; }
.floating-add { display: none; position: fixed; z-index: 7; right: max(18px, env(safe-area-inset-right)); bottom: calc(18px + env(safe-area-inset-bottom)); width: 54px; height: 54px; border: 0; border-radius: 50%; color: #fff; background: var(--accent); font-size: 1.7rem; box-shadow: 0 12px 28px rgba(13, 107, 82, .32); }
.undo-bar { position: fixed; z-index: 11; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); display: flex; align-items: center; gap: 20px; transform: translateX(-50%); min-width: min(360px, calc(100% - 32px)); border-radius: 14px; padding: 12px 16px; color: #fff; background: #1f332b; box-shadow: var(--shadow); }
.undo-bar span { flex: 1; }
.undo-bar button { border: 0; color: #a7f0d0; background: transparent; font-weight: 800; }
.dialog-tabs { display: flex; gap: 4px; margin: 18px 0; border-bottom: 1px solid var(--line); }
.dialog-tab { border: 0; border-bottom: 2px solid transparent; padding: 9px 12px; color: var(--muted); background: transparent; font-weight: 750; }
.dialog-tab.active { border-color: var(--accent); color: var(--accent); }
.template-list, .reconcile-list { display: grid; gap: 8px; margin-top: 12px; }
.template-setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.template-setting-row span { min-width: 0; }
.template-setting-row small { display: block; margin-top: 3px; color: var(--muted); }
.template-setting-row div { display: flex; gap: 3px; }
.template-setting-row button { border: 0; border-radius: 8px; padding: 6px 8px; color: var(--text); background: var(--bg); }
.template-setting-row button:disabled { opacity: .35; cursor: default; }
.template-setting-row .danger-text { color: var(--expense); }
.reconcile-row { display: grid; grid-template-columns: minmax(120px, 1fr) 190px 110px; align-items: end; gap: 12px; border-bottom: 1px solid var(--line); padding: 10px 0; }
.reconcile-row strong { display: block; }
.reconcile-row small { display: block; margin-top: 4px; color: var(--muted); }
.reconcile-diff { padding-bottom: 11px; text-align: right; font-variant-numeric: tabular-nums; font-size: .8rem; }
.small-dialog { width: min(560px, calc(100% - 24px)); }
.wide-dialog { width: min(900px, calc(100% - 24px)); }
#settingsDialog, #reconcileDialog, #reportDialog { width: min(900px, calc(100% - 24px)); }
#templateDialog, #importPreviewDialog { width: min(560px, calc(100% - 24px)); }
.warning { border-left: 4px solid var(--debt); }
.import-preview dl { display: grid; grid-template-columns: 110px 1fr; gap: 8px 14px; margin: 18px 0 0; }
.import-preview dt { color: var(--muted); }
.import-preview dd { margin: 0; font-weight: 700; }
.report-dialog-shell { padding: 0; }
.report-toolbar { position: sticky; z-index: 2; top: 0; display: flex; align-items: end; gap: 10px; border-bottom: 1px solid var(--line); padding: 14px 18px; background: var(--panel); }
.report-toolbar label { flex: 1; max-width: 200px; }
.report-content { padding: 44px 52px; color: #1d2622; background: #fff; }
.report-head { border-bottom: 3px solid #0d6b52; padding-bottom: 24px; }
.report-head p { margin-bottom: 8px; color: #0d6b52; font-size: .72rem; font-weight: 850; letter-spacing: .18em; }
.report-head h1 { margin-bottom: 10px; color: #1d2622; font-size: 2.1rem; letter-spacing: -.035em; }
.report-head span { color: #68736d; font-size: .82rem; }
.report-kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 26px 0; }
.report-kpis div { border-radius: 12px; padding: 14px; background: #f4f2ed; }
.report-kpis span { display: block; margin-bottom: 8px; color: #68736d; font-size: .75rem; }
.report-kpis strong { font-size: 1.08rem; font-variant-numeric: tabular-nums; }
.report-section { margin-top: 28px; break-inside: avoid; }
.report-section h2 { margin-bottom: 10px; color: #1d2622; font-size: 1.05rem; }
.report-section p { color: #4e5a54; line-height: 1.7; }
.report-section table { color: #1d2622; }
.report-section th { color: #68736d; background: #f8f7f3; }
.report-section td { border-color: #e2e0d9; }
.report-section th:last-child, .report-section td:last-child { text-align: right; }
.report-content footer { margin-top: 34px; border-top: 1px solid #dedbd2; padding-top: 14px; color: #68736d; font-size: .72rem; }

@media (max-width: 750px) {
  .mobile-only, .floating-add { display: block; }
  .filters:not(.filters-open) > *:not(#searchInput):not(#toggleFiltersBtn) { display: none; }
  .filters #toggleFiltersBtn { display: block; }
  .report-content { padding: 28px 20px; }
  .report-kpis { grid-template-columns: 1fr 1fr; }
  .reconcile-row { grid-template-columns: 1fr 130px; }
  .reconcile-diff { grid-column: 2; padding: 0; }
}

@media (max-width: 600px) {
  body { padding-bottom: calc(75px + env(safe-area-inset-bottom)); }
  .topbar { display: block; }
  .header-actions { margin-top: 18px; justify-content: flex-start; }
  .type-switch { overflow-x: auto; grid-template-columns: repeat(5, minmax(72px, 1fr)); }
  .quick-templates { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 3px; }
  .template-chip { flex: 0 0 auto; }
  .template-setting-row { align-items: flex-start; flex-direction: column; }
  .template-setting-row div { align-self: stretch; justify-content: flex-end; }
  .report-toolbar { flex-wrap: wrap; }
  .report-toolbar label { min-width: calc(100% - 48px); }
  .report-toolbar .primary { flex: 1; }
  .report-head h1 { font-size: 1.65rem; }
  .report-kpis { gap: 7px; }
  .report-kpis div { padding: 11px; }
  .ledger-panel .table-wrap { overflow: visible; }
  .ledger-panel table, .ledger-panel tbody { display: block; }
  .ledger-panel thead { display: none; }
  .ledger-panel tr { display: grid; grid-template-columns: 72px minmax(0, 1fr) auto; gap: 5px 10px; border-top: 1px solid var(--line); padding: 13px 0; }
  .ledger-panel td { display: block !important; min-width: 0; border: 0; padding: 0; overflow-wrap: anywhere; }
  .ledger-panel td:nth-child(1) { grid-column: 1; grid-row: 1; color: var(--muted); font-size: .75rem; }
  .ledger-panel td:nth-child(2) { grid-column: 1; grid-row: 2 / span 2; }
  .ledger-panel td:nth-child(3) { grid-column: 2; grid-row: 1; font-weight: 700; }
  .ledger-panel td:nth-child(4) { grid-column: 2; grid-row: 2; color: var(--muted); font-size: .76rem; }
  .ledger-panel td:nth-child(5) { grid-column: 2; grid-row: 3; color: var(--muted); font-size: .76rem; }
  .ledger-panel td:nth-child(6) { grid-column: 3; grid-row: 1; font-weight: 800; }
  .ledger-panel td:nth-child(7) { grid-column: 3; grid-row: 2 / span 2; align-self: end; }
  .ledger-panel .row-actions { flex-wrap: wrap; justify-content: flex-end; }
}

@media print {
  @page { size: A4; margin: 14mm; }
  body.printing-report { padding: 0; background: #fff; }
  body.printing-report > *:not(#reportDialog) { display: none !important; }
  body.printing-report #reportDialog { position: static; display: block; width: auto; max-height: none; border: 0; padding: 0; color: #1d2622; background: #fff; box-shadow: none; }
  body.printing-report #reportDialog::backdrop, body.printing-report .no-print { display: none !important; }
  body.printing-report .report-dialog-shell { padding: 0; }
  body.printing-report .report-content { padding: 0; }
  body.printing-report .report-kpis, body.printing-report .report-section { break-inside: avoid; }
}
