:root {
    --bg: #f5f4ef;
    --surface: #ffffff;
    --surface-sunken: #eeece5;
    --ink: #1e2a26;
    --ink-soft: #4b5d57;
    --muted: #798d86;
    --accent: #3c6e5d;
    --accent-ink: #ffffff;
    --accent-soft: #e3ebe6;
    --border: #dcdfd8;
    --positive: #2f7a4f;
    --negative: #b4483a;
    --warning: #a9781f;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(30, 42, 38, 0.06), 0 4px 16px rgba(30, 42, 38, 0.06);
    --font-display: "Iowan Old Style", "Palatino Linotype", Georgia, ui-serif, serif;
    --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", ui-sans-serif, sans-serif;
    --font-mono: "SF Mono", ui-monospace, "Cascadia Mono", Menlo, monospace;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #131e1b;
      --surface: #1a2723;
      --surface-sunken: #101917;
      --ink: #eceeea;
      --ink-soft: #b7c4bd;
      --muted: #7f938c;
      --accent: #6fbe9e;
      --accent-ink: #0e1a17;
      --accent-soft: #223b33;
      --border: #2a3a34;
      --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.28);
    }
  }
  :root[data-theme="dark"] {
    --bg: #131e1b;
    --surface: #1a2723;
    --surface-sunken: #101917;
    --ink: #eceeea;
    --ink-soft: #b7c4bd;
    --muted: #7f938c;
    --accent: #6fbe9e;
    --accent-ink: #0e1a17;
    --accent-soft: #223b33;
    --border: #2a3a34;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.28);
  }
  :root[data-theme="light"] {
    --bg: #f5f4ef;
    --surface: #ffffff;
    --surface-sunken: #eeece5;
    --ink: #1e2a26;
    --ink-soft: #4b5d57;
    --muted: #798d86;
    --accent: #3c6e5d;
    --accent-ink: #ffffff;
    --accent-soft: #e3ebe6;
    --border: #dcdfd8;
    --shadow: 0 1px 2px rgba(30, 42, 38, 0.06), 0 4px 16px rgba(30, 42, 38, 0.06);
  }

  * { box-sizing: border-box; }
  body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font-body); }
  h1, h2, h3 { font-family: var(--font-display); font-weight: 600; text-wrap: balance; margin: 0; }
  .num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
  button { font-family: inherit; cursor: pointer; }
  a { color: inherit; }

  .app-shell { display: none; min-height: 100vh; grid-template-columns: 232px 1fr; }
  .app-shell.active { display: grid; }

  /* ---------- Sidebar ---------- */
  .sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 20px 16px; gap: 24px; }
  .brand { display: flex; align-items: center; gap: 9px; padding: 0 4px; }
  .brand-mark { width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: var(--accent-ink); display: flex; align-items: center; justify-content: center; font-family: var(--font-display); font-weight: 700; font-size: 15px; flex: none; }
  .brand-name { font-family: var(--font-display); font-size: 17px; font-weight: 600; }
  .household-pill { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; padding: 0 4px; }

  nav.primary-nav { display: flex; flex-direction: column; gap: 2px; }
  .nav-item { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--radius); border: none; background: none; color: var(--ink-soft); font-size: 14px; text-align: left; width: 100%; }
  .nav-item svg { width: 17px; height: 17px; flex: none; opacity: 0.85; }
  .nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
  .nav-item:not(.active):hover { background: var(--surface-sunken); color: var(--ink); }

  .sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 10px; }
  .rate-strip { font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 12px; display: flex; flex-direction: column; gap: 3px; }
  .rate-strip .row { display: flex; justify-content: space-between; }
  .user-card { display: flex; align-items: center; gap: 9px; padding: 8px; border-radius: var(--radius); background: var(--surface-sunken); }
  .avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex: none; }
  .user-meta { line-height: 1.25; overflow: hidden; }
  .user-name { font-size: 13px; font-weight: 600; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
  .user-role { font-size: 11px; color: var(--muted); text-transform: capitalize; }

  /* ---------- Main ---------- */
  main { padding: 28px 36px 60px; max-width: 1080px; }
  .page-header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 22px; gap: 16px; flex-wrap: wrap; }
  .page-header h1 { font-size: 26px; }
  .page-header p.sub { margin: 4px 0 0; color: var(--muted); font-size: 13.5px; }
  .btn { display: inline-flex; align-items: center; gap: 7px; padding: 9px 16px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 13.5px; font-weight: 600; }
  .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
  .btn:hover { filter: brightness(0.97); }
  .btn:focus-visible, .nav-item:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

  /* Dashboard cards */
  .card-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 26px; }
  .stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px 18px; box-shadow: var(--shadow); }
  .stat-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-bottom: 8px; }
  .stat-value { font-size: 22px; font-weight: 700; }
  .stat-value.neg { color: var(--negative); }
  .stat-value.pos { color: var(--positive); }
  .stat-foot { font-size: 12px; color: var(--muted); margin-top: 6px; }

  .panel { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); overflow: hidden; margin-bottom: 20px; }
  .panel-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
  .panel-head h3 { font-size: 15px; }
  .panel-body { padding: 4px 18px 14px; }

  .cat-bar-row { display: grid; grid-template-columns: 130px 1fr 90px; align-items: center; gap: 12px; padding: 9px 0; font-size: 13px; }
  .cat-bar-track { height: 7px; border-radius: 4px; background: var(--surface-sunken); overflow: hidden; }
  .cat-bar-fill { height: 100%; background: var(--accent); border-radius: 4px; }
  .cat-bar-row .num { text-align: right; color: var(--ink-soft); }

  table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
  thead th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 600; padding: 8px 6px; border-bottom: 1px solid var(--border); }
  tbody td { padding: 11px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
  tbody tr:last-child td { border-bottom: none; }
  tbody tr.clickable:hover { background: var(--surface-sunken); cursor: pointer; }
  td.amount, th.amount { text-align: right; }
  .amount .num { font-weight: 600; }
  .amount .num.neg { color: var(--negative); }
  .amount .num.pos { color: var(--positive); }

  .chip { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; padding: 3px 9px; border-radius: 100px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
  .chip.muted { background: var(--surface-sunken); color: var(--muted); }
  .chip.split { background: var(--surface-sunken); color: var(--ink-soft); }
  .table-toolbar { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
  .table-toolbar select, .table-toolbar input { padding: 8px 10px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 13px; font-family: inherit; }
  .table-toolbar input[type="search"] { flex: 1; min-width: 180px; }

  /* Accounts */
  .account-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
  .account-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px 18px; box-shadow: var(--shadow); }
  .account-card .kind { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
  .account-card h3 { font-size: 16px; margin: 4px 0 10px; }
  .account-card .bal { font-size: 21px; font-weight: 700; }
  .account-card .owner { font-size: 12px; color: var(--muted); margin-top: 8px; display: flex; align-items: center; gap: 6px; }

  /* Categories tree */
  .cat-tree { display: flex; flex-direction: column; }
  .cat-parent { display: flex; align-items: center; justify-content: space-between; padding: 12px 4px; border-bottom: 1px solid var(--border); font-weight: 600; }
  .cat-children { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 4px 16px; }
  .cat-child { padding: 6px 12px; border-radius: 100px; background: var(--surface-sunken); font-size: 12.5px; color: var(--ink-soft); }
  .cat-child.add { border: 1px dashed var(--border); background: none; color: var(--muted); }

  /* Modal */
  .modal-overlay { position: fixed; inset: 0; background: rgba(15, 22, 20, 0.45); display: none; align-items: flex-start; justify-content: center; padding: 6vh 20px; z-index: 50; }
  .modal-overlay.active { display: flex; }
  .modal { background: var(--surface); border-radius: 12px; width: 100%; max-width: 560px; box-shadow: var(--shadow); border: 1px solid var(--border); }
  .modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 22px; border-bottom: 1px solid var(--border); }
  .modal-head h3 { font-size: 16px; }
  .modal-close { background: none; border: none; color: var(--muted); font-size: 18px; line-height: 1; }
  .modal-body { padding: 20px 22px; display: flex; flex-direction: column; gap: 16px; }
  .field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .field { display: flex; flex-direction: column; gap: 5px; }
  .field label { font-size: 12px; color: var(--muted); font-weight: 600; }
  .field input, .field select { padding: 9px 11px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--bg); color: var(--ink); font-size: 14px; font-family: inherit; }

  .split-list { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; background: var(--surface-sunken); }
  .split-row { display: grid; grid-template-columns: 1fr 110px 28px; gap: 8px; align-items: center; }
  .split-row select, .split-row input { padding: 7px 9px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 13px; font-family: inherit; }
  .split-row input { text-align: right; font-family: var(--font-mono); }
  .split-remove { background: none; border: none; color: var(--muted); font-size: 15px; }
  .split-add { align-self: flex-start; font-size: 12.5px; color: var(--accent); background: none; border: none; font-weight: 600; padding: 4px 2px; }
  .split-total { display: flex; justify-content: space-between; font-size: 12.5px; padding-top: 6px; border-top: 1px dashed var(--border); margin-top: 2px; }
  .split-total.ok { color: var(--positive); }
  .split-total .num { font-weight: 700; }

  .modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 22px; border-top: 1px solid var(--border); }

  /* Login screen */
  .login-shell { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); }
  .login-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 36px 32px; text-align: center; }
  .login-mark { width: 46px; height: 46px; border-radius: 12px; background: var(--accent); color: var(--accent-ink); display: flex; align-items: center; justify-content: center; font-family: var(--font-display); font-weight: 700; font-size: 22px; margin: 0 auto 18px; }
  .login-card h1 { font-size: 21px; margin-bottom: 6px; }
  .login-card p { color: var(--muted); font-size: 13.5px; margin: 0 0 26px; }
  .ms-btn { width: 100%; display: flex; align-items: center; justify-content: center; gap: 10px; padding: 11px 14px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 14px; font-weight: 600; }
  .login-foot { margin-top: 22px; font-size: 11.5px; color: var(--muted); }
  .devnav { position: fixed; bottom: 14px; left: 50%; transform: translateX(-50%); display: flex; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 100px; padding: 6px; box-shadow: var(--shadow); z-index: 60; }
  .devnav button { border: none; background: none; font-size: 11.5px; padding: 6px 11px; border-radius: 100px; color: var(--muted); font-weight: 600; }
  .devnav button.on { background: var(--accent); color: var(--accent-ink); }

  @media (max-width: 760px) {
    .card-grid, .account-grid { grid-template-columns: 1fr 1fr; }
    .field-row { grid-template-columns: 1fr; }
  }
