/* 小學數學遊戲網站殼 v1：iPad 為先、可觸控、不依賴懸停 */

:root {
  --bg-top: #e7f6fc;
  --bg-bottom: #fff6eb;
  --paper: #ffffff;
  --ink: #16324f;
  --ink-soft: #3d5a73;
  --line: #d5e6ef;
  --cyan: #00b0d8;
  --cyan-deep: #0078a3;
  --forest: #2e7d4f;
  --coral: #ef6b4a;
  --coral-deep: #d35536;
  --gold: #f4c15d;
  --shadow: 0 10px 30px rgba(22, 50, 79, 0.1);
  --radius: 1.25rem;
  --touch: 3.25rem;
  --font: "PingFang TC", "Noto Sans TC", "Hiragino Sans CNS", "Microsoft JhengHei", "Source Han Sans TC", sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --game-w: 1024px;
  --game-h: 768px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
}

html {
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom)) fixed;
}

body {
  font-family: var(--font);
  color: var(--ink);
  line-height: 1.5;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

img {
  max-width: 100%;
  height: auto;
  display: block;
}

a {
  color: var(--cyan-deep);
}

button,
.btn {
  font-family: inherit;
}

.skip-link {
  position: absolute;
  left: 0.75rem;
  top: -3rem;
  z-index: 100;
  background: var(--ink);
  color: #fff;
  padding: 0.5rem 0.9rem;
  border-radius: 0.5rem;
}

.skip-link:focus {
  top: calc(var(--safe-top) + 0.5rem);
}

.page {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 1rem 1.25rem 0;
}

.page-main {
  flex: 1 1 auto;
  width: 100%;
  max-width: 68rem;
  margin: 0 auto;
}

.site-header,
.site-footer {
  width: 100%;
  max-width: 68rem;
  margin: 0 auto;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.25rem 0 0.75rem;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  text-decoration: none;
  color: inherit;
  min-height: var(--touch);
}

.brand-mark {
  width: 2.5rem;
  height: 2.5rem;
  object-fit: contain;
}

.brand-text {
  font-weight: 700;
  font-size: 1.05rem;
}

.header-nav {
  display: flex;
  gap: 0.5rem;
}

.header-link,
.text-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch);
  padding: 0 0.85rem;
  text-decoration: none;
  color: var(--cyan-deep);
  font-weight: 600;
  border-radius: 999px;
}

.header-link:active,
.text-link:active,
.btn:active {
  transform: translateY(1px);
}

.site-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.25rem;
  align-items: center;
  justify-content: center;
  padding: 1.25rem 0 calc(1rem + var(--safe-bottom));
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.site-footer a {
  min-height: 2.75rem;
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  font-weight: 600;
}

.footer-note {
  color: var(--ink-soft);
}

.btn {
  appearance: none;
  border: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch);
  min-width: var(--touch);
  padding: 0.7rem 1.4rem;
  border-radius: 999px;
  font-size: 1.15rem;
  font-weight: 700;
  text-decoration: none;
  color: #fff;
  background: var(--coral);
  box-shadow: 0 6px 0 var(--coral-deep);
  transition: filter 0.15s ease;
}

.btn:active {
  box-shadow: 0 3px 0 var(--coral-deep);
}

.btn-primary {
  background: var(--coral);
}

.btn-secondary {
  background: var(--cyan-deep);
  box-shadow: 0 6px 0 #005e80;
}

.btn-ghost {
  background: transparent;
  color: var(--cyan-deep);
  box-shadow: none;
  border: 2px solid var(--cyan-deep);
}

/* 首頁 */
.hero {
  display: grid;
  gap: 1.25rem;
  align-items: center;
  padding: 0.5rem 0 1.5rem;
}

.hero-copy h1 {
  margin: 0 0 0.6rem;
  font-size: clamp(1.8rem, 4vw, 2.7rem);
  line-height: 1.2;
}

.hero-copy p {
  margin: 0 0 1.25rem;
  font-size: 1.15rem;
  color: var(--ink-soft);
}

.hero-guide {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
  align-items: center;
  justify-items: center;
  text-align: center;
}

.tingting {
  width: min(42vw, 16rem);
  height: auto;
  object-fit: contain;
  object-position: center bottom;
}

.bubble {
  background: var(--paper);
  border-radius: var(--radius);
  padding: 0.95rem 1.1rem;
  box-shadow: var(--shadow);
  border: 2px solid var(--line);
  font-size: 1.1rem;
  position: relative;
  max-width: 28rem;
}

.hero-guide .bubble {
  text-align: center;
  max-width: 20rem;
}

.bubble::after {
  content: "";
  position: absolute;
  width: 0.85rem;
  height: 0.85rem;
  background: var(--paper);
  border-right: 2px solid var(--line);
  border-bottom: 2px solid var(--line);
  right: -0.5rem;
  top: 1.4rem;
  transform: rotate(-45deg);
}

.hero-guide .bubble::after {
  right: auto;
  left: 50%;
  top: auto;
  bottom: -0.45rem;
  transform: translateX(-50%) rotate(45deg);
}

/* 大廳 */
.lobby-intro {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 0.85rem 1rem;
  align-items: center;
  margin: 0.25rem 0 1.25rem;
}

.lobby-intro .tingting {
  width: min(28vw, 8.5rem);
}

.lobby-intro .bubble::after {
  right: auto;
  left: -0.5rem;
  transform: rotate(135deg);
}

.lobby-kicker {
  margin: 0 0 0.25rem;
  font-weight: 700;
  color: var(--cyan-deep);
}

.lobby-intro h1 {
  margin: 0;
  font-size: clamp(1.5rem, 3vw, 2.1rem);
}

.game-groups {
  display: grid;
  gap: 1.5rem;
  padding-bottom: 1.5rem;
}

.game-group h2 {
  margin: 0 0 0.75rem;
  font-size: 1.2rem;
}

.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr));
  gap: 1rem 0.85rem;
}

.game-card {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: 1rem;
  padding: 0 0 0.7rem;
  box-shadow: var(--shadow);
  text-decoration: none;
  color: inherit;
  overflow: hidden;
}

.game-cover {
  display: block;
  margin: 0;
  background: #d8eef6;
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

.game-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.game-card h3 {
  margin: 0 0.7rem;
  font-size: 1.02rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.game-card .game-meta {
  margin: 0 0.7rem;
}

.game-card h3 {
  margin: 0;
  font-size: 1.2rem;
}

.game-card p {
  margin: 0;
  color: var(--ink-soft);
  flex: 1;
}

.game-meta {
  display: inline-flex;
  align-items: center;
  align-self: flex-start;
  background: #e6f7fb;
  color: var(--cyan-deep);
  border-radius: 999px;
  padding: 0.15rem 0.65rem;
  font-size: 0.85rem;
  font-weight: 700;
}

.placeholder-tag {
  background: #fff3d6;
  color: #8a5a00;
}

.empty-state {
  background: var(--paper);
  border-radius: var(--radius);
  border: 2px dashed var(--line);
  padding: 1.25rem;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 1rem;
  align-items: center;
}

.empty-state .tingting {
  width: min(30vw, 9rem);
}

.empty-state h2 {
  margin: 0 0 0.4rem;
}

.empty-state p {
  margin: 0;
  color: var(--ink-soft);
}

.error-banner,
.status-banner {
  background: #fff4f1;
  border: 2px solid #f3c2b6;
  color: #8a2c18;
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  margin: 0 0 1rem;
}


/* 大廳：按年級／按課題瀏覽 */
.lobby-browse {
  padding-bottom: 2rem;
}

.browse-modes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
  margin: 0 0 1.15rem;
}

.mode-btn,
.grade-btn,
.topic-card,
.back-btn {
  appearance: none;
  font-family: inherit;
  cursor: pointer;
  border: 2px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--shadow);
}

.mode-btn {
  min-height: var(--touch);
  border-radius: 999px;
  font-size: 1.15rem;
  font-weight: 700;
}

.mode-btn.is-selected,
.mode-btn[aria-selected="true"] {
  background: var(--cyan-deep);
  border-color: var(--cyan-deep);
  color: #fff;
  box-shadow: 0 6px 0 #005e80;
}

.browse-heading {
  margin: 0.35rem 0 0.75rem;
  font-size: 1.2rem;
}

.browse-hint,
.browse-kicker {
  margin: 0 0 1rem;
  color: var(--ink-soft);
}

.browse-kicker {
  font-weight: 700;
  color: var(--cyan-deep);
}

.grade-buttons {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.65rem;
  margin: 0 0 1.35rem;
}

.grade-btn {
  min-height: var(--touch);
  border-radius: 999px;
  font-size: 1.15rem;
  font-weight: 700;
}

.grade-btn.is-selected,
.topic-card.is-selected {
  border-color: var(--coral);
  background: #fff4f0;
  box-shadow: 0 6px 0 #f3c2b6;
}

.topic-group {
  margin: 0 0 1.4rem;
}

.topic-group-title {
  margin: 0 0 0.7rem;
  font-size: 1.05rem;
  color: var(--ink-soft);
}

.topic-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(15.5rem, 1fr));
  gap: 0.75rem;
}

.topic-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.45rem;
  text-align: left;
  min-height: var(--touch);
  padding: 0.95rem 1rem;
  border-radius: var(--radius);
}

.topic-name {
  font-size: 1.12rem;
  font-weight: 700;
  line-height: 1.3;
}

.topic-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem 0.75rem;
  width: 100%;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.topic-code {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #6a8296;
}

.back-btn {
  min-height: var(--touch);
  padding: 0 1.15rem;
  border-radius: 999px;
  font-size: 1rem;
  font-weight: 700;
  color: var(--cyan-deep);
  margin: 0 0 0.85rem;
}

.selection-empty {
  background: var(--paper);
  border-radius: var(--radius);
  border: 2px dashed var(--line);
  padding: 1.25rem;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 1rem;
  align-items: center;
  margin: 0.25rem 0 1rem;
}

.selection-empty .tingting {
  width: min(30vw, 8.5rem);
}

.selection-empty h2 {
  margin: 0 0 0.4rem;
}

.selection-empty p {
  margin: 0;
  color: var(--ink-soft);
}

.mode-btn:active,
.grade-btn:active,
.topic-card:active,
.back-btn:active {
  transform: translateY(1px);
}

/* 遊戲頁：外框是網站殼，遊戲只在統一的 1024×768 視窗內 */
body.is-player {
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* 遊戲頁 */
.player-page {
  padding: 0.5rem 0.75rem 0;
  flex: 1;
  min-height: 0;
  width: 100%;
}

.player-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.5rem;
}

.player-bar h1 {
  margin: 0;
  font-size: 1.15rem;
  flex: 1;
}

.player-bar .tingting {
  width: 2.6rem;
}

.player-fs-btn {
  flex: 0 0 auto;
  margin-left: auto;
  white-space: nowrap;
}

.player-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: row;
  gap: 0.6rem;
}

.game-rail {
  flex: 0 0 7.6rem;
  width: 7.6rem;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.45rem 0.4rem 0.55rem;
  background: #14324a;
  border: 2px solid #0f283a;
  border-radius: var(--radius);
  color: #fff;
  overflow: hidden;
}

.game-rail-title {
  margin: 0 0.15rem;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: #d7eef6;
}

.game-rail-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.game-rail-item {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  text-decoration: none;
  color: inherit;
  border-radius: 0.7rem;
  padding: 0.15rem;
  border: 2px solid transparent;
}

.game-rail-item:focus-visible,
.game-rail-item.is-current {
  border-color: #7be0f5;
  background: rgba(255, 255, 255, 0.08);
}

.game-rail-cover {
  display: block;
  aspect-ratio: 4 / 3;
  border-radius: 0.55rem;
  overflow: hidden;
  background: #2a5470;
}

.game-rail-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.game-rail-name {
  font-size: 0.72rem;
  line-height: 1.25;
  padding: 0 0.1rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.game-rail-note {
  margin: 0.2rem 0.15rem 0;
  font-size: 0.7rem;
  line-height: 1.35;
  color: #b9d4e0;
}

/* 手機直向、橫向都把「其他遊戲」列收起，把空間留給遊戲視窗。iPad 橫向仍顯示側欄。 */
@media (max-width: 700px), (max-height: 560px) {
  .game-rail {
    display: none !important;
  }

  .player-page {
    padding: 0.3rem 0.45rem 0;
  }

  .player-bar {
    margin-bottom: 0.3rem;
    gap: 0.35rem;
  }

  .player-bar .tingting {
    width: 1.85rem;
  }

  .player-bar h1 {
    font-size: 1rem;
  }

  .overlay-card {
    grid-template-columns: minmax(0, 1fr);
    justify-items: center;
    text-align: center;
  }

  .overlay-card .tingting {
    width: auto;
    height: min(18vh, 6.5rem);
    max-width: 6.5rem;
  }
}

.player-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  background: var(--paper);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.player-page .page-main,
.player-page {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.player-page .page-main {
  width: 100%;
  max-width: none;
}


.game-letterbox {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem;
  background: #14324a;
  box-sizing: border-box;
}

.game-slot {
  position: relative;
  flex: none;
  background: #000;
  /* 避免在縮放層上使用陰影與圓角裁切，以免每幀額外合成開銷 */
}

.game-window {
  width: var(--game-w);
  height: var(--game-h);
  transform: translateZ(0) scale(var(--game-scale, 1));
  transform-origin: top left;
  position: absolute;
  top: 0;
  left: 0;
  backface-visibility: hidden;
}

#game-frame {
  width: var(--game-w);
  height: var(--game-h);
  min-height: 0;
  border: 0;
  display: block;
  background: #fff;
  /* 讓瀏覽器把 iframe 當成獨立合成層，縮放時不必重繪遊戲畫面 */
  transform: translateZ(0);
}

.overlay {
  position: absolute;
  inset: 0;
  background: #fffaf4;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.overlay[hidden],
.hidden,
[hidden] {
  display: none !important;
}

.overlay-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 0.9rem;
  align-items: center;
  max-width: 32rem;
}

.overlay-card .tingting {
  width: min(28vw, 8rem);
}

.overlay-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.9rem;
}

.stub-card {
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 2px solid var(--line);
  padding: 1.4rem 1.25rem;
  max-width: 36rem;
  margin: 1rem auto 2rem;
}

.stub-card h1 {
  margin-top: 0;
}

.notice {
  font-size: 0.92rem;
  color: var(--ink-soft);
}

@media (min-width: 800px) {
  .grade-buttons {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  .hero {
    grid-template-columns: minmax(0, 1.05fr) minmax(16rem, 0.85fr);
    align-items: center;
    min-height: calc(100dvh - 8.5rem);
  }

  .tingting-home {
    width: min(32vw, 16rem);
    max-height: min(58dvh, 28rem);
  }
}

@media (min-width: 800px) and (max-height: 900px) {
  .hero {
    min-height: auto;
    padding-top: 1.25rem;
    padding-bottom: 1.25rem;
  }

  .tingting-home {
    max-height: 46dvh;
  }
}

@media (max-width: 799px) {
  .hero {
    text-align: center;
  }

  .hero-copy .btn {
    width: min(100%, 20rem);
  }

  .hero-guide {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .bubble::after {
    right: auto;
    left: 50%;
    top: auto;
    bottom: -0.45rem;
    transform: translateX(-50%) rotate(45deg);
  }

  .tingting-home {
    width: min(46vw, 13rem);
    max-height: 38dvh;
    order: -1;
  }

  .lobby-intro,
  .empty-state,
  .selection-empty,
  .overlay-card {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
  }

  .lobby-intro .bubble::after {
    left: 50%;
    right: auto;
    top: auto;
    bottom: -0.45rem;
    transform: translateX(-50%) rotate(45deg);
  }
}

@media (hover: hover) and (pointer: fine) {
  .btn:hover {
    filter: brightness(1.05);
  }

  .game-card:hover,
  .topic-card:hover,
  .grade-btn:hover,
  .back-btn:hover {
    border-color: var(--cyan);
  }
}

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

/* 全螢幕／沉浸模式：收起網站外框，讓 1024×768 視窗盡量佔滿畫面 */
body.is-player.is-immersive {
  padding: 0;
}

body.is-immersive .player-page {
  padding: 0;
  position: relative;
}

body.is-immersive .game-rail {
  display: none !important;
}

body.is-immersive .player-bar {
  position: absolute;
  inset: 0;
  z-index: 6;
  margin: 0;
  padding: 0;
  background: none;
  pointer-events: none;
  color: #fff;
}

body.is-immersive .player-bar h1,
body.is-immersive .player-bar .tingting {
  display: none;
}

body.is-immersive .player-bar .btn {
  position: absolute;
  pointer-events: auto;
  margin: 0;
  color: #fff;
  border-color: rgba(255, 255, 255, 0.55);
  background: rgba(20, 50, 74, 0.55);
}

body.is-immersive .player-fs-btn {
  margin-left: 0;
}

body.is-immersive .player-stage {
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

body.is-immersive .game-letterbox {
  padding: 0;
}

body.is-player {
  overscroll-behavior: none;
}
