:root {
  --bg: #eaf3e6;
  --bg-deep: #d8ead2;
  --surface: #ffffff;
  --surface-2: #f3f8f1;
  --ink: #17261d;
  --muted: #5a6e61;
  --line: #d2e0cf;
  --accent: #2e9a58;
  --accent-ink: #ffffff;
  --accent-soft: #dff2e5;
  --danger: #e5484d;
  --warn: #f59e0b;
  --board: #e9ca8c;
  --board-2: #dcb573;
  --board-line: #74542a;
  --shadow: 0 6px 24px rgba(30, 60, 40, .12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1812;
    --bg-deep: #0a120d;
    --surface: #16241b;
    --surface-2: #1c2e23;
    --ink: #e6f1e9;
    --muted: #9db2a4;
    --line: #2a3f31;
    --accent: #4cc47a;
    --accent-ink: #0b1a10;
    --accent-soft: #1f3a29;
    --board: #d8b778;
    --board-2: #c49f5e;
    --board-line: #5e4320;
    --shadow: 0 6px 24px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1812; --bg-deep: #0a120d; --surface: #16241b; --surface-2: #1c2e23; --ink: #e6f1e9; --muted: #9db2a4;
  --line: #2a3f31; --accent: #4cc47a; --accent-ink: #0b1a10; --accent-soft: #1f3a29; --board: #d8b778; --board-2: #c49f5e;
  --board-line: #5e4320; --shadow: 0 6px 24px rgba(0, 0, 0, .4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100dvh;
  background: radial-gradient(1200px 600px at 50% -10%, var(--bg-deep), var(--bg)) fixed, var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button {
  font: inherit; color: inherit; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px;
  transition: background .15s, transform .08s, border-color .15s;
}
button:hover { border-color: var(--accent); }
button:active { transform: scale(.97); }
button:disabled { opacity: .45; cursor: default; transform: none; border-color: var(--line); }
button:focus-visible, canvas:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.primary:hover { filter: brightness(1.06); }
[hidden] { display: none !important; }

/* 상단바 */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  height: 52px; padding: 0 16px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; background: none; border: 0; padding: 4px; font-weight: 800; font-size: 17px; }
.top-actions { display: flex; gap: 6px; }
.icon-btn { width: 38px; height: 38px; padding: 7px; display: grid; place-items: center; }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn .slash { display: none; }
.icon-btn[aria-pressed="false"] { color: var(--muted); }
.icon-btn[aria-pressed="false"] .slash { display: inline; }

/* 시작 화면 */
.screen { padding: 16px; }
.start-card, .records-card {
  max-width: 560px; margin: 8px auto 32px; padding: 24px 20px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow);
}
.hero { display: flex; justify-content: center; gap: 4px; margin-bottom: 4px; }
.hero img { width: 44px; height: 44px; animation: bob 2.4s ease-in-out infinite; }
.hero img:nth-child(2n) { animation-delay: -1.2s; }
.start-card h1 { text-align: center; margin: 6px 0 2px; font-size: 28px; letter-spacing: -.5px; }
.sub { text-align: center; margin: 0 0 18px; color: var(--muted); }
.resume { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; margin-bottom: 16px;
  background: var(--accent-soft); border-radius: 12px; font-weight: 600; }
.field { margin-bottom: 14px; }
.label { display: block; font-size: 13px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1 1 0; min-width: 64px; padding: 9px 6px; }
.seg button[aria-pressed="true"], .chars button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.desc { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.chars { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.chars button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 2px; font-size: 12px; }
.chars img { width: 40px; height: 40px; }
.chars button[aria-pressed="true"] img { animation: bob 1.2s ease-in-out infinite; }
.chars button.taken { opacity: .4; }
.check { display: flex; align-items: center; gap: 8px; margin: 4px 0 16px; font-size: 14px; color: var(--muted); }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.big { width: 100%; padding: 14px; font-size: 18px; border-radius: 14px; }

/* 게임 화면 */
#game { display: grid; grid-template-columns: auto 300px; gap: 20px; justify-content: center; align-items: start; }
.board-wrap {
  position: relative;
  width: min(calc(100vw - 32px - 320px), calc(100dvh - 52px - 32px));
  min-width: 300px;
  aspect-ratio: 1;
}
#board { width: 100%; height: 100%; display: block; border-radius: 14px; box-shadow: var(--shadow); touch-action: manipulation; cursor: pointer; }
.toast {
  position: absolute; left: 50%; top: 14px; transform: translate(-50%, -8px); opacity: 0; pointer-events: none;
  background: rgba(20, 20, 20, .88); color: #fff; padding: 8px 14px; border-radius: 999px; font-weight: 600; font-size: 14px;
  transition: opacity .2s, transform .2s; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.panel { display: flex; flex-direction: column; gap: 12px; }
.players { display: grid; gap: 8px; }
.pcard {
  position: relative; display: grid; grid-template-columns: 48px 1fr auto; align-items: center; gap: 10px;
  padding: 10px 12px; background: var(--surface); border: 2px solid var(--line); border-radius: 14px; overflow: hidden;
  transition: border-color .2s, box-shadow .2s;
}
.pcard.active { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.pcard.active .avatar { animation: bob 1s ease-in-out infinite; }
.pcard.thinking .prole::after { content: " · 생각 중"; animation: blink 1s steps(2) infinite; }
.avatar { width: 48px; height: 48px; border-radius: 50%; }
#pB .avatar { box-shadow: 0 0 0 3px #1c1f26; }
#pW .avatar { box-shadow: 0 0 0 3px #fbfaf3, 0 0 0 4px #b9b9ad; }
.pinfo { display: flex; flex-direction: column; min-width: 0; }
.pname { font-size: 16px; }
.prole { font-size: 13px; color: var(--muted); }
.ptime { font-variant-numeric: tabular-nums; font-weight: 800; font-size: 18px; }
.ptime.low { color: var(--danger); animation: blink .5s steps(2) infinite; }
.pbar { position: absolute; left: 0; right: 0; bottom: 0; height: 3px; }
.pbar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .1s linear; }
.pbar i.low { background: var(--danger); }
.status { min-height: 44px; padding: 10px 12px; border-radius: 12px; background: var(--surface-2); font-weight: 700; text-align: center; }
.status.alert { background: color-mix(in srgb, var(--danger) 16%, var(--surface)); color: var(--danger); }
.status.good { background: var(--accent-soft); color: var(--accent); }
.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.actions button { padding: 10px 4px; }
#hintBtn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
#resignBtn.confirm { background: var(--danger); color: #fff; border-color: var(--danger); }
.actions.replay { grid-template-columns: repeat(5, 1fr); align-items: center; }
.actions.replay #replayPlay, .actions.replay #replayExit { grid-column: span 5; }
.actions.replay #replayExit { grid-column: span 5; }
.rpos { text-align: center; font-variant-numeric: tabular-nums; font-weight: 700; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.red { background: var(--danger); } .dot.orange { background: var(--warn); } .dot.green { background: #22c55e; }
.legend .x { color: var(--danger); font-style: normal; font-weight: 800; }
.keys { margin: 0; font-size: 12px; color: var(--muted); }

/* 기록 */
.rec-head { display: flex; align-items: center; justify-content: space-between; }
.records-card h2 { margin: 8px 0; font-size: 20px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; margin-bottom: 16px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 13px; color: var(--muted); }
.stat span { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat small { display: block; color: var(--muted); font-size: 12px; }
.history { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 6px; }
.history button { width: 100%; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; text-align: left; }
.history .vs { display: flex; align-items: center; }
.history .vs img { width: 28px; height: 28px; }
.history .vs img + img { margin-left: -6px; }
.history .meta { font-size: 13px; color: var(--muted); }
.history .res { font-weight: 800; }
.res.win { color: var(--accent); } .res.lose { color: var(--danger); }
.empty { color: var(--muted); text-align: center; padding: 16px; }

/* 결과 */
.modal { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 16px; background: rgba(0, 0, 0, .45); animation: fade .25s; }
.modal-card { width: min(380px, 100%); background: var(--surface); border-radius: 22px; padding: 24px 20px; text-align: center; box-shadow: var(--shadow); animation: pop .35s cubic-bezier(.2, 1.4, .4, 1); }
.winner { width: 110px; height: 110px; animation: dance .6s ease-in-out infinite alternate; }
.modal-card.lose .winner { animation: sad 2s ease-in-out infinite; filter: grayscale(.2); }
.modal-card h2 { margin: 10px 0 4px; font-size: 26px; }
.modal-card p { margin: 0 0 18px; color: var(--muted); }
.row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.row button { flex: 1 1 90px; padding: 11px 8px; }

@keyframes bob { 50% { transform: translateY(-4px); } }
@keyframes blink { 50% { opacity: .35; } }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(.85); opacity: 0; } }
@keyframes dance { from { transform: rotate(-10deg) translateY(0); } to { transform: rotate(10deg) translateY(-10px); } }
@keyframes sad { 50% { transform: translateY(4px) rotate(-4deg); } }

@media (max-width: 860px) {
  .screen { padding: 12px; }
  #game { grid-template-columns: 1fr; gap: 10px; padding: 8px; }
  .board-wrap { width: min(calc(100vw - 16px), calc(100dvh - 52px - 200px)); min-width: 280px; margin: 0 auto; }
  .panel { max-width: 560px; width: 100%; margin: 0 auto; gap: 8px; }
  .players { grid-template-columns: 1fr 1fr; }
  .pcard { grid-template-columns: 36px 1fr auto; gap: 6px; padding: 8px; }
  .avatar { width: 36px; height: 36px; }
  .pname { font-size: 14px; }
  .prole { font-size: 12px; }
  .ptime { font-size: 15px; }
  .keys { display: none; }
  .chars { grid-template-columns: repeat(3, 1fr); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
