:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #2b2a33;
  --muted: #8a8794;
  --main: #ff6b4a;
  --main-dark: #e5522f;
  --line: #e4e0d8;
  --radius: 14px;
}

* { box-sizing: border-box; }

html {
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  font-family: system-ui, "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
}

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top))
           max(16px, env(safe-area-inset-right))
           max(16px, env(safe-area-inset-bottom))
           max(16px, env(safe-area-inset-left));
}

input { -webkit-user-select: text; user-select: text; }

.screen { display: none; text-align: center; }
.screen.active { display: block; }

.logo { font-size: 2.6rem; margin: 24px 0 4px; }
.logo span { color: var(--main); }
.tagline { color: var(--muted); margin-bottom: 24px; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}
.card h2 { margin: 0 0 12px; font-size: 1.1rem; }

.btn {
  border: 2px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 1rem;
  cursor: pointer;
}
.btn:hover { border-color: var(--main); }
.btn.primary {
  background: var(--main);
  border-color: var(--main);
  color: #fff;
  font-weight: 600;
}
.btn.primary:hover { background: var(--main-dark); }
.btn.small { padding: 6px 10px; font-size: 0.85rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.rounds { display: flex; gap: 8px; justify-content: center; margin-bottom: 14px; }
.chip {
  flex: 1;
  border: 2px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 8px 0;
  cursor: pointer;
  font-size: 0.95rem;
}
.chip.active { background: var(--ink); border-color: var(--ink); color: #fff; }

input[type="text"], #input-code {
  width: 100%;
  padding: 12px;
  font-size: 1.3rem;
  text-align: center;
  letter-spacing: 0.4em;
  border: 2px solid var(--line);
  border-radius: 10px;
  margin-bottom: 12px;
}

.error { color: #d6336c; min-height: 1.4em; }
.muted { color: var(--muted); }

.code {
  font-size: 4rem;
  font-weight: 800;
  letter-spacing: 0.25em;
  margin: 20px 0;
  color: var(--main);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  margin-bottom: 8px;
}
.timer { font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.timer.low { color: #d6336c; }

.word-box {
  background: var(--ink);
  color: #fff;
  border-radius: var(--radius);
  padding: 10px;
  margin-bottom: 10px;
}
.word-box strong { font-size: 1.6rem; margin: 0 6px; }
.word-box small { display: block; opacity: 0.7; }

.canvas-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  min-height: 0;
  container-type: size;
  padding: 8px 26px;
  margin-inline: -8px;
  background: repeating-linear-gradient(
    45deg, #d9d3c7 0 8px, #cdc7b9 8px 16px
  );
  border-radius: 14px;
}
#draw {
  width: min(100cqw, 80cqh);
  aspect-ratio: 4 / 5;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  touch-action: none;
  cursor: crosshair;
}

#stitch {
  width: 100%;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 8px;
}

.tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  margin: 10px 0;
}
.colors { display: flex; gap: 6px; }
.swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid var(--line);
  cursor: pointer;
}
.swatch.active { outline: 3px solid var(--main); }

.stars { display: flex; justify-content: center; gap: 6px; }
.star {
  font-size: 2.4rem;
  background: none;
  border: none;
  cursor: pointer;
  color: #d4d0c8;
}
.star.on { color: #ffb400; }

.final-score {
  display: flex;
  justify-content: space-around;
  margin: 16px 0;
  font-size: 1.1rem;
}
.final-score strong { display: block; font-size: 2.2rem; color: var(--main); }

.gallery { display: grid; gap: 12px; margin-bottom: 16px; }
.gallery figure { margin: 0; background: #fff; padding: 8px; border-radius: 10px; }
.gallery img { width: 100%; border: 1px solid var(--line); }
.gallery figcaption { font-size: 0.9rem; color: var(--muted); margin-top: 4px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}
.toast.show { opacity: 1; }

#draw.left { border-right: 4px dashed var(--main); }
#draw.right { border-left: 4px dashed var(--main); }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(43, 42, 51, 0.7);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.overlay.show { display: flex; }
.overlay-box {
  background: #fff;
  border-radius: var(--radius);
  padding: 24px 32px;
  text-align: center;
}
.spinner {
  width: 36px;
  height: 36px;
  margin: 0 auto 12px;
  border: 4px solid var(--line);
  border-top-color: var(--main);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#screen-game.active {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - max(16px, env(safe-area-inset-top))
                      - max(16px, env(safe-area-inset-bottom)));
}
#screen-game .word-box { padding: 6px; }
#screen-game .word-box strong { font-size: 1.3rem; }
#screen-game .tools { margin: 8px 0; gap: 6px 8px; }
#screen-game .colors { width: 100%; justify-content: center; }
#screen-game .btn.small { padding: 10px 12px; min-height: 44px; white-space: nowrap; }
#screen-game .swatch { width: 30px; height: 30px; flex: none; }
#screen-game #size { width: 90px; flex: none; }
#screen-game #btn-submit { min-height: 48px; }
#screen-game .muted { margin: 4px 0 0; min-height: 1.2em; }

#screen-game { touch-action: none; }
#screen-game #size { touch-action: pan-x; }
#screen-game { padding-inline: 0; }

.word-box .task {
  background: var(--main);
  border-radius: 8px;
  margin: 4px 0;
  padding: 4px 8px;
  font-size: 1.05rem;
}
.word-box .task b { font-size: 1.3rem; }

.parts-line { color: var(--muted); margin: 6px 0 0; font-size: 0.95rem; }

.btn.danger { background: #d6336c; border-color: #d6336c; color: #fff; }
