:root {
  --bg: #0f1620;
  --surface: #18222f;
  --surface-2: #1f2c3c;
  --border: #2c3a4d;
  --text: #e6edf3;
  --muted: #9bb0c5;
  --accent: #4aa8ff;
  --accent-2: #5ad19a;
  --warn: #ffb347;
  --danger: #ff6b6b;
  --radius: 10px;
  --maxw: 1080px;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, -apple-system, sans-serif;
  line-height: 1.7;
}

.wrap {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 20px;
}

/* ---------- header ---------- */
.page-header {
  background: linear-gradient(135deg, #16263a, #0f1620);
  border-bottom: 1px solid var(--border);
  padding: 48px 0 36px;
}

.page-header h1 {
  margin: 0 0 10px;
  font-size: clamp(1.6rem, 3vw, 2.4rem);
}

.subtitle {
  margin: 0;
  color: var(--muted);
  max-width: 720px;
}

/* ---------- toc ---------- */
.toc {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(15, 22, 32, 0.92);
  backdrop-filter: blur(6px);
  padding-top: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.toc a {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  transition: all 0.15s ease;
}

.toc a:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--surface);
}

/* ---------- sections ---------- */
main {
  padding: 32px 20px 64px;
}

section {
  padding: 28px 0;
  border-bottom: 1px solid var(--border);
}

section:last-child {
  border-bottom: none;
}

h2 {
  font-size: 1.5rem;
  margin: 0 0 16px;
  padding-left: 12px;
  border-left: 4px solid var(--accent);
}

h3 {
  font-size: 1.15rem;
  margin: 28px 0 12px;
  color: var(--accent-2);
}

h4 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.lead {
  font-size: 1.05rem;
  color: var(--text);
}

.note {
  color: var(--muted);
  font-size: 0.92rem;
  border-left: 3px solid var(--border);
  padding-left: 12px;
}

/* ---------- pipeline ---------- */
.pipeline {
  display: flex;
  align-items: stretch;
  gap: 8px;
  flex-wrap: wrap;
}

.stage-box {
  flex: 1 1 200px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stage-box strong {
  font-size: 1.1rem;
}

.stage-box small {
  color: var(--muted);
}

.stage-tag {
  align-self: flex-start;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--surface-2);
  color: var(--accent);
  padding: 2px 8px;
  border-radius: 999px;
}

.arrow {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  font-size: 1.6rem;
  font-weight: bold;
  position: relative;
  padding: 0 6px;
}

.arrow-label {
  position: absolute;
  top: -2px;
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
  font-weight: normal;
}

/* ---------- stage notes ---------- */
.stage-notes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
  margin-top: 8px;
}

.stage-note {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stage-note h4 {
  color: var(--accent-2);
  margin: 4px 0 2px;
}

.stage-note p {
  font-size: 0.92rem;
  margin: 0;
}

.stage-note ul {
  margin: 2px 0;
  padding-left: 18px;
  font-size: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stage-note .sn-core {
  margin-top: auto;
  border-top: 1px dashed var(--border);
  padding-top: 10px;
  color: var(--text);
}

/* ---------- tables ---------- */
.table-scroll {
  overflow-x: auto;
  margin: 8px 0 16px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
  min-width: 420px;
}

th,
td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: right;
}

th:first-child,
td:first-child {
  text-align: left;
}

thead th {
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
}

tbody tr:nth-child(even) {
  background: rgba(255, 255, 255, 0.02);
}

tr.sum td {
  font-weight: 700;
  border-top: 2px solid var(--accent);
}

table.legend {
  min-width: auto;
}

table.legend th {
  text-align: left;
  white-space: nowrap;
  color: var(--accent);
  background: transparent;
}

table.legend td {
  text-align: left;
}

/* ---------- queues ---------- */
.queue-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
}

.queue-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.queue-card h4 {
  color: var(--accent-2);
}

.queue {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.q-item {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  font-variant-numeric: tabular-nums;
  font-size: 0.88rem;
}

.q-arrow {
  color: var(--accent);
}

/* ---------- formulas (pure CSS) ---------- */
.formula {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin: 10px 0;
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}

.formula.sm {
  font-size: 0.95rem;
  padding: 12px 16px;
}

.formula .var {
  color: var(--accent);
  font-style: italic;
}

.formula .eq {
  color: var(--muted);
}

.frac {
  display: inline-flex;
  flex-direction: column;
  text-align: center;
  vertical-align: middle;
  margin: 0 4px;
}

.frac .num {
  padding: 0 8px 2px;
}

.frac .den {
  padding: 2px 8px 0;
  border-top: 1.5px solid currentColor;
}

sub {
  font-size: 0.7em;
}

/* ---------- examples ---------- */
.example {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin: 12px 0;
}

.example .formula {
  background: var(--surface-2);
}

/* ---------- calculator ---------- */
.preset-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--muted);
}

.preset-btn,
.calc-actions button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  transition: all 0.15s ease;
}

.preset-btn:hover,
.calc-actions button:hover {
  border-color: var(--accent);
}

.calc-actions button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #05121f;
  font-weight: 600;
}

.calc-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.calc-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.calc-card h3 {
  margin-top: 4px;
}

/* 兩張輸入表共用同一組欄寬，使 Setting 與 Quote 欄位上下對齊 */
.input-table {
  table-layout: fixed;
  min-width: 520px;
}

.input-table th:first-child,
.input-table td:first-child {
  width: 60px;
}

.input-table input {
  width: 100%;
  min-width: 0;
  font: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 5px 8px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.input-table input:focus {
  outline: none;
  border-color: var(--accent);
}

.calc-actions {
  display: flex;
  gap: 10px;
  margin: 18px 0;
}

.calc-error {
  background: rgba(255, 107, 107, 0.12);
  border: 1px solid var(--danger);
  color: #ffd0d0;
  border-radius: var(--radius);
  padding: 12px 16px;
  margin: 12px 0;
}

.results {
  margin-top: 12px;
}

.breakdown {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}

.bd-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.bd-card h4 {
  color: var(--accent-2);
  margin-bottom: 6px;
}

.bd-card table {
  min-width: auto;
  font-size: 0.85rem;
}

.bd-side {
  font-size: 0.82rem;
  color: var(--muted);
  margin: 8px 0 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.bd-empty {
  font-size: 0.82rem;
  color: var(--muted);
  font-style: italic;
  margin: 2px 0 4px;
}

.bd-partial {
  font-size: 0.8rem;
  color: var(--accent-2);
  margin: 2px 0 4px;
}

/* ---------- cases ---------- */
.case {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin: 14px 0;
}

.case h3 {
  margin-top: 4px;
}

.keypoints {
  padding-left: 20px;
}

/* ---------- spread type toggle ---------- */
.spread-type-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--muted);
}

.spread-type-btn {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 7px 14px;
  border-radius: 8px;
  transition: all 0.15s ease;
}

.spread-type-btn:hover {
  border-color: var(--accent);
}

.spread-type-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #05121f;
  font-weight: 600;
}

.keypoints li {
  margin-bottom: 8px;
}

/* ---------- footer ---------- */
.page-footer {
  border-top: 1px solid var(--border);
  padding: 24px 0;
  color: var(--muted);
  font-size: 0.85rem;
}

/* ---------- responsive ---------- */
@media (max-width: 640px) {
  .arrow {
    transform: rotate(90deg);
    padding: 10px 0;
  }

  .pipeline {
    flex-direction: column;
  }
}
