/* ---- トークン(ライト / ダーク)。グラフ色は dataviz の検証済みパレット ---- */
:root {
  color-scheme: light dark;
  --bg: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --wash: rgba(11, 11, 11, 0.04);
  --income: #2a78d6;
  --income-text: #1c5cab;
  --expense: #e34948;
  --accent: #2a78d6;
  --on-accent: #ffffff;
  --st-ok: #2a78d6;
  --st-warn: #fab219;
  --st-over: #d03b3b;
  --danger-text: #b42323;
  --track: #cde2fb;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
  --tabbar-h: 56px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --ink: #ffffff;
    --ink2: #c3c2b7;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --wash: rgba(255, 255, 255, 0.05);
    --income: #3987e5;
    --income-text: #86b6ef;
    --expense: #e66767;
    --st-ok: #3987e5;
    --st-warn: #fab219;
    --st-over: #e66767;
    --danger-text: #f19393;
    --track: #184f95;
    --shadow: 0 2px 16px rgba(0, 0, 0, 0.6);
  }
}

* { 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: 15px/1.5 system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 84px);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h2, h3 { margin: 0; }

/* ---- レイアウト ---- */
.appbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
}
.appbar h1 { margin: 0; font-size: 17px; font-weight: 600; }
main { max-width: 480px; margin: 0 auto; padding: 12px 16px 0; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; max-width: 120px; height: var(--tabbar-h);
  background: none; border: 0; color: var(--ink2);
  font-size: 11px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
}
.tabs .ti { font-size: 18px; line-height: 1; }
.tabs button[aria-current="page"] { color: var(--accent); font-weight: 600; }

#fab {
  position: fixed; right: 16px; z-index: 7;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  width: 56px; height: 56px; border-radius: 50%;
  border: 0; background: var(--accent); color: var(--on-accent);
  font-size: 28px; line-height: 1; box-shadow: var(--shadow);
}

/* ---- カード・見出し ---- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px; margin-bottom: 12px;
}
.card > h2, .section-title { font-size: 13px; font-weight: 600; color: var(--ink2); margin-bottom: 8px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.muted { color: var(--ink2); font-size: 13px; }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.income { color: var(--income-text); }
.neg { color: var(--danger-text); }
.empty { color: var(--ink2); text-align: center; padding: 20px 8px; }

.hero { text-align: center; padding: 18px 14px; }
.hero .label { font-size: 13px; color: var(--ink2); }
.hero .value { font-size: 44px; font-weight: 700; letter-spacing: -0.5px; line-height: 1.15; }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.tile .label { font-size: 12px; color: var(--ink2); }
.tile .value { font-size: 16px; font-weight: 600; }

.list { list-style: none; margin: 0; padding: 0; }
.list > li, .list > .li {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 0; border-top: 1px solid var(--grid);
}
.list > li:first-child, .list > .li:first-child { border-top: 0; }
.list .main { min-width: 0; flex: 1; }
.list .sub { display: block; color: var(--ink2); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .tap { cursor: pointer; }
.list .tap:active { background: var(--wash); }

/* ---- ボタン・入力 ---- */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  border-radius: 10px; padding: 8px 14px; min-height: 40px;
}
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; font-weight: 600; }
.btn.danger { color: var(--danger-text); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn.block { display: block; width: 100%; }
.btn:disabled { opacity: .5; }
.icon-btn { border: 0; background: none; padding: 6px 10px; font-size: 18px; color: var(--ink2); border-radius: 8px; }
.icon-btn:active { background: var(--wash); }

.field { margin-bottom: 14px; }
.field > label, .field > .lbl { display: block; font-size: 12px; color: var(--ink2); margin-bottom: 4px; }
.input {
  width: 100%; min-height: 44px; padding: 8px 12px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
}
.input:focus, .btn:focus-visible, .chip:focus-visible, .icon-btn:focus-visible, .tabs button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.input.big { font-size: 28px; font-weight: 600; text-align: right; }
.field.err .input { border-color: var(--st-over); }
.errmsg { color: var(--danger-text); font-size: 13px; margin-top: 4px; }
.errmsg::before { content: "⚠ "; }
.check { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.check input { width: 20px; height: 20px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border); background: var(--bg); border-radius: 999px;
  padding: 6px 12px; min-height: 36px; font-size: 14px;
}
.chip[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: transparent; font-weight: 600; }

.seg { display: flex; background: var(--wash); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 8px; padding: 7px 4px; min-height: 36px; font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--surface); font-weight: 600; box-shadow: 0 0 0 1px var(--border); }

.nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.nav .ttl { font-weight: 600; }

/* ---- 予算バー ---- */
.bar-row { padding: 8px 0; }
.bar-row .top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.meter { height: 8px; border-radius: 4px; background: var(--track); overflow: hidden; margin-top: 4px; }
.meter > i { display: block; height: 100%; background: var(--st-ok); border-radius: 4px; }
.meter.warn > i { background: var(--st-warn); }
.meter.over > i { background: var(--st-over); }
.badge {
  display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 999px;
  border: 1px solid currentColor; vertical-align: 1px;
}
.badge.warn { color: var(--ink); background: var(--st-warn); border-color: transparent; }
.badge.over { color: #fff; background: var(--st-over); border-color: transparent; }
.badge.fixed { color: var(--income-text); }
.badge.tag { color: var(--ink2); }

/* 入力シートの残り表示 */
.preview { font-size: 13px; margin: 0 0 14px; display: grid; gap: 4px; }
.preview .line { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.preview .st-warn { background: var(--st-warn); color: #0b0b0b; padding: 0 6px; border-radius: 4px; font-weight: 600; }
.preview .st-over { background: var(--st-over); color: #fff; padding: 0 6px; border-radius: 4px; font-weight: 600; }

/* ---- 履歴 ---- */
.day-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--ink2); margin: 14px 2px 4px; }
.entry { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 6px; }
.entry:active { background: var(--wash); }
.entry .main { min-width: 0; flex: 1; }
.entry .sub { color: var(--ink2); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- グラフ ---- */
.chart { width: 100%; height: auto; display: block; touch-action: manipulation; }
.chart text { fill: var(--muted); font-size: 10px; font-family: inherit; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .inc { fill: var(--income); }
.chart .exp { fill: var(--expense); }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .sel { fill: var(--wash); }
.chart .line { fill: none; stroke: var(--expense); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--expense); opacity: .10; stroke: none; }
.chart .dot { fill: var(--expense); stroke: var(--surface); stroke-width: 2; }
.chart .val { fill: var(--ink); font-size: 11px; font-weight: 600; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink2); margin: 2px 0 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.legend .i-inc { background: var(--income); }
.legend .i-exp { background: var(--expense); }
.readout { min-height: 22px; font-size: 13px; margin-top: 4px; }
details.table-view { margin-top: 8px; font-size: 13px; }
details.table-view summary { color: var(--ink2); cursor: pointer; }
table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th, table.t td { padding: 5px 6px; text-align: right; border-top: 1px solid var(--grid); white-space: nowrap; }
table.t th:first-child, table.t td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
.scroll-x { overflow-x: auto; }

.share { height: 4px; background: var(--grid); border-radius: 2px; margin-top: 4px; }
.share > i { display: block; height: 100%; background: var(--ink2); border-radius: 2px; opacity: .55; }
.delta { font-size: 12px; color: var(--ink2); }

/* ---- モーダル・シート ---- */
.backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, 0.45); display: flex; align-items: flex-end; justify-content: center; }
.backdrop.center { align-items: center; padding: 16px; }
.panel {
  width: 100%; max-width: 480px; max-height: 92vh; overflow-y: auto;
  background: var(--surface); border-radius: 16px 16px 0 0; padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}
.backdrop.center .panel { border-radius: 16px; max-height: 86vh; }
.panel h2 { font-size: 16px; margin-bottom: 12px; }
.panel .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.panel .actions .grow { flex: 1; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 40;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 84px);
  max-width: min(440px, calc(100vw - 32px));
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-size: 14px; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .2s;
}
#toast.show { opacity: 1; }
#toast.error { background: var(--st-over); color: #fff; }

.notice { border-left: 4px solid var(--st-warn); background: var(--wash); padding: 10px 12px; border-radius: 6px; margin-bottom: 12px; font-size: 14px; }
.notice.bad { border-left-color: var(--st-over); }

@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }
