:root {
  color-scheme: light;
  font-family: Inter, "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  --ink: #12315e;
  --muted: #52708f;
  --paper: #fffdf7;
  --panel: #ffffff;
  --line: #b9dcf6;
  --grid: #29adc0;
  --accent: #329bf3;
  --accent-soft: #eaf6ff;
  --selection: #ff8218;
  --selection-soft: #fff2df;
  --candidate: #f4b72f;
  --shadow: 0 14px 36px rgba(40, 94, 130, 0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: linear-gradient(180deg, #fffef9 0%, #f8fcf7 100%);
  padding: 22px;
}

button, summary { font: inherit; }
button { min-height: 44px; }

.sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.app-header {
  width: min(1120px, 100%);
  margin: 0 auto 16px;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  color: #fff;
  background: var(--accent);
  font-weight: 900;
  letter-spacing: -.04em;
}
.brand h1 { margin: 0; font-size: 1.4rem; line-height: 1.05; }
.brand p { margin: 4px 0 0; color: var(--muted); font-size: .82rem; }

.game-shell { width: min(1120px, 100%); margin: auto; }

.top-hud {
  position: relative;
  z-index: 5;
  display: grid;
  grid-template-columns: minmax(240px, 1fr) minmax(260px, 340px) minmax(190px, 240px) 78px;
  align-items: stretch;
  min-height: 112px;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.score-progress, .current-task, .phase-progress, .settings-panel {
  border-right: 1px solid var(--line);
}

.score-progress { display: grid; align-content: center; gap: 10px; padding: 20px 26px; }
.progress-line { display: flex; align-items: baseline; gap: 7px; }
.progress-line span { color: var(--muted); font-weight: 800; }
.progress-line strong { font-size: 2rem; line-height: 1; }
.progress-line small { color: var(--muted); font-weight: 800; }

.goal-track {
  height: 20px;
  overflow: hidden;
  border: 2px solid #b9dff2;
  border-radius: 999px;
  background: #eef8fb;
}
.goal-fill {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #45c8ba, #42a5f5);
  transition: width .3s ease;
}

.current-task {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 12px 18px;
}
.operation-symbol {
  display: grid;
  place-items: center;
  flex: 0 0 68px;
  width: 68px;
  height: 68px;
  border-radius: 20px;
  color: #fff;
  background: var(--accent);
  font-size: 3.2rem;
  font-weight: 900;
  line-height: 1;
}
.current-card {
  display: grid;
  place-items: center;
  align-content: center;
  flex: 0 0 104px;
  min-height: 92px;
  border: 3px solid #fff;
  border-radius: 20px;
  color: #fff;
  background: var(--accent);
  box-shadow: 0 8px 20px color-mix(in srgb, var(--accent) 28%, transparent);
}
.current-card > span:first-child { font-size: .72rem; font-weight: 800; opacity: .94; }
.current-card > strong { font-size: 3.5rem; line-height: 1; }

.phase-progress {
  display: grid;
  align-content: center;
  gap: 11px;
  padding: 20px 24px;
}
.phase-progress .progress-line { justify-content: space-between; }
.phase-progress .progress-line strong { color: var(--accent); font-size: 1.35rem; }
.phase-track {
  position: relative;
  height: 20px;
  overflow: hidden;
  border: 2px solid #b9dff2;
  border-radius: 999px;
  background: #eef8fb;
}
.phase-fill {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: var(--accent);
  transition: width .3s ease;
}
.phase-track:not(.session-track)::after {
  position: absolute;
  inset: 0;
  content: "";
  pointer-events: none;
  background: linear-gradient(90deg,
    transparent 31.5%, #fff 31.5% 35%,
    transparent 35% 65%, #fff 65% 68.5%,
    transparent 68.5%);
}

.settings-panel { position: relative; border-right: 0; }
.settings-panel > summary {
  display: grid;
  place-items: center;
  align-content: center;
  height: 100%;
  min-height: 76px;
  color: var(--accent);
  cursor: pointer;
  list-style: none;
}
.settings-panel > summary::-webkit-details-marker { display: none; }
.settings-panel > summary span:first-child { font-size: 2rem; line-height: 1; }
.summary-label { margin-top: 5px; font-size: .72rem; font-weight: 800; }
.settings-panel[open] > summary { background: var(--accent-soft); border-radius: 0 22px 22px 0; }
.settings-menu {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: min(360px, calc(100vw - 44px));
  padding: 18px;
  border: 2px solid var(--line);
  border-radius: 18px;
  background: #fff;
  box-shadow: var(--shadow);
}
.settings-menu section + section { margin-top: 16px; }
.settings-menu h2 { margin: 0 0 9px; font-size: .88rem; }
.mode-switcher, .formation-switcher, .difficulty-switcher { display: flex; flex-wrap: wrap; gap: 7px; }
.mode-switcher button, .formation-switcher button, .difficulty-switcher button {
  border: 2px solid #c9e3f4;
  border-radius: 999px;
  padding: 8px 12px;
  color: var(--ink);
  background: #fff;
  font-weight: 800;
  cursor: pointer;
}
.mode-switcher button.active, .formation-switcher button.active, .difficulty-switcher button.active {
  color: #fff;
  border-color: var(--accent);
  background: var(--accent);
}
.restart-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 18px; }
.restart-actions .button { margin: 0; }

.board-panel {
  margin-top: 18px;
  padding: 18px 24px 20px;
  border: 2px solid #d6ebf4;
  border-radius: 26px;
  background: rgba(255, 255, 255, .78);
}
.board-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.board-heading p { margin: 0; color: var(--muted); font-weight: 750; }
.status-chip {
  padding: 7px 12px;
  border-radius: 999px;
  color: #14687a;
  background: #e8f9f7;
  font-size: .8rem;
  font-weight: 800;
  white-space: nowrap;
}

.network-scroll {
  display: grid;
  place-items: center;
  min-height: 410px;
  overflow: hidden;
}
.network {
  display: block;
  width: min(680px, 100%);
  height: 430px;
  touch-action: none;
  user-select: none;
}
.graph-edge-line { stroke: #9ddce3; stroke-width: 9; stroke-linecap: round; }
.graph-edge.legal .graph-edge-line { stroke: var(--selection); stroke-width: 10; }
.graph-node rect { fill: #fff; stroke: var(--grid); stroke-width: 3; }
.graph-node text {
  fill: var(--ink);
  font: 800 34px Inter, "Noto Sans TC", sans-serif;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
}
.graph-node[role="button"] { cursor: pointer; }
.graph-node.legal-target rect { fill: #fff9de; stroke: var(--candidate); stroke-width: 6; }
.graph-node.selected rect { fill: var(--selection-soft); stroke: var(--selection); stroke-width: 7; }
.graph-node.selected text { fill: #c85100; }
.graph-node.known-factor rect { fill: #f3edff; stroke: #7f64d9; stroke-width: 7; stroke-dasharray: 8 4; }
.graph-node .known-factor-tag {
  fill: #5b45a9;
  font: 900 10px Inter, sans-serif;
  text-anchor: middle;
}
.graph-node:focus { outline: none; }
.graph-node:focus rect { stroke: var(--ink); stroke-width: 7; }
.drag-guide { stroke: var(--selection); stroke-width: 9; stroke-linecap: round; }

.network-caption { margin: -4px 0 0; color: var(--muted); text-align: center; font-size: .84rem; }
.button {
  border: 0;
  border-radius: 13px;
  padding: 11px 17px;
  color: #fff;
  background: var(--accent);
  font-weight: 850;
  cursor: pointer;
}
.button:hover:not(:disabled) { filter: brightness(.97); transform: translateY(-1px); }
.button:focus-visible, summary:focus-visible { outline: 4px solid #ffd66b; outline-offset: 3px; }
.button:disabled { display: none; }
.button-ghost { width: 100%; margin-top: 18px; color: var(--accent); border: 2px solid var(--accent); background: #fff; }
.button-warn { display: block; margin: 14px auto 0; color: #7b3500; background: #ffd78d; }

.assist-panel {
  width: min(760px, 100%);
  margin: 14px auto 0;
  padding: 14px 18px;
  border-radius: 16px;
  color: var(--muted);
  background: #edf8ff;
  text-align: center;
}
.assist-panel p { margin: 0; line-height: 1.5; }
.assist-panel .hint { color: var(--ink); font-weight: 800; }
.hint-button {
  min-height: 42px;
  margin-top: 10px;
  padding: 8px 18px;
  color: var(--accent);
  border: 2px solid var(--accent);
  border-radius: 999px;
  background: #fff;
  font-weight: 850;
  cursor: pointer;
}
.hint-button:disabled { opacity: .5; cursor: default; }

.history-panel { width: min(760px, 100%); margin: 14px auto 0; color: var(--muted); }
.history-panel > summary { min-height: 44px; padding: 11px 15px; color: var(--ink); cursor: pointer; text-align: center; font-weight: 800; }
.history-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #fff;
}
.history-content h2 { margin: 0 0 8px; color: var(--ink); font-size: 1rem; }
.history-content p { margin: 0; line-height: 1.55; }
.history { margin: 0; padding-left: 22px; line-height: 1.55; font-size: .84rem; }

.result-dialog {
  width: min(440px, calc(100vw - 32px));
  padding: 28px;
  color: var(--ink);
  border: 2px solid var(--line);
  border-radius: 24px;
  background: #fff;
  box-shadow: var(--shadow);
  text-align: center;
}
.result-dialog::backdrop { background: rgba(88, 133, 151, .28); backdrop-filter: blur(5px); }
.dialog-kicker { margin: 0 0 8px; color: var(--muted); font-weight: 800; }
.result-dialog h2 { margin: 0; }
.result-score { color: var(--muted); }
.result-score strong { display: block; color: var(--accent); font-size: 5rem; line-height: 1; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }
.dialog-actions .button { margin: 0; }

.phase-notice {
  position: fixed;
  z-index: 30;
  top: 22px;
  left: 50%;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 24px;
  transform: translateX(-50%);
  color: var(--ink);
  border: 2px solid var(--line);
  border-radius: 999px;
  background: #fff;
  box-shadow: var(--shadow);
  font-size: 2rem;
  font-weight: 900;
  pointer-events: none;
}
.phase-notice strong { color: var(--accent); font-size: 2.8rem; }
.phase-notice[hidden] { display: none; }

body[data-operation="subtract"] { --accent: #19aeb2; --accent-soft: #e7fbf9; }
body[data-operation="multiply"] { --accent: #e59a00; --accent-soft: #fff7dc; }
body[data-operation="divide"] { --accent: #8067d7; --accent-soft: #f2efff; }

@media (max-width: 780px) {
  body { padding: 14px; }
  .app-header { margin-bottom: 12px; }
  .brand p { display: none; }
  .top-hud { grid-template-columns: 1fr 1fr 82px; min-height: 0; }
  .score-progress { grid-column: 1 / -1; padding: 16px 18px; border-right: 0; border-bottom: 1px solid var(--line); }
  .current-task { justify-content: center; gap: 8px; padding: 10px; }
  .operation-symbol { width: 52px; height: 52px; flex-basis: 52px; font-size: 2.5rem; }
  .current-card { flex-basis: 80px; min-height: 72px; border-radius: 16px; }
  .current-card > strong { font-size: 2.8rem; }
  .phase-progress { padding: 14px; }
  .phase-progress .progress-line strong { font-size: 1.05rem; }
  .settings-panel > summary { min-height: 80px; }
  .board-panel { padding: 14px 10px 18px; }
  .board-heading { display: grid; justify-items: center; text-align: center; }
  .network-scroll { min-height: 360px; }
  .network { height: 380px; }
  .history-content { grid-template-columns: 1fr; }
  .phase-notice { top: 12px; padding: 10px 18px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
