:root {
    --bg: #F7F5F0;
    --surface: #FFFEFA;
    --ink: #263A35;
    --ink-soft: #57645C;
    --muted: #9C9486;
    --border: #E8E3D9;
    --accent: #356859;
    --accent-soft: #E4EDE8;
    --accent2: #C9825B;
    --wife: #8B6F8E;
    --wife-soft: #F1E9EF;
    --husband: #527C78;
    --husband-soft: #E7EFEC;
    --danger: #DC2626;
    --danger-soft: #FDECEC;
    --radius-lg: 18px;
    --radius-md: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(38, 58, 53, .03), 0 8px 20px rgba(38, 58, 53, .05);
  }

  * { box-sizing: border-box; }

  html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
    -webkit-tap-highlight-color: transparent;
  }

  button, input, select, textarea { font-family: inherit; }

  #app {
    max-width: 480px;
    margin: 0 auto;
    min-height: 100dvh;
    position: relative;
    padding-bottom: calc(84px + env(safe-area-inset-bottom, 0px));
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: max(16px, calc(env(safe-area-inset-top, 0px) + 12px)) 20px 16px;
    background: var(--ink);
    color: #fff;
  }
  .topbar-title { font-size: 16px; font-weight: 700; letter-spacing: .02em; }
  .topbar-user { font-size: 13px; color: #C7CFC8; display: flex; align-items: center; gap: 6px; }

  .main { padding: 16px; }

  /* ---------- Cards ---------- */
  .card {
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    padding: 18px;
    margin-bottom: 14px;
  }
  .card-title {
    font-size: 12px;
    color: var(--muted);
    font-weight: 600;
    letter-spacing: .04em;
    margin-bottom: 6px;
  }
  .card-amount { font-size: 28px; font-weight: 800; letter-spacing: -.01em; }
  .card-amount.highlight { color: var(--accent2); }
  .card-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

  .stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .stat-card {
    background: var(--surface);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
    padding: 14px;
  }
  .stat-label { font-size: 11px; color: var(--muted); font-weight: 600; }
  .stat-value { font-size: 19px; font-weight: 700; margin-top: 4px; }

  .payer-row { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
  .payer-tag {
    display: inline-flex; align-items: center; gap: 4px;
    font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  }
  .payer-tag.wife { background: var(--wife-soft); color: var(--wife); }
  .payer-tag.husband { background: var(--husband-soft); color: var(--husband); }
  .payer-amount { font-size: 13px; color: var(--ink-soft); }

  .bar-track { flex: 1; height: 8px; border-radius: 99px; background: var(--border); overflow: hidden; }
  .bar-fill { height: 100%; border-radius: 99px; }
  .bar-fill.wife { background: var(--wife); }
  .bar-fill.husband { background: var(--husband); }

  /* ---------- Bottom nav / FAB ---------- */
  .bottomnav {
    position: fixed;
    bottom: 0; left: 50%; transform: translateX(-50%);
    width: 100%; max-width: 480px;
    display: flex;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 6px 4px calc(env(safe-area-inset-bottom, 0px) + 6px);
    z-index: 20;
  }
  .navitem {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    background: none; border: none; padding: 8px 0;
    color: var(--muted); font-size: 11px; font-weight: 600;
  }
  .navitem.active { color: var(--accent); }
  .navicon { font-size: 20px; }

  .fab {
    position: fixed;
    bottom: calc(76px + env(safe-area-inset-bottom, 0px)); right: max(16px, calc(50% - 240px + 16px));
    width: 58px; height: 58px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    border: none;
    font-size: 28px;
    line-height: 58px;
    text-align: center;
    box-shadow: 0 10px 24px rgba(15, 118, 110, .35);
    z-index: 21;
    padding: 0;
  }

  /* ---------- Forms ---------- */
  .field { margin-bottom: 14px; }
  .field label {
    display: block; font-size: 12px; font-weight: 600; color: var(--ink-soft);
    margin-bottom: 6px;
  }
  .field input[type="text"],
  .field input[type="number"],
  .field input[type="date"],
  .field select,
  .field textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 15px;
    background: #FBFAF6;
    color: var(--ink);
  }
  .field textarea { resize: vertical; min-height: 60px; }
  .amount-input {
    font-size: 34px !important;
    font-weight: 800;
    text-align: center;
    padding: 16px !important;
  }

  .category-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
  }
  .category-chip {
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    padding: 12px 4px;
    border-radius: var(--radius-md);
    border: 1.5px solid var(--border);
    background: var(--surface);
    font-size: 12px;
    color: var(--ink-soft);
    font-weight: 600;
  }
  .category-chip .ic { font-size: 20px; }
  .category-chip.selected { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }

  .btn {
    display: block;
    width: 100%;
    padding: 14px;
    border-radius: var(--radius-md);
    border: none;
    font-size: 15px;
    font-weight: 700;
    text-align: center;
  }
  .btn-primary { background: var(--accent); color: #fff; }
  .btn-secondary { background: var(--surface); color: var(--ink); border: 1px solid var(--border); }
  .btn-danger { background: var(--danger-soft); color: var(--danger); }
  .btn-block + .btn-block { margin-top: 10px; }
  .btn:disabled { opacity: .5; }

  /* ---------- Lists ---------- */
  .list-item {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 4px;
    border-bottom: 1px solid var(--border);
  }
  .list-item:last-child { border-bottom: none; }
  .list-item-icon {
    width: 38px; height: 38px; border-radius: 10px;
    background: var(--accent-soft);
    display: flex; align-items: center; justify-content: center;
    font-size: 18px; flex-shrink: 0;
  }
  .list-item-main { flex: 1; min-width: 0; }
  .list-item-title { font-size: 14px; font-weight: 600; }
  .list-item-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
  .list-item-amount { font-size: 15px; font-weight: 700; white-space: nowrap; }
  .list-item-amount.settled { color: var(--muted); text-decoration: line-through; }

  .badge {
    display: inline-block; font-size: 10px; font-weight: 700;
    padding: 2px 7px; border-radius: 999px; margin-left: 6px;
  }
  .badge.unsettled { background: #FFF3D6; color: #92670C; }
  .badge.settled { background: #E4F7E9; color: #1B7A3C; }

  .empty-state { text-align: center; color: var(--muted); font-size: 13px; padding: 40px 0; }

  /* ---------- P0 loading skeletons ---------- */
  .skeleton-line {
    height: 11px; border-radius: 999px; background: var(--border); margin: 10px 0;
    animation: skeleton-pulse 1.2s ease-in-out infinite alternate;
  }
  .skeleton-line.short { width: 36%; }
  .skeleton-line.medium { width: 62%; }
  .skeleton-line.amount { width: 48%; height: 30px; margin: 14px 0; }
  .skeleton-card { min-height: 210px; }
  @keyframes skeleton-pulse { from { opacity: .55; } to { opacity: .9; } }
  @media (prefers-reduced-motion: reduce) { .skeleton-line { animation: none; } }

  /* ---------- Filter bar ---------- */
  .filter-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
  .filter-row select, .filter-row input {
    width: 100%; padding: 10px; border-radius: var(--radius-sm);
    border: 1px solid var(--border); font-size: 13px; background: var(--surface);
  }
  .search-row { display: flex; gap: 8px; margin-bottom: 12px; }
  .search-row input { flex: 1; }
  .search-row button {
    padding: 0 16px; border-radius: var(--radius-sm); border: none;
    background: var(--ink); color: #fff; font-weight: 700; font-size: 13px;
  }

  .section-title {
    font-size: 13px; font-weight: 700; color: var(--ink-soft);
    margin: 18px 0 8px; text-transform: uppercase; letter-spacing: .04em;
  }

  .more-menu-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 16px 4px; border-bottom: 1px solid var(--border);
    font-size: 14px; font-weight: 600;
  }
  .more-menu-item:last-child { border-bottom: none; }
  .more-menu-item .chev { color: var(--muted); }

  .back-row { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
  .back-btn {
    background: none; border: none; font-size: 20px; color: var(--ink); padding: 4px;
  }
  .back-title { font-size: 15px; font-weight: 700; }

  .row-between { display: flex; align-items: center; justify-content: space-between; }
  .toggle {
    width: 40px; height: 24px; border-radius: 999px; border: none;
    position: relative; background: var(--border); flex-shrink: 0;
  }
  .toggle.on { background: var(--accent); }
  .toggle .knob {
    position: absolute; top: 2px; left: 2px; width: 20px; height: 20px;
    border-radius: 50%; background: #fff; transition: left .15s;
  }
  .toggle.on .knob { left: 18px; }

  /* ---------- Reports ---------- */
  .segmented { display: flex; background: var(--border); border-radius: var(--radius-md); padding: 3px; margin-bottom: 14px; }
  .segmented button {
    flex: 1; border: none; background: none; padding: 9px;
    border-radius: calc(var(--radius-md) - 3px); font-size: 13px; font-weight: 700; color: var(--ink-soft);
  }
  .segmented button.active { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }

  .period-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
  .period-nav button {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
    width: 36px; height: 36px; font-size: 16px; color: var(--ink);
  }
  .period-nav .period-label { font-size: 15px; font-weight: 700; }

  .cat-bar-row { padding: 10px 0; }
  .cat-bar-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; font-size: 13px; }
  .cat-bar-top .name { display: flex; align-items: center; gap: 6px; font-weight: 600; }
  .cat-bar-top .amount { color: var(--ink-soft); font-weight: 700; }
  .cat-bar-track { height: 8px; border-radius: 99px; background: var(--border); overflow: hidden; }
  .cat-bar-fill { height: 100%; border-radius: 99px; background: var(--accent); }

  .month-chart { display: flex; align-items: flex-end; gap: 6px; height: 140px; padding-top: 10px; }
  .month-bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 6px; }
  .month-bar { width: 100%; max-width: 22px; border-radius: 4px 4px 0 0; background: var(--accent-soft); }
  .month-bar.has-value { background: var(--accent); }
  .month-bar-label { font-size: 10px; color: var(--muted); }

  /* ---------- Modal ---------- */
  .modal-overlay {
    position: fixed; inset: 0; background: rgba(15, 23, 42, .5);
    display: flex; align-items: flex-end; justify-content: center;
    z-index: 50;
  }
  .modal-sheet {
    background: var(--surface);
    width: 100%; max-width: 480px;
    position: relative;
    border-radius: 20px 20px 0 0;
    padding: 20px 20px calc(env(safe-area-inset-bottom, 0px) + 20px);
    max-height: 88vh;
    overflow-y: auto;
  }
  .modal-title { font-size: 16px; font-weight: 800; margin-bottom: 16px; }
  .modal-close { position: absolute; top: 14px; right: 18px; font-size: 20px; background: none; border: none; color: var(--muted); z-index: 1; }

  /* ---------- Toast ---------- */
  .toast {
    position: fixed; top: 16px; left: 50%; transform: translateX(-50%);
    background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 999px;
    font-size: 13px; font-weight: 600; z-index: 100; box-shadow: var(--shadow);
  }
  .toast.error { background: var(--danger); }
