:root {
  --bg: #f6f3ec;
  --surface: #ffffff;
  --surface-2: #f0ece2;
  --text: #2b2a27;
  --muted: #77736a;
  --border: #e2dccd;
  --accent: #4f8a3c;
  --accent-ink: #ffffff;
  --accent-soft: #e3efd9;
  --danger: #d6453d;
  --danger-soft: rgba(214, 69, 61, 0.22);
  --gold: #f2b01e;
  --sq-light: #ebecd0;
  --sq-dark: #779556;
  --sq-select: rgba(255, 214, 10, 0.55);
  --sq-last: rgba(255, 214, 10, 0.3);
  --dot: rgba(20, 30, 10, 0.28);
  --shadow: 0 2px 10px rgba(40, 35, 20, 0.08);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1d1a;
    --surface: #272824;
    --surface-2: #31332d;
    --text: #ecebe5;
    --muted: #a3a197;
    --border: #3b3d36;
    --accent: #7dbb5f;
    --accent-ink: #14200d;
    --accent-soft: #2e3b26;
    --danger: #ef6a5f;
    --danger-soft: rgba(239, 106, 95, 0.28);
    --sq-light: #cfd3b4;
    --sq-dark: #6a8650;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #1c1d1a;
  --surface: #272824;
  --surface-2: #31332d;
  --text: #ecebe5;
  --muted: #a3a197;
  --border: #3b3d36;
  --accent: #7dbb5f;
  --accent-ink: #14200d;
  --accent-soft: #2e3b26;
  --danger: #ef6a5f;
  --danger-soft: rgba(239, 106, 95, 0.28);
  --sq-light: #cfd3b4;
  --sq-dark: #6a8650;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 16px 32px;
  min-height: 100vh;
}

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

/* ---------- 홈 ---------- */

.home-hero { text-align: center; padding: 40px 0 24px; }
.logo { font-size: 64px; line-height: 1; display: flex; justify-content: center; gap: 4px; }
.home-hero h1 { margin: 12px 0 4px; font-size: 34px; letter-spacing: -0.5px; }
.tagline { margin: 0; color: var(--muted); }

.home-menu { display: grid; gap: 12px; }
.menu-card {
  display: flex; align-items: center; gap: 14px;
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
  transition: transform 0.12s ease;
}
.menu-card:active { transform: scale(0.98); }
.menu-card.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.menu-card.primary small { color: inherit; opacity: 0.85; }
.menu-icon { font-size: 28px; }
.menu-text { display: flex; flex-direction: column; gap: 2px; }
.menu-text strong { font-size: 18px; }
.menu-text small { color: var(--muted); font-size: 13px; }

/* ---------- 상단바 ---------- */

.topbar {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.topbar h2 { margin: 0; font-size: 19px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { color: var(--muted); font-size: 13px; text-align: right; }
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 18px;
}

/* ---------- 튜토리얼 목록 ---------- */

.chapter-list { display: grid; gap: 12px; }
.chapter {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.chapter.locked { opacity: 0.55; }
.chapter-head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.chapter-icon {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: var(--surface-2);
  display: grid; place-items: center;
  font-size: 32px;
}
.chapter-head h3 { margin: 0; font-size: 17px; }
.chapter-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.stage-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.stage-btn {
  min-width: 64px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 13px;
}
.stage-btn .num { font-weight: 700; }
.stage-btn .stars { font-size: 11px; letter-spacing: 1px; color: var(--muted); }
.stage-btn.cleared { border-color: var(--accent); background: var(--accent-soft); }
.stage-btn.cleared .stars { color: var(--gold); }
.stage-btn.next { border: 2px solid var(--accent); }
.stage-btn:disabled { cursor: not-allowed; opacity: 0.5; }
.stage-size { font-size: 10px; color: var(--muted); }

.footnote { text-align: center; margin-top: 20px; }
.link-btn { background: none; border: none; color: var(--muted); text-decoration: underline; font-size: 13px; }

/* ---------- 코치 ---------- */

.coach { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; }
.coach-avatar {
  flex: 0 0 auto;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid; place-items: center;
  font-size: 28px;
}
.coach-avatar .piece { font-size: 30px; color: var(--accent-ink); -webkit-text-stroke: 0; text-shadow: none; }
.coach-bubble {
  margin: 0;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px var(--radius) var(--radius) var(--radius);
  line-height: 1.5;
  font-size: 15px;
}

.status-row {
  min-height: 28px;
  display: flex; justify-content: center; align-items: center; gap: 14px; flex-wrap: wrap;
  font-size: 15px;
  margin: 6px 0;
}
.status-row .pill {
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 13px;
}
.status-row .pill.warn { background: var(--danger-soft); color: var(--danger); font-weight: 700; }

.legend { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); min-height: 18px; margin-top: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

/* ---------- 보드 ---------- */

.board-wrap { display: flex; justify-content: center; padding: 4px 0; }
.board {
  --n: 4;
  display: grid;
  grid-template-columns: repeat(var(--n), minmax(0, 1fr));
  grid-template-rows: repeat(var(--n), minmax(0, 1fr));
  width: min(100%, 420px);
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 4px 18px rgba(40, 40, 20, 0.18);
  user-select: none;
  touch-action: manipulation;
}
.board-wrap.small .board { width: min(80%, 300px); }

.sq {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  background: var(--sq-light);
  cursor: pointer;
}
.sq.dark { background: var(--sq-dark); }
.sq.last::before, .sq.selected::before, .sq.zone::before, .sq.hint::before, .sq.found::before, .sq.wrong::before {
  content: "";
  position: absolute; inset: 0;
}
.sq.last::before { background: var(--sq-last); }
.sq.selected::before { background: var(--sq-select); }
.sq.zone::before { background: repeating-linear-gradient(45deg, var(--danger-soft) 0 6px, transparent 6px 12px); }
.sq.hint::before { box-shadow: inset 0 0 0 4px #3a8ef0; animation: pulse 1s ease-in-out infinite; }
.sq.found::before { background: rgba(80, 170, 60, 0.55); }
.sq.wrong::before { background: rgba(220, 60, 50, 0.6); animation: shake 0.3s; }
.sq.check .piece { filter: drop-shadow(0 0 6px #ff2a1f) drop-shadow(0 0 10px #ff2a1f); }

.sq .coord {
  position: absolute;
  font-size: 10px;
  font-weight: 700;
  opacity: 0.7;
  pointer-events: none;
}
.sq .coord.file { right: 4px; bottom: 2px; }
.sq .coord.rank { left: 4px; top: 2px; }
.sq.dark .coord { color: var(--sq-light); }
.sq:not(.dark) .coord { color: var(--sq-dark); }

.sq .dot {
  position: absolute;
  width: 28%; height: 28%;
  border-radius: 50%;
  background: var(--dot);
  pointer-events: none;
}
.sq .dot.cap {
  width: 86%; height: 86%;
  background: transparent;
  border: 5px solid var(--dot);
}
.sq .dot.danger { background: var(--danger); opacity: 0.75; }
.sq .dot.cap.danger { background: transparent; border-color: var(--danger); }

.sq .check-mark {
  position: absolute;
  font-size: 28px;
  color: #fff;
  font-weight: 900;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}

.piece {
  position: relative;
  font-size: calc(min(100vw - 32px, 420px) / var(--n) * 0.72);
  line-height: 1;
  font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", "DejaVu Sans", serif;
  pointer-events: none;
}
.board-wrap.small .piece { font-size: calc(min(80vw, 300px) / var(--n) * 0.72); }
.piece.pw { color: #fff; -webkit-text-stroke: 1.5px #2a2a2a; paint-order: stroke fill; text-shadow: 0 2px 2px rgba(0,0,0,0.25); }
.piece.pb { color: #1f1f1f; -webkit-text-stroke: 1px #000; text-shadow: 0 2px 2px rgba(0,0,0,0.25); }
.logo .piece { font-size: 64px; }
.chapter-icon .piece { font-size: 32px; }
.piece.target { opacity: 0.9; }
.piece.pop { animation: pop 0.25s ease-out; }

.rock {
  width: 70%; height: 62%;
  border-radius: 46% 54% 40% 60% / 60% 50% 50% 40%;
  background: linear-gradient(145deg, #a8a29a, #6f6a63);
  box-shadow: inset -4px -6px 0 rgba(0, 0, 0, 0.18), 0 3px 4px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}
.star {
  font-size: calc(min(100vw - 32px, 420px) / var(--n) * 0.55);
  color: var(--gold);
  text-shadow: 0 2px 0 #b9800c, 0 4px 6px rgba(0, 0, 0, 0.25);
  animation: bob 1.6s ease-in-out infinite;
  pointer-events: none;
}
.target-tag {
  position: absolute;
  top: 4px; right: 4px;
  font-size: 11px;
  background: var(--danger);
  color: #fff;
  border-radius: 6px;
  padding: 0 4px;
  pointer-events: none;
}

@keyframes pop { from { transform: scale(0.6); } to { transform: scale(1); } }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }

/* ---------- 버튼 ---------- */

.actions { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.actions.sticky {
  position: sticky; bottom: 0;
  padding: 12px 0 calc(8px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.btn {
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 15px;
  box-shadow: var(--shadow);
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 700; }
.btn.big { width: 100%; padding: 14px; font-size: 17px; }

/* ---------- 커스텀 ---------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.panel h3 { margin: 0 0 10px; font-size: 16px; }
.panel h3 small { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 4px; }

.preset-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.preset {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 12px;
  padding: 8px;
  text-align: left;
}
.preset.active { border: 2px solid var(--accent); }
.preset strong { display: block; font-size: 14px; }
.preset small { color: var(--muted); font-size: 12px; }
.mini-board {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  aspect-ratio: 1;
  width: 100%;
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 6px;
}
.mini-board span { display: grid; place-items: center; background: var(--sq-light); font-size: 20px; line-height: 1; }
.mini-board span.dark { background: var(--sq-dark); }

.palette { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; margin-bottom: 10px; }
.palette button {
  min-width: 56px;
  padding: 6px 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  display: flex; flex-direction: column; align-items: center;
  font-size: 11px;
}
.palette button .piece { font-size: 26px; }
.palette button.active { border: 2px solid var(--accent); background: var(--accent-soft); }

.errors { list-style: none; padding: 0; margin: 10px 0 0; color: var(--danger); font-size: 13px; }
.errors li::before { content: "⚠ "; }
.errors.ok { color: var(--accent); }
.errors.ok li::before { content: "✓ "; }

.option { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.option[hidden] { display: none; }
.option-label { font-size: 13px; color: var(--muted); }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  flex: 1 1 auto;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 14px;
}
.seg button.active { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 700; }

/* ---------- 대결 ---------- */

.player-bar {
  display: flex; justify-content: space-between; align-items: center;
  width: min(100%, 420px);
  margin: 4px auto;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 14px;
}
.player-bar.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.player-bar .captured { font-size: 18px; letter-spacing: -2px; }
.player-bar .captured .piece { font-size: 18px; }

.move-list {
  margin: 14px auto 0;
  padding: 10px 10px 10px 36px;
  width: min(100%, 420px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
  columns: 2;
  min-height: 40px;
  color: var(--muted);
}
.move-list:empty::before { content: "아직 둔 수가 없어요"; margin-left: -26px; }
.move-list li { break-inside: avoid; }
.move-list li span { color: var(--text); }

/* ---------- 규칙 ---------- */

.rules { display: grid; gap: 10px; }
.rule-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex; gap: 12px; align-items: flex-start;
}
.rule-card .piece { font-size: 38px; }
.rule-card h4 { margin: 0 0 4px; font-size: 16px; }
.rule-card p { margin: 0; font-size: 14px; line-height: 1.55; color: var(--muted); }
.rule-card .rule-emoji { font-size: 32px; }

/* ---------- 모달 / 토스트 ---------- */

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: grid; place-items: center;
  padding: 16px;
  z-index: 10;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  width: min(100%, 360px);
  background: var(--surface);
  border-radius: 18px;
  padding: 22px 20px 18px;
  text-align: center;
  animation: pop 0.2s ease-out;
}
.modal-icon { font-size: 48px; line-height: 1; }
.modal-icon .piece { font-size: 48px; }
.modal h3 { margin: 8px 0 4px; font-size: 21px; }
.modal p { margin: 6px 0 14px; color: var(--muted); line-height: 1.5; }
.modal-stars { font-size: 34px; color: var(--gold); letter-spacing: 4px; min-height: 0; }
.modal-stars .off { color: var(--border); }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.modal-actions .btn { flex: 1 1 40%; }
.promo-choices { display: flex; justify-content: center; gap: 8px; margin-bottom: 8px; }
.promo-choices button {
  width: 64px; height: 64px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--sq-light);
  display: grid; place-items: center;
}
.promo-choices .piece { font-size: 44px; }

.toast {
  position: fixed;
  left: 50%; bottom: 28px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: #2b2a27;
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 20;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
  animation: pop 0.18s ease-out;
}
.toast[hidden] { display: none; }

.confetti {
  position: fixed;
  top: -10px;
  width: 8px; height: 14px;
  z-index: 11;
  pointer-events: none;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

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

.mini-board .piece { font-size: 20px; }
.palette button .piece:not(.pw):not(.pb) { color: var(--muted); }
.player-bar .side { display: inline-flex; align-items: center; gap: 6px; }
.player-bar .side .piece { font-size: 22px; }
