:root {
  --bg: #0b1020;
  --bg-soft: #10182b;
  --panel: rgba(20, 30, 52, .92);
  --panel-strong: #16213b;
  --line: rgba(255, 255, 255, .11);
  --text: #f8fbff;
  --muted: #aebbd3;
  --accent: #ff7a00;
  --accent-2: #ffac42;
  --blue: #4e7cff;
  --green: #24c982;
  --red: #ff5e6c;
  --yellow: #ffd15c;
  --shadow: 0 24px 70px rgba(0, 0, 0, .35);
  --radius: 20px;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(circle at 12% 12%, rgba(78, 124, 255, .18), transparent 32%),
    radial-gradient(circle at 88% 18%, rgba(255, 122, 0, .16), transparent 28%),
    linear-gradient(145deg, #080d19, var(--bg));
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow-x: hidden;
}
button, input, select, textarea { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid rgba(255, 172, 66, .65);
  outline-offset: 2px;
}
.hidden { display: none !important; }

.topbar {
  height: 74px;
  padding: 0 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: rgba(8, 13, 25, .82);
  backdrop-filter: blur(18px);
  position: sticky;
  top: 0;
  z-index: 30;
}
.brand {
  border: 0;
  background: transparent;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  text-align: left;
  padding: 0;
}
.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #111827;
  border-radius: 13px;
  font-weight: 900;
  font-size: 22px;
  box-shadow: 0 10px 25px rgba(255, 122, 0, .28);
}
.brand strong, .brand small { display: block; }
.brand strong { font-size: 18px; }
.brand small { color: var(--muted); font-size: 12px; margin-top: 1px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

main { min-height: calc(100vh - 74px); }
.screen { display: none; min-height: calc(100vh - 74px); padding: 28px; }
.screen.active { display: block; }
.home-screen.active { display: grid; }
.home-screen {
  grid-template-columns: minmax(0, 1fr) minmax(460px, 650px);
  align-items: center;
  gap: 55px;
  max-width: 1450px;
  margin: auto;
}
.hero-copy { max-width: 670px; padding-left: clamp(0px, 5vw, 60px); }
.eyebrow {
  display: inline-block;
  color: var(--accent-2);
  text-transform: uppercase;
  letter-spacing: .13em;
  font-size: 12px;
  font-weight: 800;
}
h1, h2, h3, p { margin-top: 0; }
h1 { font-size: clamp(46px, 6.5vw, 86px); line-height: .94; letter-spacing: -.055em; margin: 18px 0 24px; }
h2 { line-height: 1.12; }
.hero-copy p { color: var(--muted); font-size: clamp(17px, 1.7vw, 22px); line-height: 1.65; max-width: 640px; }
.hero-actions { display: flex; gap: 14px; margin-top: 34px; flex-wrap: wrap; }
.hero-demo {
  position: relative;
  min-height: 610px;
  border: 1px solid var(--line);
  background: linear-gradient(145deg, rgba(19, 31, 57, .95), rgba(10, 16, 31, .95));
  border-radius: 35px;
  box-shadow: var(--shadow);
  padding: 26px;
  overflow: hidden;
  transform: rotate(1.5deg);
}
.hero-demo::after {
  content: "";
  position: absolute;
  inset: -40%;
  background: radial-gradient(circle, rgba(255, 122, 0, .12), transparent 46%);
  animation: pulseGlow 5s ease-in-out infinite;
  pointer-events: none;
}
.demo-grid {
  position: absolute;
  inset: 26px 26px 115px;
  display: grid;
  grid-template-columns: repeat(13, 1fr);
  grid-template-rows: repeat(11, 1fr);
  gap: 3px;
}

.demo-cell { border-radius: 4px; background: rgba(255, 255, 255, .035); position: relative; }
.demo-cell.wall { background: #334568; box-shadow: inset 0 0 0 1px rgba(255,255,255,.05); }
.demo-cell.path::after { content: ""; width: 5px; height: 5px; background: var(--yellow); border-radius: 50%; position: absolute; inset: 50%; transform: translate(-50%, -50%); }
.demo-cell.player::before { content: ""; width: 65%; aspect-ratio: 1; border-radius: 50% 50% 45% 45%; background: var(--accent); position: absolute; left: 17%; top: 12%; box-shadow: 0 0 20px rgba(255,122,0,.55); }
.demo-cell.answer::before { content: "1789"; position: absolute; inset: 8%; background: #fff; color: #111827; border-radius: 7px; display: grid; place-items: center; font-weight: 900; font-size: 10px; z-index: 2; }
.demo-mission {
  position: absolute;
  bottom: 22px;
  left: 22px;
  right: 22px;
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(8,13,25,.9);
  padding: 18px 20px;
  border-radius: 17px;
  z-index: 2;
}
.demo-mission span { color: var(--accent-2); font-size: 11px; text-transform: uppercase; letter-spacing: .14em; }
.demo-mission strong { display: block; margin-top: 6px; font-size: 15px; }
@keyframes pulseGlow { 0%,100%{transform:scale(.9)} 50%{transform:scale(1.2)} }

.primary, .secondary, .danger, .preset, .text-button, .session-code, .icon-button {
  border: 0;
  cursor: pointer;
  transition: transform .16s ease, background .16s ease, border-color .16s ease, opacity .16s ease;
}
.primary:hover, .secondary:hover, .danger:hover, .preset:hover { transform: translateY(-2px); }
.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #141414;
  border-radius: 13px;
  padding: 13px 18px;
  font-weight: 850;
  box-shadow: 0 12px 30px rgba(255, 122, 0, .22);
}
.secondary, .preset {
  background: rgba(255,255,255,.06);
  border: 1px solid var(--line);
  border-radius: 13px;
  padding: 12px 17px;
  font-weight: 750;
}
.secondary:hover, .preset:hover { background: rgba(255,255,255,.1); }
.danger { background: rgba(255, 94, 108, .16); color: #ff9ba5; border: 1px solid rgba(255,94,108,.35); border-radius: 13px; padding: 12px 17px; font-weight: 800; }
.large { padding: 17px 24px; font-size: 17px; }
.small { padding: 8px 12px; font-size: 13px; }
.wide { width: 100%; }
.text-button { background: transparent; color: var(--accent-2); padding: 12px 0; text-decoration: underline; }
.icon-button { width: 40px; height: 40px; border-radius: 12px; background: rgba(255,255,255,.06); border: 1px solid var(--line); display: grid; place-items: center; }
button:disabled { opacity: .45; cursor: not-allowed; transform: none !important; }

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 28px;
  padding: 5px 10px;
  border-radius: 999px;
  background: rgba(78, 124, 255, .14);
  color: #a9c2ff;
  border: 1px solid rgba(78,124,255,.28);
  font-weight: 750;
  font-size: 12px;
}
.status-badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-badge.success { background: rgba(36,201,130,.13); color: #79e7b5; border-color: rgba(36,201,130,.25); }
.status-badge.warning { background: rgba(255,209,92,.12); color: #ffe18e; border-color: rgba(255,209,92,.24); }
.status-badge.error { background: rgba(255,94,108,.12); color: #ff9da7; border-color: rgba(255,94,108,.25); }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.lobby-screen.active { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 24px; max-width: 1300px; margin: auto; align-items: start; }
.lobby-main, .settings-summary { padding: 26px; }
.section-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; }
.section-heading.compact { align-items: center; }
.section-heading.compact h3 { margin: 0; }
.section-heading.compact span { color: var(--muted); font-size: 12px; }
.session-code { background: rgba(255,122,0,.12); border: 1px dashed rgba(255,172,66,.5); color: var(--accent-2); padding: 6px 10px; border-radius: 10px; letter-spacing: .15em; font-size: 25px; }
.players-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; margin: 25px 0; }
.player-card { min-height: 118px; background: rgba(255,255,255,.035); border: 1px solid var(--line); border-radius: 17px; padding: 17px; display: flex; align-items: center; gap: 14px; }
.player-avatar { width: 58px; height: 58px; border-radius: 18px; display: grid; place-items: center; font-weight: 950; color: #0b1020; box-shadow: inset 0 -8px 20px rgba(0,0,0,.18); }
.player-card strong, .player-card small { display: block; }
.player-card small { color: var(--muted); margin-top: 5px; }
.empty-player { border-style: dashed; opacity: .65; justify-content: center; color: var(--muted); }
.lobby-actions { display: flex; justify-content: flex-end; gap: 12px; }
.hint { color: var(--muted); line-height: 1.55; }
.settings-summary h3 { margin-bottom: 18px; }
.summary-row { display: flex; justify-content: space-between; gap: 20px; padding: 10px 0; border-bottom: 1px solid var(--line); color: var(--muted); }
.summary-row strong { color: var(--text); text-align: right; }

.game-screen { padding: 14px; }
.game-screen.active { display: block; }
.game-layout { display: grid; grid-template-columns: minmax(0, 1fr) 355px; gap: 14px; max-width: 1750px; margin: auto; }
.game-stage { padding: 12px; min-width: 0; }
.game-toolbar { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 15px; padding: 0 6px 10px; }
.game-toolbar strong { margin-left: 10px; }
.game-toolbar-right { display: flex; gap: 8px; }
.canvas-wrap { position: relative; min-height: 540px; border-radius: 16px; overflow: hidden; background: #050914; border: 1px solid rgba(255,255,255,.08); }
#gameCanvas { display: block; width: 100%; height: min(74vh, 690px); touch-action: none; }
.countdown-overlay, .pause-overlay, .blocked-overlay { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(4,7,14,.72); backdrop-filter: blur(4px); z-index: 4; }
.countdown-overlay { font-size: clamp(90px, 14vw, 180px); color: var(--accent-2); font-weight: 950; text-shadow: 0 0 50px rgba(255,122,0,.5); }
.pause-overlay { text-align: center; align-content: center; gap: 8px; }
.pause-overlay strong { font-size: 65px; letter-spacing: .15em; }
.pause-overlay span { color: var(--muted); }
.blocked-overlay { inset: auto 50% 18px auto; transform: translateX(50%); background: rgba(255,94,108,.92); border-radius: 999px; padding: 10px 17px; display: block; font-weight: 850; }
.controls-help { margin: 9px 5px 0; color: var(--muted); font-size: 12px; }
.game-sidebar { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.game-sidebar .panel { padding: 17px; box-shadow: none; }
.mission-card h2 { font-size: clamp(19px, 1.7vw, 25px); margin: 10px 0; }
.mission-meta { display: flex; flex-wrap: wrap; gap: 7px; }
.mission-meta span { border-radius: 999px; padding: 5px 9px; background: rgba(78,124,255,.12); color: #b8caff; font-size: 11px; }
.mission-progress { margin-top: 16px; display: grid; gap: 7px; }
.progress-player { display: flex; justify-content: space-between; gap: 10px; padding: 8px 9px; background: rgba(255,255,255,.035); border-radius: 10px; color: var(--muted); font-size: 12px; }
.progress-player.done { color: #8aebbf; background: rgba(36,201,130,.09); }
.score-row { display: flex; align-items: center; justify-content: space-between; }
.score-row strong { font-size: 36px; color: var(--accent-2); }
.personal-status { margin-top: 5px; color: var(--muted); font-size: 13px; }
.ranking-item { display: grid; grid-template-columns: 25px 1fr auto; gap: 9px; align-items: center; padding: 9px 0; border-bottom: 1px solid var(--line); }
.ranking-item:last-child { border-bottom: 0; }
.rank-dot { width: 12px; height: 12px; border-radius: 4px; }
.ranking-item small { display: block; color: var(--muted); }
.button-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.button-grid .wide { grid-column: 1 / -1; }
.host-answer { color: var(--accent-2); font-size: 12px; margin: 12px 0 0; }

.results-screen.active { display: grid; place-items: center; }
.results-panel { width: min(1100px, 100%); padding: 32px; text-align: center; }
.results-panel h1 { font-size: clamp(42px, 6vw, 72px); margin: 10px 0 30px; }
.results-podium { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; align-items: end; margin-bottom: 25px; }
.podium-card { background: rgba(255,255,255,.045); border: 1px solid var(--line); border-radius: 18px; padding: 20px 15px; }
.podium-card.first { min-height: 170px; border-color: rgba(255,209,92,.38); background: rgba(255,209,92,.08); order: 2; }
.podium-card.second { order: 1; }
.podium-card.third { order: 3; }
.podium-rank { font-size: 36px; display: block; }
.podium-score { font-size: 28px; color: var(--accent-2); font-weight: 900; }
.table-wrap { overflow: auto; border-radius: 14px; border: 1px solid var(--line); }
table { width: 100%; border-collapse: collapse; min-width: 720px; }
th, td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.results-actions { display: flex; justify-content: center; gap: 12px; margin-top: 25px; }

.modal {
  width: min(560px, calc(100vw - 28px));
  max-height: calc(100vh - 30px);
  overflow: auto;
  color: var(--text);
  background: #111a2f;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 22px;
  box-shadow: 0 35px 100px rgba(0,0,0,.65);
  padding: 25px;
}
.modal::backdrop { background: rgba(3,6,13,.78); backdrop-filter: blur(7px); }
.large-modal { width: min(940px, calc(100vw - 28px)); }
.modal-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 22px; }
.modal-heading h2 { margin: 7px 0 0; font-size: 29px; }
.modal label { display: grid; gap: 7px; color: var(--muted); font-size: 13px; font-weight: 700; }
.modal input, .modal select, .modal textarea {
  width: 100%;
  color: var(--text);
  background: #0a1121;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 11px;
  padding: 11px 12px;
}
.modal textarea { resize: vertical; }
.modal form > label { margin-bottom: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.form-error { color: #ff9da7; min-height: 20px; font-size: 13px; margin-top: 8px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; }
fieldset { border: 1px solid var(--line); border-radius: 15px; padding: 16px; display: grid; gap: 12px; min-width: 0; }
legend { color: var(--text); font-weight: 850; padding: 0 7px; }
.checkbox { display: flex !important; grid-template-columns: auto 1fr; align-items: center; gap: 8px !important; }
.checkbox input { width: 17px; height: 17px; accent-color: var(--accent); }
.preset-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 15px; }
.preset.active { border-color: rgba(255,172,66,.65); background: rgba(255,122,0,.15); }
.rules-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 13px; }
.rules-grid article { background: rgba(255,255,255,.04); border: 1px solid var(--line); border-radius: 16px; padding: 18px; }
.rules-grid article > span { width: 34px; height: 34px; border-radius: 10px; background: rgba(255,122,0,.15); color: var(--accent-2); display: grid; place-items: center; font-weight: 900; }
.rules-grid h3 { margin: 13px 0 8px; }
.rules-grid p { color: var(--muted); line-height: 1.55; margin-bottom: 0; }

.duel-modal { width: min(720px, calc(100vw - 28px)); }
.duel-header { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; }
.duel-timer { width: 80px; height: 80px; border-radius: 22px; background: rgba(255,122,0,.13); border: 1px solid rgba(255,172,66,.35); display: grid; place-items: center; align-content: center; }
.duel-timer span { font-size: 30px; font-weight: 950; line-height: 1; }
.duel-timer small { color: var(--muted); font-size: 9px; }
.duel-opponent { color: var(--muted); }
#duelPrompt { font-size: 23px; line-height: 1.4; margin: 18px 0; }
.duel-answers { display: grid; gap: 9px; margin-bottom: 14px; }
.answer-choice { display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,.045); border: 1px solid var(--line); padding: 12px; border-radius: 12px; cursor: pointer; color: var(--text) !important; }
.answer-choice:has(input:checked) { border-color: rgba(255,172,66,.65); background: rgba(255,122,0,.12); }
.answer-choice input { width: auto; accent-color: var(--accent); }
.duel-feedback { min-height: 25px; margin-top: 13px; font-weight: 800; }
.duel-feedback.success { color: #75e8b2; }
.duel-feedback.error { color: #ff9da7; }

.questions-modal { width: min(1250px, calc(100vw - 24px)); }
.questions-layout { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(360px, .75fr); gap: 18px; }
.question-tools { display: grid; grid-template-columns: 1fr 190px auto auto; gap: 8px; margin-bottom: 10px; }
.file-button { cursor: pointer; text-align: center; }
.file-button input { display: none; }
.questions-list { border: 1px solid var(--line); border-radius: 14px; max-height: 610px; overflow: auto; }
.question-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; padding: 13px; border-bottom: 1px solid var(--line); cursor: pointer; }
.question-row:last-child { border-bottom: 0; }
.question-row:hover { background: rgba(255,255,255,.04); }
.question-row strong { display: block; }
.question-row small { color: var(--muted); }
.question-row-actions { display: flex; align-items: center; gap: 5px; }
.question-editor { border: 1px solid var(--line); border-radius: 14px; padding: 15px; display: grid; grid-template-columns: 1fr 1fr; gap: 11px; align-content: start; }
.question-editor label { margin: 0 !important; }
.wide-field { grid-column: 1 / -1; }
.question-editor-actions { display: flex; gap: 8px; justify-content: flex-end; }

.toast-stack { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: grid; gap: 9px; width: min(360px, calc(100vw - 36px)); }
.toast { background: #17223c; border: 1px solid var(--line); border-left: 4px solid var(--blue); border-radius: 13px; padding: 13px 15px; box-shadow: 0 16px 45px rgba(0,0,0,.35); animation: toastIn .25s ease both; }
.toast.coin { border-left-color: var(--yellow); }
.toast.error { border-left-color: var(--red); }
.toast.mission-success { border-left-color: var(--green); }
@keyframes toastIn { from { transform: translateX(25px); opacity: 0; } }

@media (max-width: 1100px) {
  .home-screen { grid-template-columns: 1fr; padding-top: 60px; }
  .hero-demo { min-height: 500px; transform: none; }
  .game-layout { grid-template-columns: 1fr; }
  .game-sidebar { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); }
  .mission-card, .host-controls { grid-column: 1 / -1; }
  #gameCanvas { height: 62vh; }
  .questions-layout { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .topbar { padding: 0 14px; }
  .brand small, #connectionBadge, #rulesButton { display: none; }
  .screen { padding: 16px; }
  .home-screen { gap: 30px; }
  .hero-copy { padding-left: 0; }
  h1 { font-size: 49px; }
  .hero-demo { min-height: 390px; padding: 14px; }
  .demo-grid { inset: 14px 14px 105px; }
  .lobby-screen.active { grid-template-columns: 1fr; }
  .players-grid, .form-grid, .rules-grid, .game-sidebar { grid-template-columns: 1fr; }
  .game-screen { padding: 7px; }
  .game-layout { gap: 7px; }
  #gameCanvas { height: 58vh; }
  .mission-card { grid-column: auto; }
  .question-tools { grid-template-columns: 1fr; }
  .question-editor { grid-template-columns: 1fr; }
  .wide-field { grid-column: auto; }
  .results-podium { grid-template-columns: 1fr; align-items: stretch; }
  .podium-card.first, .podium-card.second, .podium-card.third { order: initial; min-height: auto; }
}

@media print {
  body { background: white; color: black; }
  .topbar, .results-actions { display: none; }
  .screen { display: none !important; }
  #resultsScreen { display: block !important; padding: 0; }
  .results-panel { box-shadow: none; border: 0; background: white; }
  th, td { border-color: #ccc; }
}
