:root {
  --bg: #eef2f7;
  --card: #ffffff;
  --ink: #16243a;
  --muted: #5b6b82;
  --line: #d3dceb;
  --teal: #0e7490;
  --teal-d: #155e75;
  --amber: #b45309;
  --host: #7c3aed;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
.hidden { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans TC", "PingFang TC", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
.wrap {
  max-width: 1000px;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) clamp(12px, 3vw, 28px)
           calc(60px + env(safe-area-inset-bottom));
}

/* 頂部列 */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 0 16px;
}
.back-link { color: var(--muted); text-decoration: none; font-weight: 700; font-size: .9rem; white-space: nowrap; }
.back-link:hover { color: var(--teal); }
.brand { flex: 1; }
.brand-title { font-size: clamp(1.1rem, 3.4vw, 1.5rem); font-weight: 900; color: var(--teal-d); }
.brand-sub { color: var(--muted); font-size: .85rem; }
.version-badge {
  background: var(--teal-d); color: #fff; font-weight: 800;
  padding: 6px 12px; border-radius: 999px; font-size: .8rem; white-space: nowrap;
}

/* 卡片 / 面板 */
.card, .panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 10px 28px rgba(22, 36, 58, .06);
}
.card h2 { margin: 0 0 10px; font-size: 1.1rem; }
.panel { margin-top: 12px; }
.notice { color: var(--muted); font-size: .9rem; margin: 8px 0; }
.notice.center { text-align: center; }
h3 { margin: 6px 0 12px; color: var(--teal-d); }

/* 首頁排版 */
.home-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.home-grid .host-card { grid-column: 1 / -1; }
@media (max-width: 720px) { .home-grid { grid-template-columns: 1fr; } }

/* 表單 */
.field-row { display: flex; gap: 8px; flex-wrap: wrap; }
.field-row input {
  flex: 1; min-width: 0;
  padding: 12px 14px; font-size: 1rem;
  border: 2px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
}
.field-row input:focus { outline: none; border-color: var(--teal); }
.big-field input { font-size: 1.4rem; text-align: center; font-weight: 800; padding: 16px; }

/* 按鈕 */
.btn {
  border: none; border-radius: 10px; padding: 12px 18px;
  font-weight: 800; font-size: 1rem; cursor: pointer;
  background: #e7edf5; color: var(--ink); transition: transform .05s, filter .15s;
}
.btn:hover { filter: brightness(.97); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--teal); color: #fff; }
.btn.host { background: var(--host); color: #fff; }
.btn.ghost { background: transparent; border: 2px solid var(--line); color: var(--muted); }
.btn.small { padding: 7px 12px; font-size: .85rem; }
.btn.big { font-size: 1.15rem; padding: 16px 26px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* QR */
.qr-box { display: inline-grid; place-items: center; background: #fff; padding: 8px; border-radius: 10px; border: 1px solid var(--line); }
.qr-box.big { padding: 12px; }
.qr-url { font-size: .75rem; color: var(--muted); word-break: break-all; margin-top: 6px; }
.home-qr { margin-top: 14px; text-align: center; }
.home-qr-title { font-weight: 700; font-size: .85rem; color: var(--muted); margin-bottom: 6px; }

/* 課堂列表 */
.session-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.session-pill {
  display: flex; flex-direction: column; align-items: flex-start;
  border: 2px solid var(--line); background: #fff; border-radius: 10px;
  padding: 8px 14px; cursor: pointer;
}
.session-pill:hover { border-color: var(--teal); background: #ecfeff; }
.sp-code { font-weight: 900; font-size: 1.1rem; letter-spacing: 2px; color: var(--teal-d); }
.sp-meta { font-size: .75rem; color: var(--muted); }

/* 房間頂列 */
.room-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 4px 2px;
}
.phase-pill { background: var(--teal-d); color: #fff; font-weight: 800; padding: 6px 14px; border-radius: 999px; }
.room-id { font-weight: 800; color: var(--muted); }
.room-bar .btn { margin-left: auto; }

/* 倒數計時 */
.countdown {
  font-weight: 900; font-variant-numeric: tabular-nums;
  background: #ecfeff; color: var(--teal-d);
  border: 2px solid #a5f3fc; border-radius: 999px;
  padding: 4px 14px; font-size: 1.05rem; min-width: 64px; text-align: center;
}
.countdown:empty { display: none; }
.countdown.urgent { background: #fee2e2; color: #b91c1c; border-color: #fca5a5; animation: pulse .8s infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }

/* 規則說明卡 */
.rules-card {
  background: #fffbeb; border: 1px solid #fde68a; border-left: 5px solid var(--amber);
  border-radius: 10px; padding: 12px 14px; margin: 10px 0 16px;
}
.rules-title { font-weight: 900; color: var(--amber); margin-bottom: 4px; }
.rules-card ul { margin: 0; padding-left: 20px; }
.rules-card li { line-height: 1.7; font-size: .95rem; }
.rules-card li b { color: var(--teal-d); }

/* 學生大塊輸入區（iPad 友善） */
.entry { display: flex; flex-direction: column; gap: 12px; margin-top: 6px; }
.entry input {
  width: 100%; padding: 18px; font-size: 1.6rem; font-weight: 800; text-align: center;
  border: 2px solid var(--line); border-radius: 12px; background: #fff; color: var(--ink);
}
.entry input:focus { outline: none; border-color: var(--teal); }
.btn.block { width: 100%; padding: 18px; font-size: 1.25rem; }

/* 老師主控 */
.host-panel, .player-panel { margin-top: 10px; }
.host-join { text-align: center; }
.big-code {
  font-size: clamp(2.6rem, 12vw, 5rem); font-weight: 900; letter-spacing: 8px;
  color: var(--teal-d); margin: 4px 0 12px;
}
.host-actions { margin-top: 18px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.progress-num { font-size: 1.3rem; font-weight: 800; margin: 10px 0; }
.progress-num b { color: var(--teal); font-size: 1.6rem; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip { background: #eef2f7; border-radius: 999px; padding: 4px 12px; font-weight: 700; font-size: .85rem; }
.chip.done { background: #dcfce7; color: #166534; }
.chip.pending { background: #f1f5f9; color: #94a3b8; }

/* 等待 / 鎖定 */
.wait-box, .locked-box {
  text-align: center; padding: 30px 16px; font-size: 1.2rem; font-weight: 700;
  background: #f8fafc; border-radius: 12px; border: 1px dashed var(--line);
}
.locked-box b { color: var(--teal); font-size: 1.6rem; }

/* 顯示舞台：文字雲 / 矩陣 */
.display-stage {
  background: #f8fbfd; border: 1px solid var(--line); border-radius: 14px;
  padding: 18px; margin: 10px 0;
}
.display-stage.mini { padding: 12px; }

.word-cloud {
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center;
  gap: 6px 18px; min-height: 120px;
}
.cloud-word { font-weight: 900; line-height: 1; display: inline-block; }

.num-matrix { display: grid; gap: 8px; justify-content: center; }
.matrix-cell {
  display: grid; place-items: center;
  min-width: 46px; aspect-ratio: 1;
  background: #fff; border: 2px solid var(--line); border-radius: 10px;
  font-weight: 800; font-size: 1.15rem; color: var(--teal-d);
}
.display-stage.mini .matrix-cell { min-width: 34px; font-size: .95rem; }
.matrix-cell.mid { background: #fef3c7; border-color: var(--amber); color: var(--amber); box-shadow: 0 0 0 2px #fde68a; }

/* 公佈答案 */
.reveal-answer { text-align: center; margin: 6px 0 12px; }
.answer-label { color: var(--muted); font-weight: 700; }
.answer-value { font-size: clamp(3rem, 14vw, 5.5rem); font-weight: 900; color: var(--amber); line-height: 1; }
.reveal-explain p { font-size: 1rem; line-height: 1.7; }
.reveal-explain b { color: var(--teal-d); }

/* 我的成績 */
.my-score { text-align: center; margin: 14px 0; }
.my-rank { font-size: 1.2rem; font-weight: 700; }
.my-rank b { font-size: 1.8rem; }
.my-total { color: var(--muted); font-weight: 700; margin-top: 4px; }
table.my-rounds, .leaderboard table {
  width: 100%; border-collapse: collapse; margin: 8px 0;
}
table.my-rounds th, table.my-rounds td,
.leaderboard th, .leaderboard td {
  padding: 8px 10px; text-align: center; border-bottom: 1px solid var(--line);
}
table.my-rounds th, .leaderboard th { color: var(--muted); font-size: .85rem; }

/* 排行榜 */
.leaderboard { max-height: 420px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.leaderboard td.rank { font-weight: 900; }
.leaderboard td.total { font-weight: 900; color: var(--teal); }
.leaderboard tr.me { background: #ecfeff; }
.leaderboard tr.me td { font-weight: 800; }

.in-room .topbar .brand-sub { display: none; }

/* iPad / 平板：放大字級與點擊區、矩陣格與文字雲 */
@media (min-width: 700px) {
  .panel { padding: 26px; }
  .btn { padding: 14px 22px; font-size: 1.05rem; }
  .btn.big { font-size: 1.3rem; padding: 18px 30px; }
  .matrix-cell { font-size: 1.5rem; }
  .display-stage.mini .matrix-cell { font-size: 1.3rem; }
  .word-cloud { min-height: 160px; gap: 8px 24px; }
  .rules-card li { font-size: 1.05rem; }
  .entry input { font-size: 1.9rem; padding: 22px; }
  .btn.block { font-size: 1.4rem; padding: 22px; }
  .progress-num { font-size: 1.5rem; }
  .progress-num b { font-size: 1.9rem; }
}

/* 觸控裝置：所有按鈕至少 48px 高，好按 */
@media (pointer: coarse) {
  .btn { min-height: 48px; }
}
