:root {
  color-scheme: light;
  --bg: #FAFAF7;
  --card: #FFFFFF;
  --border: #E5E5E0;
  --ink: #1E1E1E;
  --muted: #6B6B66;
  --pos: #1B8A5A;
  --neg: #C0392B;
  --accent: #159957;
  --accent-ink: #FFFFFF;      /* text on accent fills */
  --step-active: #F2F2EE;     /* pressed control */
  --accent-tint: #ECF7F0;     /* "today" chip bg */
  --chip-bg: #EFEFEA;         /* neutral badge bg */
  --neg-tint: #FBECEA;        /* edited-late badge bg */
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --nav-h: 60px;
}

/* Dark palette. Applies when the OS prefers dark (unless the user forced light
   via Settings), OR when the user explicitly forces dark. `data-theme` is set
   on <html> by a pre-paint script from localStorage. Explicit color-scheme per
   state (not the "light dark" dual value that mis-rendered native controls on
   the osTA/PJAX side). The two dark blocks below are identical — keep in sync. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121311;
    --card: #1D1E1A;
    --border: #34352F;
    --ink: #ECECE4;
    --muted: #9C9C93;
    --pos: #47B583;
    --neg: #E5766A;
    --accent: #2FB673;
    --accent-ink: #0E1A12;
    --step-active: #26271F;
    --accent-tint: #16281E;
    --chip-bg: #262620;
    --neg-tint: #2E1B18;
    --shadow: 0 1px 3px rgba(0,0,0,.5);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121311;
  --card: #1D1E1A;
  --border: #34352F;
  --ink: #ECECE4;
  --muted: #9C9C93;
  --pos: #47B583;
  --neg: #E5766A;
  --accent: #2FB673;
  --accent-ink: #0E1A12;
  --step-active: #26271F;
  --accent-tint: #16281E;
  --chip-bg: #262620;
  --neg-tint: #2E1B18;
  --shadow: 0 1px 3px rgba(0,0,0,.5);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "Red Hat Display", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.45;
  overflow-x: hidden;
}

.app-header {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
}
.brand { font-weight: 700; font-size: 18px; text-decoration: none; color: var(--ink); }
.brand-mark { color: var(--accent); }

.app-main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--nav-h) + 80px);
}

/* ---- day head ---- */
.day-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.day-title { font-size: 20px; font-weight: 700; }
.today-chip { font-size: 12px; font-weight: 600; color: var(--accent); background: var(--accent-tint); padding: 2px 8px; border-radius: 999px; vertical-align: middle; }
.day-status { display: flex; gap: 8px; align-items: center; }
.countdown { font-size: 13px; color: var(--muted); }
.badge { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px; }
.badge.lock { background: var(--chip-bg); color: var(--muted); }
.badge.late { background: var(--neg-tint); color: var(--neg); }

/* ---- day navigation (prev / date / next) ---- */
.day-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.nav-arrow { font-size: 22px; line-height: 1; text-decoration: none; color: var(--accent); padding: 6px 14px; min-width: 44px; text-align: center; border-radius: 8px; }
.nav-arrow.disabled { color: var(--muted); opacity: .35; }
.day-jump-label { display: inline-flex; align-items: center; gap: 6px; }
.picker-btn { background: none; border: none; font-size: 16px; cursor: pointer; padding: 4px; line-height: 1; }
.day-jump { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.edit-anyway { background: none; border: 1px solid var(--border); border-radius: 999px; color: var(--muted); font: inherit; font-size: 12px; padding: 3px 10px; cursor: pointer; }

/* ---- zones & choices ---- */
.zone { margin-bottom: 18px; }
.zone-name { font-size: 15px; font-weight: 700; margin: 0 0 8px; }
.zone-icon { margin-right: 4px; }
.choices { display: flex; flex-direction: column; gap: 10px; }
.choice {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px;
}
.choice-main { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.choice-label { font-weight: 500; }
.choice-points { color: var(--muted); font-size: 13px; white-space: nowrap; }

.stepper { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.step {
  width: 44px; height: 44px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--card); color: var(--ink);
  font-size: 22px; line-height: 1; cursor: pointer;
}
.step:active { background: var(--step-active); }
.step:disabled { opacity: .4; cursor: default; }
.qty { min-width: 28px; text-align: center; font-weight: 700; font-size: 18px; }
.line-total { margin-left: auto; font-weight: 600; color: var(--muted); min-width: 60px; text-align: right; }
.line-total.pos { color: var(--pos); }
.line-total.neg { color: var(--neg); }

/* ---- note ---- */
.note-box { margin-top: 8px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; }
.note-box summary { cursor: pointer; font-weight: 600; }
.note-box textarea { width: 100%; margin-top: 8px; font: inherit; font-size: 16px; border: 1px solid var(--border); border-radius: 8px; padding: 8px; resize: vertical; background: var(--card); color: var(--ink); }
.note-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }

/* ---- net bar ---- */
.net-bar {
  position: fixed; left: 0; right: 0; bottom: var(--nav-h);
  max-width: 640px; margin: 0 auto;
  background: var(--card); border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
}
.net-label { font-size: 13px; letter-spacing: .08em; color: var(--muted); }
.net-value { font-size: 30px; font-weight: 700; }
.net-value.pos { color: var(--pos); }
.net-value.neg { color: var(--neg); }

/* ---- buttons ---- */
.btn { font: inherit; font-weight: 600; padding: 10px 16px; min-height: 44px; border-radius: 8px; border: 1px solid var(--border); background: var(--card); color: var(--ink); cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.muted { color: var(--muted); font-size: 13px; }

/* ---- empty states ---- */
.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty h1 { color: var(--ink); }
.empty-actions { margin: 20px 0; }

/* ---- bottom nav ---- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; height: var(--nav-h);
  background: var(--card); border-top: 1px solid var(--border);
  display: flex; padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; text-decoration: none; color: var(--muted); font-size: 11px;
}
.bottom-nav a.active { color: var(--accent); }
.nav-glyph { font-size: 18px; line-height: 1; }

/* ---- manage page ---- */
.step.small { width: 36px; height: 36px; font-size: 18px; }
.btn.small { padding: 6px 10px; min-height: 36px; font-size: 13px; }

.apply-banner, .refresh-banner {
  background: var(--accent-tint); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 14px; margin-bottom: 14px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; font-size: 14px; flex-wrap: wrap;
}

.manage-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.reorder-hint { font-size: 12px; }
.m-zone {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 12px; margin-bottom: 14px;
}
.m-zone-head { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }
.m-zone.collapsed .m-zone-head { margin-bottom: 0; }
.m-zone.collapsed .m-zone-body { display: none; }
.zone-toggle { flex: 0 0 auto; width: 30px; height: 38px; padding: 0; background: none; border: none; color: var(--muted); font-size: 12px; cursor: pointer; }
.zone-toggle::before { content: '▾'; }
.m-zone.collapsed .zone-toggle::before { content: '▸'; }
.zone-count { flex: 0 0 auto; min-width: 18px; text-align: center; font-size: 12px; }
.m-zone-head input, .m-clabel {
  font: inherit; font-size: 16px; border: 1px solid var(--border);
  border-radius: 8px; padding: 8px; background: var(--card); color: var(--ink);
}
.m-icon { flex: 0 0 46px; width: 46px; text-align: center; padding-left: 2px; padding-right: 2px; }
.m-name { flex: 1 1 auto; min-width: 0; font-weight: 700; }
.m-choices { display: flex; flex-direction: column; gap: 8px; }
.m-choice {
  display: flex; align-items: center; gap: 6px;
  padding: 8px; border: 1px solid var(--border); border-radius: 8px;
}
.m-clabel { flex: 1 1 auto; min-width: 0; }
.m-pts-stepper { display: flex; align-items: center; gap: 3px; flex: 0 0 auto; }
.m-pts { min-width: 30px; text-align: center; font-weight: 700; }

/* drag-to-reorder handle (grab and move; touch-action:none so it doesn't scroll) */
.drag-handle {
  flex: 0 0 auto; width: 24px; height: 38px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: none; color: var(--muted);
  font-size: 15px; letter-spacing: -3px; cursor: grab; touch-action: none;
}
.drag-handle:active { cursor: grabbing; }
.row-archive {
  flex: 0 0 auto; background: none; border: 1px solid var(--border);
  border-radius: 6px; color: var(--muted); font-size: 11px;
  padding: 6px 7px; cursor: pointer; white-space: nowrap;
}
.m-zone.dragging, .m-choice.dragging {
  box-shadow: 0 12px 28px rgba(0,0,0,.4); transform: scale(1.02);
  transition: box-shadow .12s ease, transform .12s ease;
}
.drag-placeholder {
  box-sizing: border-box; border: 2px dashed var(--border); border-radius: 10px;
  background: color-mix(in srgb, var(--accent) 9%, transparent);
}
.add-choice { margin-top: 10px; }
.add-zone { margin-bottom: 24px; }

.archived-box {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 14px; margin-bottom: 24px;
}
.archived-box summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.arch-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; padding: 8px 0; border-top: 1px solid var(--border);
}
.arch-row:first-of-type { border-top: none; margin-top: 8px; }

/* ---- history ---- */
.hist-monthnav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.hist-monthlabel { font-weight: 700; font-size: 18px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 24px; }
.cal-head { text-align: center; font-size: 11px; color: var(--muted); padding: 4px 0; }
.cal-cell {
  aspect-ratio: 1 / 1; border: 1px solid var(--border); border-radius: 8px;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: 4px 5px; text-decoration: none; color: var(--ink); overflow: hidden;
}
.cal-cell.blank { border: none; }
.cal-cell.future { opacity: .4; border-style: dashed; }
.cal-cell.empty { color: var(--muted); }
.cal-num { font-size: 11px; color: var(--muted); }
.cal-late { margin-left: 1px; font-size: 9px; }
.cal-net { font-size: 15px; font-weight: 700; text-align: right; }
.cal-dot { text-align: right; color: var(--border); font-size: 16px; line-height: .6; }

.hist-range { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.range-toggle a {
  text-decoration: none; color: var(--muted); font-size: 13px; padding: 4px 10px;
  border: 1px solid var(--border); border-radius: 999px; margin-left: 6px;
}
.range-toggle a.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.chart-wrap { display: flex; gap: 8px; align-items: stretch; }
.chart-scale { display: flex; flex-direction: column; justify-content: space-between; font-size: 11px; color: var(--ink); padding: 2px 0; }
.hist-chart { width: 100%; height: auto; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); }
.hist-chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.hist-chart .zero-line { stroke: var(--border); stroke-width: 1; }
.hist-chart .pt { fill: var(--accent); }
.chart-empty { padding: 24px; text-align: center; }

/* ---- settings ---- */
.setting-group {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 16px;
}
.setting-group h2 { margin: 0 0 4px; font-size: 15px; }
.theme-toggle { display: flex; gap: 8px; margin-top: 10px; }
.theme-toggle .btn { flex: 1; }
.theme-opt.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.settings-note { margin-top: 4px; }

/* ---- toast ---- */
.toast { position: fixed; bottom: calc(var(--nav-h) + 90px); left: 50%; transform: translateX(-50%); background: #1E1E1E; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; opacity: 0; transition: opacity .2s; z-index: 50; }
.toast.show { opacity: .95; }
