/* ============================================================
   孫子兵法・決策實驗室 — 共用設計系統
   血統：simulation-lab（看不見的勢/虛實/地形 → 看得見的實驗）
   配色：墨黑底 + 朱砂紅 accent（印章/戰旗）+ 玉青 第二語意（利/勢/數據）
   紀律：一個主 accent（朱砂=互動/品牌）、玉青=策略語意、三層文字、token 化
   ============================================================ */

:root {
  color-scheme: dark;
  --bg:        #0b0c10;   /* 墨黑（近中性，微冷） */
  --bg2:       #0f1016;
  --panel:     #15161e;   /* 卡片（如陳年絹帛） */
  --panel2:    #1c1d28;   /* 卡片二階 */
  --border:    #262835;
  --border-strong: #3a3d4e;

  --text:  #ece7db;       /* 絹白（非純白） */
  --dim:   #a39d8e;       /* 次要 */
  --muted: #6c6757;       /* 最弱 */

  --accent:        #e15a42;   /* 朱砂：唯一互動/品牌主色 */
  --accent-bright: #f2825f;
  --accent-press:  #c44a34;
  --on-accent:     #160f0c;

  --jade:  #5cc08c;       /* 玉青：策略/利/勢/數據語意（已宣告的第二語意）*/
  --good:  #5cc08c;
  --warn:  #e8b455;
  --bad:   #ef6573;
  --info:  #b79cf0;

  --r:  14px;             /* 容器圓角 */
  --r2: 10px;             /* 內件/控件圓角 */
  --focus: 0 0 0 2px rgba(225,90,66,.5);

  --maxw: 880px;          /* 純文章欄寬 */
  --maxw-wide: 1140px;    /* 含 3D / 側欄 */

  --serif: "Noto Serif TC", Georgia, "Songti TC", "STSong", serif;
  --sans:  "Noto Sans TC", -apple-system, "PingFang TC", system-ui, sans-serif;
  --mono:  "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background:
    radial-gradient(1100px 560px at 50% -10%, rgba(225,90,66,.06), transparent 70%),
    radial-gradient(900px 500px at 85% 8%, rgba(92,192,140,.045), transparent 70%),
    var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 17px;
  line-height: 1.85;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------- typography ---------- */
.serif { font-family: var(--serif); }
.mono  { font-family: var(--mono); }

h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.28; }
h1 { font-size: clamp(30px, 5vw, 44px); margin: 0 0 14px; letter-spacing: .01em; }
h2 { font-size: clamp(23px, 3.4vw, 29px); margin: 0; }
h3 { font-size: 19px; margin: 0 0 6px; }
p  { margin: 14px 0; }
b, strong { font-weight: 600; }
a { color: var(--accent-bright); text-decoration: none; }
a:hover { text-decoration: underline; }

.num-tab { font-variant-numeric: tabular-nums; }

/* micro label */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-family: var(--mono); font-size: 10.5px; font-weight: 500;
  letter-spacing: .16em; text-transform: uppercase; color: var(--dim);
  border: 1px solid var(--border); padding: 4px 10px; border-radius: 999px;
}
.chip.accent { color: var(--accent-bright); border-color: color-mix(in srgb, var(--accent) 45%, transparent); }
.chip.jade   { color: var(--jade); border-color: color-mix(in srgb, var(--jade) 45%, transparent); }

/* ---------- layout ---------- */
.wrap      { max-width: var(--maxw); margin: 0 auto; padding: 0 22px; }
.wrap-wide { max-width: var(--maxw-wide); margin: 0 auto; padding: 0 22px; }

/* ---------- top book bar ---------- */
.book-bar {
  position: sticky; top: 0; z-index: 50;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.book-bar .inner {
  max-width: var(--maxw-wide); margin: 0 auto; padding: 12px 22px;
  display: flex; align-items: center; justify-content: flex-start; gap: 16px;
}
.book-bar .brand { display: flex; align-items: center; gap: 12px; min-width: 0; margin-right: auto; }
.book-bar .mark {
  width: 30px; height: 30px; border-radius: 7px; flex-shrink: 0;
  display: grid; place-items: center; font-family: var(--serif); font-weight: 700; font-size: 17px;
  background: linear-gradient(150deg, var(--accent), var(--accent-press)); color: var(--text);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.08);
}
.book-bar .btitle { font-family: var(--serif); font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-bar .home-link { font-family: var(--mono); font-size: 11px; letter-spacing: .1em; text-transform: uppercase; color: var(--dim); }
.book-bar .home-link:hover { color: var(--accent-bright); text-decoration: none; }
/* 雙語 toggle（i18n.js 注入到 book-bar 右上） */
.lang-toggle { font-family: var(--mono); font-size: 11px; letter-spacing: .06em; color: var(--dim); background: var(--panel2); border: 1px solid var(--border); border-radius: 999px; padding: 4px 11px; cursor: pointer; transition: all .15s ease; flex-shrink: 0; }
.lang-toggle:hover { border-color: var(--accent); color: var(--accent-bright); }
.lang-toggle:focus-visible { outline: none; box-shadow: var(--focus); }
/* brand 變可點連結（點左上回目錄） */
.book-bar a.brand { text-decoration: none; cursor: pointer; }
.book-bar a.brand:hover { text-decoration: none; }
.book-bar a.brand:hover .btitle { color: var(--accent-bright); }
.book-bar a.brand:hover .mark { filter: brightness(1.1); }
.book-bar a.brand:focus-visible { outline: none; box-shadow: var(--focus); border-radius: 8px; }

/* progress reading bar */
#read-progress { position: fixed; top: 0; left: 0; height: 2px; width: 0; background: var(--accent); z-index: 60; transition: width .1s linear; }

/* ---------- chapter hero ---------- */
.ch-hero { padding: 56px 0 8px; }
.ch-kicker { font-family: var(--mono); font-size: 12px; letter-spacing: .18em; text-transform: uppercase; color: var(--accent-bright); margin-bottom: 14px; }
.ch-hero h1 { max-width: 20ch; }
.ch-hero .deck { font-size: 19px; color: var(--dim); max-width: 42ch; margin-top: 10px; }

/* ---------- part divider ---------- */
.part-divider { display: flex; align-items: center; gap: 16px; margin: 18px 0 8px; }
.part-divider .pn { font-family: var(--mono); font-size: 12px; letter-spacing: .15em; color: var(--muted); white-space: nowrap; }
.part-divider .ln { height: 1px; flex: 1; background: var(--border); }

/* ---------- section heading ---------- */
section { margin: 52px 0; }
.sec-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 18px; }
.sec-head .sn { font-family: var(--mono); font-size: 13px; color: var(--accent); font-weight: 500; }

/* ---------- lead / prose ---------- */
.lead { font-size: 19px; color: var(--dim); line-height: 1.8; }
.prose p { color: var(--text); }
.prose p.dim { color: var(--dim); }

/* 中英並列術語 */
.term { color: var(--accent-bright); font-weight: 600; }
.en { font-family: var(--mono); font-size: .9em; color: var(--dim); }

/* ---------- 原文簡牘塊（classical text quote） ---------- */
.classic {
  border-left: 3px solid var(--accent);
  background: linear-gradient(90deg, color-mix(in srgb, var(--accent) 7%, var(--panel)), var(--panel));
  border-radius: 0 var(--r2) var(--r2) 0;
  padding: 18px 22px; margin: 24px 0;
}
.classic .yuanwen {
  font-family: var(--serif); font-size: 20px; line-height: 1.9; color: var(--text); font-weight: 500;
}
.classic .yuanwen b { color: var(--accent-bright); font-weight: 600; }
.classic .baihua { font-size: 14.5px; color: var(--dim); margin: 10px 0 0; line-height: 1.75; }
.classic .src { font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; color: var(--muted); margin-top: 10px; text-transform: uppercase; }

/* ---------- 歷史戰例 故事塊（History story） ---------- */
.story {
  background: linear-gradient(90deg, color-mix(in srgb, var(--jade) 7%, var(--panel)), var(--panel));
  border: 1px solid var(--border); border-left: 3px solid var(--jade);
  border-radius: 0 var(--r2) var(--r2) 0; padding: 18px 22px; margin: 24px 0;
}
.story .st-label { font-family: var(--mono); font-size: 10.5px; letter-spacing: .14em; text-transform: uppercase; color: var(--jade); margin-bottom: 8px; }
.story .st-title { font-family: var(--serif); font-size: 19px; font-weight: 600; color: var(--text); margin: 0 0 8px; }
.story .st-body { font-size: 15px; color: var(--dim); line-height: 1.85; margin: 0; }
.story .st-body b { color: var(--text); font-weight: 600; }
.story .st-tie { margin: 12px 0 0; font-size: 14px; color: var(--accent-bright); line-height: 1.7; }
.story .st-tie b { color: var(--accent-bright); }

/* ---------- callouts ---------- */
.callout {
  border: 1px solid var(--border); border-left-width: 3px;
  background: var(--panel); border-radius: var(--r2);
  padding: 16px 20px; margin: 22px 0;
}
.callout .co-label { font-family: var(--mono); font-size: 10.5px; letter-spacing: .14em; text-transform: uppercase; margin-bottom: 6px; }
.callout.plain   { border-left-color: var(--jade); }    .callout.plain   .co-label { color: var(--jade); }
.callout.insight { border-left-color: var(--accent); }  .callout.insight .co-label { color: var(--accent-bright); }
.callout.warn    { border-left-color: var(--warn); }    .callout.warn    .co-label { color: var(--warn); }
.callout.analogy { border-left-color: var(--good); background: color-mix(in srgb, var(--good) 6%, var(--panel)); } .callout.analogy .co-label { color: var(--good); }
.callout.modern  { border-left-color: var(--info); background: color-mix(in srgb, var(--info) 6%, var(--panel)); } .callout.modern .co-label { color: var(--info); }
.callout p:first-of-type { margin-top: 0; } .callout p:last-child { margin-bottom: 0; }

/* ---------- cards ---------- */
.card-grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(2, 1fr); }
.cols-3 { grid-template-columns: repeat(3, 1fr); }
.cols-4 { grid-template-columns: repeat(4, 1fr); }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--r);
  padding: 20px; transition: border-color .18s ease, transform .18s ease;
}
.card.hover:hover { border-color: var(--border-strong); transform: translateY(-2px); }
.card h3 { font-family: var(--serif); }
.card .sub { font-size: 13px; color: var(--muted); }
.card p { font-size: 15px; color: var(--dim); }

/* big number */
.bignum { font-family: var(--mono); font-weight: 500; font-variant-numeric: tabular-nums; line-height: 1; }
.bignum.xl { font-size: clamp(32px, 6vw, 52px); color: var(--accent-bright); }
.bignum.lg { font-size: 30px; color: var(--text); }
.bignum .unit { font-size: .4em; color: var(--dim); margin-left: 4px; letter-spacing: .05em; }

/* stat rows */
.stat-row { display: flex; justify-content: space-between; align-items: center; padding: 11px 0; border-bottom: 1px solid var(--border); gap: 12px; }
.stat-row:last-child { border-bottom: none; }
.stat-label { font-size: 13.5px; color: var(--dim); }
.stat-val { font-family: var(--mono); font-size: 15px; font-variant-numeric: tabular-nums; }
.up { color: var(--bad); } .down { color: var(--good); } .neu { color: var(--dim); }

/* ---------- 利害雙欄（九變：智者之慮必雜於利害） ---------- */
.lihai { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 20px 0; }
.lihai .col { border: 1px solid var(--border); border-radius: var(--r2); padding: 14px 16px; }
.lihai .col.li  { border-top: 2px solid var(--good); }
.lihai .col.hai { border-top: 2px solid var(--bad); }
.lihai .col .h { font-family: var(--mono); font-size: 11px; letter-spacing: .12em; text-transform: uppercase; margin-bottom: 8px; }
.lihai .col.li .h  { color: var(--good); }
.lihai .col.hai .h { color: var(--bad); }
.lihai .col p { font-size: 14px; color: var(--dim); margin: 6px 0; }

/* ---------- 知彼知己 2×2 矩陣 ---------- */
.matrix { display: grid; grid-template-columns: auto 1fr 1fr; gap: 8px; margin: 18px 0; align-items: stretch; }
.matrix .mh { font-family: var(--mono); font-size: 11px; letter-spacing: .08em; color: var(--muted); display: flex; align-items: center; justify-content: center; text-align: center; padding: 8px; }
.matrix .cell { border: 1px solid var(--border); border-radius: var(--r2); padding: 14px; background: var(--panel); }
.matrix .cell .res { font-family: var(--serif); font-size: 17px; font-weight: 600; margin-bottom: 4px; }
.matrix .cell .desc { font-size: 13px; color: var(--dim); }
.matrix .cell.win  { border-color: color-mix(in srgb, var(--good) 50%, var(--border)); } .matrix .cell.win .res { color: var(--good); }
.matrix .cell.even { border-color: color-mix(in srgb, var(--warn) 45%, var(--border)); } .matrix .cell.even .res { color: var(--warn); }
.matrix .cell.lose { border-color: color-mix(in srgb, var(--bad) 50%, var(--border)); } .matrix .cell.lose .res { color: var(--bad); }
.matrix .rh { writing-mode: vertical-rl; text-orientation: upright; font-family: var(--mono); font-size: 11px; letter-spacing: .1em; color: var(--muted); display: flex; align-items: center; justify-content: center; }

/* ---------- 3D / interactive panel ---------- */
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--r); overflow: hidden; }
.panel-head { padding: 12px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.panel-head .ttl { font-size: 14px; color: var(--dim); font-weight: 500; }
.panel-head .hint { font-family: var(--mono); font-size: 10.5px; color: var(--muted); letter-spacing: .04em; }
.panel-foot { padding: 11px 18px; border-top: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: 16px; font-family: var(--mono); font-size: 11px; color: var(--dim); }
.three-mount { width: 100%; position: relative; touch-action: none; }
.stage { background: radial-gradient(120% 90% at 50% 0%, var(--bg2), var(--bg)); }
.legend-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; vertical-align: middle; margin-right: 5px; }

.sim-grid { display: grid; grid-template-columns: 1.45fr 1fr; gap: 18px; align-items: start; }

/* ---------- controls ---------- */
.btn {
  font-family: var(--sans); font-size: 14px; font-weight: 500;
  background: var(--panel2); border: 1px solid var(--border); color: var(--dim);
  padding: 9px 14px; border-radius: var(--r2); cursor: pointer; transition: all .16s ease;
  text-align: left; display: block; width: 100%;
}
.btn:hover { border-color: var(--accent); color: var(--text); }
.btn.active { background: color-mix(in srgb, var(--accent) 16%, var(--panel2)); border-color: var(--accent); color: var(--accent-bright); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 40%, transparent); }
.btn:focus-visible { outline: none; box-shadow: var(--focus); }
.btn .bt-sub { font-size: 11.5px; color: var(--muted); margin-top: 3px; font-weight: 400; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row .btn { width: auto; }

.pill-btn {
  font-family: var(--mono); font-size: 12px; letter-spacing: .04em;
  background: var(--accent); color: var(--on-accent); border: none; font-weight: 600;
  padding: 9px 18px; border-radius: 999px; cursor: pointer; transition: all .16s ease;
}
.pill-btn:hover { background: var(--accent-bright); }
.pill-btn:active { background: var(--accent-press); transform: translateY(1px); }
.pill-btn:focus-visible { outline: none; box-shadow: var(--focus); }
.pill-btn.ghost { background: transparent; color: var(--dim); border: 1px solid var(--border); }
.pill-btn.ghost:hover { border-color: var(--accent); color: var(--accent-bright); }

/* sliders */
.ctrl { margin: 16px 0; }
.ctrl label { display: flex; justify-content: space-between; font-size: 13px; color: var(--dim); margin-bottom: 8px; }
.ctrl label .v { font-family: var(--mono); color: var(--accent-bright); font-variant-numeric: tabular-nums; }
input[type=range] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 4px;
  background: var(--border-strong); border-radius: 999px; outline: none;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: 3px solid var(--bg); box-shadow: 0 0 0 1px var(--accent);
}
input[type=range]::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; background: var(--accent); cursor: pointer; border: 3px solid var(--bg); }
input[type=range]:focus-visible::-webkit-slider-thumb { box-shadow: var(--focus); }

/* readout box */
.readout { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r2); padding: 16px 18px; }
.readout .verdict-label { font-family: var(--mono); font-size: 10.5px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); margin-bottom: 8px; }
.verdict-text { font-size: 14.5px; line-height: 1.7; }

/* ---------- tables ---------- */
.data-table { width: 100%; border-collapse: collapse; font-size: 14.5px; }
.data-table th { text-align: left; padding: 11px 12px; font-family: var(--mono); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); border-bottom: 1px solid var(--border); font-weight: 500; }
.data-table td { padding: 11px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.data-table tr:last-child td { border-bottom: none; }
.data-table td.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--r); }

/* tag */
.tag { display: inline-block; font-family: var(--mono); font-size: 10px; padding: 2px 8px; border-radius: 5px; letter-spacing: .08em; }
.tag.red    { background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--accent-bright); }
.tag.jade   { background: color-mix(in srgb, var(--jade) 22%, transparent); color: var(--jade); }
.tag.green  { background: color-mix(in srgb, var(--good) 20%, transparent); color: var(--good); }
.tag.amber  { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }

/* ---------- chapter nav (prev/next) ---------- */
.ch-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 64px 0 24px; }
.ch-nav a { display: block; background: var(--panel); border: 1px solid var(--border); border-radius: var(--r); padding: 18px 20px; transition: border-color .18s ease; }
.ch-nav a:hover { border-color: var(--accent); text-decoration: none; }
.ch-nav .dir { font-family: var(--mono); font-size: 10.5px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); }
.ch-nav .nt { font-family: var(--serif); font-size: 16px; color: var(--text); margin-top: 6px; }
.ch-nav .next { text-align: right; }
.ch-nav .disabled { opacity: .4; pointer-events: none; }

/* ---------- footer / sources ---------- */
.foot { border-top: 1px solid var(--border); margin-top: 48px; padding: 26px 0 60px; font-size: 13px; color: var(--muted); }
.sources { font-size: 12.5px; color: var(--muted); line-height: 1.8; }
.sources a { color: var(--dim); }
.disclaimer { margin-top: 18px; padding: 16px 18px; background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r2); font-size: 12.5px; color: var(--muted); line-height: 1.7; }

/* ---------- helpers ---------- */
.mt0 { margin-top: 0; } .mb0 { margin-bottom: 0; }
.center { text-align: center; }
.fade-in { animation: fadeRise .6s ease both; }
@keyframes fadeRise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

/* ---------- responsive ---------- */
@media (max-width: 880px) {
  body { font-size: 16px; }
  .sim-grid { grid-template-columns: 1fr; }
  .cols-2, .cols-3, .cols-4 { grid-template-columns: 1fr; }
  .lihai { grid-template-columns: 1fr; }
  .ch-nav { grid-template-columns: 1fr; }
  .matrix { grid-template-columns: auto 1fr; }
  .matrix .rh { display: none; }
}
@media (max-width: 560px) {
  .book-bar .btitle { display: none; }
}

/* ---------- a11y ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}
canvas { outline: none !important; display: block; }
/* 不全域清掉 focus outline（a11y）：互動元件各自有 box-shadow focus ring，
   其餘可聚焦元素（連結等）保留瀏覽器預設焦點環，鍵盤使用者才看得到。 */

/* ---------- light / print fallback ---------- */
@media print {
  :root { --bg:#fff; --bg2:#f6f5f2; --panel:#fff; --panel2:#f6f5f2; --border:#d8d3c7; --text:#161310; --dim:#4a443a; --muted:#736d60; --accent:#c0432c; --accent-bright:#a3381f; --jade:#1d7a4f; }
  body { background: #fff; }
  .book-bar, #read-progress, .ch-nav { display: none; }
  .panel, .card { break-inside: avoid; }
}
