:root {
  --bg: #121214;
  --surface: #1e1e22;
  --border: #3a3a40;
  --text: #f2f2f4;
  --muted: #9a9aa3;
  --accent: #e2557b;
  --ok: #538d4e;
  --table: #1b1e24;
  --frame: #272b34;
  --frame-line: #4b5262;
  --bar: #16181c;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #fbfaf8;
    --surface: #ffffff;
    --border: #d3d6da;
    --text: #1a1a1b;
    --muted: #6b6b70;
    --table: #e9e4da;
    --frame: #dcd6ca;
    --frame-line: #b4ab9b;
    --bar: #f4f1ea;
  }
}

* { 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: 640px;
  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; line-height: 1;
}
#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; max-width: 420px; width: 100%; margin: 0 auto; }
.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, textarea {
  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%;
}
textarea { resize: vertical; min-height: 44px; }
input:focus, textarea:focus { border-color: var(--accent); }
button.primary {
  background: var(--accent); color: #fff; border: 0; border-radius: 10px;
  padding: 14px; font-size: 17px; font-weight: 700;
}
button.primary:disabled { opacity: 0.6; cursor: default; }
.or { text-align: center; color: var(--muted); font-size: 13px; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; text-transform: uppercase; letter-spacing: 3px; font-weight: 700; }
.join-row button, .dialog-actions button:not(.primary) {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 0 18px; font-weight: 700;
}
button.link { background: none; border: 0; color: var(--muted); text-decoration: underline; font-size: 14px; padding: 6px; }

/* Album */
#album { display: flex; flex-direction: column; }
.room-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 0 4px; flex-wrap: wrap; }
.chip {
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  padding: 6px 12px; font-size: 13px; color: var(--muted);
}
.chip b { color: var(--text); letter-spacing: 2px; }
#players { display: flex; gap: 6px; flex-wrap: wrap; }
.player {
  display: inline-flex; align-items: center; gap: 5px; font-size: 13px;
  padding: 4px 9px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted);
}
.player.online { color: var(--text); }
.player .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--c); opacity: 0.3; }
.player.online .dot { opacity: 1; }
.player.waiting { border-style: dashed; }

#cards {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px;
  padding: 12px 0 32px;
}
.card {
  position: relative; display: flex; flex-direction: column; text-align: left; padding: 0;
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface); overflow: hidden;
}
.card:active { transform: scale(0.98); }
.card .thumb { position: relative; aspect-ratio: 4 / 3; background: #000; display: block; overflow: hidden; }
.card .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card.secret .thumb img { filter: blur(16px) saturate(0.5); transform: scale(1.15); }
.card .q {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 44px; font-weight: 800; color: #fff; text-shadow: 0 2px 10px rgba(0, 0, 0, 0.6);
}
.card .live {
  position: absolute; left: 6px; top: 6px; display: flex; align-items: center; gap: 4px;
  font-size: 11px; font-weight: 700; color: #fff; background: rgba(0, 0, 0, 0.55);
  padding: 3px 8px 3px 6px; border-radius: 999px;
}
.card .live i { width: 8px; height: 8px; border-radius: 50%; background: var(--c); display: inline-block; }
.card .info { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px 9px; min-width: 0; }
.card .info b { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .info small { font-size: 12px; color: var(--muted); }
.card .bar { height: 3px; background: var(--border); }
.card .bar i { display: block; height: 100%; background: var(--accent); transition: width 0.4s; }
.card.done .bar i { background: var(--ok); }
.card.new { align-items: center; justify-content: center; gap: 2px; border-style: dashed; min-height: 170px; color: var(--muted); }
.card.new .plus { font-size: 40px; line-height: 1; color: var(--accent); }
.card.new b { color: var(--text); }
.empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 16px; line-height: 1.5; }

/* New puzzle dialog */
dialog {
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 16px;
  padding: 18px 20px; width: min(94vw, 420px); max-height: 94vh; overflow: auto;
  display: flex; flex-direction: column; gap: 12px;
}
dialog:not([open]) { display: none; }
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog h2 { margin: 0; font-size: 20px; }
.pick { display: flex; flex-direction: column; align-items: center; gap: 8px; cursor: pointer; }
.pick-empty {
  width: 100%; height: 170px; border: 2px dashed var(--border); border-radius: 12px;
  display: grid; place-items: center; font-size: 48px;
}
#pick-canvas { border-radius: 10px; max-width: 100%; }
.pick-label { font-size: 14px; color: var(--accent); font-weight: 700; }
.range input { width: 100%; accent-color: var(--accent); padding: 0; background: none; border: 0; }
.range span { color: var(--text); font-weight: 600; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.check input, #play-menu input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }
.dialog-actions button { padding: 12px 18px; }

/* Puzzle table */
#play {
  position: fixed; inset: 0; z-index: 10; background: var(--table);
  display: flex; flex-direction: column; overflow: hidden;
}
.play-top, .play-bottom {
  flex: none; display: flex; align-items: center; gap: 6px; background: var(--bar);
  border-bottom: 1px solid var(--border); position: relative; z-index: 2;
}
.play-top { height: calc(52px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 6px 0; }
.play-top .icon-btn { position: static; }
.play-info { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.play-info b { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.play-info > span { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
#play-here { display: flex; }
.who {
  width: 26px; height: 26px; border-radius: 50%; margin-left: -6px;
  background: var(--c); color: #fff; font-size: 13px; font-weight: 700;
  display: grid; place-items: center; border: 2px solid var(--bar);
}
#table { flex: 1 1 0; min-height: 0; width: 100%; display: block; touch-action: none; cursor: grab; }
.play-bottom {
  height: calc(60px + env(safe-area-inset-bottom)); padding: 0 12px env(safe-area-inset-bottom);
  justify-content: center; gap: 14px; border-bottom: 0; border-top: 1px solid var(--border);
}
.round {
  width: 46px; height: 46px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); font-size: 20px; line-height: 1; padding: 0;
}
.round.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.round.heart { color: var(--accent); }
.round:active { transform: scale(0.92); }

#play-menu {
  position: absolute; top: calc(52px + env(safe-area-inset-top)); right: 8px; z-index: 3;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 6px; display: flex; flex-direction: column; min-width: 200px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}
#play-menu label { display: flex; align-items: center; gap: 10px; padding: 10px; font-size: 14px; }
#play-menu button.danger { background: none; border: 0; border-top: 1px solid var(--border); text-align: left; padding: 10px; color: #e25555; font-size: 14px; }

#play-loading, #preview, #done {
  position: absolute; inset: 0; z-index: 4; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 12px;
}
#play-loading { background: var(--table); color: var(--muted); }
.spinner {
  width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#preview { background: rgba(0, 0, 0, 0.88); cursor: zoom-out; padding: 16px; }
#preview img { max-width: 100%; max-height: 100%; border-radius: 8px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5); }
#done { background: rgba(0, 0, 0, 0.45); padding: 16px; }
.done-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 22px 20px;
  width: min(92vw, 380px); text-align: center; display: flex; flex-direction: column; gap: 10px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
}
.done-card h2 { margin: 0; font-size: 26px; }
.done-card p { margin: 0; color: var(--muted); }
.done-msg { color: var(--text) !important; font-size: 17px; white-space: pre-wrap; padding: 10px 12px; background: var(--bg); border-radius: 10px; }
#done-snaps { font-size: 13px; color: var(--muted); display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.snap-who { display: inline-flex; align-items: center; gap: 5px; color: var(--text); }
.snap-who i { width: 9px; height: 9px; border-radius: 50%; background: var(--c); }
.result-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
.result-actions button:not(.primary) { background: none; border: 1px solid var(--border); border-radius: 10px; padding: 12px; }

#fx { position: absolute; inset: 0; z-index: 3; pointer-events: none; overflow: hidden; }
.fx-heart {
  position: absolute; bottom: 60px; opacity: 0; line-height: 1;
  animation: rise 3.4s ease-out forwards;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
}
@keyframes rise {
  0% { transform: translateY(0) scale(0.5) rotate(-10deg); opacity: 0; }
  12% { opacity: 1; }
  100% { transform: translateY(-85vh) scale(1.2) rotate(12deg); opacity: 0; }
}

#toast {
  position: fixed; top: 70px; left: 50%; transform: translateX(-50%) translateY(-10px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-weight: 700; font-size: 14px; opacity: 0; transition: opacity 0.15s, transform 0.15s;
  pointer-events: none; z-index: 100; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
