:root {
  color-scheme: light;
  --ink: #18202b;
  --muted: #617086;
  --paper: #fffdf8;
  --line: #d8dee9;
  --red: #d83b4c;
  --blue: #2176d2;
  --gold: #e6a21a;
  --green: #16845f;
  --bg: #f3f5f0;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans TC", "Segoe UI", sans-serif;
  background:
    linear-gradient(120deg, rgba(216, 59, 76, .10), transparent 34%),
    linear-gradient(240deg, rgba(33, 118, 210, .12), transparent 36%),
    var(--bg);
  color: var(--ink);
}

button, input { font: inherit; }
button { cursor: pointer; }

.app {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 16px;
}

.battle-active .app {
  width: min(1120px, 100%);
  padding: 136px 10px 10px;
}

.battle-active .topbar {
  display: none;
}

.topbar, .fight-stage, .panel, .home-grid > section {
  background: rgba(255, 253, 248, .94);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 16px 36px rgba(24, 32, 43, .08);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
}

.back-link {
  color: var(--blue);
  font-weight: 800;
  text-decoration: none;
}

.brand { font-weight: 900; font-size: clamp(1.3rem, 3vw, 2.2rem); }
.sub { color: var(--muted); font-size: .95rem; }
.badge { padding: 6px 10px; border-radius: 999px; background: #18202b; color: white; font-weight: 800; }

.home-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 14px;
}

.home-grid > section, .panel { padding: 16px; }
.hero-panel,
.lobby-panel {
  grid-column: 1 / -1;
}

.hero-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 230px;
  gap: 18px;
  align-items: center;
}

.hero-panel h2 {
  font-size: clamp(1.35rem, 3vw, 2.2rem);
}

.home-qr-card {
  display: grid;
  justify-items: center;
  gap: 8px;
  padding: 12px;
  border: 1px solid #c8d6e8;
  border-radius: 8px;
  background: #eef5ff;
}

.home-qr-title {
  color: var(--blue);
  font-weight: 900;
}

.home-qr-canvas {
  width: 160px;
  height: 160px;
  padding: 6px;
  border-radius: 8px;
  background: white;
}

.home-qr-canvas canvas {
  display: block;
  width: 148px;
  height: 148px;
}

.home-qr-url {
  color: var(--muted);
  font-size: .72rem;
  font-weight: 700;
  overflow-wrap: anywhere;
  text-align: center;
}
h1, h2, h3, p { margin-top: 0; }
h2 { font-size: 1.15rem; }

.battle-active .panel { padding: 10px; }
.battle-active h2 { font-size: 1rem; margin-bottom: 8px; }
.battle-active h3 { font-size: .96rem; margin: 8px 0 6px; }

.field-row, .mode-row, .join-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

input {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
}

.field-row input { flex: 1 1 180px; }

.btn, .mode-btn {
  border: 0;
  border-radius: 6px;
  min-height: 42px;
  padding: 9px 13px;
  background: #e9eef7;
  color: var(--ink);
  font-weight: 800;
}

.btn.primary { background: var(--ink); color: white; }
.btn.red { background: var(--red); color: white; }
.btn.blue { background: var(--blue); color: white; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.mode-btn.active { outline: 3px solid rgba(230, 162, 26, .35); background: #fff0c9; }

.notice {
  min-height: 28px;
  margin-top: 10px;
  color: var(--muted);
  font-weight: 700;
}

.teacher-panel {
  grid-column: 1 / -1;
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}

.teacher-create-row {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) 110px auto auto;
  gap: 10px;
  align-items: end;
}

.teacher-create-row label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-weight: 900;
}

select {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  font: inherit;
}

.teacher-room-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 10px;
}

.student-room-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.student-create-panel {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f7fafc;
}

.student-create-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.room-size.one { background: #e9f7ef; color: #0b6b4c; }
.room-size.two { background: #eef5ff; color: #145ca9; }
.room-size.three { background: #fff4df; color: #8a5200; }

.student-room-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #fff;
}

.student-room-card.size-1,
.teacher-room-card.size-1 {
  border-left: 8px solid #16845f;
}

.student-room-card.size-2,
.teacher-room-card.size-2 {
  border-left: 8px solid #2176d2;
}

.student-room-card.size-3,
.teacher-room-card.size-3 {
  border-left: 8px solid #e6a21a;
}

.student-room-code {
  font-size: 1.5rem;
  font-weight: 900;
  letter-spacing: .12em;
}

.student-room-actions {
  display: flex;
  gap: 8px;
}

.student-room-actions .btn {
  min-width: 70px;
}

.teacher-room-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #fff;
}

.teacher-room-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
}

.teacher-room-code {
  font-size: 1.55rem;
  font-weight: 900;
  letter-spacing: .12em;
}

.teacher-room-meta {
  color: var(--muted);
  font-weight: 800;
  margin-bottom: 5px;
}

.teacher-room-card a {
  color: var(--blue);
  font-size: .78rem;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.teacher-qr {
  width: 104px;
  height: 104px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px;
  background: #fff;
}

.teacher-qr canvas {
  display: block;
  width: 94px;
  height: 94px;
}

.teacher-room-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.teacher-dashboard {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin: 10px 0 14px;
}

.stat-card {
  display: grid;
  gap: 2px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.stat-card b {
  font-size: 1.5rem;
}

.stat-card span {
  color: var(--muted);
  font-weight: 800;
}

.leaderboard-table {
  overflow-x: auto;
  margin-bottom: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.leaderboard-table table {
  width: 100%;
  min-width: 680px;
  border-collapse: collapse;
}

.leaderboard-table th,
.leaderboard-table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
}

.leaderboard-table th {
  background: #f2f5f9;
  color: var(--muted);
  font-size: .86rem;
}

.leaderboard-table tr:last-child td {
  border-bottom: 0;
}

.btn.danger {
  color: #b91c1c;
  border-color: #f0b9b9;
}

.waiting-banner {
  margin: 8px 0;
  padding: 9px 10px;
  border: 1px solid #f1c76f;
  border-radius: 8px;
  background: #fff6d8;
  color: #7a4c00;
  font-weight: 900;
}

.room-code {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 96px;
  min-height: 48px;
  margin: 8px 0;
  border: 2px dashed var(--gold);
  border-radius: 8px;
  font-size: 1.8rem;
  font-weight: 900;
  letter-spacing: .12em;
}

.room-qr {
  width: 132px;
  height: 132px;
  margin: 10px auto 6px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.room-qr canvas {
  display: block;
  width: 120px;
  height: 120px;
}

.room-join-url {
  color: var(--muted);
  font-size: .72rem;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.fight-stage {
  position: fixed;
  top: 8px;
  left: 8px;
  right: 8px;
  z-index: 30;
  width: auto;
  margin-top: 0;
  padding: 8px;
  background-image: linear-gradient(rgba(255, 253, 248, .86), rgba(255, 253, 248, .9)), url("images/battle-bg.svg");
  background-size: cover;
  background-position: center 42%;
}

.hp-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
}

.hp-card { min-width: 0; }
.hp-label { display: flex; justify-content: space-between; font-weight: 900; font-size: .9rem; }
.hp-bar { height: 12px; background: #dfe5ee; border-radius: 999px; overflow: hidden; }
.hp-fill { height: 100%; width: 100%; transition: width .25s ease; }
.hp-fill.red { background: linear-gradient(90deg, #ad2030, var(--red)); }
.hp-fill.blue { background: linear-gradient(90deg, #145ca9, var(--blue)); }
.versus { font-weight: 900; font-size: 1.05rem; color: var(--muted); white-space: nowrap; }

.fighters {
  position: relative;
  min-height: 118px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: end;
  gap: 10px;
  padding: 4px 12px 0;
  background:
    linear-gradient(#ffffffaa, #ffffff44),
    repeating-linear-gradient(90deg, transparent 0 42px, rgba(24, 32, 43, .06) 42px 43px);
  border-radius: 8px;
  margin-top: 6px;
  overflow: hidden;
}

.fighter {
  position: relative;
  min-height: 106px;
  display: flex;
  align-items: end;
  justify-content: center;
  transition: transform .18s ease;
}

.fighter.attack-red { transform: translateX(92px) scale(1.08); }
.fighter.attack-blue { transform: translateX(-92px) scale(1.08); }
.fighter.hit { animation: hit .28s ease; }
.fighter.hit::after {
  content: "";
  position: absolute;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 236, 149, .95) 0 22%, rgba(238, 78, 92, .55) 35%, transparent 68%);
  transform: translateY(-28px);
  animation: impact-flash .32s ease;
  pointer-events: none;
}

.avatar {
  width: 96px;
  height: 118px;
  position: relative;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center bottom;
  filter: drop-shadow(0 10px 8px rgba(24, 32, 43, .2));
}

.avatar::before,
.avatar::after { content: none; }

.avatar.red { background-image: url("images/red-fighter.png"); }
.avatar.blue { background-image: url("images/blue-fighter.png"); }

@keyframes hit {
  30% { transform: translateX(10px) rotate(2deg); filter: brightness(1.2); }
  60% { transform: translateX(-8px) rotate(-2deg); }
}

@keyframes impact-flash {
  0% { opacity: 0; transform: translateY(-28px) scale(.55); }
  45% { opacity: 1; transform: translateY(-28px) scale(1.12); }
  100% { opacity: 0; transform: translateY(-28px) scale(1.35); }
}

.attack-effect {
  position: absolute;
  left: 50%;
  bottom: 34px;
  z-index: 2;
  width: 124px;
  height: 92px;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 0;
  border-radius: 0;
  background: url("images/attack-frame-1.svg") center / contain no-repeat;
  border: 0;
  color: #7a4b00;
  font-weight: 900;
  text-shadow: 0 2px 0 #fff;
  opacity: 0;
  transform: translate(-50%, 10px) scale(.9);
  pointer-events: none;
}

.attack-effect.effect-red { animation: blast-red .62s steps(1, end); }
.attack-effect.effect-blue { animation: blast-blue .62s steps(1, end); }
.attack-effect.effect-heal {
  animation: heal-pop .72s steps(1, end);
  background: url("images/heal-frame-1.svg") center / contain no-repeat;
  color: var(--green);
}
.attack-effect.effect-penalty { animation: penalty-pop .42s ease; background: #fff1f2; color: var(--red); border-color: var(--red); }

@keyframes blast-red {
  0% { opacity: 0; transform: translate(-128%, 12px) scale(.78); background-image: url("images/attack-frame-1.svg"); }
  18% { opacity: 1; transform: translate(-94%, 2px) scale(.9); background-image: url("images/attack-frame-1.svg"); }
  38% { opacity: 1; transform: translate(-68%, -8px) scale(1); background-image: url("images/attack-frame-2.svg"); }
  58% { opacity: 1; transform: translate(-46%, -12px) scale(1.1); background-image: url("images/attack-frame-3.svg"); }
  78% { opacity: 1; transform: translate(-30%, -10px) scale(1.2); background-image: url("images/attack-frame-4.svg"); }
  100% { opacity: 0; transform: translate(12%, 0) scale(1.08); background-image: url("images/attack-frame-5.svg"); }
}

@keyframes blast-blue {
  0% { opacity: 0; transform: translate(28%, 12px) scale(.78) scaleX(-1); background-image: url("images/attack-frame-1.svg"); }
  18% { opacity: 1; transform: translate(-6%, 2px) scale(.9) scaleX(-1); background-image: url("images/attack-frame-1.svg"); }
  38% { opacity: 1; transform: translate(-32%, -8px) scale(1) scaleX(-1); background-image: url("images/attack-frame-2.svg"); }
  58% { opacity: 1; transform: translate(-54%, -12px) scale(1.1) scaleX(-1); background-image: url("images/attack-frame-3.svg"); }
  78% { opacity: 1; transform: translate(-70%, -10px) scale(1.2) scaleX(-1); background-image: url("images/attack-frame-4.svg"); }
  100% { opacity: 0; transform: translate(-112%, 0) scale(1.08) scaleX(-1); background-image: url("images/attack-frame-5.svg"); }
}

@keyframes heal-pop {
  0% { opacity: 0; transform: translate(-50%, 14px) scale(.7); background-image: url("images/heal-frame-1.svg"); }
  20% { opacity: 1; transform: translate(-50%, 4px) scale(.86); background-image: url("images/heal-frame-1.svg"); }
  40% { opacity: 1; transform: translate(-50%, -6px) scale(1); background-image: url("images/heal-frame-2.svg"); }
  60% { opacity: 1; transform: translate(-50%, -14px) scale(1.16); background-image: url("images/heal-frame-3.svg"); }
  80% { opacity: .85; transform: translate(-50%, -18px) scale(1.28); background-image: url("images/heal-frame-4.svg"); }
  100% { opacity: 0; transform: translate(-50%, -8px) scale(1.42); background-image: url("images/heal-frame-5.svg"); }
}

@keyframes penalty-pop {
  0% { opacity: 0; transform: translate(-50%, 8px) scale(.8) rotate(-4deg); }
  45% { opacity: 1; transform: translate(-50%, -8px) scale(1.12) rotate(3deg); }
  100% { opacity: 0; transform: translate(-50%, 0) scale(.95); }
}

.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(24, 32, 43, .46);
}

.result-dialog {
  width: min(520px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  box-shadow: 0 24px 60px rgba(24, 32, 43, .22);
  padding: 20px;
}

.result-title {
  font-size: 1.8rem;
  font-weight: 900;
  text-align: center;
  margin-bottom: 14px;
  display: grid;
  justify-items: center;
  gap: 8px;
}

.result-title::before {
  content: "";
  width: 76px;
  height: 76px;
  background: url("images/mvp-badge.svg") center / contain no-repeat;
}

.result-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.result-grid > div {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f7fafc;
}

.result-grid span {
  display: block;
  color: var(--muted);
  font-weight: 900;
  font-size: .82rem;
}

.result-grid b {
  display: block;
  margin-top: 4px;
  font-size: 1.2rem;
}

.result-stats {
  max-height: 220px;
  overflow: auto;
}

.result-stats table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}

.result-stats th,
.result-stats td {
  padding: 7px;
  border-bottom: 1px solid var(--line);
  text-align: left;
}

.result-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 14px;
}

.play-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 270px;
  gap: 10px;
  margin-top: 0;
}

.play-grid > * {
  min-width: 0;
}

.problem {
  font-size: clamp(1.3rem, 3vw, 2.1rem);
  font-weight: 900;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  border-radius: 8px;
  background: #f7f0df;
}

.steps, .choices, .point-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
}

.step-btn, .choice-btn, .point-btn, .move-card {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 9px 11px;
  font-weight: 800;
}

.step-btn.done { background: #e4f6ed; color: var(--green); }
.choice-btn.selected, .point-btn.active, .move-card.selected { border-color: var(--gold); background: #fff1cc; }

.equation-board {
  display: grid;
  gap: 6px;
  margin-bottom: 8px;
}

.equation-step {
  display: grid;
  gap: 5px;
}

.equation-row {
  display: grid;
  grid-template-columns: 70px minmax(118px, .32fr) minmax(220px, 1fr) minmax(118px, .32fr);
  gap: 8px;
  align-items: center;
  min-height: 48px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcff;
}

.equation-row.locked {
  color: #9099a8;
  background: #f0f2f5;
}

.equation-row.current {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(230, 162, 26, .18);
}

.row-meta {
  grid-column: 1;
  color: var(--muted);
  font-size: .82rem;
  font-weight: 900;
}

.equation-line {
  grid-column: 3;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.1rem, 2.6vw, 1.65rem);
  font-weight: 900;
  overflow-wrap: anywhere;
  white-space: nowrap;
  overflow: hidden;
}

.math-expression {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .18em;
  white-space: nowrap;
}

.math-symbol {
  display: inline-block;
  min-width: 1.05em;
  text-align: center;
  font-family: "Cambria Math", "Times New Roman", serif;
  font-weight: 900;
  line-height: 1;
}

.math-frac {
  display: inline-grid;
  grid-template-rows: auto auto;
  align-items: center;
  justify-items: center;
  min-width: 1.05em;
  margin: 0 .05em;
  font-family: "Cambria Math", "Times New Roman", serif;
  font-weight: 900;
  line-height: .9;
  vertical-align: middle;
}

.math-frac .num {
  display: block;
  width: 100%;
  padding: 0 .15em .08em;
  border-bottom: .08em solid currentColor;
  text-align: center;
}

.math-frac .den {
  display: block;
  padding-top: .08em;
  text-align: center;
}

.drop-zone {
  min-height: 34px;
  display: grid;
  place-items: center;
  border: 2px dashed #d8b04d;
  border-radius: 8px;
  color: #7a4b00;
  background: #fff8df;
  font-weight: 900;
}

.side-drop-row {
  display: contents;
}

.side-zone {
  min-height: 48px;
  font-size: .82rem;
  padding: 6px 5px;
}

.left-zone { grid-column: 2; grid-row: 1; }
.right-zone { grid-column: 4; grid-row: 1; }

.left-zone {
  background: #eef5ff;
  border-color: #8fb8e8;
  color: #164c86;
}

.right-zone {
  background: #fff8df;
  border-color: #d8b04d;
  color: #7a4b00;
}

.choose-direct {
  grid-column: 4;
  min-height: 48px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #eef7f2;
  color: var(--green);
  font-weight: 900;
  font-size: .9rem;
}

.drop-zone.over {
  background: #ffe99d;
  transform: translateY(-1px);
}

.drop-zone.done {
  background: #e4f6ed;
  border-color: var(--green);
  color: var(--green);
}

.move-card {
  box-shadow: 0 5px 0 rgba(24, 32, 43, .12);
  touch-action: manipulation;
}

.step-choice-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  padding: 8px;
  border-radius: 8px;
  background: #f7f0df;
}

.step-choice-row.waiting {
  display: block;
  color: var(--muted);
  font-weight: 900;
  text-align: center;
}

.term-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 42px;
  min-height: 32px;
  margin: 0 4px;
  padding: 4px 10px;
  border: 2px solid #d8b04d;
  border-radius: 8px;
  background: #fff1aa;
  color: #5b3a00;
  font: inherit;
  font-weight: 900;
  box-shadow: 0 4px 0 rgba(122, 75, 0, .18);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.term-chip.selected {
  outline: 3px solid rgba(230, 162, 26, .45);
  background: #ffe37a;
}

.touch-drag-ghost {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(1.08);
  opacity: .94;
  box-shadow: 0 10px 24px rgba(24, 32, 43, .28);
}

.static-chip {
  cursor: default;
  box-shadow: none;
}

.move-card:active {
  transform: translateY(2px);
  box-shadow: 0 3px 0 rgba(24, 32, 43, .12);
}

.move-done {
  color: var(--green);
  font-weight: 900;
}

.solution-stage.locked {
  opacity: .42;
  pointer-events: none;
  filter: grayscale(.2);
}

.numberline {
  position: relative;
  height: 136px;
  margin: 8px 0 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  touch-action: none;
}

.axis {
  position: absolute;
  left: 36px;
  right: 36px;
  top: 70px;
  height: 4px;
  background: #283241;
}

.tick {
  position: absolute;
  top: 56px;
  width: 2px;
  height: 28px;
  background: #283241;
}

.tick span {
  position: absolute;
  top: 30px;
  transform: translateX(-50%);
  font-size: .75rem;
  color: var(--muted);
}

.solution-ray {
  position: absolute;
  top: 64px;
  height: 16px;
  border-radius: 999px;
  background: rgba(22, 132, 95, .32);
  display: none;
}

.point {
  position: absolute;
  top: 56px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 4px solid var(--green);
  background: #fff;
  transform: translate(-50%, 0);
  touch-action: none;
}

.point.closed { background: var(--green); }

.direction-band {
  position: absolute;
  left: 24px;
  right: 24px;
  top: 8px;
  height: 42px;
  display: grid;
  grid-template-columns: 86px minmax(0, 1fr) 86px;
  align-items: center;
  gap: 8px;
  padding: 0 6px;
  border-radius: 8px;
  background: #eef7f2;
  touch-action: none;
}

.direction-btn {
  min-height: 32px;
  border: 1px solid #b8d9ca;
  border-radius: 6px;
  background: #fff;
  color: var(--green);
  font-weight: 900;
}

.direction-btn.active {
  background: var(--green);
  color: white;
}

.direction-band span {
  text-align: center;
  color: var(--green);
  font-weight: 900;
}

.direction-ray {
  position: absolute;
  top: 26px;
  height: 10px;
  border-radius: 999px;
  background: rgba(22, 132, 95, .42);
  display: none;
  pointer-events: none;
}

.vertical-ray {
  position: absolute;
  top: 26px;
  width: 8px;
  height: 42px;
  border-radius: 999px;
  background: rgba(22, 132, 95, .42);
  display: none;
  transform: translateX(-50%);
  pointer-events: none;
}

.drag-hint {
  color: var(--muted);
  font-size: .9rem;
  margin-bottom: 0;
}

.log {
  max-height: 150px;
  overflow: auto;
  padding: 0;
  margin: 0;
  list-style: none;
}

.log li {
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
}

.member-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 8px;
}

.member {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px;
  background: #fff;
}

.hidden { display: none !important; }

@media (max-width: 820px) {
  .home-grid, .play-grid { grid-template-columns: 1fr; }
  .hero-panel { grid-template-columns: 1fr; }
  .home-qr-card { justify-self: center; width: min(100%, 260px); }
  .student-room-card { grid-template-columns: 1fr; }
  .student-room-actions { justify-content: stretch; }
  .student-room-actions .btn { flex: 1; }
  .student-create-row { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .teacher-create-row { grid-template-columns: 1fr 1fr; }
  .battle-active .app { padding: 128px 8px 8px; }
  .hp-row { grid-template-columns: 1fr auto 1fr; }
  .versus { text-align: center; }
  .fighters { min-height: 102px; padding-inline: 6px; }
  .fighter { min-height: 96px; }
  .avatar { width: 78px; height: 96px; }
  .avatar::before { left: 8px; }
  .equation-row { grid-template-columns: 1fr 1fr; }
  .row-meta { grid-column: 1 / -1; }
  .equation-line { grid-column: 1 / -1; }
  .left-zone { grid-column: 1; grid-row: auto; }
  .right-zone { grid-column: 2; grid-row: auto; }
  .step-choice-row { grid-template-columns: 1fr 1fr; }
}

@media (min-width: 821px) and (max-width: 1180px) {
  .battle-active .app { padding-top: 132px; }
  .play-grid { grid-template-columns: minmax(0, 1fr) 240px; }
  .equation-row { grid-template-columns: 58px minmax(96px, .28fr) minmax(190px, 1fr) minmax(96px, .28fr); gap: 5px; }
  .equation-line { font-size: clamp(1rem, 2.4vw, 1.45rem); }
  .term-chip { min-width: 36px; padding: 3px 7px; }
  .side-zone { font-size: .76rem; min-height: 42px; }
  .room-code { min-height: 38px; font-size: 1.35rem; }
  .member-grid { grid-template-columns: 1fr; }
}

@media print {
  body { background: white; }
  .topbar,
  .home-grid > section:not(.teacher-panel),
  .section-head,
  .teacher-create-row,
  .teacherNotice,
  .teacher-room-actions {
    display: none !important;
  }
  .app {
    width: 100%;
    padding: 0;
  }
  .teacher-panel {
    border: 0;
    box-shadow: none;
    padding: 0;
  }
  .teacher-room-list {
    grid-template-columns: repeat(2, 1fr);
    gap: 14mm;
  }
  .teacher-room-card {
    break-inside: avoid;
    border: 2px solid #111;
    min-height: 120mm;
    display: grid;
    align-content: center;
    text-align: center;
  }
  .teacher-room-main {
    grid-template-columns: 1fr;
    justify-items: center;
  }
  .teacher-room-code {
    font-size: 3rem;
  }
  .teacher-qr,
  .teacher-qr canvas {
    width: 52mm !important;
    height: 52mm !important;
  }
}
/* 除法格鬥場：維持原模板的血條與角色，工作臺按次方分欄。 */
.long-division-head {display:flex; flex-direction:column; gap:8px; border-bottom:3px solid #2176d2; padding:12px; margin-bottom:12px;}
.coefficient-grid {display:grid;grid-template-columns:repeat(auto-fit,minmax(76px,1fr));gap:8px;}
.coefficient-grid label,.quotient-fields label {display:flex;flex-direction:column;gap:6px;text-align:center;font-weight:800;}
.coefficient-grid input,.quotient-fields input,.identity-slots select {width:100%;min-height:48px;font-size:20px;border:2px solid #a7b5c9;border-radius:8px;background:white;color:#18202b;box-sizing:border-box;padding:8px;}
.quotient-fields {display:grid;grid-template-columns:1fr 1fr;gap:12px;}
.identity-slots {display:flex; flex-wrap:wrap;gap:8px;align-items:center;}
.identity-slots select {max-width:100%;width:auto;font-size:17px;flex:1 1 160px;}
.equation-row.current {display:block;}
.equation-row p {overflow-wrap:anywhere;}
fieldset {border:0;padding:0;} fieldset label {display:flex;align-items:center;gap:10px;min-height:48px;padding:8px;background:#eef5ff;border-radius:8px;margin-top:8px;}
fieldset input {width:24px;height:24px;flex:none;}
button:disabled {opacity:.6;cursor:wait;}
.app {padding-bottom:calc(16px + env(safe-area-inset-bottom));}
@media(max-width:600px){.coefficient-grid{grid-template-columns:repeat(2,minmax(0,1fr));}.identity-slots{flex-direction:column;align-items:stretch;}.identity-slots select{flex:none;width:100%;}.equation-line{font-size:18px;}.field-row{flex-wrap:wrap;}}
/* 血條跟隨捲動，但不遮住題目與倒數。 */
.battle-active .app {padding-top:12px!important;}
.fight-stage {position:sticky;top:8px;left:auto;right:auto;margin-bottom:12px;}
.btn,.mode-btn,input,select {min-height:48px;}
.match-clock {text-align:center;font-weight:800;margin:0 0 8px;}
/* 直式除法：商式在上，除式在左；乘回與相減依次方垂直對齊。 */
.long-division {display:grid;grid-template-columns:88px repeat(var(--columns),minmax(0,1fr));gap:0;font-size:20px;margin:16px 0;background:#fff;border-radius:8px;padding:8px;}
.division-label {display:flex;align-items:center;justify-content:center;font-weight:800;color:#617086;min-height:58px;font-size:15px;}
.degree-heading {text-align:center;font-size:14px;color:#617086;padding:6px;}
.division-cell {display:flex;align-items:center;justify-content:center;min-height:58px;min-width:0;font-size:20px;font-weight:800;padding:6px 3px;text-align:center;white-space:nowrap;}
.division-cell sup {font-size:.68em;align-self:flex-start;margin-top:6px;}
.division-divisor {display:flex;flex-direction:column;align-items:center;justify-content:center;border-right:3px solid #18202b;font-weight:900;gap:6px;font-size:18px;}
.division-divisor span {font-size:12px;color:#617086;}
.dividend-cells,.division-term-row {grid-column:2/-1;display:grid;grid-template-columns:repeat(var(--columns),minmax(0,1fr));}
.dividend-cells {border-top:3px solid #18202b;}
.remainder-row {border-top:2px solid #18202b;margin-bottom:6px;}
.zero-term {color:#9ca7b4;font-weight:500;}
.minus-label {flex-direction:column;color:#18202b;font-size:26px;line-height:1.1;}
.minus-label small {font-size:12px;color:#617086;}
.drop-cell {background:#fff9e9;border:2px dashed #c89a36;border-radius:8px;margin:3px;white-space:normal;font-size:13px;color:#735118;}
.drop-cell.filled {background:#fff;font-size:20px;color:#18202b;border-style:solid;}
.drop-cell.drag-over {background:#d9efff;border-color:#2176d2;}
.confirmed {color:#145ca9;}
.phase-heading {font-size:18px!important;margin-top:14px!important;}
.term-shelf {background:#eaf2fc;border:1px solid #b5cce8;border-radius:10px;padding:10px;margin:10px 0;position:sticky;top:8px;z-index:12;}
.term-shelf small {display:block;color:#617086;font-size:13px;margin-top:6px;}
.term-cards {display:flex;flex-wrap:wrap;gap:8px;margin-top:8px;}
.term-card {min-height:48px;min-width:64px;border:2px solid #8aa9cc;border-radius:8px;background:white;font-size:19px;font-weight:800;padding:8px;touch-action:none;user-select:none;-webkit-user-select:none;cursor:grab;}
.term-card.selected-card {outline:3px solid #2176d2;background:#d9efff;}
.drag-term-ghost {position:fixed;z-index:9999;pointer-events:none;transform:translate(-50%,-50%);background:#fff;border:3px solid #2176d2;padding:12px;border-radius:10px;box-shadow:0 8px 22px #0003;font-size:22px;font-weight:900;}
.verification-work {background:#eef5ff;border-radius:8px;padding:12px;}
.verification-row {display:grid;grid-template-columns:repeat(var(--columns),minmax(0,1fr));}
/* 拖拉時不讓固定血條遮住商項，卡片架可隨計算列捲動。 */
.fight-stage {position:relative;top:auto;}
@media(max-width:600px){.long-division{grid-template-columns:64px repeat(var(--columns),minmax(0,1fr));padding:4px;font-size:16px;}.division-cell{font-size:16px;min-height:56px;}.drop-cell{font-size:11px;padding:4px 1px;}.drop-cell.filled{font-size:16px;}.division-divisor{font-size:15px;}.term-card{min-width:58px;font-size:17px;}.term-cards{gap:6px;}}

/* 驗算角色與運算符始終同列，窄螢幕可水平捲動。 */
.identity-slots{flex-direction:row;flex-wrap:nowrap;align-items:center;gap:4px;white-space:nowrap;overflow-x:auto;padding:4px 0;}
.identity-slots select{flex:1 0 64px;min-width:64px;width:0;max-width:none;font-size:14px;padding:4px 2px;}

.creator-side-label{display:block;font-weight:800;margin:10px 0 6px;}
.creator-side-select{min-height:48px;width:100%;font-size:18px;border:2px solid #a7b5c9;border-radius:8px;padding:8px;margin-bottom:12px;background:#fff;color:#18202b;}
.room-roster{margin-top:8px;line-height:1.6;font-size:15px;overflow-wrap:anywhere;}
.room-creator{color:#617086;font-size:13px;margin-bottom:2px;}
.roster-red{color:#a62c39;}.roster-blue{color:#215ba5;}

.identity-slots .identity-cell{flex:1 0 64px;min-width:64px;width:0;min-height:58px;margin:0;font-size:12px;padding:4px 2px;}
.identity-slots .identity-cell.filled{font-size:14px;}
.expression-card{max-width:100%;white-space:normal;overflow-wrap:anywhere;}

/* 相似形作答工作台：固定戰況；圖與拖拉一同在主視窗。 */
.battle-active .topbar{display:none;}
.battle-active .app{padding-top:118px!important;}
.battle-active .fight-stage{position:fixed;top:0;left:0;right:0;width:min(1100px,100%);margin:auto;z-index:50;height:108px;min-height:0;box-sizing:border-box;padding:8px 14px;border-radius:0 0 14px 14px;box-shadow:0 5px 20px #0003;overflow:hidden;}
.fight-stage .hp-row{position:relative;z-index:3;gap:12px;}
.fight-stage .hp-card{padding:4px 10px;}
.fight-stage .hp-label{font-size:14px;}
.fight-stage .hp-bar{height:12px;}
.fight-stage .match-clock{margin:0 0 4px;font-size:14px;}
.fight-stage .fighters{position:absolute;inset:22px 25%;height:82px;min-height:0;opacity:.35;pointer-events:none;}
.fight-stage .fighter{transform:scale(.4);transform-origin:center center;}
.fight-stage .versus{font-size:17px;padding:4px;}
.play-grid{display:block;}
.play-grid>aside{margin-top:12px;}
.battle-active .play-grid>aside{display:none;}
.battle-active .play-grid>aside.expanded{display:block;}
.panel{min-width:0;}
.play-grid>.panel>h2{display:none;}
.problem{font-size:20px!important;line-height:1.5;margin:0 0 8px;padding:8px 12px;overflow-wrap:anywhere;}
.step-trail{display:flex;gap:6px;flex-wrap:wrap;font-size:13px;margin:8px 0;}
.step-trail span{padding:4px 7px;background:#e9edf4;border-radius:6px;}
.step-trail .current{background:#235a97;color:white;}.step-trail .done{background:#d2f2df;color:#13653d;}
.workbench{display:grid;grid-template-columns:minmax(0,1.15fr) minmax(0,1fr);gap:12px;align-items:center;}
.geometry-canvas{position:relative;width:100%;aspect-ratio:480/320;max-height:320px;margin:auto;}
.geometry-canvas svg{width:100%;height:100%;overflow:visible;}
.geometry-canvas line{stroke:#233c54;stroke-width:2.5;}
.geometry-canvas .dashed{stroke-dasharray:5 5;stroke:#6686a4;}
.geometry-canvas .construction{stroke:#208a8a;stroke-width:3;}
.geometry-canvas .poly{fill:#e4f0fa;stroke:#254969;stroke-width:2.5;}
.geometry-canvas .poly-1{fill:#ffead6;}
.geometry-canvas text{font-size:18px;fill:#152d43;text-anchor:middle;paint-order:stroke;stroke:#fff;stroke-width:5px;stroke-linejoin:round;}
.geometry-canvas .side-label{font-size:16px;}
.geometry-canvas .vertex-label{font-weight:800;}
.geometry-canvas circle{fill:#233c54;}
.geometry-canvas .mark{fill:none;stroke:#a04918;stroke-width:2;}
.geometry-canvas .point-target{fill:#e5f4ff;stroke:#2679b9;stroke-dasharray:4 3;}
.geometry-canvas .point-number{font-size:16px;}
.point-slot,.point-handle{position:absolute;transform:translate(-50%,-50%);width:48px;height:48px;min-width:48px;border-radius:50%;z-index:2;touch-action:none;padding:0;}
.point-slot{border:2px dashed #2680b9;background:#eef9ff;color:#1a507c;font-size:18px;}
.point-handle{background:#2079c4!important;color:white!important;border:3px solid white!important;box-shadow:0 2px 8px #0005;z-index:4;cursor:grab;}
.correct-point{background:#c7f5db;border-color:#188252;}
.math-emblem{font-size:48px;text-align:center;color:#265982;padding:50px 12px;background:#eff5fb;border-radius:12px;font-weight:900;}
.math-emblem span{font-size:19px;font-weight:500;}
.action-panel{padding:12px;background:#eff5fb;border-radius:12px;min-width:0;}
.action-panel h3{margin:0 0 10px;font-size:22px;}
.answer-slot{width:100%;min-height:62px;font-size:19px;margin:0 0 12px;background:#fff9e8;}
.correct-answer{border:2px solid #218257;background:#dff6e9!important;color:#166039;}
.term-cards{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:8px;margin:0;}
.term-card{min-width:0;width:100%;font-size:18px;line-height:1.4;overflow-wrap:anywhere;white-space:normal;min-height:58px;}
.phase-feedback{font-size:15px;line-height:1.45;min-height:42px;margin:12px 0 0;}.phase-feedback.success{color:#15673f;font-weight:700;}
.point-instruction{font-size:18px;line-height:1.6;}
.solution-stage{margin:8px 0;font-size:14px;}
#playNotice{font-size:17px;line-height:1.5;min-height:26px;margin:8px 0;}
.field-row{flex-wrap:wrap;gap:8px;}
#checkAttack{min-width:120px;}
.btn,input,select{min-height:48px;font-size:16px;}
@media(max-width:650px){.workbench{grid-template-columns:1fr;gap:4px;}.geometry-canvas{max-width:400px;}.math-emblem{display:none;}.action-panel{padding:10px;}.problem{font-size:17px!important;}.term-card{font-size:16px;padding:7px 4px;}.panel{padding:12px;}.battle-active .app{padding-top:112px!important;}.battle-active .fight-stage{height:104px;}.step-trail{font-size:12px;}.action-panel h3{font-size:19px;}}
@media(min-width:651px) and (max-height:850px){.workbench{grid-template-columns:minmax(0,1.05fr) minmax(0,1fr);}.geometry-canvas{max-height:260px;}.problem{font-size:18px!important;}.panel{padding:14px;}.step-trail{margin:5px 0;}}
@media(prefers-reduced-motion:reduce){*,*::before,*::after{animation:none!important;transition:none!important;}}

.identity-slots .identity-cell.filled{white-space:nowrap;flex:1 0 auto;width:auto;min-width:max-content;font-size:14px;padding:6px;}

.geometry-canvas{max-height:none;max-width:480px;}
@media(min-width:651px) and (max-height:850px){.geometry-canvas{max-height:none;max-width:390px;}}

table{border-collapse:collapse;width:100%;margin:14px 0;}th,td{padding:10px;text-align:left;border-bottom:1px solid #d8e2ef;}details{margin:10px 0;}summary{cursor:pointer;min-height:48px;padding:12px;background:#eaf2fb;border-radius:8px;}

.point-handle{position:static;transform:none;width:64px;height:64px;display:block;margin:8px auto;}

@media(max-width:650px){.geometry-canvas{max-width:290px;}.point-instruction{font-size:16px;margin:6px 0;}.phase-feedback{min-height:24px;margin-top:6px;}.action-panel h3{margin-top:0!important;}.point-handle{width:56px;height:56px;margin:4px auto;}}
