/* =========================================================================
 * くらしAI — デザインシステム
 * ========================================================================= */
:root {
  --bg: #f4f5fb;
  --card: #ffffff;
  --card-2: #f8f9fd;
  --text: #1e2233;
  --text-dim: #6b7185;
  --border: #e6e8f0;
  --accent: #6366f1;
  --accent-soft: #eef0fe;
  --good: #22c55e;
  --good-soft: #e7f8ee;
  --good-text: #15803d;
  --warn: #f59e0b;
  --warn-soft: #fef4e2;
  --warn-text: #b45309;
  --danger: #ef4444;
  --danger-soft: #fdeaea;
  --danger-text: #dc2626;
  --info: #06b6d4;
  --shadow: 0 1px 2px rgba(20,25,50,.04), 0 8px 24px rgba(20,25,50,.06);
  --radius: 16px;
  --radius-sm: 10px;
  --sidebar-w: 232px;
  --font: -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", Meiryo, system-ui, sans-serif;
}
[data-theme="dark"] {
  --bg: #0f1117;
  --card: #181b24;
  --card-2: #1f2330;
  --text: #e7e9f3;
  --text-dim: #9aa0b5;
  --border: #2a2f3d;
  --accent: #818cf8;
  --accent-soft: #232843;
  --good-soft: #14301f;
  --good-text: #4ade80;
  --warn-soft: #36280f;
  --warn-text: #fbbf24;
  --danger-soft: #3a1c1c;
  --danger-text: #f87171;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  display: flex;
  font-size: 15px;
  line-height: 1.55;
}

/* ---- サイドバー ---------------------------------------------------- */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 18px; }
.brand-logo { font-size: 26px; }
.brand-name { font-size: 19px; font-weight: 600; letter-spacing: .2px; }
.brand-name b { color: var(--accent); font-weight: 800; }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav-bottom { margin-top: auto; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 12px; border-radius: var(--radius-sm);
  color: var(--text-dim); cursor: pointer; user-select: none;
  font-weight: 500; transition: background .15s, color .15s;
}
.nav-item:hover { background: var(--card-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-ico { font-size: 18px; width: 22px; text-align: center; }

/* ---- メイン -------------------------------------------------------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 24px;
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.page-title { font-size: 18px; font-weight: 700; margin: 0; flex: 1; }
.topbar-actions { display: flex; gap: 8px; }
.menu-btn { display: none; }
.content { padding: 22px 24px 60px; max-width: 1080px; width: 100%; margin: 0 auto; outline: none; }

/* ---- ボタン -------------------------------------------------------- */
.btn {
  appearance: none; border: 1px solid transparent; cursor: pointer;
  font-family: inherit; font-size: 14px; font-weight: 600;
  padding: 9px 16px; border-radius: var(--radius-sm);
  background: var(--accent); color: #fff; transition: filter .15s, background .15s;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { filter: brightness(.95); }
.btn.ghost { background: var(--card-2); color: var(--text); border-color: var(--border); }
.btn.ghost:hover { background: var(--border); }
.btn.danger { background: #dc2626; color: #fff; }
/* ダークモードでは明るいアクセント上の白文字がAA未達のため前景を濃色に */
[data-theme="dark"] .btn:not(.ghost):not(.danger) { color: #0f1117; }
/* アクセント背景に白文字を載せるバッジ類も同様に濃色化（LPの.tag / 料金モーダルの.tier-badge） */
[data-theme="dark"] .tier-badge,
[data-theme="dark"] .price .tag { color: #0f1117; }
.btn.sm { padding: 6px 11px; font-size: 13px; }
.btn.block { width: 100%; justify-content: center; }
.icon-btn {
  appearance: none; border: none; background: transparent; cursor: pointer;
  font-size: 18px; width: 38px; height: 38px; border-radius: 10px; color: var(--text);
  display: inline-flex; align-items: center; justify-content: center; transition: background .15s;
}
.icon-btn:hover { background: var(--card-2); }

/* ---- フォーム ------------------------------------------------------ */
input, select, textarea {
  font-family: inherit; font-size: 14px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--card); color: var(--text); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; color: var(--text-dim); margin-bottom: 5px; font-weight: 600; }
.amount-preview { display: block; margin-top: 4px; min-height: 1.1em; font-size: 12.5px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 0; }

/* ---- カード -------------------------------------------------------- */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 18px; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 0; }
.card-title { font-size: 15px; font-weight: 700; margin: 0; }
.card-body { padding: 16px 18px; }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }

/* ---- 統計 ---------------------------------------------------------- */
.stat { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.stat-label { font-size: 12.5px; color: var(--text-dim); font-weight: 600; }
.stat-value { font-size: 26px; font-weight: 800; margin-top: 4px; letter-spacing: -.5px; }
.stat-sub { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.stat.good .stat-value { color: var(--good-text); }
.stat.warn .stat-value { color: var(--warn-text); }
.stat.danger .stat-value { color: var(--danger-text); }

/* ---- ピル / バッジ ------------------------------------------------- */
.pill { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--card-2); color: var(--text-dim); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }
.pill.good { background: var(--good-soft); color: var(--good-text); }
.pill.warn { background: var(--warn-soft); color: var(--warn-text); }
.pill.danger { background: var(--danger-soft); color: var(--danger-text); }

/* ---- インサイト（AI） --------------------------------------------- */
.insight { display: flex; gap: 10px; align-items: flex-start; padding: 11px 13px; border-radius: var(--radius-sm); background: var(--card-2); margin-bottom: 8px; font-size: 13.5px; }
.insight .ico { font-size: 16px; line-height: 1.5; }
.insight.warn { background: var(--warn-soft); }
.insight.good { background: var(--good-soft); }
.insight.tip { background: var(--accent-soft); }
.ai-badge { font-size: 11px; font-weight: 700; color: var(--accent); background: var(--accent-soft); padding: 2px 8px; border-radius: 999px; }

/* ---- リスト項目 ---------------------------------------------------- */
.list { display: flex; flex-direction: column; }
.item { display: flex; align-items: center; gap: 12px; padding: 11px 4px; border-bottom: 1px solid var(--border); }
.item:last-child { border-bottom: none; }
.item-main { flex: 1; min-width: 0; }
.item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-sub { font-size: 12.5px; color: var(--text-dim); margin-top: 1px; }
.item-amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.item.done .item-title { text-decoration: line-through; color: var(--text-dim); }

/* チェックボックス */
.check { width: 22px; height: 22px; border-radius: 7px; border: 2px solid var(--border); cursor: pointer; flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center; background: var(--card); transition: all .15s; }
.check.on { background: var(--good); border-color: var(--good); color: #fff; }

/* ---- 空状態 -------------------------------------------------------- */
.empty { text-align: center; color: var(--text-dim); padding: 30px 10px; }
.empty-ico { font-size: 34px; margin-bottom: 8px; }

/* ---- グラフ -------------------------------------------------------- */
.chart, .donut, .spark { display: block; max-width: 100%; }
.chart-x { fill: var(--text-dim); }
.donut-center { fill: var(--text); font-weight: 800; }
.donut-sub { fill: var(--text-dim); }
.legend { display: flex; flex-direction: column; gap: 6px; }
.legend-item { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.legend-dot { width: 11px; height: 11px; border-radius: 3px; flex: 0 0 auto; }
.legend-val { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text-dim); }

/* ---- 予算バー ------------------------------------------------------ */
.bar-track { height: 8px; background: var(--card-2); border-radius: 999px; overflow: hidden; margin-top: 6px; }
.bar-fill { height: 100%; border-radius: 999px; background: var(--accent); transition: width .4s; }
.bar-fill.over { background: var(--danger); }

/* ---- セグメント / チップ ------------------------------------------- */
.seg { display: inline-flex; background: var(--card-2); border-radius: var(--radius-sm); padding: 3px; gap: 3px; flex-wrap: wrap; }
.seg button { appearance: none; border: none; background: transparent; padding: 7px 13px; border-radius: 8px; cursor: pointer; font-family: inherit; font-size: 13px; font-weight: 600; color: var(--text-dim); }
.seg button.on { background: var(--card); color: var(--accent); box-shadow: var(--shadow); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 11px; border-radius: 999px; background: var(--card-2); border: 1px solid var(--border); font-size: 13px; cursor: pointer; }
.chip .x { color: var(--text-dim); font-weight: 700; }
.chip.add { border-style: dashed; color: var(--accent); }

/* ---- 習慣グリッド -------------------------------------------------- */
.hgrid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-top: 8px; }
.hcell { aspect-ratio: 1; border-radius: 6px; background: var(--card-2); display: flex; align-items: center; justify-content: center; font-size: 10px; color: var(--text-dim); }
.hcell.on { background: var(--good); color: #fff; }
.hcell.today { outline: 2px solid var(--accent); }

/* ---- 気分ボタン ---------------------------------------------------- */
.moods { display: flex; gap: 8px; justify-content: space-between; }
.mood-btn { flex: 1; font-size: 26px; padding: 10px 0; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--card); cursor: pointer; transition: all .15s; }
.mood-btn:hover { background: var(--card-2); }
.mood-btn.on { background: var(--accent-soft); border-color: var(--accent); transform: scale(1.05); }

/* ---- レシピカード -------------------------------------------------- */
.recipe { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; background: var(--card); }
.recipe h4 { margin: 0 0 6px; font-size: 15px; }
.cov { height: 6px; background: var(--card-2); border-radius: 999px; overflow: hidden; margin: 8px 0; }
.cov > div { height: 100%; background: var(--good); }

/* ---- トースト ------------------------------------------------------ */
.toast-wrap { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 999px; font-size: 13.5px; font-weight: 600; box-shadow: var(--shadow); opacity: 0; transform: translateY(10px); transition: all .25s; max-width: 90vw; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.good { background: var(--good); color: #fff; }
.toast.warn { background: var(--warn); color: #fff; }
.toast.danger { background: var(--danger); color: #fff; }

/* ---- モーダル ------------------------------------------------------ */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,17,23,.45); z-index: 90; display: flex; align-items: center; justify-content: center; padding: 18px; opacity: 0; transition: opacity .2s; }
/* hidden属性で確実に消す（.modal-backdrop{display:flex}がUAの[hidden]を上書きして
   透明な覆いが画面全体のタップを奪う重大バグを防ぐ） */
.modal-backdrop[hidden] { display: none !important; }
.modal-backdrop.show { opacity: 1; }
.modal { background: var(--card); border-radius: var(--radius); width: 100%; max-width: 460px; max-height: 88vh; overflow: auto; box-shadow: var(--shadow); transform: scale(.96); transition: transform .2s; }
.modal-backdrop.show .modal { transform: scale(1); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2, .modal-head h3 { margin: 0; font-size: 16px; font-weight: 700; }
.modal-body { padding: 18px; }
.modal-foot { padding: 14px 18px; border-top: 1px solid var(--border); display: flex; gap: 10px; justify-content: flex-end; }

/* ---- Pro / 料金 ---------------------------------------------------- */
.pro-feat { list-style: none; padding: 0; margin: 12px 0; }
.pro-feat li { padding: 7px 0; border-bottom: 1px dashed var(--border); font-size: 14px; }
.pro-feat li:last-child { border-bottom: none; }
.tiers { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 14px 0; }
.tier { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px 10px; text-align: center; position: relative; background: var(--card-2); }
.tier-best { border-color: var(--accent); border-width: 2px; }
.tier-badge { position: absolute; top: -10px; left: 50%; transform: translateX(-50%); background: var(--accent); color: #fff; font-size: 10.5px; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.tier-name { font-size: 13px; font-weight: 700; color: var(--text-dim); margin-bottom: 4px; }
.tier-price { margin-bottom: 10px; }
.tier-price b { font-size: 20px; }
@media (max-width: 520px) { .tiers { grid-template-columns: 1fr; } }

/* ---- セクション見出し --------------------------------------------- */
.section-title { font-size: 13px; font-weight: 700; color: var(--text-dim); text-transform: none; margin: 22px 0 10px; }
.hint { font-size: 12.5px; color: var(--text-dim); }
.muted { color: var(--text-dim); }
.text-good { color: var(--good-text); } .text-danger { color: var(--danger-text); } .text-warn { color: var(--warn-text); }
.center { text-align: center; }
.flex { display: flex; gap: 10px; align-items: center; }
.flex.between { justify-content: space-between; }
.flex.wrap { flex-wrap: wrap; }
.mt { margin-top: 12px; } .mb { margin-bottom: 12px; }

/* ---- 単体起動モード ------------------------------------------------ */
body.single-app .sidebar { display: none; }
body.single-app .menu-btn { display: none; }
body.single-app .tabbar { display: none !important; }

/* ---- レスポンシブ -------------------------------------------------- */
@media (max-width: 820px) {
  .grid.cols-4 { grid-template-columns: repeat(2, 1fr); }
  .grid.cols-3 { grid-template-columns: 1fr; }
  .grid.cols-2 { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .sidebar {
    position: fixed; z-index: 80; left: 0; top: 0; transform: translateX(-100%);
    transition: transform .25s; box-shadow: var(--shadow);
  }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open::after { content: ""; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 70; }
  /* ナビ展開中もメニューボタン(topbar内)を暗幕より前面に出して再タップで閉じられるように */
  body.nav-open .topbar { z-index: 85; }
  .menu-btn { display: inline-flex; }
  .content { padding: 16px 14px 60px; }
  .topbar { padding: 12px 14px; }
  body { font-size: 14.5px; }
}

/* ===== Cycle1: アクセシビリティ / モバイル / 質感 ===== */

/* キーボード操作時のみフォーカスリング */
a:focus-visible, button:focus-visible, [role="button"]:focus-visible, [role="link"]:focus-visible,
[role="checkbox"]:focus-visible, .nav-item:focus-visible, .seg button:focus-visible,
.chip:focus-visible, .mood-btn:focus-visible, .check:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* スキップリンク */
.skip-link { position: absolute; left: -999px; top: 8px; z-index: 200; background: var(--accent); color: #fff; padding: 8px 14px; border-radius: 8px; font-weight: 700; }
.skip-link:focus { left: 8px; }

/* スピナー */
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* クリック可能カードのマイクロインタラクション */
.is-clickable { transition: transform .15s, box-shadow .15s; }
.is-clickable:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(20,25,50,.10); }
.is-clickable:active { transform: translateY(0); }

/* トーストの取り消しアクション */
.toast { pointer-events: auto; }
.toast .undo { margin-left: 12px; text-decoration: underline; cursor: pointer; font-weight: 800; }

/* 予算超過の色以外の手掛かり */
.over-tag { font-size: 11px; font-weight: 700; color: var(--danger); margin-left: 6px; }

/* 空状態のCTA */
.empty .btn { margin-top: 14px; }

/* safe-area / タップ領域 / ボトムシート（モバイル） */
@media (max-width: 720px) {
  input, select, textarea { font-size: 16px; }                 /* iOSのフォーカス自動ズーム防止 */
  .topbar { padding-top: max(12px, env(safe-area-inset-top)); padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .content { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .icon-btn { width: 44px; height: 44px; }
  .seg button { min-height: 44px; }
  .chip { min-height: 40px; }
  .item { padding-top: 13px; padding-bottom: 13px; }
  .toast-wrap { bottom: calc(22px + env(safe-area-inset-bottom)); }
  .modal-backdrop { align-items: flex-end; padding: 0; }
  .modal { max-width: none; width: 100%; max-height: 92vh; border-radius: 16px 16px 0 0; }
  .modal-foot { position: sticky; bottom: 0; background: var(--card); padding-bottom: max(14px, env(safe-area-inset-bottom)); }
}

/* 動きを減らす設定への配慮 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; scroll-behavior: auto !important; }
  .mood-btn.on { transform: none; }
  .modal { transform: none !important; }
}

/* ===== ボトムナビ / FAB（モバイルのみ表示） ===== */
.tabbar { display: none; }
.fab { display: none; }
@media (max-width: 720px) {
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
    background: color-mix(in srgb, var(--card) 95%, transparent); backdrop-filter: blur(8px);
    border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom);
  }
  .tab-item {
    flex: 1; min-height: 56px; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; font-size: 11px; color: var(--text-dim); text-decoration: none; padding: 8px 0;
  }
  .tab-item span:first-child { font-size: 21px; line-height: 1; }
  .tab-item.active { color: var(--accent); font-weight: 700; }
  .content { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .fab {
    display: flex; position: fixed; right: 16px; bottom: calc(66px + env(safe-area-inset-bottom));
    width: 56px; height: 56px; border-radius: 50%; z-index: 61;
    align-items: center; justify-content: center; font-size: 28px; line-height: 1;
    background: var(--accent); color: #fff; border: none; box-shadow: var(--shadow); cursor: pointer;
  }
  [data-theme="dark"] .fab { color: #0f1117; }
  .fab[hidden] { display: none; }
  .toast-wrap { bottom: calc(80px + env(safe-area-inset-bottom)); }
}
