/* ============================================================
   デザイントークン
   スマホでの利用を前提に、面（背景/カード/入力欄）を3層に分け、
   金額の大小で明確な階層を作る。ダークモードにも対応する。
   ============================================================ */
:root {
  color-scheme: light;
  --bg: #f5f5f3;
  --surface: #ffffff;
  --surface-2: #efefec;
  --border: #e5e4e0;
  --text: #1b1b19;
  --text-muted: #6f6f69;
  --text-faint: #9c9c96;
  --accent: #2f7d5d;
  --accent-soft: #e2efe9;
  --danger: #bc4630;
  --danger-soft: #fae9e5;
  --warn: #a8791f;
  --warn-soft: #f7eeda;

  --radius: 16px;
  --radius-sm: 10px;
  --nav-h: 60px;

  --fs-hero: 2.15rem;
  --fs-title: 1.15rem;
  --fs-body: 0.94rem;
  --fs-label: 0.8rem;
  --fs-caption: 0.76rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #131513;
    --surface: #1c1f1d;
    --surface-2: #262a27;
    --border: #2f3431;
    --text: #eceeeb;
    --text-muted: #a0a49f;
    --text-faint: #757a75;
    --accent: #63b48c;
    --accent-soft: #1e3329;
    --danger: #e0745c;
    --danger-soft: #3a221d;
    --warn: #d6a548;
    --warn-soft: #362c17;
  }
}

* { box-sizing: border-box; }

/* IDセレクタでdisplayを指定している要素でもhidden属性が必ず勝つようにする
   （UAスタイルシートの[hidden]は詳細度が低く、何もしないと効かないため） */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", sans-serif;
  font-size: var(--fs-body);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { line-height: 1.35; margin: 0; font-weight: 700; }

.num { font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }

/* ============================================================
   ログイン画面
   ============================================================ */
#login-screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  padding: 2rem 1.5rem;
  text-align: center;
}
#login-screen .login-mark {
  width: 56px; height: 56px;
  border-radius: 18px;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  margin-bottom: 0.3rem;
}
#login-screen h1 { font-size: 1.45rem; letter-spacing: 0.01em; }
#login-screen p { margin: 0; color: var(--text-muted); font-size: var(--fs-body); }

/* ============================================================
   アプリシェル
   ============================================================ */
#app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
#app-header .brand {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: 0.01em;
}
#app-header .brand svg { color: var(--accent); }
#account-button {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: none;
  border: none;
  padding: 0.3rem;
  border-radius: 999px;
  color: var(--text-muted);
  cursor: pointer;
}
.avatar {
  width: 30px; height: 30px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: var(--fs-caption);
  font-weight: 700;
}

#view-container {
  max-width: 640px;
  margin: 0 auto;
  padding: 0.9rem 1rem calc(var(--nav-h) + 1.5rem + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

/* ボトムナビ（親指の届く位置に主要導線を置く） */
#nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  display: flex;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
#nav a {
  flex: 1 1 0;
  min-width: 0;
  height: var(--nav-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  text-decoration: none;
  color: var(--text-faint);
  font-size: 0.68rem;
  font-weight: 600;
  transition: color 0.15s;
}
#nav a.active { color: var(--accent); }
#nav a svg { width: 22px; height: 22px; }

/* ============================================================
   カード・セクション
   ============================================================ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.05rem;
}
.card + .card { margin-top: 0; }

.section-label {
  font-size: var(--fs-label);
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  padding-left: 0.15rem;
}

.card-title {
  font-size: var(--fs-title);
  margin-bottom: 0.7rem;
}

/* ヒーロー（今月の支出） */
.hero-label {
  font-size: var(--fs-label);
  font-weight: 700;
  color: var(--text-muted);
}
.hero-amount {
  font-size: var(--fs-hero);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.15;
  margin: 0.15rem 0 0.5rem;
}
.hero-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 1rem;
  font-size: var(--fs-caption);
  color: var(--text-muted);
}
.hero-sub b { color: var(--text); font-weight: 700; }

/* 先月比（絶対額だけでは増減が分からないため添える） */
.hero-compare {
  font-size: var(--fs-caption);
  color: var(--text-muted);
  margin: -0.25rem 0 0.55rem;
}
.hero-compare b { font-weight: 700; color: var(--text); }
.hero-compare.is-up b { color: var(--danger); }
.hero-compare.is-down b { color: var(--accent); }

/* 進捗バー */
.bar {
  position: relative;
  height: 7px;
  border-radius: 999px;
  background: var(--surface-2);
}
.bar > span {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.35s ease;
}
.bar.is-warn > span { background: var(--warn); }
.bar.is-over > span { background: var(--danger); }

/* 今日の位置を示す基準線。線より右まで塗られていれば「使うのが早い」 */
.bar.has-pace::after {
  content: '';
  position: absolute;
  top: -3px;
  bottom: -3px;
  left: var(--pace);
  width: 2px;
  margin-left: -1px;
  border-radius: 1px;
  background: var(--text-muted);
  opacity: 0.5;
}

/* 構成比ドーナツ */
.donut-wrap {
  position: relative;
  margin: 0.2rem auto 1rem;
}
.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  text-align: center;
}
.donut-center-label { font-size: var(--fs-caption); color: var(--text-muted); }
.donut-center-value {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

/* カテゴリの色ドット（ドーナツと一覧の行をつなぐ） */
.cat-dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 999px;
  margin-right: 0.45rem;
  vertical-align: 1px;
  flex: none;
}

/* カテゴリの色選択 */
.color-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}
.color-swatch {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 2px solid transparent;
  outline: 1px solid var(--border);
  outline-offset: -1px;
  padding: 0;
  cursor: pointer;
}
.color-swatch.is-active {
  border-color: var(--text);
  outline-color: transparent;
}

/* カテゴリ別の行 */
.cat-row + .cat-row { margin-top: 0.85rem; }
.cat-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}
.cat-name { font-weight: 600; }
.cat-amount { font-size: var(--fs-caption); color: var(--text-muted); }
.cat-amount b { color: var(--text); font-size: var(--fs-body); font-weight: 700; }
.cat-foot {
  margin-top: 0.28rem;
  font-size: var(--fs-caption);
  color: var(--text-faint);
  text-align: right;
}
.cat-foot.is-over { color: var(--danger); font-weight: 700; }

/* 行リスト */
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.7rem;
  padding: 0.62rem 0;
  border-bottom: 1px solid var(--border);
}
.row:last-child { border-bottom: none; }
.row-main { min-width: 0; }
.row-title { font-weight: 600; overflow-wrap: anywhere; }
.row-sub {
  font-size: var(--fs-caption);
  color: var(--text-muted);
  overflow-wrap: anywhere;
}
.row-amount { font-weight: 700; white-space: nowrap; }
.row-amount.is-in { color: var(--accent); }

.day-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0.9rem 0 0.25rem;
  font-size: var(--fs-caption);
  font-weight: 700;
  color: var(--text-muted);
}
.day-head:first-child { padding-top: 0.1rem; }
.day-head .day-total { color: var(--text-faint); font-weight: 600; }

.tag {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 700;
  padding: 0.05rem 0.4rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  vertical-align: 1px;
}
.tag.is-muted { background: transparent; color: var(--text-faint); font-weight: 600; }

/* 設定ハブ */
.menu-item {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.85rem 0.2rem;
  border-bottom: 1px solid var(--border);
  text-decoration: none;
  color: var(--text);
}
.menu-item:last-child { border-bottom: none; }
.menu-icon {
  width: 34px; height: 34px;
  flex: none;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
}
.menu-text { flex: 1 1 auto; min-width: 0; }
.menu-text small { display: block; color: var(--text-muted); font-size: var(--fs-caption); }
.menu-item .chev { color: var(--text-faint); flex: none; }

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  color: var(--text-muted);
  text-decoration: none;
  font-size: var(--fs-caption);
  font-weight: 600;
  margin-bottom: 0.1rem;
}

/* ============================================================
   フォーム・ボタン
   ============================================================ */
.field { margin-bottom: 0.85rem; }
.field:last-of-type { margin-bottom: 0; }
.field-label {
  display: block;
  font-size: var(--fs-label);
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 0.3rem;
}

input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  padding: 0.62rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  appearance: none;
}
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 17px) 55%, calc(100% - 12px) 55%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 2rem;
}
input.amount-input {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 0.5rem 0.7rem;
}
.field-row { display: flex; gap: 0.6rem; }
.field-row > * { flex: 1 1 0; min-width: 0; }

button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.62rem 1rem;
  transition: opacity 0.15s, background 0.15s;
}
button:active { opacity: 0.75; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--surface-2); color: var(--text); }
.btn-ghost { background: none; color: var(--text-muted); padding: 0.4rem 0.5rem; }
.btn-danger-text { background: none; color: var(--danger); padding: 0.4rem 0.5rem; }
.btn-block { width: 100%; }
.btn-lg { padding: 0.8rem 1rem; font-size: 1rem; }

/* 画面下の主要アクション（ボトムナビの上に浮かせる） */
.fab-bar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 0.7rem + env(safe-area-inset-bottom));
  z-index: 15;
  width: min(640px, calc(100% - 2rem));
  display: flex;
  gap: 0.6rem;
}
.fab-bar button {
  flex: 1 1 auto;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.16);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}
.fab-bar .btn-icon { flex: 0 0 auto; }

/* ============================================================
   ボトムシート（記録フォーム）
   ============================================================ */
dialog.sheet {
  border: none;
  padding: 0;
  background: transparent;
  width: 100%;
  max-width: 640px;
  max-height: 100dvh;
  margin: auto auto 0;
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-panel {
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 0.6rem 1.1rem calc(1.1rem + env(safe-area-inset-bottom));
  max-height: 92dvh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 0.24s cubic-bezier(0.2, 0.8, 0.2, 1);
}
dialog.sheet.is-open .sheet-panel { transform: none; }
@media (min-width: 641px) {
  dialog.sheet { margin: auto; }
  .sheet-panel { border-radius: var(--radius); }
}
.sheet-grip {
  width: 38px; height: 4px;
  border-radius: 999px;
  background: var(--border);
  margin: 0 auto 0.7rem;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.8rem;
}
.sheet-head h2 { font-size: var(--fs-title); }

/* ============================================================
   状態表示
   ============================================================ */
.empty-state {
  padding: 1.6rem 1rem;
  text-align: center;
  color: var(--text-faint);
  font-size: var(--fs-caption);
}
.loading-text {
  text-align: center;
  color: var(--text-faint);
  padding: 2.5rem 1rem;
  font-size: var(--fs-caption);
}
.notice {
  font-size: var(--fs-caption);
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.7rem;
}
.notice.is-info { background: var(--accent-soft); color: var(--accent); }
.notice.is-error { background: var(--danger-soft); color: var(--danger); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + 1.2rem + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(0.6rem);
  background: var(--text);
  color: var(--bg);
  padding: 0.6rem 1rem;
  border-radius: 999px;
  font-size: var(--fs-caption);
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
  z-index: 100;
  max-width: calc(100% - 2rem);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast-error { background: var(--danger); color: #fff; }
.toast-undo-mode {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 0.9rem;
}
.toast-undo {
  background: none;
  color: var(--accent);
  font-weight: 700;
  padding: 0.1rem 0;
  text-decoration: underline;
  text-underline-offset: 2px;
  flex: none;
}

/* 月切り替え */
.month-switcher {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.9rem;
  margin-bottom: 0.2rem;
}
.month-switcher-label {
  font-size: var(--fs-title);
  font-weight: 700;
  min-width: 8em;
  text-align: center;
}
.month-switcher .btn-ghost { padding: 0.4rem; color: var(--text-muted); }
.month-switcher .btn-ghost:disabled { opacity: 0.3; cursor: default; }
.month-switcher .btn-ghost:disabled:active { opacity: 0.3; }

/* カテゴリ選択チップ */
.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}
.chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 0.4rem 0.85rem;
  font-size: var(--fs-caption);
  font-weight: 600;
  cursor: pointer;
}
.chip.is-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* 支出の絞り込み */
.filter-bar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.15rem;
}
.filter-bar input[type="search"] { flex: 1 1 auto; }
.filter-bar select { flex: 0 0 auto; width: auto; max-width: 40%; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
