:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --text: #1f1d1a;
  --muted: #77736b;
  --line: #e4e0d6;
  --primary: #27466e;
  --primary-soft: #e3e8f0;
  --on-primary: #ffffff;
  --expense: #b3432f;
  --income: #3b7650;
  --warn: #a8701c;
  --sun: #b3432f;
  --sat: #27466e;
  --shadow: none;
  --radius: 10px;
  --tabbar-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161513;
    --surface: #1f1e1b;
    --surface-2: #2a2825;
    --text: #ebe7df;
    --muted: #9c978d;
    --line: #34322e;
    --primary: #9db7da;
    --primary-soft: #26303d;
    --on-primary: #14171c;
    --expense: #e08a78;
    --income: #8cc29d;
    --warn: #d9a656;
    --sun: #e08a78;
    --sat: #9db7da;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", "Zen Kaku Gothic New", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-feature-settings: "palt";
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  max-width: 520px;
  margin: 0 auto;
  /* 画面の高さに固定し、中央だけをスクロールさせる（タブバーが動かない） */
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.expense { color: var(--expense); }
.income { color: var(--income); }

/* ---------- top bar ---------- */
.topbar {
  flex: none; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 6px) 12px 6px;
  background: var(--bg);
}
.title { font-size: 17px; font-weight: 700; margin: 0; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text);
  font-size: 22px; line-height: 1;
}
.icon-btn[hidden] { visibility: hidden; display: block; }

.view {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  align-content: start;
  padding: 4px 16px 16px;
  display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr);
}

/* ---------- cards ---------- */
.card {
  background: var(--surface); border-radius: var(--radius);
  border: 1px solid var(--line); padding: 12px 16px;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.card-head h2 { font-size: 13px; margin: 0; color: var(--muted); font-weight: 600; letter-spacing: .04em; }
.link { background: none; border: 0; color: var(--primary); font-size: 13px; font-weight: 600; padding: 4px; }
.link.more { width: 100%; padding-top: 8px; color: var(--muted); }
.head-val { font-size: 14px; font-weight: 700; }

/* summary */
.summary .label { font-size: 12px; color: var(--muted); }
.summary .big { font-size: 30px; font-weight: 700; letter-spacing: -.01em; line-height: 1.2; }
.summary .row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 6px; }
.pill {
  border-top: 1px solid var(--line); padding: 8px 0 0;
  display: flex; justify-content: space-between; align-items: baseline; gap: 6px;
}
.pill b { font-size: 16px; }
.budget { margin-top: 8px; }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; display: flex; }
.bar > span { display: block; height: 100%; }
.budget .bar > span { background: var(--primary); }
.budget.over .bar > span { background: var(--expense); }
.budget-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 4px; }

/* rows */
.rows { display: grid; grid-template-columns: minmax(0, 1fr); }
.row-item {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 0; border-top: 1px solid var(--line);
  background: none; border-left: 0; border-right: 0; border-bottom: 0;
  width: 100%; text-align: left;
}
.rows > .row-item:first-child { border-top: 0; }
.ic {
  width: 34px; height: 34px; flex: none; border-radius: 8px;
  display: grid; place-items: center; font-size: 15px; font-weight: 700;
  color: var(--c, var(--muted)); background: color-mix(in srgb, var(--c, var(--muted)) 13%, transparent);
}
.row-main { flex: 1; min-width: 0; }
.row-main .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; }
.row-main .t > span:first-child { overflow: hidden; text-overflow: ellipsis; }
.row-main .s { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.amt { font-weight: 700; white-space: nowrap; }
.badge {
  font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 4px; flex: none;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.danger { background: color-mix(in srgb, var(--expense) 16%, transparent); color: var(--expense); }
.badge.ok { background: color-mix(in srgb, var(--income) 16%, transparent); color: var(--income); }
.pay-btn {
  border: 1px solid var(--primary); color: var(--primary); background: none;
  border-radius: 6px; padding: 5px 10px; font-size: 13px; font-weight: 700; white-space: nowrap;
}
.pay-btn.done { border-color: var(--line); color: var(--muted); }

.empty { text-align: center; color: var(--muted); font-size: 13px; padding: 10px 0 4px; }
.empty .btn { margin-top: 8px; }

/* wallets strip */
.wallet-strip { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; margin: 0 -4px; padding: 0 4px; }
.wallet-strip::-webkit-scrollbar { display: none; }
.wchip {
  flex: none; min-width: 110px; background: none; border-radius: 0; padding: 2px 16px 2px 0;
  border: 0; border-right: 1px solid var(--line); text-align: left; margin-right: 8px;
}
.wchip:last-child { border-right: 0; }
.wchip .n { font-size: 12px; color: var(--muted); }
.wchip b { display: block; font-size: 15px; }

/* category breakdown */
.cat-bar { height: 10px; gap: 2px; background: none; }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; margin-top: 8px; font-size: 12px; }
.legend div { display: flex; align-items: center; gap: 6px; min-width: 0; }
.legend i { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.legend span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- calendar ---------- */
.cal { padding: 10px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.cal-wd { text-align: center; font-size: 11px; color: var(--muted); padding: 2px 0 4px; font-weight: 600; }
.cal-wd.sun, .cal-day.sun .d { color: var(--sun); }
.cal-wd.sat, .cal-day.sat .d { color: var(--sat); }
.cal-day {
  min-height: 46px; border: 0; background: none; border-radius: 6px;
  padding: 3px 2px; display: flex; flex-direction: column; align-items: center; gap: 1px;
  font-size: 11px; line-height: 1.2;
}
.cal-day.out { visibility: hidden; }
.cal-day .d { font-size: 13px; font-weight: 500; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 6px; }
.cal-day.today .d { background: var(--text); color: var(--bg) !important; }
.cal-day.sel { background: var(--primary-soft); box-shadow: inset 0 0 0 1px var(--primary); }
.cal-day .ex { color: var(--expense); }
.cal-day .in { color: var(--income); }
.dots { display: flex; gap: 2px; }
.dots i { width: 10px; height: 3px; border-radius: 1px; background: var(--warn); }
.dots i.paid { background: var(--muted); opacity: .5; }
.dots i.late { background: var(--expense); }
.dots i.in { background: var(--income); }
.cal-legend { display: flex; gap: 12px; justify-content: center; font-size: 11px; color: var(--muted); margin-top: 6px; }
.cal-legend i { display: inline-block; width: 10px; height: 3px; border-radius: 1px; background: var(--warn); margin-right: 4px; vertical-align: middle; }
.cal-legend i.paid { background: var(--muted); opacity: .5; }
.cal-legend i.late { background: var(--expense); }
.cal-legend i.in { background: var(--income); }

/* ---------- list ---------- */
.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  border: 1px solid var(--line); border-radius: 8px; padding: 2px; gap: 2px; background: var(--surface);
}
.seg button {
  border: 0; background: none; border-radius: 6px; padding: 7px 0; font-weight: 600; font-size: 14px; color: var(--muted);
}
.seg button.on { background: var(--text); color: var(--bg); }
.seg.type button.on[data-v="expense"] { background: var(--expense); color: #fff; }
.seg.type button.on[data-v="income"] { background: var(--income); color: #fff; }
.day-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 2px 0; }

/* ---------- buttons & forms ---------- */
.btn {
  border: 0; border-radius: 8px; padding: 12px 16px; font-weight: 700;
  background: var(--primary); color: var(--on-primary);
}
.btn.ghost { background: none; color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: none; color: var(--expense); }
.btn.block { width: 100%; }
.btn.sm { padding: 7px 12px; font-size: 13px; }
.btn-row { display: flex; gap: 8px; }
.btn-row > * { flex: 1; }

.form { display: grid; gap: 12px; }
.field { display: grid; gap: 4px; }
.field > label, .field > .lbl { font-size: 12px; color: var(--muted); font-weight: 600; }
.input, select.input {
  width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  padding: 10px 12px; font-size: 16px; outline: none;
}
.input:focus { border-color: var(--primary); }
.amount-input {
  font-size: 32px; font-weight: 700; text-align: right; padding: 8px 12px; border: 0;
  border-bottom: 2px solid var(--line); border-radius: 0; background: none; width: 100%; outline: none;
}
.amount-input:focus { border-color: var(--primary); }
.amount-wrap { display: flex; align-items: baseline; gap: 4px; }
.amount-wrap span { font-size: 22px; font-weight: 700; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.cat-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.cat-grid label { cursor: pointer; }
.cat-grid input { position: absolute; opacity: 0; pointer-events: none; }
.cat-grid .c {
  display: grid; justify-items: center; gap: 3px; padding: 7px 2px 5px; border-radius: 8px;
  font-size: 11px; background: var(--surface); border: 1px solid var(--line);
}
.cat-grid .c b {
  width: 26px; height: 26px; display: grid; place-items: center; border-radius: 6px; font-size: 13px;
  color: var(--c, var(--muted)); background: color-mix(in srgb, var(--c, var(--muted)) 13%, transparent);
}
.cat-grid input:checked + .c { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); }

.switch { position: relative; width: 48px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: .2s; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: .2s; }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::after { transform: translateX(20px); }

.set-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.set-row:first-of-type { border-top: 0; }
.set-row .row-main .s { line-height: 1.4; white-space: normal; }
.set-row .row-main .t { white-space: normal; }

/* ---------- tab bar ---------- */
.tabbar {
  flex: none; z-index: 20;
  width: 100%;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  background: var(--surface); border-top: 1px solid var(--line);
}
.tab {
  border: 0; background: none; display: grid; justify-items: center; gap: 1px;
  font-size: 10px; color: var(--muted); font-weight: 600; padding: 6px 0;
}
.tab-ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; }
.tab.on { color: var(--text); }
.tab.on .tab-ic { stroke-width: 2; }
.fab {
  justify-self: center; width: 48px; height: 48px; border-radius: 50%; border: 0;
  background: var(--text); color: var(--bg); font-size: 28px; line-height: 1; font-weight: 300;
}

/* ---------- sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); animation: fade .2s; }
.sheet-panel {
  position: absolute; left: 50%; bottom: 0; transform: translateX(-50%);
  width: 100%; max-width: 520px; max-height: 92dvh; overflow-y: auto;
  background: var(--bg); border-radius: 14px 14px 0 0;
  padding: 6px 16px calc(16px + env(safe-area-inset-bottom));
  animation: up .22s ease-out;
}
.sheet-grip { width: 40px; height: 4px; border-radius: 4px; background: var(--line); margin: 4px auto 10px; }
.sheet h3 { margin: 0 0 12px; font-size: 17px; }
@keyframes up { from { transform: translate(-50%, 30%); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px;
  display: flex; gap: 12px; align-items: center; font-size: 14px; max-width: calc(100% - 32px);
  box-shadow: 0 4px 14px rgba(0,0,0,.15);
}
.toast[hidden] { display: none; }
.toast button { background: none; border: 0; white-space: nowrap; color: inherit; font-weight: 700; text-decoration: underline; text-underline-offset: 3px; }

.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }
.amt small { font-size: 11px; font-weight: 500; margin-right: 2px; }

/* 支出の推移（単一系列の棒。表示中の月だけ濃く、金額を直接表示） */
.trend-bars { display: grid; grid-template-columns: repeat(6, 1fr); gap: 2px; height: 104px; }
.trend-col {
  position: relative; border: 0; background: none; padding: 16px 4px 0;
  display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px;
}
.trend-bar { width: 60%; max-width: 28px; border-radius: 4px 4px 0 0; background: color-mix(in srgb, var(--text) 18%, transparent); }
.trend-col.cur .trend-bar { background: var(--primary); }
.trend-m { font-size: 11px; color: var(--muted); line-height: 1; padding-top: 2px; border-top: 1px solid var(--line); width: 100%; }
.trend-col.cur .trend-m { color: var(--text); font-weight: 700; }
.trend-val { position: absolute; top: 0; left: 50%; transform: translateX(-50%); font-size: 11px; font-weight: 700; white-space: nowrap; }
.trend-bars > :first-child .trend-val { left: 0; transform: none; }
.trend-bars > :last-child .trend-val { left: auto; right: 0; transform: none; }
@media (hover: hover) {
  .trend-col:not(.cur):hover .trend-bar { background: color-mix(in srgb, var(--primary) 55%, transparent); }
  .trend-col:not(.cur):hover::after {
    content: attr(data-tip); position: absolute; top: 0; left: 50%; transform: translateX(-50%);
    font-size: 11px; font-weight: 700; white-space: nowrap; background: var(--text); color: var(--bg); padding: 1px 5px; border-radius: 4px;
  }
}

.lack { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 12px; font-weight: 700; color: var(--expense); }
.seg.mini { margin-bottom: 4px; }
.seg.mini button { padding: 5px 0; font-size: 13px; }
.day-head.in-card { padding: 10px 0 2px; }

/* ---------- login ---------- */
.login {
  position: fixed; inset: 0; z-index: 40; background: var(--bg);
  overflow-y: auto; display: flex; padding: env(safe-area-inset-top) 20px env(safe-area-inset-bottom);
}
.login[hidden] { display: none; }
.login-box { width: 100%; max-width: 380px; margin: auto; padding: 24px 0; display: grid; gap: 14px; }
.login-brand { display: grid; justify-items: center; gap: 8px; margin-bottom: 8px; }
.login-brand img { border-radius: 14px; }
.login-brand h2 { margin: 0; font-size: 22px; letter-spacing: .04em; }
.or { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 12px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.login-msg { min-height: 20px; font-size: 13px; color: var(--muted); }
.login-msg.err { color: var(--expense); font-weight: 700; }
.muted-link { color: var(--muted); }
.login .link { justify-self: center; }
.btn:disabled { opacity: .55; }

/* ---------- 明細の取り込み ---------- */
details.map { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--surface); }
details.map summary { font-size: 13px; color: var(--muted); cursor: pointer; }
.imp-head { display: flex; justify-content: space-between; align-items: center; font-size: 13px; font-weight: 700; }
.imp-head .link { padding: 4px 6px; }
.imp-list { display: grid; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 0 12px; }
.imp-row { display: grid; grid-template-columns: auto 40px minmax(0, 1fr) auto; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px; }
.imp-row:first-child { border-top: 0; }
.imp-row input { width: 18px; height: 18px; accent-color: var(--primary); }
.imp-row.off { opacity: .55; }
.imp-date { color: var(--muted); font-size: 12px; }
.imp-memo { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.imp-memo small { display: block; font-size: 11px; color: var(--muted); }
.imp-amt { font-weight: 700; white-space: nowrap; }
