:root {
  --bg: #121214;
  --surface: #1e1e22;
  --border: #3a3a40;
  --text: #f2f2f4;
  --muted: #9a9aa3;
  --correct: #538d4e;
  --present: #c9a227;
  --absent: #3a3a3c;
  --key: #818384;
  --accent: #e2557b;
  --tile: min(14vw, 58px);
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #fbfaf8;
    --surface: #ffffff;
    --border: #d3d6da;
    --text: #1a1a1b;
    --muted: #6b6b70;
    --absent: #787c7e;
    --key: #d3d6da;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Malgun Gothic", sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#app {
  max-width: 500px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 12px env(safe-area-inset-bottom);
}

header {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  height: 52px;
  border-bottom: 1px solid var(--border);
}
header h1 { margin: 0; font-size: 26px; letter-spacing: 1px; font-weight: 800; }
header h1 span { color: var(--accent); }
.icon-btn {
  position: absolute;
  background: none; border: 0; font-size: 22px; padding: 8px;
}
#btn-leave { left: 0; }
.header-right { position: absolute; right: 0; display: flex; align-items: center; gap: 2px; }
.header-right .icon-btn { position: static; }
#btn-lang {
  display: flex; padding: 2px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); font-size: 12px; font-weight: 700;
}
#btn-lang span { padding: 4px 8px; border-radius: 999px; color: var(--muted); }
#btn-lang span.on { background: var(--accent); color: #fff; }

/* Lobby */
#lobby { display: flex; flex-direction: column; gap: 14px; padding: 40px 8px; }
.tagline { text-align: center; color: var(--muted); margin: 0 0 16px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
input {
  font: inherit; font-size: 17px; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; outline: none; width: 100%;
}
input:focus { border-color: var(--accent); }
.join-row { display: flex; gap: 8px; }
#in-code { text-transform: uppercase; letter-spacing: 4px; }
.or { text-align: center; color: var(--muted); font-size: 13px; }
button.primary, #lobby button, .result-actions button, dialog button {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 10px; padding: 12px 16px; font-weight: 600; white-space: nowrap;
}
button.primary, #lobby button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; font-size: 14px; padding: 6px; }

/* Game */
#game { display: flex; flex-direction: column; flex: 1; }
.room-bar { display: flex; justify-content: space-between; align-items: center; padding: 10px 0 4px; gap: 8px; }
.chip {
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  padding: 6px 12px; font-size: 13px;
}
.chip b { letter-spacing: 2px; }
#players { display: flex; gap: 10px; font-size: 13px; flex-wrap: wrap; justify-content: flex-end; }
.player { display: flex; align-items: center; gap: 5px; }
.player .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); opacity: .35; }
.player.online .dot { opacity: 1; box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 30%, transparent); }
.player.me { font-weight: 700; }
.game-title { text-align: center; color: var(--muted); font-size: 13px; padding: 4px 0 8px; }

#board { display: flex; justify-content: center; gap: 14px; margin: 0 auto; }
#board.two { --tile: min(calc((min(100vw, 500px) - 84px) / 10), 40px); --gap: 4px; gap: 12px; }
.grid { display: grid; gap: var(--gap, 5px); align-content: start; }
.grid.solved .row:not(.done) .tile { opacity: .35; }
.row { display: grid; grid-template-columns: repeat(5, var(--tile)); grid-auto-rows: var(--tile); gap: var(--gap, 5px); position: relative; }
.row .by {
  position: absolute; left: -16px; top: 50%; width: 8px; height: 8px;
  margin-top: -4px; border-radius: 50%; background: var(--c);
}
.tile {
  border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--tile) * .55); font-weight: 800; text-transform: uppercase;
  user-select: none;
}
.tile.filled { border-color: var(--muted); animation: pop .1s; }
.tile.s0, .tile.s1, .tile.s2 { border-color: transparent; color: #fff; }
.tile.s0 { background: var(--absent); }
.tile.s1 { background: var(--present); }
.tile.s2 { background: var(--correct); }
.tile.flip { animation: flip .5s ease both; }
.row.shake { animation: shake .4s; }
@keyframes pop { 50% { transform: scale(1.1); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }

#partner-drafts { min-height: 34px; display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 0 2px; }
.pdraft { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.pdraft .mini {
  width: 22px; height: 22px; border: 1.5px solid var(--c); border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; text-transform: uppercase; color: var(--text); font-size: 13px;
}
.pdraft .label { color: var(--c); font-weight: 600; margin-right: 4px; }

#result { text-align: center; padding: 16px 0; margin-top: auto; }
#result-text { font-size: 15px; margin-bottom: 10px; }
#result-text b { letter-spacing: 2px; text-transform: uppercase; color: var(--correct); }
.mode-row { text-align: center; padding-top: 6px; }
.variant {
  display: inline-flex; padding: 3px; gap: 2px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
}
.variant button { border: 0; background: none; border-radius: 999px; padding: 6px 14px; font-size: 13px; font-weight: 600; color: var(--muted); }
.variant button.on { background: var(--text); color: var(--bg); }
.result-actions { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.result-actions button { padding: 9px 12px; font-size: 14px; }

#keyboard { margin-top: auto; padding: 8px 0 6px; display: flex; flex-direction: column; gap: 7px; user-select: none; }
.krow { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1; max-width: 44px; height: 56px; border: 0; border-radius: 5px;
  background: var(--key); font-weight: 700; font-size: 14px; text-transform: uppercase; padding: 0;
}
.key.wide { max-width: 68px; flex: 1.5; font-size: 12px; }
.key.s0 { background: var(--absent); color: #fff; opacity: .55; }
@media (prefers-color-scheme: light) { .key.s0 { opacity: 1; } }
.key.s1 { background: var(--present); color: #fff; }
.key.s2 { background: var(--correct); color: #fff; }
.game-menu { text-align: center; padding-bottom: 6px; }

#toast {
  position: fixed; top: 70px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  font-weight: 600; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 10;
}
#toast.show { opacity: 1; }

dialog {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 14px; width: min(92vw, 380px); padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
dialog h2 { margin: 0 0 14px; text-align: center; font-size: 18px; }
dialog h3 { font-size: 14px; margin: 18px 0 8px; }
dialog form { text-align: center; margin-top: 18px; }
.stat-row { display: flex; justify-content: space-around; text-align: center; }
.stat-row b { display: block; font-size: 26px; }
.stat-row span { font-size: 12px; color: var(--muted); }
.dist-row { display: flex; align-items: center; gap: 6px; font-size: 13px; margin: 3px 0; }
.dist-row .bar { background: var(--absent); color: #fff; padding: 2px 6px; text-align: right; min-width: 22px; border-radius: 3px; }
.dist-row .bar.hl { background: var(--correct); }

@media (max-height: 640px) {
  :root { --tile: min(12vw, 48px); }
  .key { height: 48px; }
}
.key.split { color: #fff; }
