:root {
  --bg: #f6f7fb;
  --ink: #19202e;
  --muted: #687386;
  --line: #dfe4ee;
  --panel: #ffffff;
  --input: #ffffff;
  --soft: #fbfcff;
  --accent: #126b7f;
  --accent-strong: #0d5363;
  --good: #16825d;
  --bad: #bd3d35;
  --warn: #9a6a00;
}

body.dark {
  --bg: #11151b;
  --ink: #eef3f6;
  --muted: #a5b1bd;
  --line: #2c3542;
  --panel: #181e27;
  --input: #111820;
  --soft: #141a22;
  --accent: #41b3c8;
  --accent-strong: #7bd6e5;
  --good: #6bdbad;
  --bad: #ff8a83;
  --warn: #f0c15f;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Arial, "Noto Sans KR", sans-serif;
  color: var(--ink);
  background: var(--bg);
}

button, input, select {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: var(--input);
  color: var(--ink);
  border-radius: 6px;
  padding: 10px 12px;
  cursor: pointer;
}

button:hover { border-color: var(--accent); }
button:disabled { opacity: .55; cursor: not-allowed; }

.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}

.primary:hover {
  background: var(--accent-strong);
}

input, select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  background: var(--input);
  color: var(--ink);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 18px;
  align-items: center;
  min-height: 64px;
  padding: 10px 22px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  backdrop-filter: blur(10px);
}

.brand {
  color: var(--ink);
  text-decoration: none;
  font-size: 24px;
  font-weight: 800;
}

.nav {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.nav-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
}

.auth {
  display: flex;
  gap: 8px;
  align-items: center;
}

.theme-toggle {
  min-width: 58px;
}

.auth form {
  display: grid;
  grid-template-columns: 130px 130px auto auto;
  gap: 8px;
  align-items: center;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 26px 18px 60px;
}

.view { display: none; }
.view.active { display: block; }

.layout {
  display: grid;
  grid-template-columns: 290px 1fr;
  gap: 18px;
}

.panel,
.game-surface,
#recordsView,
#rankingView {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
}

.panel h2,
.toolbar h2 {
  margin: 0 0 16px;
  font-size: 20px;
}

.panel label {
  display: block;
  margin: 14px 0 8px;
  color: var(--muted);
  font-weight: 700;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.segmented button.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, var(--panel));
  color: var(--accent-strong);
  font-weight: 700;
}

.checks {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.checks label {
  display: flex;
  gap: 7px;
  align-items: center;
  margin: 0;
  color: var(--ink);
  font-weight: 400;
}

.checks input {
  width: auto;
  min-height: auto;
}

.panel .primary {
  width: 100%;
  margin-top: 18px;
}

#readyRoom {
  width: 100%;
  margin-top: 14px;
  min-height: 56px;
  font-size: 20px;
  font-weight: 900;
  border-width: 2px;
}

#readyRoom.ready-active {
  background: #ffd84d;
  border-color: #d99a00;
  color: #2b2200;
  box-shadow: 0 0 0 4px color-mix(in srgb, #ffd84d 28%, transparent);
}

#readyRoom.ready-done {
  background: color-mix(in srgb, var(--good) 18%, var(--panel));
  border-color: var(--good);
  color: var(--good);
}

.game-surface {
  min-height: 420px;
  display: grid;
  grid-template-rows: auto auto 1fr auto auto auto;
  gap: 16px;
}

.stats {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-weight: 700;
}

.question {
  display: grid;
  place-items: center;
  min-height: 210px;
  font-size: clamp(42px, 9vw, 92px);
  font-weight: 900;
  text-align: center;
}

.answer-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.answer-row input {
  font-size: 22px;
  text-align: center;
}

.result {
  min-height: 54px;
  color: var(--muted);
  font-weight: 700;
}

.result.good { color: var(--good); }
.result.bad { color: var(--bad); }
.result.warn { color: var(--warn); }

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.toolbar h2 {
  margin-right: auto;
  margin-bottom: 0;
}

.toolbar select {
  width: 180px;
}

#recordOperations,
#rankingOperations {
  width: 250px;
  min-width: 220px;
}

.list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--input);
}

.item strong { display: block; margin-bottom: 3px; }
.item small { color: var(--muted); }

.item.current-record {
  border-color: var(--accent);
  outline: 3px solid color-mix(in srgb, var(--accent) 35%, transparent);
  outline-offset: 2px;
}

.metric {
  text-align: right;
  font-size: 20px;
  font-weight: 900;
  color: var(--accent-strong);
}

#recordChart {
  width: 100%;
  max-height: 260px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
}

.compact {
  margin-top: 0;
}

.compact .item {
  padding: 10px;
}

.finish-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.finish-actions span {
  margin-right: auto;
}

.rank-title {
  color: var(--muted);
  font-weight: 800;
}

.duel-state {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 10px;
}

.player {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.player.host {
  border-color: var(--accent);
}

.player-actions {
  margin-top: 8px;
}

.player-actions button {
  padding: 7px 9px;
}

.room-info {
  color: var(--muted);
  font-weight: 800;
  line-height: 1.5;
}

.invite-link {
  margin: 8px 0 2px;
}

.invite-link small {
  display: block;
  color: var(--muted);
  font-weight: 800;
  margin-bottom: 6px;
}

.invite-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px;
}

.invite-row input {
  min-height: 38px;
  font-size: 13px;
}

.invite-row button {
  padding: 8px 10px;
}

.admin-panel {
  max-width: 780px;
  margin: 0 auto;
}

@media (max-width: 820px) {
  .topbar {
    position: static;
    grid-template-columns: auto 1fr auto auto;
    gap: 10px 8px;
    padding: 16px 14px;
    align-items: center;
  }

  .brand {
    grid-column: 1;
    grid-row: 1;
    font-size: 28px;
  }

  .theme-toggle {
    grid-column: 3;
    grid-row: 1;
    min-width: 58px;
    width: auto;
    min-height: 40px;
    padding: 7px 10px;
  }

  .nav {
    grid-column: 1 / -1;
    grid-row: 2;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    margin-top: 2px;
  }

  .nav-btn {
    min-height: 48px;
    padding: 8px 6px;
  }

  .auth {
    grid-column: 4;
    grid-row: 1;
    width: auto;
    justify-content: end;
    gap: 6px;
  }

  .auth button {
    min-height: 40px;
    padding: 7px 10px;
  }

  .auth > strong {
    margin-right: 2px;
    white-space: nowrap;
  }

  .auth:has(form) {
    grid-column: 1 / -1;
    grid-row: 3;
    width: 100%;
  }

  .auth form {
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .auth form button {
    min-height: 46px;
  }

  main {
    padding: 10px 10px 44px;
  }

  .toolbar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    align-items: stretch;
  }

  .toolbar h2 {
    grid-column: 1 / -1;
    margin: 0;
  }

  .toolbar select,
  .toolbar button {
    width: 100%;
    min-width: 0;
  }

  #recordMode,
  #rankingMode {
    grid-column: 1 / -1;
  }

  #recordDifficulty,
  #rankingDifficulty {
    grid-column: 1;
  }

  #recordOperations,
  #rankingOperations {
    grid-column: 2;
    width: 100%;
    min-width: 0;
  }

  #refreshRecords {
    grid-column: 1 / -1;
  }

  .layout {
    grid-template-columns: 1fr;
  }

  .question {
    min-height: 170px;
    font-size: 48px;
  }
}
