:root {
  --bg: #FDFBF7;
  --tile: #FFFFFF;
  --ink: #1E2530;
  --muted: #64748B;
  --border: #2D3748;
  --shadow-color: #2D3748;
  --accent: #FFD23F;
  --field: #FFFFFF;
  --focus: #2D3748;
  --panel: #FFFFFF;
  --on-panel: #1E2530;
  --panel-muted: #64748B;
  --panel-line: #E2E8F0;
  --warn-bg: #EF4444;
  --warn-fg: #FFFFFF;
  --bar-h: 88px;

  --tint-video: #E0F2FE;
  --tint-music: #FCE7F3;
  --tint-ai: #D1FAE5;
  --tint-storage: #FEF3C7;
  --tint-game: #FFEDD5;
  --tint-book: #EDE9FE;
  --tint-health: #ECFCCB;
  --tint-tool: #F1F5F9;
  --tint-dev: #E0E7FF;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141824;
    --tile: #1F2433;
    --ink: #F8FAFC;
    --muted: #A0AEC0;
    --border: #4A5568;
    --shadow-color: #0F121C;
    --accent: #FBBF24;
    --field: #1F2433;
    --focus: #F8FAFC;
    --panel: #1F2433;
    --on-panel: #F8FAFC;
    --panel-muted: #A0AEC0;
    --panel-line: #334155;
    --warn-bg: #F87171;
    --warn-fg: #1E2530;

    --tint-video: #1E3A8A;
    --tint-music: #831843;
    --tint-ai: #064E3B;
    --tint-storage: #78350F;
    --tint-game: #7C2D12;
    --tint-book: #4C1D95;
    --tint-health: #365314;
    --tint-tool: #334155;
    --tint-dev: #312E81;

    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: "Hiragino Maru Gothic ProN", "Quicksand", "Nunito", "Yu Gothic UI", sans-serif;
  font-size: 16px; line-height: 1.5; font-variant-numeric: tabular-nums;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.app {
  max-width: 1200px; margin: 0 auto;
  padding: 0 16px calc(var(--bar-h) + 32px + env(safe-area-inset-bottom, 0px));
}
.top { padding: 18px 0 6px; }
h1 {
  margin: 0 0 12px; font-size: 26px; line-height: 1.2; font-weight: 900; letter-spacing: -0.01em;
}
.load-error {
  margin: 0 0 12px; padding: 10px 14px; border-radius: 14px;
  background: var(--warn-bg); color: var(--warn-fg); font-weight: 800;
  border: 2px solid var(--border); box-shadow: 0 3px 0 var(--shadow-color);
}
.search { display: block; margin-bottom: 12px; position: relative; }
.search input {
  width: 100%; height: 48px; border: 2.5px solid var(--border); border-radius: 16px;
  background: var(--field); padding: 0 16px; font-size: 15px; font-weight: 700;
  color: var(--ink); box-shadow: 0 4px 0 var(--shadow-color); transition: all 0.15s ease;
}
.search input:focus {
  outline: none; transform: translateY(2px); box-shadow: 0 2px 0 var(--shadow-color);
}
.search input::placeholder { color: var(--muted); opacity: 0.85; }

/* カテゴリチップ */
.chips {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  display: flex; gap: 8px; overflow-x: auto; padding: 8px 16px 14px; margin: 0 -16px;
  background: var(--bg); scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 44px; padding: 0 16px; border-radius: 999px;
  border: 2px solid var(--border); background: var(--tile);
  font-weight: 800; font-size: 14px; color: var(--ink);
  box-shadow: 0 3px 0 var(--shadow-color);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  transition: all 0.12s ease;
}
.chip:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 0 var(--shadow-color);
}
.chip[aria-pressed=true] {
  background: var(--accent); color: #1E2530; border-color: var(--border);
  transform: translateY(2px); box-shadow: 0 1px 0 var(--shadow-color);
}
.chip[data-all][aria-pressed=true] {
  background: var(--accent); color: #1E2530;
}

/* レイアウト・グリッド */
.layout { display: block; }
.grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px; margin: 8px 0 0; padding: 0; list-style: none;
}

/* 立体触感タイル */
.tile {
  position: relative; border-radius: 20px; border: 2.5px solid var(--border);
  background: var(--tile); box-shadow: 0 5px 0 var(--shadow-color);
  display: flex; flex-direction: column; min-width: 0;
  transition: all 0.15s cubic-bezier(0.34, 1.56, 0.64, 1);
  color: var(--ink); user-select: none;
}
.tile:hover {
  transform: translateY(-2px);
  box-shadow: 0 7px 0 var(--shadow-color);
}
.tile.on {
  transform: translateY(3px);
  box-shadow: 0 2px 0 var(--shadow-color);
  border-color: var(--border);
}
.tile.on[data-group="動画"] { background: var(--tint-video); }
.tile.on[data-group="音楽"] { background: var(--tint-music); }
.tile.on[data-group="AI"] { background: var(--tint-ai); }
.tile.on[data-group="ストレージ"] { background: var(--tint-storage); }
.tile.on[data-group="ゲーム"] { background: var(--tint-game); }
.tile.on[data-group="本・雑誌"] { background: var(--tint-book); }
.tile.on[data-group="学び・健康"] { background: var(--tint-health); }
.tile.on[data-group="家計簿・ツール"] { background: var(--tint-tool); }
.tile.on[data-group="開発"] { background: var(--tint-dev); }

.tile-main {
  all: unset; box-sizing: border-box; cursor: pointer; display: flex; flex-direction: column; gap: 4px;
  padding: 14px 14px 12px; min-height: 116px; flex: 1; border-radius: inherit;
}
.tile-main:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.t-cat { font-size: 12px; font-weight: 800; opacity: 0.75; padding-right: 32px; overflow-wrap: anywhere; }
.t-name { font-size: 17px; line-height: 1.25; font-weight: 900; padding-right: 28px; word-break: keep-all; overflow-wrap: anywhere; }
.t-price { margin-top: auto; padding-top: 6px; font-size: 16px; font-weight: 900; }
.t-plan { display: block; font-size: 12px; font-weight: 800; opacity: 0.8; line-height: 1.35; overflow-wrap: anywhere; }

/* チェックマーク */
.check {
  position: absolute; top: 12px; right: 12px; width: 24px; height: 24px;
  border-radius: 50%; border: 2.5px solid var(--border); background: var(--tile);
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s ease; pointer-events: none;
}
.on .check {
  background: #10B981; border-color: var(--border); color: #FFFFFF;
}
.on .check::after {
  content: "✓"; font-size: 14px; font-weight: 900;
}

.badge {
  display: inline-block; align-self: flex-start; margin: 4px 0 0;
  padding: 2px 8px; border-radius: 6px; background: var(--warn-bg); color: var(--warn-fg);
  font-size: 11px; font-weight: 900; border: 1.5px solid var(--border);
}

/* タイル内のコントロール */
.ctl {
  padding: 0 12px 14px; display: grid; gap: 8px;
  border-top: 2px dashed var(--border); margin-top: 2px; padding-top: 10px;
}
.ctl select {
  width: 100%; min-height: 40px; border: 2px solid var(--border); border-radius: 12px;
  background: var(--tile); color: var(--ink); padding: 0 10px; font-size: 13px; font-weight: 800;
  box-shadow: 0 2px 0 var(--shadow-color);
}
.seg {
  display: grid; grid-template-columns: 1fr 1fr; border: 2px solid var(--border);
  border-radius: 12px; overflow: hidden; background: var(--tile);
  box-shadow: 0 2px 0 var(--shadow-color);
}
.seg button {
  min-height: 36px; border: 0; background: var(--tile); color: var(--ink);
  font-size: 13px; font-weight: 900; transition: all 0.1s ease;
}
.seg button[aria-pressed=true] {
  background: var(--border); color: #FFFFFF;
}
.only {
  margin: 0; min-height: 36px; display: flex; align-items: center;
  font-size: 13px; font-weight: 800; opacity: 0.8;
}
.empty {
  grid-column: 1 / -1; padding: 32px 12px; text-align: center;
  color: var(--muted); font-weight: 800;
}

/* 手入力フォーム */
.manual {
  margin-top: 32px; border: 2.5px solid var(--border); border-radius: 24px;
  padding: 20px 18px 22px; background: var(--tile);
  box-shadow: 0 5px 0 var(--shadow-color);
}
.manual h2 { margin: 0 0 14px; font-size: 18px; font-weight: 900; }
.f { display: grid; gap: 14px; }
.f label, .f .lab { display: grid; gap: 6px; font-size: 13px; font-weight: 800; color: var(--muted); }
.f input[type=text] {
  height: 46px; border: 2px solid var(--border); border-radius: 12px;
  background: var(--field); padding: 0 12px; width: 100%; color: var(--ink);
  font-weight: 800; box-shadow: 0 2px 0 var(--shadow-color);
}
.cur {
  display: grid; grid-template-columns: 1fr 1fr; border: 2px solid var(--border);
  border-radius: 12px; overflow: hidden; box-shadow: 0 2px 0 var(--shadow-color);
}
.cur label { position: relative; display: block; }
.cur input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.cur span {
  display: flex; align-items: center; justify-content: center; min-height: 40px;
  background: var(--field); color: var(--ink); font-weight: 900; font-size: 14px;
}
.cur input:checked + span { background: var(--border); color: #FFFFFF; }
.err { margin: 0; color: var(--warn-bg); font-weight: 800; font-size: 13px; }
.add {
  min-height: 48px; border: 2.5px solid var(--border); border-radius: 14px;
  background: var(--accent); color: #1E2530; font-weight: 900; font-size: 15px;
  box-shadow: 0 4px 0 var(--shadow-color); transition: all 0.12s ease;
}
.add:active {
  transform: translateY(2px); box-shadow: 0 2px 0 var(--shadow-color);
}

.notes { margin-top: 28px; color: var(--muted); font-size: 13px; font-weight: 600; line-height: 1.6; }
.notes p { margin: 0 0 6px; }

/* 明細・サイドパネル */
.panel { background: var(--panel); color: var(--on-panel); }
.d-total { display: none; }
.panel h2 { margin: 0 0 10px; font-size: 17px; font-weight: 900; }
.rows { list-style: none; margin: 0 0 20px; padding: 0; }
.rows li {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
  padding: 10px 0; border-bottom: 2px dotted var(--panel-line); font-size: 14px;
}
.rows .n { font-weight: 900; overflow-wrap: anywhere; }
.rows .p { display: block; font-size: 12px; opacity: 0.75; font-weight: 700; }
.rows .r { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.rows .a { font-weight: 900; white-space: nowrap; }
.rm {
  min-height: 32px; padding: 0 12px; border: 2px solid var(--border); border-radius: 8px;
  background: var(--tile); color: var(--ink); font-weight: 900; font-size: 12px;
  box-shadow: 0 2px 0 var(--shadow-color); transition: all 0.1s ease;
}
.rm:active { transform: translateY(2px); box-shadow: none; }
.none { color: var(--panel-muted); margin: 0 0 20px; font-weight: 700; }
.sum {
  display: grid; gap: 6px; margin: 0 0 20px; padding: 14px; border-radius: 16px;
  background: var(--bg); border: 2px solid var(--border); box-shadow: 0 3px 0 var(--shadow-color);
}
.sum div { display: flex; justify-content: space-between; gap: 12px; font-weight: 800; font-size: 14px; }
.sum .usd { border-top: 2px dashed var(--border); padding-top: 8px; margin-top: 4px; }

/* 節約アドバイス */
.hints { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.hint {
  padding: 14px; border-radius: 16px; background: #FFFBEB; color: #1E2530;
  border: 2px solid var(--border); box-shadow: 0 3px 0 var(--shadow-color);
  font-weight: 800; font-size: 13.5px; line-height: 1.45;
}
.hint button {
  display: block; margin-top: 10px; min-height: 38px; padding: 0 16px;
  border: 2px solid var(--border); border-radius: 10px; background: var(--accent);
  color: #1E2530; font-weight: 900; font-size: 13px;
  box-shadow: 0 2px 0 var(--shadow-color); transition: all 0.1s ease;
}
.hint button:active { transform: translateY(2px); box-shadow: none; }

/* スマホ固定バー */
.bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; width: 100%;
  border: 0; border-top: 3px solid var(--border); border-radius: 24px 24px 0 0; text-align: left;
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 4px 16px;
  min-height: var(--bar-h); padding: 12px 20px calc(12px + env(safe-area-inset-bottom, 0px));
  background: var(--panel); color: var(--on-panel); box-shadow: 0 -4px 0 var(--shadow-color);
}
.b-lab { font-size: 12px; font-weight: 800; color: var(--panel-muted); }
.b-total { font-size: 34px; line-height: 1.05; font-weight: 900; color: var(--ink); letter-spacing: -0.01em; }
.b-total small { font-size: 15px; color: var(--muted); margin-left: 2px; }
.b-usd { font-size: 15px; font-weight: 900; color: #D97706; margin-left: 8px; }
.b-side { text-align: right; font-size: 13px; font-weight: 800; line-height: 1.5; }
.b-side .up {
  display: inline-block; width: 14px; height: 14px; border: solid var(--border);
  border-width: 3.5px 3.5px 0 0; transform: rotate(-45deg) translate(-2px, 2px);
  transition: transform .2s; margin-left: 6px;
}
.bar[aria-expanded=true] .up { transform: rotate(135deg) translate(-2px, 2px); }

/* シート */
.backdrop {
  position: fixed; inset: 0; z-index: 10; background: rgba(15, 23, 42, 0.6);
  opacity: 0; visibility: hidden; transition: opacity .2s, visibility .2s;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: var(--bar-h); z-index: 15;
  max-height: min(74vh, calc(100vh - var(--bar-h) - 24px)); overflow: auto;
  border-top: 3px solid var(--border); border-radius: 28px 28px 0 0;
  padding: 10px 20px 24px; transform: translateY(calc(100% + var(--bar-h)));
  visibility: hidden; transition: transform .25s cubic-bezier(0.16, 1, 0.3, 1), visibility .2s;
}
.open .sheet { transform: none; visibility: visible; }
.open .backdrop { opacity: 1; visibility: visible; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.grab { width: 44px; height: 5px; border-radius: 3px; background: var(--border); margin: 6px auto 6px; }
.close {
  min-height: 38px; padding: 0 16px; border: 2px solid var(--border); border-radius: 10px;
  background: var(--bg); color: var(--ink); font-weight: 900; font-size: 13px;
  box-shadow: 0 2px 0 var(--shadow-color);
}
.aside { display: none; }
.sheet [data-detail], .sheet #sheet-detail { display: flex; flex-direction: column; }
.sheet .sec-hints { order: -1; margin-bottom: 20px; }

/* PC用メディアクエリ (900px以上) */
@media (min-width: 900px) {
  .app { padding-bottom: 44px; padding-top: 10px; }
  .chips { position: static; margin: 0; padding-left: 0; padding-right: 0; flex-wrap: wrap; overflow: visible; }
  .layout { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 32px; align-items: start; }
  .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 14px; }
  .bar, .sheet, .backdrop { display: none; }
  .aside {
    display: block; position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow: auto;
    border-radius: 26px; padding: 24px; margin-top: 14px; border: 3px solid var(--border);
    box-shadow: 0 8px 0 var(--shadow-color);
  }
  .d-total { display: block; margin-bottom: 18px; }
  .d-total .b-lab { display: block; margin-bottom: 4px; }
  .d-total .b-total { font-size: 44px; }
  .d-total .yr { font-weight: 800; margin-top: 4px; font-size: 15px; }
}

@media (prefers-reduced-motion: reduce) {
  .sheet, .backdrop, .b-side .up, .tile { transition: none; }
}
