:root { --navy:#1f3864; --blue:#2e5496; --line:#e2e8f0; --muted:#7a8699; --bg:#f4f6fa; }
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; font-family: system-ui, "Hiragino Sans", Meiryo, sans-serif; color: #1a2233; background: var(--bg); }
.muted { color: var(--muted); font-size: 13px; }
.error { color: #c62828; font-size: 13px; min-height: 18px; }

/* ログイン */
.center { display: flex; min-height: 100vh; align-items: center; justify-content: center; }

/* アプリ・レイアウト */
.app { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--navy); color: #cdd8ea; display: flex; flex-direction: column; padding: 16px 0; }
.brand { font-weight: 700; color: #fff; padding: 8px 20px 18px; display: flex; flex-direction: column; }
.brand span { font-weight: 400; font-size: 11px; color: #8fa6c8; margin-top: 2px; }
.sidebar nav { display: flex; flex-direction: column; }
.sidebar nav a { padding: 11px 20px; color: #cdd8ea; cursor: pointer; font-size: 14px; border-left: 3px solid transparent; }
.sidebar nav a:hover { background: rgba(255,255,255,.06); }
.sidebar nav a.active { background: rgba(255,255,255,.12); color: #fff; border-left-color: #e8662a; }
.sidebar-foot { margin-top: auto; padding: 16px 20px 4px; display: flex; flex-direction: column; gap: 8px; }
.sidebar-foot .muted { color: #9db2d4; }

.content { padding: 22px 28px; overflow: auto; }
h1 { font-size: 22px; margin: 0 0 16px; color: var(--navy); }
h1 select { font-size: 14px; vertical-align: middle; }
h2 { font-size: 15px; margin: 0 0 12px; color: var(--navy); }

.card { background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 18px; margin: 0 0 16px; box-shadow: 0 1px 3px rgba(0,0,0,.04); max-width: 380px; }
.card { max-width: none; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }

label { display: block; font-size: 13px; margin: 10px 0; color: #33415c; }
input, select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; background: #fff; }
input.inline { border: 1px solid transparent; background: transparent; width: 100%; }
input.inline:hover, input.inline:focus { border-color: var(--line); background: #fff; }
.saved { outline: 2px solid #2e7d32 !important; transition: outline .2s; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row input, .row select { flex: 0 1 auto; }
#acc-search { flex: 1 1 240px; }
button { background: var(--navy); color: #fff; border: 0; padding: 9px 16px; border-radius: 8px; font-size: 14px; cursor: pointer; }
button.ghost { background: transparent; color: #cdd8ea; border: 1px solid rgba(255,255,255,.3); }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
.badge { background: var(--blue); color: #fff; border-radius: 10px; padding: 2px 8px; font-size: 12px; }
.badge.sm { background: #e8662a; font-size: 10px; padding: 1px 6px; }
.mt { margin-top: 12px; }
details summary { cursor: pointer; color: var(--blue); font-size: 14px; }

/* ボタン小・リンク */
button.mini { padding: 4px 10px; font-size: 12px; }
button.ghost.dark { color: var(--navy); border-color: var(--line); }
.link { color: var(--blue); cursor: pointer; font-size: 14px; }

/* 予算グリッド */
.gridwrap { overflow: auto; max-height: 68vh; margin-top: 12px; border: 1px solid var(--line); border-radius: 8px; }
#grid { border-collapse: separate; border-spacing: 0; font-size: 13px; min-width: 900px; }
#grid th, #grid td { border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); padding: 4px 6px; text-align: right; white-space: nowrap; }
#grid th { position: sticky; top: 0; background: var(--navy); color: #fff; z-index: 2; }
#grid th:first-child, #grid td.acc, #grid td:first-child { text-align: left; position: sticky; left: 0; background: #fff; z-index: 1; }
#grid th:first-child { z-index: 3; }
#grid .cell { width: 68px; text-align: right; border: 1px solid transparent; background: transparent; padding: 3px 4px; }
#grid .cell:hover, #grid .cell:focus { border-color: var(--blue); background: #fff; }
#grid .cell.from-actual { background: #e6f4ea; }
#grid .cell.budget-diff { background: #fff4e5; }
#grid th .hint { font-size: 9px; background: #e8662a; border-radius: 6px; padding: 0 4px; margin-left: 3px; vertical-align: middle; }
.legend { display: flex; gap: 14px; align-items: center; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 4px; vertical-align: middle; border: 1px solid var(--line); }
.legend .sw.actual { background: #e6f4ea; }
.legend .sw.diff { background: #fff4e5; }
.legend .sw.computed { background: #e8eefc; }
#grid .cell.computed { background: #e8eefc; }

/* 変数(KPI)モデル */
#model-card h3 { margin: 14px 0 6px; font-size: 14px; color: var(--navy); }
#model-card .modelwrap { max-height: 40vh; }
#model-card .mcell { width: 76px; padding: 5px 6px; }
#model-card .k-name { min-width: 140px; }
#model-card .f-expr { flex: 1 1 260px; font-family: ui-monospace, Menlo, monospace; font-size: 13px; }
#model-card code { background: #eef1f8; border-radius: 4px; padding: 1px 5px; }
#drivers th, #drivers td, .fd-table th, .fd-table td { white-space: nowrap; }
.fml-block { border: 1px solid var(--line); border-left: 3px solid var(--blue); border-radius: 8px; padding: 12px; margin-bottom: 12px; background: #fbfcfe; }
.fml-block .eq { font-weight: 700; color: var(--navy); }
.fml-block .modelwrap { max-height: 28vh; margin-top: 8px; }
.fml-block .fd-add { margin-top: 8px; }

/* 数式トークン（チップ） */
.tok-list { display: inline-flex; flex-wrap: wrap; gap: 4px; align-items: center; flex: 1 1 260px; min-height: 30px; padding: 3px 6px; border: 1px dashed var(--line); border-radius: 8px; background: #fff; }
.chip { display: inline-block; padding: 2px 9px; border-radius: 12px; font-size: 13px; cursor: pointer; user-select: none; }
.chip.kpi { background: #dbe7fb; color: #1f3864; border: 1px solid #b9cdf0; }
.chip.acc { background: #e8e2f4; color: #4a3a76; border: 1px solid #d0c4ea; }
.chip.month, .chip.num { background: #fdeedc; color: #7a4a12; border: 1px solid #f3d9b7; }
.chip.op { background: #eef1f8; color: #33415c; border: 1px solid var(--line); font-weight: 700; }
.chip:hover { opacity: .7; }
.tok-tools { margin-top: 8px; }
.tok-tools .opbtns { display: inline-flex; gap: 4px; }
.tok-tools .tok-op, .tok-tools .tok-par { background: #eef1f8; color: var(--navy); font-weight: 700; min-width: 34px; }

/* 変数検索サジェスト */
.tok-searchwrap { position: relative; flex: 0 1 280px; }
.tok-search { width: 100%; }
.tok-suggest { position: absolute; top: calc(100% + 4px); left: 0; z-index: 20; min-width: 280px; max-height: 280px; overflow: auto;
  background: #fff; border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 18px rgba(0,0,0,.12); }
.tok-suggest .tok-group { padding: 6px 10px 2px; font-size: 11px; color: var(--muted); font-weight: 600; position: sticky; top: 0; background: #fff; }
.tok-suggest .tok-item { padding: 5px 12px; font-size: 13px; cursor: pointer; }
.tok-suggest .tok-item.active, .tok-suggest .tok-item:hover { background: #e8eefc; }
.tok-suggest .tok-item b { color: var(--blue); }
.tok-suggest .tok-empty { padding: 8px 12px; }
#grid .band td { background: var(--blue); color: #fff; font-weight: 700; text-align: left; position: sticky; left: 0; }
#grid .subtotal td { background: #eef1f8; font-weight: 700; }
#grid .subtotal td:first-child { background: #eef1f8; }
#grid .calc td { background: #c9d6ea; font-weight: 700; }
#grid .calc td:first-child { background: #c9d6ea; }
#grid .rowtotal { background: #f7f9fc; font-weight: 600; }

/* 予実・予算対比テーブル */
#vartable, #cmptable { border-collapse: separate; border-spacing: 0; font-size: 13px; min-width: 720px; width: 100%; }
#vartable th, #vartable td, #cmptable th, #cmptable td { border-bottom: 1px solid var(--line); padding: 6px 10px; }
#vartable th, #cmptable th { position: sticky; top: 0; background: var(--navy); color: #fff; }
#vartable th:first-child, #vartable td:first-child, #cmptable th:first-child, #cmptable td:first-child { text-align: left; position: sticky; left: 0; background: #fff; }
#vartable .num, #cmptable .num { text-align: right; white-space: nowrap; }
#vartable .band td, #cmptable .band td { background: var(--blue); color: #fff; font-weight: 700; }
#vartable .subtotal td, #cmptable .subtotal td { background: #eef1f8; font-weight: 700; }
#vartable .subtotal td:first-child, #cmptable .subtotal td:first-child { background: #eef1f8; }
#vartable .calc td, #cmptable .calc td { background: #c9d6ea; font-weight: 700; }
#vartable .calc td:first-child, #cmptable .calc td:first-child { background: #c9d6ea; }
.num.pos { color: #2e7d32; }
.num.neg { color: #c62828; }
th.num, td.num { text-align: right; white-space: nowrap; }

/* ダッシュボード */
.kpis { display: flex; gap: 28px; flex-wrap: wrap; margin: 6px 0 10px; }
.kpi .lbl { font-size: 12px; color: var(--muted); }
.kpi .num { font-size: 28px; font-weight: 700; color: var(--navy); }
.kpi .num.neg { color: #c62828; }
.kpi .unit { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 4px; }
.stat { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.stat span { color: var(--muted); flex: 0 0 auto; }
.stat b { text-align: right; font-weight: 600; }
#dash-changes td { font-size: 12px; }

/* ログインカードは狭く */
form.card { max-width: 360px; }
.sso-btn { display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%; margin-top: 8px;
  padding: 11px 16px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: #1a2233;
  font-size: 14px; font-weight: 600; text-decoration: none; }
.sso-btn:hover { background: #f4f6fa; }
form.card h1 { font-size: 20px; }
form.card input { width: 100%; margin-top: 4px; }
form.card button { width: 100%; margin-top: 8px; }

/* 予実アラート・差異理由・要因分解・ドリルダウン */
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; }
.dot.red { background: #c62828; }
.dot.yellow { background: #f4a623; }
.dot.green { background: #2e7d32; }
#vartable .statuscell { text-align: center; }
#vartable .notecell { max-width: 260px; font-size: 12px; }
#vartable .ops { white-space: nowrap; }
#vartable .ops button { margin-right: 4px; }
h3 { font-size: 14px; margin: 12px 0 6px; color: var(--navy); }
#note-history td, #axis-result td, #drill-body td { font-size: 13px; }
#factor-body table { max-width: 760px; }
#factor-body .subtotal td { background: #eef1f8; font-weight: 700; }
#factor-body .calc td { background: #c9d6ea; font-weight: 700; }

/* KPI実績・CF計算書 */
#kpi-grid { border-collapse: separate; border-spacing: 0; font-size: 13px; min-width: 900px; }
#kpi-grid th { position: sticky; top: 0; background: var(--navy); color: #fff; }
#kpi-grid th:first-child, #kpi-grid td:first-child { text-align: left; position: sticky; left: 0; background: #fff; z-index: 1; }
#kpi-grid .kpicell { width: 76px; text-align: right; border: 1px solid transparent; background: transparent; padding: 3px 4px; }
#kpi-grid .kpicell:hover, #kpi-grid .kpicell:focus { border-color: var(--blue); background: #fff; }
#kpi-grid .kpicell::placeholder { color: #b6c0d0; }
#cf-table { border-collapse: separate; border-spacing: 0; font-size: 13px; min-width: 720px; }
#cf-table th { position: sticky; top: 0; background: var(--navy); color: #fff; }
#cf-table th:first-child, #cf-table td:first-child { text-align: left; position: sticky; left: 0; background: #fff; }
#cf-table .subtotal td { background: #eef1f8; font-weight: 700; }
#cf-table .calc td { background: #c9d6ea; font-weight: 700; }
#cf-table td.neg { color: #c62828; }

/* ===== モバイル（〜820px）: サイドバー→上部ナビ・1カラム化・表はカード内横スクロール ===== */
@media (max-width: 820px) {
  /* minmax(0,1fr)＋min-width:0 で、ナビや表の最小コンテンツ幅がページ幅を押し広げるのを防ぐ */
  .app { grid-template-columns: minmax(0, 1fr); min-height: auto; }
  .sidebar, .content { min-width: 0; }
  .sidebar nav { min-width: 0; }
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: sticky; top: 0; z-index: 50; padding: 6px 10px 0; }
  .brand { flex-direction: row; align-items: baseline; gap: 8px; padding: 6px 8px; font-size: 15px; }
  .brand span { margin-top: 0; }
  .sidebar nav { flex-direction: row; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .sidebar nav::-webkit-scrollbar { display: none; }
  .sidebar nav a { padding: 10px 12px; white-space: nowrap; border-left: none; border-bottom: 3px solid transparent; font-size: 13px; }
  .sidebar nav a.active { border-left-color: transparent; border-bottom-color: #e8662a; }
  .sidebar-foot { margin-top: 0; flex-direction: row; align-items: center; justify-content: space-between; padding: 6px 8px; gap: 10px; }
  .sidebar-foot .muted { font-size: 11px; }
  .sidebar-foot button { padding: 5px 10px; font-size: 12px; }

  .content { padding: 12px; }
  h1 { font-size: 18px; margin-bottom: 10px; }
  h2 { font-size: 14px; }
  .grid2 { grid-template-columns: 1fr; gap: 12px; }
  .card { padding: 12px; margin-bottom: 12px; overflow-x: auto; } /* 幅広の表はカード内で横スクロール */
  .kpis { gap: 16px; }
  .kpi .num { font-size: 22px; }
  .gridwrap { max-height: 62vh; }
  table { font-size: 12px; }
  th, td { padding: 6px 8px; }
  #vartable .notecell { max-width: 150px; }
  #vartable .ops button { margin-bottom: 2px; }

  /* iOSはfont-size16px未満の入力にフォーカスすると自動ズームするため16pxに固定 */
  input, select { font-size: 16px; padding: 8px; }
  #grid .cell, #kpi-grid .kpicell, #model-card .mcell { font-size: 14px; }
  button { padding: 10px 14px; } /* タップターゲット確保 */
  button.mini { padding: 6px 10px; font-size: 12px; }
  .row { gap: 6px; }
  .row label { font-size: 12px; }

  /* ダッシュボードの統計行は縦に余裕を */
  .stat { flex-wrap: wrap; }
  .stat b { text-align: left; }

  /* ログイン */
  body.center { padding: 16px; }
}

/* セグメント別収支テーブル（予実テーブルと同スタイル） */
#segtable { border-collapse: separate; border-spacing: 0; font-size: 13px; min-width: 640px; width: 100%; }
#segtable th, #segtable td { border-bottom: 1px solid var(--line); padding: 6px 10px; }
#segtable th { position: sticky; top: 0; background: var(--navy); color: #fff; }
#segtable th:first-child, #segtable td:first-child { text-align: left; position: sticky; left: 0; background: #fff; }
#segtable .num { text-align: right; white-space: nowrap; }
#segtable .subtotal td { background: #eef1f8; font-weight: 700; }
#segtable .subtotal td:first-child { background: #eef1f8; }
#segtable .calc td { background: #c9d6ea; font-weight: 700; }
#segtable .calc td:first-child { background: #c9d6ea; }
#segtable .grp { text-align: center; background: var(--blue); color: #fff; }
