:root {
  --ink: #1f1a33;
  --muted: #6b6485;
  --paper: #f8f7fc;
  --panel: #ffffff;
  --line: #ddd7ee;
  --primary: #5b3ca8;
  --primary-dark: #432a80;
  --correct: #168a54;
  --wrong: #b4232a;
  --gold: #e0a928;
}

* {
  box-sizing: border-box;
}

.hidden {
  display: none !important;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(135deg, rgba(91, 60, 168, 0.14), transparent 32%),
    linear-gradient(315deg, rgba(224, 169, 40, 0.14), transparent 36%),
    #f3f1fa;
  font-family: Arial, Helvetica, sans-serif;
}

.game {
  position: relative;
  display: grid;
  grid-template-rows: auto auto auto auto;
  gap: 12px;
  padding: 16px;
}

.topbar,
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

h1,
h2,
p {
  margin: 0;
}

h2 {
  font-size: 18px;
}

#contextLabel,
.section-heading p {
  color: var(--muted);
  font-size: 13px;
}

button {
  border: 0;
  border-radius: 8px;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.52;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.header-actions button {
  min-height: 40px;
  padding: 10px 14px;
  color: white;
  background: var(--primary);
}

.header-actions #startButton {
  background: var(--correct);
}

.header-actions #editButton {
  background: var(--primary-dark);
}

.header-actions #resetButton {
  background: #6b7280;
}

.hud {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
}

/* Non-hosts don't see the Status tile, so the remaining two share the row. */
.hud.solo {
  grid-template-columns: 1fr 1fr;
}

.meter,
.players-panel,
.action-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 12px 26px rgba(31, 26, 51, 0.08);
}

.meter {
  display: grid;
  gap: 4px;
  min-width: 0;
  padding: 10px 12px;
}

.meter span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.meter strong {
  overflow: hidden;
  font-size: 18px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Status is the primary at-a-glance indicator, so it is styled most prominently. */
.meter-phase {
  border-color: var(--primary-dark);
  border-left: 6px solid var(--gold);
  background: linear-gradient(135deg, rgba(67, 42, 128, 0.96), rgba(67, 42, 128, 0.88));
  box-shadow: 0 12px 26px rgba(31, 26, 51, 0.22);
}

.meter-phase span {
  color: var(--gold);
  letter-spacing: 0.04em;
}

.meter-phase strong {
  color: white;
  font-size: 22px;
  line-height: 1.15;
}

.meter-timer.urgent strong {
  color: var(--wrong);
}

.table {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 380px);
  gap: 16px;
  align-items: start;
}

/* Non-hosts don't see the Players panel, so the quiz panel takes the full row. */
.table.solo {
  grid-template-columns: 1fr;
}

.players-panel,
.action-panel {
  display: grid;
  gap: 12px;
  padding: 14px;
}

.players {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.player {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-left: 3px solid transparent;
  border-radius: 999px;
  background: var(--panel);
}

.player strong {
  overflow: hidden;
  color: inherit;
  font-size: 12px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player.eliminated {
  color: #94908f;
  background: #f1eff5;
  border-left-color: var(--wrong);
}

.player.eliminated strong {
  text-decoration: line-through;
}

.player.answered {
  border-left-color: var(--correct);
}

.player.current {
  border-color: var(--gold);
  border-left-color: var(--gold);
  background: rgba(224, 169, 40, 0.14);
  box-shadow: 0 0 0 2px var(--gold);
}

.actions {
  display: grid;
  gap: 10px;
}

.choice {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  padding: 12px 14px;
  color: var(--ink);
  background: #f1effa;
  text-align: left;
}

.choice:not(:disabled):hover,
.choice:not(:disabled):focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.choice.selected {
  color: white;
  background: var(--primary);
}

.choice.correct {
  color: white;
  background: var(--correct);
}

.choice.wrong {
  color: white;
  background: var(--wrong);
}

.choice span {
  color: inherit;
  font-size: 13px;
  opacity: 0.78;
}

.question-prompt {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  font-size: 20px;
  font-weight: 900;
  line-height: 1.3;
}

.notice {
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: #334155;
  background: #f8fafc;
  line-height: 1.45;
  white-space: pre-line;
}

.outcome {
  display: grid;
  gap: 6px;
  padding: 14px;
  border: 1px solid var(--line);
  border-left: 6px solid var(--muted);
  border-radius: 8px;
  background: var(--panel);
}

.outcome strong {
  font-size: 18px;
  line-height: 1.25;
}

.outcome p {
  color: #475569;
  font-size: 14px;
  line-height: 1.45;
}

.outcome.correct {
  border-left-color: var(--correct);
  background: #f0fdf4;
}

.outcome.wrong {
  border-left-color: var(--wrong);
  background: #fef2f2;
}

.outcome.safe {
  border-left-color: var(--primary);
  background: #f3f1fa;
}

.quiz-title {
  margin: 0;
  color: var(--ink);
  font-size: 22px;
  font-weight: 900;
  line-height: 1.25;
}

.top-rank-list {
  display: grid;
  gap: 12px;
}

.top-rank-list > strong {
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
  text-align: center;
  text-transform: uppercase;
}

.top-rank-list ol {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.top-rank-item {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 46px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  box-shadow: 0 8px 18px rgba(31, 26, 51, 0.08);
  animation: rank-pop 220ms ease both;
  animation-delay: calc((var(--rank-order, 1) - 1) * 45ms);
}

.top-rank-item.rank-1 {
  --rank-order: 1;
  border-color: var(--gold);
  background: #fff8df;
}

.top-rank-item.rank-2 {
  --rank-order: 2;
}

.top-rank-item.rank-3 {
  --rank-order: 3;
}

.top-rank-item.rank-4 {
  --rank-order: 4;
}

.top-rank-item.rank-5 {
  --rank-order: 5;
}

.rank-badge {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  color: white;
  background: var(--primary);
  font-size: 13px;
  font-weight: 900;
}

.rank-name {
  min-width: 0;
  overflow: hidden;
  font-size: 14px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-value {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

@keyframes rank-pop {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* --- Setup builder --- */

.setup-form {
  min-width: 0;
  display: grid;
  gap: 14px;
}

.setup-global {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.setup-actions {
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}

.setup-question-toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 14px;
}

.setup-global label {
  min-width: 0;
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.setup-global select,
.setup-global input,
.question-row input[type="text"],
.question-row select {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: white;
  font: inherit;
}

.question-row {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.question-row-head {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.question-row-number {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  color: white;
  background: var(--primary);
  font-size: 13px;
  font-weight: 900;
}

.question-type-select {
  flex: 1 1 0;
}

.question-summary {
  flex: 1 1 0;
  min-width: 0;
  overflow: hidden;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.setup-link {
  min-height: 28px;
  padding: 0;
  color: var(--primary);
  background: transparent;
  font-size: 13px;
  text-align: left;
}

.setup-link:hover,
.setup-link:focus-visible {
  color: var(--primary-dark);
  text-decoration: underline;
  outline: 0;
}

.question-toggle {
  flex: 0 0 auto;
}

.remove-question-button {
  flex: 0 0 auto;
  min-height: 32px;
  padding: 0 10px;
  color: white;
  background: var(--wrong);
}

.choice-fields {
  display: grid;
  gap: 6px;
}

.choice-field-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.choice-field-row input[type="text"] {
  flex: 1;
}

.truefalse-fields {
  display: flex;
  gap: 16px;
}

.truefalse-fields label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 800;
}

.setup-add-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.setup-add-actions button {
  min-height: 40px;
  padding: 0 14px;
  color: var(--primary);
  background: white;
  border: 1px solid var(--primary);
}

.save-setup-button {
  min-height: 46px;
  color: white;
  background: var(--correct);
}

.panel-button {
  min-height: 44px;
  padding: 0 16px;
  color: var(--primary);
  background: white;
  border: 1px solid var(--primary);
}

.game-page-button {
  min-height: 44px;
  padding: 0 16px;
  color: white;
  background: var(--primary-dark);
}

.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(31, 26, 51, 0.5);
}

.result-overlay.hidden {
  display: none;
}

.result-box {
  display: grid;
  gap: 16px;
  width: min(92vw, 560px);
  max-height: 90vh;
  overflow-y: auto;
  padding: 24px;
  border: 2px solid var(--gold);
  border-radius: 8px;
  background: rgba(248, 250, 252, 0.98);
  box-shadow: 0 24px 70px rgba(31, 26, 51, 0.32);
}

.rank-dialog {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
}

.rank-dialog .result-box {
  margin: auto;
  width: min(92vw, 440px);
  border-color: var(--primary);
}

.rank-dialog #resultMessage {
  font-size: clamp(26px, 5vmin, 38px);
}

.result-actions {
  display: flex;
  gap: 8px;
}

.result-actions button {
  flex: 1;
  min-height: 44px;
  padding: 10px 14px;
  color: white;
  background: var(--correct);
}

.result-actions button.secondary {
  background: #6b7280;
}

#resultMessage {
  font-size: clamp(28px, 6vmin, 48px);
  font-weight: 900;
  line-height: 1.1;
  text-align: center;
}

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

.reveal {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

@media (max-width: 820px) {
  .hud {
    width: 100%;
    grid-template-columns: 1fr 1fr;
  }

  .meter-phase {
    grid-column: 1 / -1;
  }

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

}

@media (max-width: 640px) {
  .players {
    gap: 5px;
  }

  .player {
    padding: 4px 8px;
  }

  .player strong {
    font-size: 11px;
  }
}
