:root {
  --bg: #1f1b17;
  --surface: #2a2521;
  --border: #46403a;
  --text: #f5f0ea;
  --muted: #a89f95;
  --accent: #e8703a;
  --accent2: #5ec26a;
  --gold: #ffd36b;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #fbf6ee;
    --surface: #ffffff;
    --border: #e0d6c8;
    --text: #221c17;
    --muted: #7a6f65;
  }
}

* { 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;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#app {
  max-width: 520px;
  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; }
#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[type="text"], input:not([type]) {
  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, .panel button {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 10px; padding: 12px 16px; font-weight: 600; white-space: nowrap;
}
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:disabled { opacity: .5; cursor: default; }
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; font-size: 14px; padding: 6px; }
button.big { font-size: 18px; padding: 14px; }

/* Room */
#room { display: flex; flex-direction: column; gap: 12px; padding: 12px 0 24px; }
.room-bar { display: flex; justify-content: space-between; align-items: center; 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 { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
#players li {
  display: flex; align-items: center; gap: 6px; font-size: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px 6px 8px;
}
#players .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--c); opacity: .35; }
#players li.online .dot { opacity: 1; box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 30%, transparent); }
#players .tag { font-size: 11px; color: var(--muted); }
#players .tag.rtc { color: var(--accent2); }
#players .tag.relay { color: var(--gold); }
#players li.waiting { color: var(--muted); border-style: dashed; }
#room h2 { font-size: 15px; margin: 8px 0 0; color: var(--muted); }
#room h3 { font-size: 13px; margin: 10px 0 4px; color: var(--muted); }
#levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
#levels .chapter { grid-column: 1 / -1; font-size: 12px; font-weight: 700; color: var(--muted); margin-top: 6px; }
.level {
  text-align: left; background: var(--surface); border: 2px solid var(--border); border-radius: 12px; padding: 10px;
  display: flex; flex-direction: column; gap: 4px; min-height: 76px;
}
.level.on { border-color: var(--accent); }
.level:disabled { cursor: default; }
.level b { font-size: 14px; }
.level .stars { color: var(--gold); font-size: 13px; letter-spacing: 1px; }
.level .stars .off { opacity: .25; }
.level .best { font-size: 11px; color: var(--muted); }
#level-detail { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; line-height: 1.5; }
.stars-line { color: var(--gold); font-size: 12px; margin-top: 4px; }
.recipes { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; text-align: left; }
.recipes li { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.recipes canvas { width: 36px; height: 36px; flex: none; }
.recipes b { display: block; }
.recipes span { color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.controls { color: var(--muted); font-size: 13px; }
.controls summary { cursor: pointer; }
.controls p { margin: 6px 0 0; }
#start-hint { text-align: center; }

/* Game */
#game { position: fixed; inset: 0; background: #1f1b17; color: #fff; overflow: hidden; touch-action: none; }
#stage { position: absolute; inset: 0; }
#canvas { display: block; }
#btn-pause {
  position: absolute; top: 10px; right: 10px; width: 44px; height: 40px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,.25); background: rgba(255,255,255,.12); color: #fff; font-size: 18px;
}
#conn { position: absolute; left: 50%; transform: translateX(-50%); top: 62px; font-size: 11px; color: rgba(255,255,255,.7); pointer-events: none; }
#conn.bad { color: #ff8c6b; }

#touch { position: absolute; inset: 0; pointer-events: none; display: none; }
#game.touch #touch { display: block; }
.joy-zone { position: absolute; left: 0; top: 60px; bottom: 0; width: 45%; pointer-events: auto; }
.joy-base {
  position: absolute; width: 110px; height: 110px; margin: -55px 0 0 -55px; border-radius: 50%;
  background: rgba(255,255,255,.12); border: 2px solid rgba(255,255,255,.3);
}
.joy-knob { position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%; background: rgba(255,255,255,.6); }
.touch-btns { position: absolute; right: 0; bottom: 0; width: 230px; height: 140px; pointer-events: none; }
.tbtn {
  position: absolute; width: 68px; height: 68px; border-radius: 50%; border: 2px solid rgba(255,255,255,.4);
  background: rgba(255,255,255,.18); color: #fff; font-weight: 700; font-size: 14px; touch-action: none; user-select: none; pointer-events: auto;
}
.tbtn-a { width: 92px; height: 92px; right: 18px; bottom: 18px; background: rgba(232,112,58,.55); border-color: rgba(255,255,255,.6); font-size: 16px; }
.tbtn-b { right: 126px; bottom: 44px; background: rgba(94,194,106,.45); }
.tbtn.on { filter: brightness(1.4); transform: scale(.95); }

.overlay { position: absolute; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 5; }
.panel {
  background: var(--surface, #2a2521); color: var(--text, #fff); border: 1px solid var(--border, #46403a); border-radius: 16px;
  padding: 20px 24px; min-width: 260px; max-width: 92vw; max-height: 92vh; overflow: auto; text-align: center;
  display: flex; flex-direction: column; gap: 10px;
}
.panel h2 { margin: 0 0 4px; font-size: 20px; }
.panel h3 { margin: 8px 0 0; font-size: 13px; color: var(--muted); text-align: left; }
.pause-row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
#pause-overlay .panel { min-width: 320px; max-width: min(92vw, 560px); }
#pause-controls { font-size: 12px; }
.panel p { margin: 0; }
#tap-start .panel { font-size: 18px; font-weight: 700; padding: 28px 36px; }
#result-level { color: var(--muted); font-size: 14px; }
#result-stars { font-size: 34px; letter-spacing: 4px; color: var(--gold); }
#result-stars .off { opacity: .25; }
#result-score { font-size: 28px; font-weight: 800; }
#result-score small { display: block; font-size: 13px; color: var(--muted); font-weight: 600; }
#result-score .new { color: var(--accent2); }
#result-stats { display: flex; justify-content: center; gap: 18px; font-size: 13px; color: var(--muted); }
#result-stats b { display: block; font-size: 18px; color: var(--text); }
.result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin-top: 6px; }

#rotate { display: none; position: absolute; inset: 0; background: #1f1b17; z-index: 9; align-items: center; justify-content: center; font-size: 18px; text-align: center; padding: 20px; }
@media (orientation: portrait) and (pointer: coarse) { #game.touch #rotate { display: flex; } }

#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: 20;
}
#toast.show { opacity: 1; }
