:root {
  --bg: #080d1f;
  --bg-soft: #0c1229;
  --board: #0e1430;
  --dot: rgba(255, 255, 255, 0.16);
  --ink: #f3f6ff;
  --muted: #9fabcc;
  --card: #151d3f;
  --card-2: #1c2549;
  --line: rgba(255, 255, 255, 0.12);
  --accent: #4dabf7;
  --accent-ink: #04122a;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

[data-theme='light'] {
  --bg: #e6ecf9;
  --bg-soft: #f7f9ff;
  --board: #ffffff;
  --dot: rgba(26, 38, 74, 0.22);
  --ink: #101833;
  --muted: #5a6683;
  --card: #ffffff;
  --card-2: #f2f5fe;
  --line: rgba(18, 28, 60, 0.14);
  --accent: #1c6fd4;
  --accent-ink: #ffffff;
  --shadow: 0 10px 26px rgba(30, 45, 90, 0.12);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang HK", "PingFang TC",
    "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body { overflow: hidden; }

button {
  font-family: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

/* ---------------- 遊戲畫面 ---------------- */

#game {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  background: radial-gradient(120% 90% at 50% 0%, var(--bg-soft) 0%, var(--bg) 70%);
}

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px 4px;
}

.chip {
  min-height: 50px;
  min-width: 50px;
  padding: 0 18px;
  border-radius: 16px;
  background: var(--card);
  border: 1px solid var(--line);
  font-size: 17px;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: 0.02em;
}

.chip.icon { padding: 0; font-size: 22px; }

.hud-mid {
  font-size: 19px;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 0.03em;
  white-space: nowrap;
}

#boardWrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  padding: 4px 10px;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 9px;
  padding: 6px 12px calc(10px + env(safe-area-inset-bottom));
}

.tools button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 68px;
  border-radius: 18px;
  background: var(--card);
  border: 1px solid var(--line);
  font-size: 18px;
  font-weight: 700;
  color: var(--ink);
  transition: transform 0.12s ease, background 0.12s ease;
}

.tools button:active { transform: scale(0.96); background: var(--card-2); }
.tools button:disabled { opacity: 0.4; }
.t-ic { font-size: 20px; line-height: 1; }

/* ---------------- 彈出層 ---------------- */

.overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: calc(env(safe-area-inset-top) + 14px) 16px
    calc(env(safe-area-inset-bottom) + 14px);
  background: var(--bg);
  overflow: auto;
  z-index: 20;
  animation: fade 0.18s ease both;
}

.overlay.show { display: flex; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.panel {
  width: 100%;
  max-width: 560px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 26px;
  padding: 26px 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel.center { align-items: center; text-align: center; }

.panel h2 {
  margin: 0;
  font-size: 30px;
  letter-spacing: 0.02em;
}

/* 主頁 */

.home-inner {
  width: 100%;
  max-width: 620px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.home-inner h1 {
  margin: 0;
  font-size: clamp(40px, 9vw, 64px);
  font-weight: 800;
  letter-spacing: 0.06em;
  background: linear-gradient(100deg, #ffd43b, #ff6b9d 38%, #4dabf7 72%, #51cf66);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.home-inner .sub {
  margin: 2px 0 22px;
  font-size: 17px;
  color: var(--muted);
  letter-spacing: 0.08em;
}

.home-inner .pick {
  margin: 0 0 10px;
  font-size: 20px;
  font-weight: 700;
  align-self: flex-start;
  color: var(--muted);
}

.sizes {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  width: 100%;
}

.size-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 18px 20px;
  border-radius: 22px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.size-card:active { transform: scale(0.975); border-color: var(--accent); }

.sc-name { font-size: 30px; font-weight: 800; letter-spacing: 0.06em; }
.sc-grid { font-size: 16px; color: var(--muted); font-weight: 600; letter-spacing: 0.04em; }
.sc-note { margin-top: 8px; font-size: 15px; font-weight: 700; color: var(--accent); letter-spacing: 0.06em; }

.home-links {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.link {
  padding: 14px 26px;
  border-radius: 16px;
  font-size: 17px;
  font-weight: 600;
  color: var(--muted);
  background: var(--card);
  border: 1px solid var(--line);
}

/* 設定 / 玩法 */

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.row-label { font-size: 19px; font-weight: 700; }

.seg {
  display: flex;
  gap: 6px;
  padding: 5px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 15px;
}

.seg button {
  min-width: 62px;
  min-height: 48px;
  padding: 0 14px;
  border-radius: 11px;
  font-size: 18px;
  font-weight: 700;
  color: var(--muted);
}

.seg button.on { background: var(--accent); color: var(--accent-ink); }

.big {
  min-height: 66px;
  border-radius: 18px;
  font-size: 21px;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: var(--card-2);
  border: 1px solid var(--line);
  color: var(--ink);
  width: 100%;
}

.big.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.big.ghost { background: transparent; color: var(--muted); border-color: var(--line); }
.big:active { transform: scale(0.98); }

.steps {
  margin: 0;
  padding-left: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  font-size: 19px;
  line-height: 1.55;
}

.steps b { color: var(--accent); }
.calm { margin: 4px 0 0; font-size: 17px; line-height: 1.6; color: var(--muted); }

.tick {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 40px;
  font-weight: 800;
  background: rgba(81, 207, 102, 0.16);
  color: #51cf66;
  border: 2px solid rgba(81, 207, 102, 0.4);
}

.win-sub { margin: -4px 0 6px; font-size: 19px; color: var(--muted); }
.win-sub b { color: var(--ink); }

/* 提示條 */

#toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 74px);
  transform: translate(-50%, -10px);
  padding: 13px 22px;
  border-radius: 16px;
  background: rgba(20, 28, 58, 0.92);
  border: 1px solid var(--line);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.02em;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 40;
  max-width: 88vw;
  text-align: center;
}

[data-theme='light'] #toast { background: rgba(24, 34, 68, 0.92); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 橫向：細螢幕高度 */

@media (max-height: 560px) {
  .home-inner h1 { font-size: 34px; }
  .home-inner .sub { margin-bottom: 12px; }
  .size-card { padding: 12px 16px; }
  .sc-name { font-size: 24px; }
  .tools button { min-height: 58px; font-size: 16px; }
  .hud { padding-top: 4px; }
}
