/* ═══════════════════════════════════════════════════
   線對稱實驗室 · 系列共用樣式
   配色：主色 #14427a、輔色 #2a6cb6、強調色 #e74c3c
   設計：鏡像/摺紙視覺語彙、iPad 觸控優先
   ═══════════════════════════════════════════════════ */

:root {
  --primary: #14427a;
  --primary-light: #2a6cb6;
  --primary-bg: #eef4fc;
  --card-bg: #ffffff;
  --card-border: #d6deea;
  --text: #1a1a2e;
  --text-muted: #5a6170;
  --success: #1b8a4a;
  --danger: #d94a4a;
  --warning: #e8a020;
  --gold: #c89020;
  --disabled-bg: #e8ecf2;
  --disabled-text: #99a2b0;
  --shadow: 0 12px 32px rgba(20,66,122,.10);
  --radius: 12px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Noto Sans TC", -apple-system, "Microsoft JhengHei", sans-serif;
  background: linear-gradient(180deg, #eef5ff 0%, #f6f9fd 100%);
  color: var(--text);
  min-height: 100vh;
  /* 防止拖曳對稱軸/點格時把文字或圖形選取反白(iPad/桌機) */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

a { color: inherit; }

button {
  font: inherit;
  min-height: 48px;               /* iPad 觸控最小 48px */
  min-width: 48px;
  border: 2px solid var(--card-border);
  background: var(--card-bg);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 18px;
  font-weight: 700;
  cursor: pointer;
  transition: all .15s;
  touch-action: manipulation;
}
button:hover { border-color: var(--primary); background: var(--primary-bg); }
button:active { transform: scale(.97); }
button:disabled {
  opacity: .45;
  cursor: not-allowed;
  pointer-events: none;
}

button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
button.primary:hover { background: var(--primary-light); border-color: var(--primary-light); }

button.danger {
  color: var(--danger);
  border-color: #f0c0c0;
}
button.danger:hover { border-color: var(--danger); background: #fef2f2; }

button.small { min-height: 44px; padding: 4px 12px; font-size: .85em; }

/* ═══ 系列入口頁 ═══ */
.series-index {
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 20px 60px;
}
.series-index.lobby-index {
  max-width: none;
  padding: 0;
  background: #071426;
  color: #f6fbff;
  overflow-x: hidden;
}
.mirror-bg, .mirror-grid {
  position: fixed;
  inset: 0;
  pointer-events: none;
}
.mirror-bg {
  z-index: -2;
  background:
    radial-gradient(circle at 18% 12%, rgba(78, 168, 255, .26), transparent 34%),
    radial-gradient(circle at 82% 22%, rgba(255, 184, 77, .18), transparent 32%),
    linear-gradient(180deg, #071426 0%, #12345c 55%, #eaf3ff 100%);
}
.mirror-grid {
  z-index: -1;
  background-image:
    linear-gradient(rgba(255,255,255,.07) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.07) 1px, transparent 1px);
  background-size: 42px 42px;
  mask-image: linear-gradient(180deg, #000 0%, rgba(0,0,0,.75) 46%, transparent 100%);
  -webkit-mask-image: linear-gradient(180deg, #000 0%, rgba(0,0,0,.75) 46%, transparent 100%);
}
.lobby-shell {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 18px 22px 56px;
}
.lobby-topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  margin-bottom: 18px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
}
.brand-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: linear-gradient(135deg, #f7fbff, #91c8ff);
  color: #0a2948;
  box-shadow: 0 4px 0 rgba(0,0,0,.25);
}
.brand strong {
  display: block;
  font-size: 1.1em;
  color: #fff;
}
.brand small {
  display: block;
  margin-top: 2px;
  color: #b9dcff;
  font-size: .68em;
  letter-spacing: .16em;
}
.teacher-link {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  border: 1px solid rgba(255,255,255,.26);
  border-radius: 999px;
  color: #e6f3ff;
  text-decoration: none;
  font-weight: 700;
}
.lobby-hero {
  padding: 18px 0 20px;
}
.hero-badge {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 5px 14px;
  border-radius: 999px;
  background: rgba(255,255,255,.12);
  color: #d8ebff;
  font-size: .86em;
  font-weight: 800;
}
.lobby-hero h1 {
  margin: 16px 0 8px;
  color: #fff;
  font-size: clamp(2.1rem, 6vw, 4.8rem);
  line-height: 1;
  letter-spacing: 0;
}
.lobby-hero p {
  max-width: 720px;
  color: #dbeeff;
  font-size: 1.05em;
  line-height: 1.7;
}
.hero-chips {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 14px;
}
.hero-chips span {
  padding: 7px 12px;
  border-radius: 999px;
  background: rgba(255,255,255,.12);
  color: #fff;
  font-size: .86em;
  font-weight: 800;
}
.cta-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin: 12px 0 18px;
}
.cta-card {
  position: relative;
  min-height: 168px;
  display: block;
  padding: 22px 24px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,.14);
  background: rgba(255,255,255,.94);
  color: #153152;
  text-decoration: none;
  box-shadow: 0 8px 0 rgba(0,0,0,.18), 0 18px 38px rgba(0,0,0,.22);
}
.cta-card:hover {
  transform: translateY(-2px);
  border-color: #67a9ef;
}
.cta-card.primary-card {
  background: linear-gradient(180deg, #ffffff 0%, #e8f4ff 100%);
}
.cta-card.demo-card {
  background: linear-gradient(180deg, #fffaf0 0%, #eef7ff 100%);
}
.cta-icon {
  display: block;
  font-size: 2rem;
  margin-bottom: 8px;
}
.cta-card h2 {
  margin: 0 32px 6px 0;
  color: #143f70;
  font-size: 1.18em;
}
.cta-card p {
  color: #526173;
  font-size: .92em;
  line-height: 1.55;
}
.cta-arrow {
  position: absolute;
  right: 20px;
  top: 20px;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #14427a;
  color: #fff;
  font-weight: 900;
}
.home-qr-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 22px;
  align-items: center;
  margin: 18px 0;
  padding: 20px;
  border-radius: 8px;
  background: rgba(255,255,255,.96);
  color: #153152;
  box-shadow: 0 8px 28px rgba(0,0,0,.16);
}
.home-qr-text h2 {
  margin: 0 0 8px;
  color: #143f70;
}
.home-qr-text p {
  color: #526173;
}
.home-qr-url {
  margin-top: 8px;
  color: #68788d;
  font-size: .82em;
  word-break: break-all;
}
.home-qr-canvas {
  width: 178px;
  min-height: 178px;
  display: grid;
  place-items: center;
  border: 2px solid #d6e2ef;
  border-radius: 8px;
  background: #fff;
}
.home-qr-canvas svg {
  width: 158px;
  height: 158px;
  display: block;
}
.rules-paper {
  margin-top: 18px;
  padding: 22px 26px;
  border-radius: 8px;
  background: #fff8e6;
  color: #20334a;
  border: 1px solid #ecd89d;
}
.rules-paper h2 {
  color: #143f70;
  margin-bottom: 10px;
}
.rules-paper ol {
  padding-left: 22px;
  line-height: 1.8;
}
.rules-paper li {
  margin-bottom: 4px;
}
.series-index h1 {
  font-size: 1.8em;
  color: var(--primary);
  margin-bottom: 4px;
}
.series-index .subtitle {
  color: var(--text-muted);
  margin-bottom: 28px;
  font-size: .95em;
}

.mode-cards {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.mode-card {
  display: block;
  text-decoration: none;
  background: var(--card-bg);
  border: 2px solid var(--card-border);
  border-radius: var(--radius);
  padding: 20px 24px;
  color: var(--text);
  transition: all .2s;
}
.mode-card:hover { border-color: var(--primary); box-shadow: var(--shadow); }
.mode-card.disabled {
  opacity: .55;
  cursor: not-allowed;
  pointer-events: none;
  background: var(--disabled-bg);
  color: var(--disabled-text);
}
.mode-card .icon { font-size: 2em; margin-bottom: 4px; display: block; }
.mode-card h2 { font-size: 1.2em; margin-bottom: 4px; color: var(--primary); }
.mode-card p { font-size: .9em; color: var(--text-muted); }
.mode-card .badge {
  display: inline-block;
  margin-top: 8px;
  background: var(--primary-bg);
  color: var(--primary);
  padding: 2px 10px;
  border-radius: 20px;
  font-size: .78em;
  font-weight: 700;
}
.mode-card.disabled .badge {
  background: var(--disabled-bg);
  color: var(--disabled-text);
}

/* ═══ 單人模式共用 ═══ */
.solo-app {
  max-width: 720px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           max(40px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

/* 頂部狀態列 */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 14px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  margin-bottom: 18px;
}
.topbar .stage-progress {
  font-weight: 700;
  color: var(--primary);
  font-size: .9em;
  white-space: nowrap;
}
.topbar .score-display {
  margin-left: auto;
  font-weight: 700;
  font-size: .9em;
}
.topbar .score-display .total { color: var(--gold); font-size: 1.15em; }

/* 規則說明卡 */
.rules-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 18px;
  position: relative;
}
.rules-card h3 {
  font-size: 1.05em;
  color: var(--primary);
  margin-bottom: 10px;
}
/* 用絕對定位做編號，避免 CSS Grid 吃光 inline 文字造成逐字換行 */
.rules-card ol {
  list-style: none;
  counter-reset: rule;
  padding: 0;
}
.rules-card ol > li {
  counter-increment: rule;
  position: relative;
  padding-left: 32px;
  margin-bottom: 6px;
  font-size: .92em;
  line-height: 1.5;
}
.rules-card ol > li::before {
  content: counter(rule);
  position: absolute;
  left: 0;
  top: 2px;
  width: 22px;
  height: 22px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  font-size: .78em;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ═══ 排行榜 / 玩家名稱 ═══ */
.leaderboard-card { margin-top: 14px; }
.leaderboard-head {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.leaderboard-head h3 { margin-right: auto; }
.player-name-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 150px;
  color: var(--primary);
  font-size: .84em;
  font-weight: 700;
}
.player-name-field input {
  min-height: 42px;
  border: 2px solid var(--card-border);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
}
.player-name-field input:focus { outline: none; border-color: var(--primary); }
.leaderboard-list {
  display: grid;
  gap: 8px;
  color: var(--text-muted);
  font-size: .9em;
}
.leaderboard-row {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 9px 10px;
  border: 1px solid var(--card-border);
  border-radius: 8px;
  background: #fbfdff;
}
.leaderboard-row strong { color: var(--primary); }
.leaderboard-row small { display: block; color: var(--text-muted); margin-top: 2px; }
.leaderboard-rank {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--primary-bg);
  color: var(--primary);
  font-weight: 900;
}
.leaderboard-score {
  color: var(--gold);
  font-weight: 900;
  white-space: nowrap;
}

/* ═══ 畫布區（SVG）═══ */
.canvas-wrap {
  position: relative;
  background: var(--card-bg);
  border: 2px solid var(--card-border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 12px;
  touch-action: none;            /* 防止拖曳時捲動頁面 */
}
.canvas-wrap svg {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;   /* 觸控拖曳對稱軸時不捲動頁面 */
}

/* ═══ 控制列 ═══ */
.controls-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 12px;
}
.controls-row.center { justify-content: center; }

/* ═══ 判斷按鈕（關卡 1）═══ */
.judge-btns {
  display: flex;
  gap: 16px;
  justify-content: center;
  flex-wrap: wrap;
}
.judge-btns button {
  font-size: 1.3em;
  padding: 14px 40px;
  flex: 0 1 200px;
}

/* ═══ 回饋面板 ═══ */
.feedback-banner {
  padding: 12px 16px;
  border-radius: 10px;
  margin-bottom: 12px;
  font-weight: 700;
  font-size: .95em;
  text-align: center;
  animation: fadeIn .3s ease;
}
.feedback-banner.correct { background: #e6f7ee; color: var(--success); border: 1px solid #a3e4bf; }
.feedback-banner.wrong { background: #fef2f2; color: var(--danger); border: 1px solid #f5c6c6; }
.feedback-banner.info { background: var(--primary-bg); color: var(--primary); border: 1px solid #bdd4f0; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }

/* ═══ 關卡 3 點選熱區 ═══ */
.clickable-vertex, .clickable-edge, .clickable-angle {
  cursor: pointer;
  transition: opacity .15s;
  touch-action: manipulation;
}
.clickable-vertex:hover, .clickable-edge:hover { opacity: .7; }

/* ═══ 關卡 4 格點 ── */
/* 注意:stroke-width 是 SVG 使用者單位(格子約 1.25 單位),要用很小的值 */
.grid-cell {
  cursor: pointer;
  transition: fill .15s;
  touch-action: manipulation;
  stroke: #ccd6e4;
  stroke-width: 0.04;
}
.grid-cell:hover { stroke: var(--primary); stroke-width: 0.1; }
.grid-cell.empty { fill: #f8f9fb; }
.grid-cell.pre-filled { fill: var(--primary-bg); }
.grid-cell.student-filled { fill: #b8d4f8; }
.grid-cell.correct-overlay { fill: #7ce0a0; }
.grid-cell.missing { fill: #fff3bf; stroke: var(--warning); stroke-width: 0.1; }
.grid-cell.wrong-cell { fill: #fcc; stroke: var(--danger); stroke-width: 0.1; }

/* ═══ 摺紙動畫用 ── */
.reflect-ghost {
  fill: var(--danger);
  fill-opacity: 0.35;
  stroke: var(--danger);
  stroke-opacity: 0.7;
  stroke-width: 1.5;
  stroke-dasharray: 6 3;
  animation: ghostPulse .8s ease-in-out;
}
.reflect-match {
  fill: var(--success);
  fill-opacity: 0.2;
  stroke: var(--success);
  stroke-opacity: 0.6;
  stroke-width: 2;
  animation: matchFlash .6s ease;
}

@keyframes ghostPulse {
  0% { opacity: 0; transform: scale(0.9); }
  40% { opacity: 1; }
  100% { opacity: .7; transform: scale(1); }
}
@keyframes matchFlash {
  0% { fill-opacity: 0; stroke-opacity: 0; }
  60% { fill-opacity: .4; stroke-opacity: 1; }
  100% { fill-opacity: .2; stroke-opacity: .6; }
}

/* ═══ 總結畫面 ── */
.summary-panel {
  background: var(--card-bg);
  border: 2px solid var(--card-border);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
}
.summary-panel h2 { color: var(--primary); margin-bottom: 16px; }
.summary-panel table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
  font-size: .9em;
}
.summary-panel th, .summary-panel td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--card-border);
  text-align: center;
}
.summary-panel th { color: var(--text-muted); font-weight: 500; }
.summary-panel .final-score { font-size: 2em; font-weight: 900; color: var(--gold); margin: 12px 0; }

/* ═══ RWD ═══ */
@media (max-width: 520px) {
  .lobby-shell { padding: 14px 14px 40px; }
  .lobby-topbar { align-items: flex-start; }
  .teacher-link { font-size: .82em; padding: 6px 10px; }
  .cta-grid { grid-template-columns: 1fr; }
  .cta-card { min-height: 142px; padding: 18px; }
  .home-qr-card { grid-template-columns: 1fr; }
  .home-qr-canvas { width: 100%; }
  .series-index h1 { font-size: 1.35em; }
  .topbar { font-size: .85em; padding: 8px 10px; gap: 6px; }
  .rules-card ol > li { font-size: .85em; }
  .judge-btns button { font-size: 1.1em; padding: 12px 28px; }
}

/* ═══ 平板（iPad,主力裝置）優化 ═══ */
@media (min-width: 700px) {
  /* 放大整體字級(連帶放大所有 em 字,含題目/規則行內樣式),手臂距離更好讀 */
  body { font-size: 18px; }
  .solo-app { max-width: 880px; }
  .series-index { max-width: 820px; }
  .series-index.lobby-index { max-width: none; }

  .topbar { padding: 14px 22px; gap: 14px; }
  .rules-card { padding: 22px 28px; }
  .rules-card ol > li { padding-left: 38px; }
  .rules-card ol > li::before { width: 26px; height: 26px; }

  /* 觸控按鈕加大、加寬間距 */
  button { min-height: 58px; padding: 12px 28px; }
  button.small { min-height: 50px; }
  .controls-row { gap: 16px; }
  .judge-btns { gap: 28px; }
  .judge-btns button { padding: 22px 70px; }

  /* 畫布置中且限寬,避免在大螢幕被拉到過大 */
  .canvas-wrap { max-width: 620px; margin-left: auto; margin-right: auto; }
}

@media (min-width: 900px) {
  .lobby-shell { padding-left: 30px; padding-right: 30px; }
  .lobby-hero { padding-top: 26px; }
  .cta-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .cta-card { min-height: 182px; padding: 18px 18px; }
  .cta-card h2 { font-size: 1.02em; }
  .cta-card p { font-size: .84em; }
}

@media (min-width: 700px) and (max-height: 820px) {
  .lobby-shell { padding-top: 12px; }
  .lobby-hero { padding: 8px 0 12px; }
  .lobby-hero h1 { font-size: clamp(2rem, 5vw, 3.4rem); }
  .cta-card { min-height: 156px; padding: 16px 16px; }
  .cta-icon { font-size: 1.6rem; margin-bottom: 5px; }
  .home-qr-card { padding: 16px 18px; }
  .home-qr-canvas { width: 148px; min-height: 148px; }
  .home-qr-canvas svg { width: 132px; height: 132px; }
}

/* ═══ 平板橫放:雙欄(畫布左、規則右),壓低高度讓整局不必捲動 ═══ */
@media (min-width: 700px) and (orientation: landscape) {
  .solo-app {
    max-width: 1100px;
    padding-top: max(10px, env(safe-area-inset-top));
    display: grid;
    grid-template-columns: 1fr 300px;
    grid-template-areas: "top  top" "main rules";
    column-gap: 22px;
    align-items: start;
  }
  .topbar { grid-area: top; margin-bottom: 12px; }
  #mainContent { grid-area: main; min-width: 0; }
  .rules-card { grid-area: rules; margin-bottom: 0; padding: 16px 20px; position: sticky; top: 10px; }
  .rules-card ol > li { font-size: .98em; }

  /* 畫布以視窗高度為限,置中於左欄 */
  .canvas-wrap { max-width: min(520px, 58vh); margin-bottom: 8px; }

  /* 身分頁/教師頁為全幅,不套雙欄 */
  .solo-app:has(.identity-gate), .solo-app:has(.teacher-view) {
    display: block; max-width: 880px;
  }
}

/* ═══ 身分輸入頁 ═══ */
.identity-gate { display: flex; justify-content: center; padding: 24px 0; }
.id-card {
  background: var(--card-bg); border: 2px solid var(--card-border);
  border-radius: var(--radius); padding: 30px 28px; max-width: 440px; width: 100%; text-align: center;
}
.id-card h2 { color: var(--primary); margin-bottom: 8px; }
.id-card > p { color: var(--text-muted); font-size: .95em; margin-bottom: 20px; }
.id-fields { display: flex; gap: 16px; justify-content: center; margin-bottom: 20px; }
.id-fields label { display: flex; flex-direction: column; gap: 6px; font-weight: 700; color: var(--primary); font-size: .9em; }
.id-fields input {
  width: 130px; font: inherit; font-size: 1.4em; text-align: center;
  padding: 12px; border: 2px solid var(--card-border); border-radius: 10px; min-height: 54px;
}
.id-fields input:focus { border-color: var(--primary); outline: none; }
.id-card button.primary { width: 100%; font-size: 1.15em; }
.id-err { color: var(--danger); margin-top: 12px; font-weight: 700; min-height: 1.2em; }

/* ═══ 教師檢視頁 ═══ */
.teacher-view h2 { color: var(--primary); margin-bottom: 14px; }
.t-controls { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.t-controls select { font: inherit; min-height: 44px; padding: 6px 10px; border: 2px solid var(--card-border); border-radius: 8px; }
.t-table { width: 100%; border-collapse: collapse; background: var(--card-bg); border-radius: var(--radius); overflow: hidden; }
.t-table th, .t-table td { padding: 10px 12px; border-bottom: 1px solid var(--card-border); text-align: center; }
.t-table th { background: var(--primary-bg); color: var(--primary); font-size: .9em; }
.t-table tr:hover td { background: #f3f8ff; }
.t-sum { margin-top: 10px; color: var(--text-muted); font-size: .9em; }
