:root {
  color-scheme: light dark;
  --bg: #F5F5F7;
  --surface: #FFFFFF;
  --ink: #1D1D1F;
  --muted: #6A6A70;
  --line: #E3E3E8;
  --accent: #4A50D8;
  --accent-ink: #FFFFFF;
  --track: #E6E6F2;
  --radius: 16px;
  --gutter: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1C1C1E;
    --ink: #F5F5F7;
    --muted: #A1A1A8;
    --line: #38383C;
    --accent: #9297F7;
    --accent-ink: #0B0B20;
    --track: #2C2C3A;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}

button, input, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
h1, h2, p { margin: 0; }

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) var(--gutter) calc(env(safe-area-inset-bottom) + 96px);
}

/* Header */
.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.topbar h1 { font-size: 17px; font-weight: 600; }
.icon-btn {
  width: 44px; height: 44px; margin-right: -10px;
  display: grid; place-items: center; color: var(--muted);
}
.icon-btn svg { width: 22px; height: 22px; }

/* Cards */
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-top: 12px; }
.section-title { font-size: 13px; font-weight: 600; color: var(--muted); margin: 24px 0 8px; }
.row-between { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

/* This week */
.hero-head { display: flex; align-items: center; justify-content: space-between; min-height: 32px; }
.hero-label { font-size: 15px; color: var(--muted); }
.share-btn { min-height: 44px; margin: -6px -8px -6px 0; padding: 0 8px; color: var(--accent); font-weight: 500; font-size: 15px; }
.hero-number { font-size: 56px; line-height: 1.1; font-weight: 650; letter-spacing: -0.025em; margin-top: 4px; }
.meter { height: 8px; border-radius: 4px; background: var(--track); margin: 14px 0 12px; overflow: hidden; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.hero-detail { font-size: 15px; color: var(--muted); }

/* Add field */
.add-form { display: flex; gap: 8px; }
.add-input {
  flex: 1; min-width: 0; height: 48px; padding: 0 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  font-size: 16px; outline: none;
}
.add-input:focus { border-color: var(--accent); }
.add-input::placeholder { color: var(--muted); }
.primary-btn {
  height: 48px; padding: 0 18px; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink); font-weight: 600;
}
.secondary-btn {
  height: 44px; padding: 0 14px; border-radius: 12px;
  border: 1px solid var(--line); font-weight: 500;
}
.combo { margin-top: 12px; }
.combo-hint { font-size: 14px; color: var(--muted); margin: 8px 2px 0; }
.combo-list {
  margin-top: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  max-height: 264px; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.combo-item {
  display: flex; gap: 12px; width: 100%; min-height: 44px; padding: 10px 14px;
  text-align: left; border-bottom: 1px solid var(--line);
}
.combo-item:last-child { border-bottom: 0; }
.combo-item .num { font-weight: 600; min-width: 48px; }

/* Entries */
.entry { display: flex; align-items: center; min-height: 44px; border-top: 1px solid var(--line); }
.entry:first-child { border-top: 0; }
.entry .num { font-weight: 600; min-width: 56px; }
.entry .label { flex: 1; min-width: 0; color: var(--muted); overflow-wrap: anywhere; }
.entry .remove { width: 44px; height: 44px; margin-right: -14px; color: var(--muted); font-size: 18px; }
.day-head { font-weight: 600; }
.empty { color: var(--muted); padding: 4px 0; }

/* Weight row */
.weight-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; }
.link-btn { min-height: 44px; padding: 0 2px; color: var(--accent); font-weight: 500; }
.weight-form { display: flex; gap: 8px; margin-top: 8px; }
.weight-form .add-input { height: 44px; }
.weight-form .primary-btn { height: 44px; }

/* Ledger */
.week { padding: 0; overflow: hidden; }
.week-toggle { display: block; width: 100%; padding: 14px 16px; text-align: left; }
.week-title { font-weight: 600; }
.balance { font-weight: 600; white-space: nowrap; }
.week-body { padding: 0 16px 8px; border-top: 1px solid var(--line); }
.day-row {
  display: flex; justify-content: space-between; width: 100%; min-height: 44px; align-items: center;
  border-bottom: 1px solid var(--line); text-align: left;
}
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; padding: 6px 0; }
input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.week-actions { display: flex; justify-content: flex-end; }
.stat-number { font-size: 28px; font-weight: 650; letter-spacing: -0.015em; }

/* Bottom tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 2;
  display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab { flex: 1; max-width: 260px; height: 54px; font-weight: 500; font-size: 15px; color: var(--muted); }
.tab[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* Sheets */
dialog {
  width: min(100vw - 32px, 488px); max-height: calc(100dvh - 48px);
  padding: 20px 16px; border: 0; border-radius: 20px;
  background: var(--bg); color: var(--ink);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sheet-head h2 { font-size: 20px; font-weight: 650; }
.field { display: block; margin-top: 12px; }
.field > span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.field .add-input { width: 100%; }
.height-fields { display: flex; gap: 8px; }
textarea.add-input { height: 96px; padding: 10px 14px; width: 100%; resize: vertical; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

/* Toast */
#toast {
  position: fixed; left: 16px; right: 16px; z-index: 5; pointer-events: none;
  bottom: calc(env(safe-area-inset-bottom) + 66px);
  display: flex; justify-content: center;
}
.toast-body {
  pointer-events: auto; display: flex; align-items: center; gap: 12px;
  max-width: 488px; min-height: 44px; padding: 4px 8px 4px 16px; border-radius: 12px;
  background: var(--ink); color: var(--bg); font-size: 15px;
}
.toast-body button { min-height: 40px; padding: 0 8px; font-weight: 600; color: inherit; text-decoration: underline; }
.toast-body span { padding: 8px 8px 8px 0; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.add-input:focus-visible { outline: none; }
