/* 字型：所有平台（電腦、iPad、手機）都用網站自帶的「自由香港楷書」，不用裝置內建字型，顯示完全一樣。
   自由香港楷書依《香港小學學習字詞表》的書寫要求製作（CC BY 4.0，freehkfonts.opensource.hk）。
   它未收錄的字（例如「暱」）由網上字型「霞鶩文楷 TC」補上（同樣是楷書，各平台也一樣）。 */
@import url('https://fonts.googleapis.com/css2?family=LXGW+WenKai+TC:wght@400;700&display=swap');
@font-face {
  font-family: "Free HK Kai";
  src: url("../fonts/Free-HK-Kai.woff2") format("woff2");
  font-display: swap;
}

:root {
  --font: "Free HK Kai", "LXGW WenKai TC", serif;
  --bg: #fff7e8;
  --surface: #ffffff;
  --text: #2b2a33;
  --muted: #7a7684;
  --line: #ece4d4;
  --primary: #3d7cff;
  --game-color: #3d7cff;
  --ok: #22a95b;
  --ok-bg: #dcf7e6;
  --bad: #e5484d;
  --bad-bg: #fde3e3;
  --warn: #e5484d;
  --shadow: 0 4px 0 rgba(0, 0, 0, .08), 0 8px 24px rgba(0, 0, 0, .06);
  --radius: 20px;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
body.playing { overflow: hidden; -webkit-user-select: none; user-select: none; }

#app { min-height: 100%; }

.screen {
  max-width: 960px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right))
           max(24px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

h1 { font-size: clamp(28px, 5vw, 40px); margin: 0; }
h2 { font-size: 22px; margin: 12px 0 0; }
.muted { color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 16px; margin: 0; }

.back-link { color: var(--muted); text-decoration: none; font-size: 18px; align-self: flex-start; padding: 6px 0; }

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  font: inherit; font-weight: 700;
  padding: 14px 24px; border-radius: 16px; border: 0;
  text-decoration: none; cursor: pointer;
  background: var(--surface); color: var(--text);
  box-shadow: var(--shadow);
  transition: transform .1s;
}
.btn:active { transform: translateY(2px); }
.btn.primary { background: var(--game-color); color: #fff; }
.btn.danger { background: var(--bad); color: #fff; }
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 2px var(--line); }
.btn.big { font-size: 24px; padding: 18px 28px; }
.btn.small { font-size: 15px; padding: 6px 12px; border-radius: 10px; }
.btn:disabled { opacity: .5; }
.next-btn { display: flex; margin: 16px auto 0; min-width: 200px; }

.chip {
  font: inherit; cursor: pointer;
  display: flex; flex-direction: column; align-items: center;
  padding: 12px 18px; border-radius: 16px;
  border: 3px solid var(--line); background: var(--surface); color: var(--text);
}
.chip small { color: var(--muted); font-size: 14px; }
.chip.on { border-color: var(--game-color); background: color-mix(in srgb, var(--game-color) 12%, white); }

/* ---------- 首頁 ---------- */
.home-header { text-align: center; padding: 16px 0 4px; }
.home-header p { margin: 4px 0 0; color: var(--muted); font-size: 20px; }
.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}
.game-card {
  display: flex; flex-direction: column; gap: 6px;
  padding: 24px; border-radius: 24px;
  background: var(--surface); color: var(--text); text-decoration: none;
  box-shadow: var(--shadow);
  border-top: 10px solid var(--card);
  transition: transform .1s;
}
.game-card:active { transform: scale(.98); }
.card-icon { font-size: 56px; line-height: 1; }
.card-title { font-size: 24px; font-weight: 800; }
.card-desc { color: var(--muted); }
/* 遊戲開始畫面：顯示由哪個科目和年級進入 */
.grade-tag {
  align-self: center; margin: 0; padding: 6px 18px; border-radius: 999px;
  background: color-mix(in srgb, var(--game-color) 14%, white); color: var(--game-color);
  font-weight: 900; font-size: 20px;
}

/* 主頁選單：科目、年級的大卡片和目前位置 */
.menu-card { align-items: center; text-align: center; padding: 32px 24px; }
.menu-card .card-icon {
  width: 96px; height: 96px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--card) 14%, white);
  color: var(--card); font-size: 56px; font-weight: 900;
}
.menu-card .card-title { font-size: 30px; }
.crumbs { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; color: var(--muted); font-size: 17px; }
.crumbs:empty { display: none; }
.crumbs a { color: var(--muted); }
.crumbs span:last-child { color: var(--text); font-weight: 700; }
.crumbs .sep { color: var(--line); }
.home-footer { display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; padding-top: 12px; }
.home-footer a { color: var(--text); font-weight: 700; text-decoration: none; }
.home-footer a.muted { color: var(--muted); font-weight: 400; }

/* ---------- 開始／結算 ---------- */
.setup, .result { max-width: 560px; }
.hero { text-align: center; }
.hero-icon { font-size: 72px; line-height: 1.1; }
.hero p { color: var(--muted); margin: 6px 0 0; font-size: 20px; }
.board-pick { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.board-pick .chip b { font-size: 22px; }
.name-input {
  font: inherit; font-size: 26px; text-align: center;
  padding: 14px; border-radius: 16px; border: 3px solid var(--line);
  background: var(--surface); color: var(--text); width: 100%;
}
.name-input:focus { outline: none; border-color: var(--game-color); }
.form-error { color: var(--bad); min-height: 1.5em; margin: 0; text-align: center; font-weight: 700; }
.final-score { font-size: clamp(48px, 12vw, 80px); font-weight: 900; color: var(--game-color); line-height: 1.2; }
.submit-status { font-weight: 700; }
.actions { display: flex; flex-direction: column; gap: 10px; }

/* ---------- 排行榜 ---------- */
.board-wrap { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; }
.rank-list { list-style: none; margin: 0; padding: 0; }
.rank-list li {
  display: grid; grid-template-columns: 48px 1fr auto; align-items: center; gap: 8px;
  padding: 10px 12px; border-radius: 12px; font-size: 20px;
}
.rank-list li + li { border-top: 1px solid var(--line); }
.rank-list li.me { background: color-mix(in srgb, var(--game-color) 18%, white); font-weight: 800; }
.rank-list .rank { font-size: 24px; text-align: center; font-weight: 800; }
.rank-list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-list .score { font-weight: 800; font-variant-numeric: tabular-nums; }
.tabs { display: flex; flex-wrap: wrap; gap: 8px; }
.tabs:empty { display: none; }
.tabs .chip { flex-direction: row; gap: 4px; padding: 10px 16px; }

/* ---------- 遊戲中 ---------- */
.play {
  max-width: none; height: 100dvh; padding-top: max(12px, env(safe-area-inset-top));
  gap: 12px;
}
.hud { display: flex; align-items: center; gap: 12px; font-size: 22px; font-weight: 800; }
.hud-quit {
  width: 48px; height: 48px; border-radius: 50%; border: 0; font-size: 22px;
  background: var(--surface); box-shadow: var(--shadow); cursor: pointer; color: var(--text);
}
.hud-status { flex: 1; color: var(--muted); }
.hud-timer, .hud-score {
  background: var(--surface); padding: 6px 16px; border-radius: 999px; box-shadow: var(--shadow);
  font-variant-numeric: tabular-nums;
}
.hud-timer:empty { display: none; }
.hud-timer.warn { color: var(--warn); }
.hud-score { color: var(--game-color); }
.stage { flex: 1; display: flex; align-items: center; justify-content: center; min-height: 0; }
/* 開始前的規則卡（中英對照）：半透明，蓋在遊戲示範畫面上 */
.stage { position: relative; }
.rules-overlay {
  position: fixed;               /* 蓋住整個螢幕：規則框由螢幕最頂到最底 */
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 0;
  background: rgba(255, 247, 232, .25);
  pointer-events: none;          /* 框外仍然可以按左上角的 ✕ 離開 */
}
.rules-overlay .rules-card { pointer-events: auto; }
@keyframes rules-pop { from { transform: scale(.9); } }
.rules-demo-tip { margin: 0; text-align: center; color: var(--muted); font-size: 14px; }
.rules-card {
  width: min(680px, 100%);
  height: 100%;                    /* 由螢幕最頂到最底 */
  overflow-y: auto;
  justify-content: space-evenly;   /* 內容平均分佈在整個高度 */
  background: rgba(255, 255, 255, .72);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  border-radius: 0;
  box-shadow: var(--shadow);
  border-top: 10px solid var(--game-color);
  padding: 20px 26px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.rules-title { margin: 0; font-size: 28px; text-align: center; }
.rules-title small { display: block; font-size: 17px; color: var(--muted); font-weight: 700; margin-top: 2px; }
.rules-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.rules-list li { display: flex; align-items: center; gap: 14px; }
.rules-icon { font-size: 30px; width: 42px; text-align: center; flex-shrink: 0; }
.rules-img { width: 38px; height: 38px; border-radius: 50%; vertical-align: middle; box-shadow: 0 0 0 2px #fff, 0 1px 4px rgba(0,0,0,.15); }
.rules-text b { display: block; font-size: 22px; line-height: 1.35; }
.rules-text small { display: block; font-size: 16px; color: var(--muted); font-family: system-ui, sans-serif; }
.rules-start { align-self: center; min-width: 240px; margin-top: 4px; }
@media (max-height: 640px) {
  .rules-text b { font-size: 19px; }
  .rules-list { gap: 6px; }
}

.count-in { font-size: 120px; font-weight: 900; color: var(--game-color); animation: pop .5s ease-out; }
@keyframes pop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* 需要特定方向時，方向不對就蓋上提示 */
.rotate-hint {
  display: none; position: fixed; inset: 0; z-index: 10;
  background: var(--bg); flex-direction: column; align-items: center; justify-content: center;
  font-size: 26px; font-weight: 800; text-align: center;
}
.rotate-icon { font-size: 80px; }
@media (orientation: portrait) {
  body[data-orientation="landscape"] .rotate-hint { display: flex; }
}
@media (orientation: landscape) {
  body[data-orientation="portrait"] .rotate-hint { display: flex; }
}

/* ---------- 管理頁 ---------- */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; display: flex; flex-direction: column; gap: 12px; max-width: 420px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
#panel { display: flex; flex-direction: column; gap: 16px; }
.card.wide { max-width: 640px; }
.card select { font: inherit; padding: 10px 12px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); color: var(--text); }
.admin-table tr.off td { color: var(--muted); text-decoration: line-through; }
.admin-table tr.off td:last-child { text-decoration: none; }
.nowrap { white-space: nowrap; }
.card label, .row label { display: flex; flex-direction: column; gap: 4px; font-weight: 700; }
.card input, .row select {
  font: inherit; padding: 10px 12px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); color: var(--text);
}
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.admin-table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.admin-table th, .admin-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); font-size: 16px; }
.admin-table th { background: #faf5ea; }
.admin-table td:last-child { text-align: right; }

/* ---------- iPad Air 4（820×1180）及更大的螢幕：字和卡片放大，填滿畫面 ---------- */
@media (min-width: 800px) and (min-height: 700px) {
  body { font-size: 20px; }
  .screen:not(.play) { max-width: 1120px; gap: 22px; }
  .screen.home { min-height: 100dvh; justify-content: center; }
  h1 { font-size: 52px; }
  h2 { font-size: 28px; }
  .back-link { font-size: 22px; }
  .home-header p { font-size: 26px; }
  .crumbs { font-size: 22px; }
  .home-footer { font-size: 24px; gap: 32px; }
  .game-grid { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 28px; }
  .game-card { padding: 36px 28px; gap: 12px; min-height: 300px; justify-content: center; }
  .card-icon { font-size: 84px; }
  .card-title { font-size: 34px; }
  .card-desc { font-size: 21px; }
  .menu-card .card-icon { width: 140px; height: 140px; font-size: 84px; }
  .menu-card .card-title { font-size: 42px; }
  .setup, .result { max-width: 720px; }
  .hero-icon { font-size: 110px; }
  .hero p { font-size: 24px; }
  .grade-tag { font-size: 24px; padding: 8px 24px; }
  .name-input { font-size: 34px; padding: 18px; }
  .btn { padding: 18px 28px; }
  .btn.big { font-size: 32px; padding: 24px 32px; }
  .final-score { font-size: 110px; }
  .rank-list li { font-size: 26px; padding: 14px 16px; grid-template-columns: 64px 1fr auto; }
  .rank-list .rank { font-size: 30px; }
  .tabs .chip { font-size: 22px; padding: 12px 20px; }
  .hud { font-size: 26px; }
  .rules-title { font-size: 36px; }
  .rules-text b { font-size: 26px; }
  .rules-text small { font-size: 18px; }
  .rules-icon { font-size: 38px; width: 52px; }
}
/* iPad 直放：遊戲卡片一行一張，圖示在左，字在右 */
@media (min-width: 800px) and (min-height: 700px) and (orientation: portrait) {
  .game-grid { grid-template-columns: 1fr; }
  .game-card:not(.menu-card) {
    min-height: 200px;
    display: grid; grid-template-columns: auto 1fr; align-content: center; column-gap: 32px;
  }
  .game-card:not(.menu-card) .card-icon { grid-row: span 2; font-size: 110px; align-self: center; }
  .game-card:not(.menu-card) .card-title { font-size: 40px; align-self: end; }
  .game-card:not(.menu-card) .card-desc { font-size: 24px; }
}

/* ---------- 手機橫放（例如 iPhone 844×390）：頂欄和規則卡縮細，讓出空間給遊戲 ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  .play {
    padding: max(6px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
             max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    gap: 6px;
  }
  .hud { font-size: 17px; gap: 8px; }
  .hud-quit { width: 36px; height: 36px; font-size: 18px; }
  .hud-timer, .hud-score { padding: 3px 12px; }
  .rules-card { width: min(820px, 100%); padding: 10px 18px 12px; gap: 8px; border-top-width: 6px; }
  .rules-title { font-size: 22px; }
  .rules-title small { font-size: 14px; }
  .rules-list { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 18px; }
  .rules-list li { gap: 8px; }
  .rules-icon { font-size: 22px; width: 30px; }
  .rules-img { width: 28px; height: 28px; }
  .rules-text b { font-size: 16px; line-height: 1.3; }
  .rules-text small { font-size: 12px; }
  .rules-start { font-size: 20px; padding: 10px 24px; }
  .rules-demo-tip { font-size: 12px; }
}

/* ---------- 大電腦螢幕（16:9，闊 1400 以上）：右邊欄和字再大一點 ---------- */
@media (min-width: 1400px) and (min-height: 700px) {
  .hud { font-size: 28px; }
  .hud-quit { width: 56px; height: 56px; font-size: 26px; }
}
/* 手機直放：頂欄縮細 */
@media (orientation: portrait) and (max-width: 600px) {
  .play {
    padding: max(6px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
             max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    gap: 6px;
  }
  .hud { font-size: 17px; gap: 8px; }
  .hud-quit { width: 36px; height: 36px; font-size: 18px; }
  .hud-timer, .hud-score { padding: 3px 12px; }
}
/* 手機橫放：頂欄（✕、題數、分數）不另佔一行，改為浮在右邊欄頂部，遊戲框用盡整個高度 */
@media (orientation: landscape) and (max-height: 500px) {
  .play { position: relative; }
  .hud {
    position: absolute; z-index: 5;
    top: max(6px, env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right));
    width: var(--side-w, 210px); gap: 6px;
  }
  .hud-status { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
  .play .side, .play .bw-side { padding-top: 44px; }
}

/* ---------- 題目旁的配圖（表情符號）：半透明、大一點，幫助理解詞語意思 ---------- */
.q-card, .bw-card { position: relative; }
.q-pic, .bw-pic {
  position: absolute; top: 4px; right: 6px;
  font-size: 76px; line-height: 1; opacity: .45;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
  pointer-events: none; user-select: none;
}
.q-pic:empty, .bw-pic:empty { display: none; }
.q-label, .q-text, .q-feedback, .bw-step, .bw-target, .bw-word, .bw-timer, .bw-scores { position: relative; z-index: 1; }
@media (min-width: 1400px) and (min-height: 700px) { .q-pic, .bw-pic { font-size: 96px; } }
@media (orientation: landscape) and (max-height: 500px) { .q-pic, .bw-pic { font-size: 48px; } }
@media (orientation: portrait) and (max-width: 600px) { .bw-pic { font-size: 52px; } }

/* 表情符號圖片（js/emoji.js 換上的 Twemoji）：大小跟文字一樣 */
img.emoji { width: 1em; height: 1em; vertical-align: -0.12em; display: inline-block; }
.credits { margin: 4px 0 0; text-align: center; font-size: 13px; color: var(--muted); }
.credits a { color: var(--muted); }
