:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --ok: #16a34a;
  --board-bg: #dfe3e8;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 6px 20px rgba(0, 0, 0, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --panel: #181b22;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #2a2f3a;
    --accent: #3b82f6;
    --board-bg: #1f232b;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #181b22;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --border: #2a2f3a;
  --accent: #3b82f6;
  --board-bg: #1f232b;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  word-break: keep-all;
  overflow-wrap: anywhere;
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

h1, h2 { margin: 0; line-height: 1.3; }
h2 { font-size: 17px; }

a { color: var(--accent); }

button, input, select { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 7px 12px;
  min-height: 36px;
}
button:disabled { opacity: 0.5; cursor: default; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.ghost { background: transparent; }
.icon {
  min-width: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
}
.wide { width: 100%; }

input, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--panel);
  min-height: 40px;
}
input[type="range"] { padding: 0; min-height: 28px; accent-color: var(--accent); }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span { font-weight: 600; font-size: 14px; display: flex; justify-content: space-between; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.hint { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.error { color: var(--danger); font-size: 14px; margin: 0 0 12px; }
.meta { color: var(--muted); font-size: 13px; }
.empty { color: var(--muted); text-align: center; padding: 24px 0; margin: 0; }

.site-footer { text-align: center; padding: 20px 16px; font-size: 13px; }
.site-footer a { color: var(--muted); }

/* Entry */
.view.entry {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.entry-card { width: 100%; max-width: 400px; }
.entry-card .brand { font-size: 26px; margin-bottom: 8px; }
.lead { color: var(--muted); margin: 0 0 20px; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-size: 18px; flex: 1; }
.me { display: flex; align-items: center; gap: 10px; }
.nickname { font-weight: 600; }

/* Lobby */
.lobby-grid {
  max-width: 1100px;
  margin: 20px auto 0;
  padding: 0 16px;
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 16px;
  align-items: start;
}
.create h2, .rooms h2 { margin-bottom: 14px; }
.rooms-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.rooms-head h2 { margin: 0; flex: 1; }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 3px; border-radius: 9px; }
.tabs button { border: none; background: transparent; min-height: 30px; padding: 4px 12px; border-radius: 7px; }
.tabs button[aria-selected="true"] { background: var(--panel); box-shadow: var(--shadow); font-weight: 600; }

.room-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.room {
  display: grid;
  gap: 4px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  text-decoration: none;
  color: inherit;
}
.room:hover { border-color: var(--accent); }
.room-top { display: flex; align-items: center; gap: 8px; }
.room-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-meta { color: var(--muted); font-size: 13px; }
.room-progress { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.room-progress .progress-bar { flex: 1; }

.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--bg); color: var(--muted); }
.badge.active { background: rgba(22, 163, 74, 0.12); color: var(--ok); }
.badge.won { background: rgba(37, 99, 235, 0.12); color: var(--accent); }

.pager { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 14px; }

.progress-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--bg);
  overflow: hidden;
  min-width: 80px;
}
.progress-bar span { display: block; height: 100%; width: 0; background: var(--ok); transition: width 0.3s; }

/* Game */
.view.game { height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.game-bar { flex-wrap: wrap; row-gap: 8px; }
.room-title { flex: 1; min-width: 140px; display: grid; }
.room-title h1 { font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress { display: grid; gap: 2px; min-width: 180px; }
.my-score { display: flex; align-items: center; gap: 8px; font-weight: 700; font-variant-numeric: tabular-nums; }
.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 4px;
  background: var(--border);
  flex: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}
.offline { color: var(--danger); font-size: 13px; }

.game-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 260px;
}
.board-wrap {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--board-bg);
}
.board-wrap.boom { animation: boom 0.45s ease-out; }
@keyframes boom {
  0% { box-shadow: inset 0 0 0 6px rgba(220, 38, 38, 0.8); }
  100% { box-shadow: inset 0 0 0 0 rgba(220, 38, 38, 0); }
}
#board {
  position: absolute;
  inset: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  cursor: pointer;
}
.minimap {
  position: absolute;
  right: 12px;
  bottom: 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  background: var(--panel);
  touch-action: none;
  cursor: crosshair;
}
.board-controls {
  position: absolute;
  left: 12px;
  top: 12px;
  display: flex;
  gap: 6px;
}
.board-controls button {
  background: var(--panel);
  box-shadow: var(--shadow);
  min-width: 40px;
  font-weight: 600;
}
#mode-toggle[aria-pressed="true"] { background: #fef3c7; border-color: #f59e0b; color: #92400e; }
.coords {
  position: absolute;
  left: 12px;
  bottom: 12px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 8px;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.coords:empty { display: none; }
.banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 8px 14px;
  font-weight: 600;
  text-align: center;
}
.banner.won { background: #dcfce7; border-color: #86efac; color: #14532d; }

.side {
  border-left: 1px solid var(--border);
  background: var(--panel);
  padding: 14px 16px;
  overflow: auto;
  min-height: 0;
}
.side h2 { font-size: 15px; margin-bottom: 10px; }
.participants { list-style: none; margin: 0 0 16px; padding: 0; display: grid; gap: 2px; }
.participants li {
  display: grid;
  grid-template-columns: 22px 14px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 5px 6px;
  border-radius: 6px;
  font-size: 14px;
}
.participants li.me { background: var(--bg); font-weight: 700; }
.participants .rank { color: var(--muted); font-size: 12px; text-align: right; font-variant-numeric: tabular-nums; }
.participants .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.participants .score { font-variant-numeric: tabular-nums; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 0 0 12px; }
.stats div { background: var(--bg); border-radius: 8px; padding: 8px 10px; }
.stats dt { font-size: 12px; color: var(--muted); }
.stats dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.help { color: var(--muted); font-size: 12px; margin: 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  max-width: calc(100% - 32px);
  background: #111827;
  color: #f9fafb;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind="error"] { background: #7f1d1d; }

/* Privacy notice */
.privacy {
  max-width: 760px;
  margin: 20px auto;
  padding: 0 16px;
  display: grid;
  gap: 16px;
}
.notice h2 { font-size: 20px; margin-bottom: 8px; }
.notice p, .notice ul { margin: 0 0 12px; }
.notice ul { padding-left: 22px; }

@media (max-width: 860px) {
  .lobby-grid { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
  .game-body { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .side { border-left: none; border-top: 1px solid var(--border); max-height: 30dvh; padding: 10px 16px; }
  .side .help, .side .stats { display: none; }
  .game-bar .meta#game-meta { display: none; }
  .progress { min-width: 0; flex: 1 1 100%; order: 5; }
  #share-link { display: none; }
  .coords { display: none; }
  .banner { top: 62px; }
}
