/* ===== デザイントークン ===== */
:root {
  --bg: #F3F5F4;          /* 地 */
  --surface: #FFFFFF;     /* 面 */
  --surface-2: #F8FAF9;
  --ink: #1C2A30;         /* 墨 */
  --ink-2: #4A5A61;
  --ink-3: #7B8A90;
  --line: #DCE3E2;
  --line-2: #E9EEED;
  --ai: #1E5470;          /* 藍：基調 */
  --ai-2: #2E6F8E;
  --ai-soft: #E3EEF3;
  --take: #3E8E6B;        /* 若竹：資産増・黒字 */
  --take-soft: #E4F2EA;
  --shu: #C2453A;         /* 朱：赤字・不足 */
  --shu-soft: #FBE8E5;
  --kin: #B8901A;         /* 金：ライフイベント */
  --kin-soft: #FBF3DC;
  --sidebar: #17384A;
  --sidebar-ink: #C9D8DF;
  --on-ai: #FFFFFF;       /* 藍の上の文字 */
  --radius-s: 4px;
  --radius-m: 8px;
  --shadow: 0 1px 2px rgba(23,56,74,.06), 0 2px 8px rgba(23,56,74,.05);
  --font: "BIZ UDPGothic", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  --font-mincho: "Shippori Mincho", "Hiragino Mincho ProN", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101A1F; --surface: #17242B; --surface-2: #1B2A32; --ink: #E6EEF0; --ink-2: #B4C3C8; --ink-3: #85979D;
    --line: #2A3B43; --line-2: #22323A; --ai: #6FB0D2; --ai-2: #8CC3DE; --ai-soft: #1D3644;
    --take: #6CC197; --take-soft: #19352A; --shu: #E8786C; --shu-soft: #3A2220; --kin: #E1BE57; --kin-soft: #3A3219;
    --sidebar: #0C171C; --sidebar-ink: #A9BCC4; --on-ai: #0C1A21; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101A1F; --surface: #17242B; --surface-2: #1B2A32; --ink: #E6EEF0; --ink-2: #B4C3C8; --ink-3: #85979D;
  --line: #2A3B43; --line-2: #22323A; --ai: #6FB0D2; --ai-2: #8CC3DE; --ai-soft: #1D3644;
  --take: #6CC197; --take-soft: #19352A; --shu: #E8786C; --shu-soft: #3A2220; --kin: #E1BE57; --kin-soft: #3A3219;
  --sidebar: #0C171C; --sidebar-ink: #A9BCC4; --on-ai: #0C1A21; color-scheme: dark;
}

/* ===== ベース ===== */
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 14px; line-height: 1.65; -webkit-font-smoothing: antialiased; }
a { color: var(--ai); }
button, input, select, textarea { font: inherit; color: inherit; }
.num, td.n, .kpi-value { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--ai-2); outline-offset: 2px; }
h1, h2, h3, h4 { margin: 0; line-height: 1.35; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ===== レイアウト ===== */
.shell { display: grid; grid-template-columns: 252px 1fr; min-height: 100vh; }
.sidebar { background: var(--sidebar); color: var(--sidebar-ink); position: sticky; top: 0; height: 100vh; overflow-y: auto; padding: 18px 0 32px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 20px 18px; color: #fff; text-decoration: none; }
.brand svg { flex: none; }
.brand b { font-size: 15px; display: block; letter-spacing: .02em; }
.brand small { font-size: 11px; color: var(--sidebar-ink); }
.nav-group { margin-top: 14px; }
.nav-group > span { display: block; padding: 0 20px 4px; font-size: 11px; color: #7FA0AF; }
.nav a { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 20px 7px 17px; color: var(--sidebar-ink); text-decoration: none; border-left: 3px solid transparent; font-size: 13.5px; }
.nav a:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav a.on { background: rgba(255,255,255,.08); color: #fff; border-left-color: var(--kin); }
.nav a em { font-style: normal; font-size: 11px; color: #7FA0AF; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 14px; padding: 10px 28px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar .who { display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.topbar .who b { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .who small { color: var(--ink-3); font-size: 12px; }
.topbar .spacer { flex: 1; }
.topbar .scenario { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-2); white-space: nowrap; }
.topbar select { padding: 5px 8px; border: 1px solid var(--line); border-radius: var(--radius-s); background: var(--surface); }
.menu-btn { display: none; background: none; border: 0; padding: 6px; cursor: pointer; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--ai-soft); color: var(--ai); display: grid; place-items: center; font-size: 12px; font-weight: 700; }
.content { padding: 24px 28px 64px; max-width: 1360px; width: 100%; }

/* ===== 家族の年齢ライン ===== */
.ageline { background: var(--surface); border-bottom: 1px solid var(--line); padding: 10px 28px 12px; overflow-x: auto; }
.ageline table { border-collapse: collapse; font-size: 12px; }
.ageline th { text-align: left; font-weight: 400; color: var(--ink-3); padding: 1px 12px 1px 0; white-space: nowrap; position: sticky; left: 0; background: var(--surface); }
.ageline td { min-width: 34px; text-align: center; padding: 1px 0; color: var(--ink-2); }
.ageline tr.yr td { color: var(--ink-3); font-size: 11px; }
.ageline td.now { background: var(--ai-soft); color: var(--ai); font-weight: 700; }
.ageline td.ev { position: relative; }
.ageline td.ev::after { content: ""; position: absolute; left: 50%; bottom: -3px; width: 6px; height: 6px; margin-left: -3px; border-radius: 50%; background: var(--kin); }
.ageline td.retire { color: var(--kin); font-weight: 700; }
.ageline td.out { color: var(--line); }

/* ===== ページ見出し ===== */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; font-weight: 700; }
.page-head p { margin: 4px 0 0; color: var(--ink-2); max-width: 70ch; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.crumb { font-size: 12px; color: var(--ink-3); margin-bottom: 4px; }

/* ===== グリッド・カード ===== */
.grid { display: grid; gap: 16px; }
.grid > *, .stack { min-width: 0; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g-side { grid-template-columns: minmax(0, 1fr) 340px; }
.span2 { grid-column: span 2; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); box-shadow: var(--shadow); min-width: 0; }
.card-h { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line-2); }
.card-h h3 { font-size: 15px; }
.card-h small { color: var(--ink-3); font-size: 12px; }
.card-b { padding: 16px; }
.card-b.flush { padding: 0; }
.card.plain { box-shadow: none; }
.section-title { font-size: 13px; color: var(--ink-2); font-weight: 700; margin: 22px 0 8px; padding-left: 8px; border-left: 3px solid var(--ai); }

/* ===== KPI ===== */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 12px 14px; }
.kpi-label { font-size: 12px; color: var(--ink-2); }
.kpi-value { font-size: 22px; font-weight: 700; margin-top: 2px; }
.kpi-value small { font-size: 12px; font-weight: 400; color: var(--ink-3); margin-left: 3px; }
.kpi-note { font-size: 11.5px; color: var(--ink-3); }
.kpi.good .kpi-value { color: var(--take); }
.kpi.bad { border-color: color-mix(in srgb, var(--shu) 40%, var(--line)); background: var(--shu-soft); }
.kpi.bad .kpi-value { color: var(--shu); }
.kpi.event .kpi-value { color: var(--kin); }

/* ===== フォーム ===== */
.form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 18px; }
.form.c3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.form.c1 { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field.span2 { grid-column: span 2; }
.field.full { grid-column: 1 / -1; }
.field > label, .field > .lbl { font-size: 12px; color: var(--ink-2); }
.field .req { color: var(--shu); margin-left: 3px; }
.field .hint { font-size: 11.5px; color: var(--ink-3); }
.ctl { display: flex; align-items: center; gap: 6px; }
.ctl .unit { color: var(--ink-3); font-size: 12px; white-space: nowrap; }
input[type=text], input[type=number], input[type=date], input[type=month], input[type=email], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: var(--radius-s); background: var(--surface); min-height: 34px;
}
input[type=search] { -webkit-appearance: none; appearance: none; }
input[type=number] { text-align: right; font-variant-numeric: tabular-nums; }
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--ai-2); outline: none; box-shadow: 0 0 0 3px var(--ai-soft); }
input[type=range] { width: 100%; accent-color: var(--ai); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-s); overflow: hidden; flex-wrap: wrap; }
.seg label { display: inline-flex; align-items: center; cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { padding: 6px 12px; font-size: 13px; border-right: 1px solid var(--line); background: var(--surface); }
.seg label:last-child span { border-right: 0; }
.seg input:checked + span { background: var(--ai); color: var(--on-ai); }
.seg input:focus-visible + span { outline: 2px solid var(--ai-2); outline-offset: -2px; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.check input { accent-color: var(--ai); width: 16px; height: 16px; }
.range-row { display: grid; grid-template-columns: 1fr 70px; gap: 10px; align-items: center; }
.range-row output { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ===== ボタン ===== */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 7px 14px; border-radius: var(--radius-s); border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-size: 13px; white-space: nowrap; text-decoration: none; color: var(--ink); min-height: 34px; }
.btn:hover { border-color: var(--ai-2); }
.btn.primary { background: var(--ai); border-color: var(--ai); color: var(--on-ai); font-weight: 700; }
.btn.primary:hover { background: var(--ai-2); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ai); }
.btn.danger { color: var(--shu); }
.btn.sm { padding: 3px 9px; font-size: 12px; min-height: 28px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn svg { width: 15px; height: 15px; }

/* ===== タブ ===== */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 8px 14px; cursor: pointer; color: var(--ink-2); white-space: nowrap; margin-bottom: -1px; }
.tabs button.on { color: var(--ai); border-bottom-color: var(--ai); font-weight: 700; }
.card .tabs { padding: 0 12px; margin-bottom: 0; }

/* ===== テーブル ===== */
.tbl-wrap { overflow: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th { text-align: left; font-weight: 700; font-size: 12px; color: var(--ink-2); background: var(--surface-2); border-bottom: 1px solid var(--line); padding: 8px 10px; white-space: nowrap; }
.tbl td { border-bottom: 1px solid var(--line-2); padding: 8px 10px; vertical-align: middle; }
.tbl td.n, .tbl th.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl tr:hover td { background: color-mix(in srgb, var(--ai-soft) 40%, transparent); }
.tbl tr.click { cursor: pointer; }
.tbl .neg { color: var(--shu); }
.tbl .pos { color: var(--take); }

/* ===== キャッシュフロー表 ===== */
.cf-wrap { overflow: auto; max-height: calc(100vh - 260px); border: 1px solid var(--line); border-radius: var(--radius-m); background: var(--surface); }
table.cf { border-collapse: separate; border-spacing: 0; font-size: 12.5px; }
.cf th, .cf td { padding: 5px 10px; border-bottom: 1px solid var(--line-2); white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; min-width: 64px; }
.cf thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 2; color: var(--ink-2); font-weight: 700; }
.cf thead tr:nth-child(2) th { top: 30px; font-weight: 400; color: var(--ink-3); font-size: 11.5px; }
.cf thead tr:nth-child(3) th { top: 58px; font-weight: 400; color: var(--ink-3); font-size: 11.5px; }
.cf .lbl { position: sticky; left: 0; background: var(--surface); text-align: left; z-index: 1; min-width: 200px; border-right: 1px solid var(--line); }
.cf thead .lbl { z-index: 3; background: var(--surface-2); }
.cf tr.l0 td { font-weight: 700; background: var(--surface-2); }
.cf tr.l0 td.lbl { background: var(--surface-2); }
.cf tr.l1 td.lbl { padding-left: 26px; }
.cf tr.l2 td.lbl { padding-left: 42px; color: var(--ink-2); }
.cf tr.total td { font-weight: 700; border-top: 1px solid var(--line); }
.cf tr.asset td { background: var(--ai-soft); font-weight: 700; }
.cf tr.asset td.lbl { background: var(--ai-soft); }
.cf td.neg { color: var(--shu); }
.cf td.ev-col { background-image: linear-gradient(var(--kin-soft), var(--kin-soft)); }
.cf .tog { background: none; border: 0; cursor: pointer; width: 18px; color: var(--ink-3); padding: 0; margin-right: 4px; }
.cf .ev-cell { color: var(--kin); font-size: 11px; text-align: left; white-space: normal; min-width: 64px; max-width: 90px; line-height: 1.35; }

/* ===== バッジ・チップ ===== */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; background: var(--line-2); color: var(--ink-2); white-space: nowrap; }
.badge.ai { background: var(--ai-soft); color: var(--ai); }
.badge.good { background: var(--take-soft); color: var(--take); }
.badge.bad { background: var(--shu-soft); color: var(--shu); }
.badge.ev { background: var(--kin-soft); color: var(--kin); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; gap: 6px; align-items: center; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); font-size: 12px; color: var(--ink-2); text-decoration: none; }
.chip b { color: var(--ai); font-weight: 700; font-variant-numeric: tabular-nums; }
.chip:hover { border-color: var(--ai-2); }
.feature-foot { margin-top: 28px; padding-top: 14px; border-top: 1px dashed var(--line); }
.feature-foot > div:first-child { font-size: 12px; color: var(--ink-3); margin-bottom: 8px; }

/* ===== 補助 ===== */
.muted { color: var(--ink-3); }
.small { font-size: 12px; }
.right { text-align: right; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 16px; }
.note { padding: 10px 14px; border-radius: var(--radius-m); background: var(--ai-soft); color: var(--ink); font-size: 13px; }
.note.warn { background: var(--kin-soft); }
.note.bad { background: var(--shu-soft); }
.chart-box { position: relative; height: 280px; }
.chart-box.tall { height: 360px; }
.chart-box.short { height: 200px; }
.empty { padding: 32px; text-align: center; color: var(--ink-3); }
hr.sep { border: 0; border-top: 1px solid var(--line-2); margin: 16px 0; }
.list-plain { list-style: none; padding: 0; margin: 0; }
.list-plain li { padding: 8px 0; border-bottom: 1px solid var(--line-2); }
.list-plain li:last-child { border-bottom: 0; }
.steps { counter-reset: s; list-style: none; padding: 0; margin: 0; }
.steps li { counter-increment: s; position: relative; padding: 4px 0 4px 32px; }
.steps li::before { content: counter(s); position: absolute; left: 0; top: 5px; width: 20px; height: 20px; border-radius: 50%; background: var(--ai-soft); color: var(--ai); font-size: 11px; display: grid; place-items: center; font-weight: 700; }
.mini-bar { height: 6px; border-radius: 3px; background: var(--line-2); overflow: hidden; }
.mini-bar > i { display: block; height: 100%; background: var(--ai); }
.prose p { margin: 0 0 10px; max-width: 75ch; }
.kbd { font-size: 11px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 3px; padding: 0 5px; color: var(--ink-2); }

/* ===== チャット（AIアドバイザー） ===== */
.chat { display: flex; flex-direction: column; gap: 10px; max-height: 520px; overflow-y: auto; padding: 16px; }
.msg { max-width: 78%; padding: 10px 14px; border-radius: 12px; font-size: 13.5px; }
.msg.me { align-self: flex-end; background: var(--ai); color: var(--on-ai); border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; background: var(--surface-2); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.chat-input { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line-2); }

/* ===== 提案書プレビュー ===== */
.paper { background: #fff; color: #1C2A30; width: 100%; max-width: 794px; margin: 0 auto; padding: 48px 56px; box-shadow: 0 2px 16px rgba(0,0,0,.12); border-radius: 2px; }
.paper h1, .paper h2 { font-family: var(--font-mincho); }
.paper + .paper { margin-top: 24px; }

/* ===== モーダル・トースト ===== */
.modal-back { position: fixed; inset: 0; background: rgba(16,30,38,.45); display: grid; place-items: center; z-index: 100; padding: 16px; }
.modal { background: var(--surface); border-radius: var(--radius-m); width: min(640px, 100%); max-height: calc(100vh - 32px); overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal .card-h { position: sticky; top: 0; background: var(--surface); }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line-2); }
#toast-root { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: var(--radius-m); font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,.2); animation: toast-in .2s ease-out; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ===== ログイン ===== */
.login { min-height: 100vh; display: grid; grid-template-columns: 1.1fr 1fr; }
.login-art { background: var(--sidebar); color: #fff; padding: 56px; display: flex; flex-direction: column; justify-content: space-between; overflow: hidden; }
.login-art h2 { font-family: var(--font-mincho); font-size: 30px; font-weight: 700; line-height: 1.5; }
.login-art p { color: var(--sidebar-ink); max-width: 36ch; }
.login-form { display: grid; place-items: center; padding: 32px 16px; }
.login-form form { width: min(360px, 100%); display: flex; flex-direction: column; gap: 14px; }

/* ===== レスポンシブ ===== */
@media (max-width: 1100px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g-side { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; width: 260px; z-index: 50; transform: translateX(-100%); transition: transform .2s; }
  .shell.nav-open .sidebar { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .menu-btn { display: inline-flex; }
  .topbar, .ageline { padding-left: 16px; padding-right: 16px; }
  .content { padding: 16px 16px 48px; }
  .g2, .g3, .g4, .form, .form.c3 { grid-template-columns: 1fr; }
  .span2, .field.span2 { grid-column: auto; }
  .topbar .scenario span { display: none; }
  .topbar { gap: 8px; }
  .topbar .who small, .topbar .hide-sm, .topbar .avatar { display: none; }
  .topbar select { max-width: 130px; }
  .login { grid-template-columns: 1fr; }
  .login-art { display: none; }
  .paper { padding: 28px 20px; }
}

/* ===== 印刷（PDF出力） ===== */
@media print {
  .sidebar, .topbar, .ageline, .page-head .actions, .feature-foot, .no-print { display: none !important; }
  .shell { display: block; }
  .content { padding: 0; max-width: none; }
  body { background: #fff; }
  .paper { box-shadow: none; padding: 0; max-width: none; page-break-after: always; }
}
