:root {
  --bg: #f7f3ec;
  --surface: #fffdf9;
  --ink: #4b4038;
  --muted: #8a7d72;
  --line: #e8dfd3;
  --accent: #6f9a86;
  --accent-ink: #ffffff;
  --accent-soft: #e4efe8;
  --warn: #c2705a;
  --warn-soft: #f7e6df;
  --income: #5a8aab;
  --income-soft: #e2edf4;
  --shadow: 0 1px 2px rgba(75, 64, 56, .06), 0 4px 14px rgba(75, 64, 56, .06);
  --radius: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1f1c19; --surface: #2a2622; --ink: #efe7dc; --muted: #a89c90; --line: #3a352f;
    --accent: #8fbaa5; --accent-ink: #1f1c19; --accent-soft: #2f3b35;
    --warn: #e39a80; --warn-soft: #3d2c26; --income: #8db6d3; --income-soft: #26323b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1f1c19; --surface: #2a2622; --ink: #efe7dc; --muted: #a89c90; --line: #3a352f;
  --accent: #8fbaa5; --accent-ink: #1f1c19; --accent-soft: #2f3b35;
  --warn: #e39a80; --warn-soft: #3d2c26; --income: #8db6d3; --income-soft: #26323b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

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

/* 上部 */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
}
.period { font-size: 14px; color: var(--muted); font-feature-settings: "tnum"; }
.sync {
  border: 1px solid var(--line); background: var(--surface); color: var(--muted);
  border-radius: 999px; padding: 4px 12px; font-size: 12px;
}
.sync.pending { color: var(--warn); border-color: var(--warn); }
.sync.ok::before { content: "● "; color: var(--accent); }

/* 残額 */
.budget {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 16px; margin-bottom: 12px;
}
.budget .label { font-size: 13px; color: var(--muted); }
.budget .remain { font-size: 30px; font-weight: 700; letter-spacing: .01em; font-feature-settings: "tnum"; line-height: 1.2; }
.budget .remain small { font-size: 15px; font-weight: 500; margin-right: 4px; }
.budget .perday { font-size: 15px; margin-top: 2px; }
.budget .perday b { font-feature-settings: "tnum"; }
.budget .bar { height: 6px; border-radius: 99px; background: var(--line); margin-top: 8px; overflow: hidden; }
.budget .bar i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.budget .sub { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 6px; }
.budget.low .remain, .budget.low .perday b { color: var(--warn); }
.budget.low .bar i, .budget.over .bar i { background: var(--warn); }
.budget.over { background: var(--warn-soft); }
.budget.over .remain { color: var(--warn); }
.budget .empty { font-size: 14px; color: var(--muted); }

/* 入力 */
.entry {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 16px 16px;
}
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border-radius: 12px; padding: 3px; }
.seg button { border: 0; background: transparent; border-radius: 10px; padding: 7px; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--ink); font-weight: 700; box-shadow: var(--shadow); }
.entry.income .seg button.on { color: var(--income); }

.amount {
  display: flex; align-items: baseline; gap: 6px;
  border-bottom: 2px solid var(--line); margin: 8px 0 8px; padding: 0 4px;
}
.amount:focus-within { border-color: var(--accent); }
.entry.income .amount:focus-within { border-color: var(--income); }
.amount .yen { font-size: 24px; color: var(--muted); }
.amount input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent;
  font-size: 40px; font-weight: 700; font-feature-settings: "tnum"; text-align: right; padding: 0;
}
.amount.shake { animation: shake .3s; border-color: var(--warn); }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

.opts { display: grid; gap: 8px; margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); }
.opt { display: grid; grid-template-columns: 64px 1fr; align-items: center; gap: 8px; }
.lbl { font-size: 12px; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 999px;
  padding: 4px 11px; font-size: 13px; color: var(--muted);
}
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--ink); font-weight: 600; }
.field {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px;
  padding: 6px 10px; font-size: 14px; min-height: 36px;
}
.field:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field.notToday { border-color: var(--warn); color: var(--warn); font-weight: 600; }

.hint { font-size: 12px; color: var(--muted); text-align: center; margin: 4px 0 8px; }
.cats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.cat {
  border: 0; border-radius: 14px; background: var(--accent-soft); color: var(--ink);
  min-height: 52px; padding: 6px 4px; font-size: 13px; font-weight: 600; line-height: 1.3;
  transition: transform .08s;
}
.cat:active { transform: scale(.95); }
.cat.yk { position: relative; }
.entry.income .cat { background: var(--income-soft); }

/* 一覧 */
.recent { margin-top: 18px; }
h2 { font-size: 14px; color: var(--muted); font-weight: 600; margin: 0 0 6px 4px; }
.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list:empty { display: none; }
.list li + li { border-top: 1px solid var(--line); }
.row {
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 2px 10px;
  width: 100%; text-align: left; border: 0; background: transparent; padding: 10px 14px;
}
.row .cat-name { font-weight: 600; font-size: 15px; }
.row .amt { font-weight: 700; font-feature-settings: "tnum"; }
.row .amt.in { color: var(--income); }
.row .meta { grid-column: 1 / -1; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag { display: inline-block; font-size: 11px; color: var(--warn); border: 1px solid var(--warn); border-radius: 6px; padding: 0 5px; margin-left: 6px; font-weight: 500; vertical-align: 1px; }
.empty-msg { color: var(--muted); font-size: 14px; text-align: center; padding: 20px 0; }

/* 履歴 */
.hist-head { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 12px; }
.hist-head .pname { font-weight: 700; font-feature-settings: "tnum"; }
.iconbtn { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; width: 40px; height: 36px; font-size: 16px; }
.iconbtn:disabled { opacity: .35; }
.totals { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.totals div { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); padding: 8px 10px; }
.totals span { display: block; font-size: 11px; color: var(--muted); }
.totals b { font-feature-settings: "tnum"; font-size: 15px; }
.totals .neg { color: var(--warn); }
.day { margin: 14px 0 6px 4px; font-size: 13px; color: var(--muted); display: flex; justify-content: space-between; padding-right: 4px; }
details.trash { margin-top: 24px; }
details.trash summary { font-size: 14px; color: var(--muted); cursor: pointer; margin: 0 0 8px 4px; }
.trash .row { opacity: .75; }
.linkbtn { border: 0; background: none; color: var(--accent); font-weight: 600; font-size: 13px; padding: 4px 0; }

/* 設定 */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card h3 { margin: 0 0 8px; font-size: 15px; }
.card p { margin: 6px 0; font-size: 14px; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline .field { flex: 1; font-size: 18px; font-feature-settings: "tnum"; }
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 9px 16px; font-size: 15px; font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--warn); border-color: var(--warn); background: transparent; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ログイン */
.login { text-align: center; padding: 18vh 8px 0; }
.login h1 { font-size: 24px; margin: 0 0 8px; }
.login .gbtn { margin: 20px 0; min-width: 240px; padding: 12px 20px; font-size: 16px; }

/* タブ */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(3, 1fr);
  max-width: 520px; margin: 0 auto;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button { border: 0; background: transparent; padding: 14px 0 12px; font-size: 14px; color: var(--muted); }
.tabs button.on { color: var(--accent); font-weight: 700; }

/* お知らせ */
.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 10;
  max-width: 488px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--ink); color: var(--bg); border-radius: 12px; padding: 12px 14px; font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
}
.toast button { border: 0; background: none; color: var(--bg); font-weight: 700; text-decoration: underline; padding: 0; white-space: nowrap; }

/* 修正ダイアログ */
dialog {
  border: 0; border-radius: 18px; padding: 18px 16px; width: min(480px, calc(100vw - 32px));
  background: var(--surface); color: var(--ink); box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(30, 25, 20, .45); }
dialog h3 { margin: 0 0 12px; font-size: 17px; }
.form { display: grid; gap: 10px; }
.form label { display: grid; gap: 3px; font-size: 12px; color: var(--muted); }
.form .field { font-size: 16px; color: var(--ink); }
.form .by { font-size: 12px; color: var(--muted); }
.actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 16px; }
.actions .right { display: flex; gap: 8px; }

@media (max-width: 360px) {
  .cats { grid-template-columns: repeat(3, 1fr); }
  .amount input { font-size: 34px; }
}
