:root {
  --bg: #f3f5f9;
  --bg-2: #ffffff;
  --ink: #17202a;
  --ink-2: #5b6675;
  --line: #dfe4ec;
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --shadow: 0 10px 30px rgba(20, 30, 50, .10);
  --radius: 14px;
  --stage-bg: radial-gradient(circle at 50% 40%, #ffffff, #e9edf5 70%);
}

[data-theme="dark"] {
  --bg: #0f141b;
  --bg-2: #171e28;
  --ink: #eef2f7;
  --ink-2: #97a3b4;
  --line: #27313e;
  --accent: #7c74ff;
  --accent-ink: #0f141b;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  --stage-bg: radial-gradient(circle at 50% 40%, #1c2532, #0f141b 70%);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

kbd {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 6px;
  font: inherit;
  font-size: 12px;
}

code {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0 4px;
  font-size: 12px;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  flex: none;
}

.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: -.01em; }

.brand-mark {
  width: 22px; height: 22px; border-radius: 50%;
  background:
    conic-gradient(#e74c3c 0 25%, #f4d03f 0 50%, #2ecc71 0 75%, #3498db 0);
  box-shadow: inset 0 0 0 3px var(--bg-2), 0 0 0 1px var(--line);
}

.wheel-title {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 9px;
  color: var(--ink);
  font-size: 16px;
  font-weight: 600;
  padding: 6px 10px;
}
.wheel-title:hover { border-color: var(--line); }
.wheel-title:focus { outline: none; border-color: var(--accent); background: var(--bg); }

.topbar-actions { display: flex; gap: 6px; flex: none; }

.btn {
  font: inherit;
  font-weight: 600;
  color: var(--ink);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 13px;
  cursor: pointer;
  transition: background .12s, border-color .12s, transform .06s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.icon { padding: 8px 10px; line-height: 1; }
.btn.danger { color: #e0464a; }

/* ---------- layout ---------- */

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 360px;
}

.stage {
  background: var(--stage-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  min-width: 0;
  padding: 20px;
  position: relative;
}

.wheel-wrap {
  position: relative;
  width: min(74vh, 100%);
  aspect-ratio: 1;
  max-width: 720px;
}

#wheel {
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
  filter: drop-shadow(0 12px 26px rgba(0, 0, 0, .22));
  touch-action: manipulation;
}

.pointer {
  position: absolute;
  top: 50%;
  right: -6px;
  width: 52px;
  height: 40px;
  transform: translateY(-50%);
  transform-origin: 86% 50%;
  background: var(--bg-2);
  clip-path: polygon(100% 0, 100% 100%, 0 50%);
  filter: drop-shadow(-3px 2px 3px rgba(0, 0, 0, .3));
  pointer-events: none;
  will-change: transform;
}

.spin-hint {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: 19%; height: 19%;
  border-radius: 50%;
  border: none;
  background: var(--bg-2);
  color: var(--ink);
  font: 700 clamp(11px, 1.6vw, 17px)/1 inherit;
  letter-spacing: .06em;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .25);
  display: grid;
  place-items: center;
  padding: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.spin-hint:hover { filter: brightness(1.05); }
.spin-hint:disabled { cursor: default; opacity: .8; }

.stage-note { margin: 0; color: var(--ink-2); font-size: 13px; }

/* ---------- side panel ---------- */

.panel {
  background: var(--bg-2);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.tabs { display: flex; border-bottom: 1px solid var(--line); flex: none; }

.tab {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 13px 8px;
  font: inherit;
  font-weight: 600;
  color: var(--ink-2);
  cursor: pointer;
}
.tab.active { color: var(--ink); border-bottom-color: var(--accent); }

.pill {
  display: inline-block;
  min-width: 20px;
  padding: 1px 6px;
  margin-left: 4px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--line);
  font-size: 11px;
  color: var(--ink-2);
}

.tabpane { display: none; flex: 1; min-height: 0; flex-direction: column; padding: 12px; gap: 10px; }
.tabpane.active { display: flex; }

.panel-tools { display: flex; flex-wrap: wrap; gap: 6px; flex: none; }

.chip {
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-2);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 11px;
  cursor: pointer;
}
.chip:hover { color: var(--ink); border-color: var(--accent); }

#entries {
  flex: 1;
  min-height: 120px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--ink);
  padding: 12px;
  font: 14px/1.7 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#entries:focus { outline: none; border-color: var(--accent); }

.hint { margin: 0; font-size: 12px; color: var(--ink-2); flex: none; }

.results {
  flex: 1;
  overflow-y: auto;
  margin: 0;
  padding: 0 0 0 4px;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.results li {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}
.results li .idx { color: var(--ink-2); font-size: 12px; min-width: 22px; }
.results li .who { font-weight: 600; flex: 1; word-break: break-word; }
.results li .when { color: var(--ink-2); font-size: 11.5px; }

/* ---------- modals ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 22, .55);
  backdrop-filter: blur(3px);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 20px;
}
.modal-backdrop[hidden] { display: none; }

.confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

.winner-card {
  position: relative;
  background: var(--bg-2);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 34px 40px;
  text-align: center;
  max-width: min(640px, 92vw);
  animation: pop .28s cubic-bezier(.2, 1.4, .5, 1);
}

@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.winner-label { margin: 0 0 6px; color: var(--ink-2); font-size: 13px; text-transform: uppercase; letter-spacing: .14em; }

.winner-name {
  margin: 0 0 22px;
  font-size: clamp(30px, 6vw, 60px);
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: -.02em;
  word-break: break-word;
}

.winner-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.sheet {
  background: var(--bg-2);
  border-radius: 18px;
  box-shadow: var(--shadow);
  width: min(520px, 96vw);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
}

.sheet-head, .sheet-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  flex: none;
}
.sheet-head { border-bottom: 1px solid var(--line); }
.sheet-foot { border-top: 1px solid var(--line); justify-content: flex-end; }
.sheet-head h2 { margin: 0; font-size: 17px; flex: 1; }

.sheet-body { padding: 8px 18px 16px; overflow-y: auto; }

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 11px 0;
  border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: none; }
.row > span { color: var(--ink); }
.row input[type="range"] { width: 160px; }
.row output { color: var(--ink-2); font-size: 13px; min-width: 34px; text-align: right; }
.row input[type="range"] + output { grid-column: 3; }
.row:has(input[type="range"]) { grid-template-columns: 1fr auto auto; }

.row select, .row input[type="text"] {
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 7px 10px;
  min-width: 170px;
}
.row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.saved-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.saved-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 10px 12px;
  background: var(--bg);
}
.saved-list .meta { flex: 1; min-width: 0; }
.saved-list .name { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved-list .sub { color: var(--ink-2); font-size: 12px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg-2);
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 13.5px;
  box-shadow: var(--shadow);
  z-index: 90;
  animation: rise .2s ease;
  display: flex;
  align-items: center;
  gap: 12px;
}
.toast[hidden] { display: none; }

.toast-undo {
  font: inherit;
  font-weight: 700;
  color: var(--bg-2);
  background: none;
  border: none;
  border-left: 1px solid rgba(255, 255, 255, .25);
  padding: 0 0 0 12px;
  cursor: pointer;
  text-decoration: underline;
}
[data-theme="dark"] .toast-undo { border-left-color: rgba(0, 0, 0, .25); }
.toast-undo[hidden] { display: none; }

/* ---------- new surfaces ---------- */

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.group-title {
  margin: 18px 0 2px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--ink-2);
}
.group-title:first-child { margin-top: 4px; }

.row > span small {
  display: block;
  font-size: 12px;
  font-weight: 400;
  color: var(--ink-2);
  margin-top: 2px;
}

.row input[type="number"] {
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 7px 10px;
  width: 90px;
}

.row[hidden] { display: none; }

.winner-list {
  list-style: none;
  margin: 0 0 22px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 46vh;
  overflow-y: auto;
  text-align: left;
}
.winner-list[hidden] { display: none; }

.winner-list li {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 10px 16px;
  border-radius: 11px;
  background: var(--bg);
  border: 1px solid var(--line);
  font-size: clamp(17px, 2.6vw, 24px);
  font-weight: 700;
}
.winner-list li span { color: var(--ink-2); font-size: 13px; font-weight: 600; min-width: 26px; }

.teams-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.team {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  padding: 12px 14px;
}
.team h4 {
  margin: 0 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--ink-2);
  display: flex;
  align-items: center;
  gap: 7px;
}
.team h4::before {
  content: "";
  width: 10px; height: 10px;
  border-radius: 3px;
  background: var(--team-color, var(--accent));
}
.team ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.team li { font-weight: 600; word-break: break-word; }

.fairness {
  flex: none;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--bg);
  padding: 0 12px;
}
.fairness[hidden] { display: none; }
.fairness summary { cursor: pointer; padding: 10px 0; font-weight: 600; font-size: 13px; }
.fairness > div { padding-bottom: 10px; }

.fair-row {
  display: grid;
  grid-template-columns: 1fr 42px;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 12.5px;
}
.fair-row .bar {
  grid-column: 1 / -1;
  height: 5px;
  border-radius: 3px;
  background: var(--line);
  overflow: hidden;
  position: relative;
}
.fair-row .bar i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.fair-row .bar b {
  position: absolute;
  top: -2px;
  width: 2px;
  height: 9px;
  background: var(--ink-2);
  border-radius: 1px;
}
.fair-row .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fair-row .n { text-align: right; color: var(--ink-2); }
.fair-note { margin: 8px 0 0; font-size: 12px; color: var(--ink-2); }

.qr-panel {
  position: absolute;
  right: 26px;
  bottom: 26px;
  background: #fff;
  border-radius: 14px;
  padding: 12px 12px 8px;
  box-shadow: var(--shadow);
  text-align: center;
}
.qr-panel[hidden] { display: none; }
.qr-panel canvas { display: block; image-rendering: pixelated; width: 180px; height: 180px; }
.qr-panel p { margin: 6px 0 0; font-size: 11px; font-weight: 600; color: #444; }

body:not(.presenting) .qr-panel { display: none; }

@media (prefers-reduced-motion: reduce) {
  .winner-card { animation: none; }
  .toast { animation: none; }
  .pointer { transition: none; }
}

@keyframes rise { from { transform: translate(-50%, 10px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

/* ---------- fullscreen presentation ---------- */

body.presenting .panel,
body.presenting .topbar { display: none; }
body.presenting .wheel-wrap { width: min(88vh, 96vw); max-width: none; }

/* ---------- responsive ---------- */

@media (max-width: 900px) {
  body { overflow: auto; }
  .layout { grid-template-columns: 1fr; }
  .panel { border-left: none; border-top: 1px solid var(--line); min-height: 46vh; }
  .wheel-wrap { width: min(92vw, 520px); }
  .topbar { flex-wrap: wrap; }
  .wheel-title { order: 3; flex-basis: 100%; }
  .topbar-actions { margin-left: auto; }
}
