:root {
  --bg: #1b1446;
  --bg2: #2a1f6b;
  --panel: rgba(255, 255, 255, 0.08);
  --panel-strong: rgba(255, 255, 255, 0.14);
  --text: #ffffff;
  --muted: #c7c0f0;
  --accent: #ffd23f;
  --accent-ink: #1b1446;
  --primary: #7c5cff;
  --correct: #22c55e;
  --pass: #f97316;
  --timeout: #8b86a8;
  --danger: #ef4444;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(circle at 20% 0%, var(--bg2), var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body { overscroll-behavior: none; }

main {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(16px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

h1, h2, h3 { margin: 0; line-height: 1.15; }
h1 { font-size: 2.6rem; font-weight: 900; letter-spacing: -0.02em; }
h2 { font-size: 1.5rem; font-weight: 800; }
h3 { font-size: 1.05rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
p { margin: 0; line-height: 1.45; }
.muted { color: var(--muted); }
.center { text-align: center; }
.grow { flex: 1; }
.row { display: flex; gap: 10px; align-items: center; }
.row > .grow { min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.hidden { display: none !important; }

.logo {
  text-align: center;
  padding: 24px 0 8px;
}
.logo .card-icon {
  display: inline-block;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 900;
  font-size: 2rem;
  padding: 6px 18px;
  border-radius: 12px;
  transform: rotate(-6deg);
  box-shadow: 0 8px 0 rgba(0,0,0,0.25);
  margin-bottom: 14px;
}
.logo p { margin-top: 6px; color: var(--muted); }

.panel {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

label { font-weight: 600; font-size: 0.95rem; color: var(--muted); }

input[type=text], textarea, select {
  width: 100%;
  font: inherit;
  font-size: 1.15rem;
  color: var(--text);
  background: rgba(0, 0, 0, 0.25);
  border: 2px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
}
input[type=text]:focus, textarea:focus, select:focus { border-color: var(--primary); }
textarea { min-height: 120px; resize: vertical; font-size: 1rem; }
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 20px) 50%, calc(100% - 14px) 50%; background-size: 6px 6px; background-repeat: no-repeat; padding-right: 36px; }
select option { color: #000; }
input.code { text-transform: uppercase; letter-spacing: 0.3em; font-weight: 800; text-align: center; }

button {
  font: inherit;
  font-weight: 800;
  font-size: 1.1rem;
  border: none;
  border-radius: 14px;
  padding: 14px 18px;
  color: var(--text);
  background: var(--panel-strong);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.06s ease, filter 0.15s ease;
}
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.45; cursor: default; transform: none; }
button.primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 5px 0 rgba(0, 0, 0, 0.3); }
button.secondary { background: var(--primary); }
button.ghost { background: transparent; color: var(--muted); padding: 10px; font-weight: 600; }
button.danger { background: transparent; color: #ffb4b4; }
button.big { font-size: 1.35rem; padding: 18px; width: 100%; }
button.small { font-size: 0.9rem; padding: 8px 12px; border-radius: 10px; }
button.icon { padding: 6px 10px; font-size: 1rem; background: transparent; color: var(--muted); }

/* Lobby */
.room-code {
  text-align: center;
  padding: 18px 12px 14px;
}
.room-code .code {
  font-size: 3.4rem;
  font-weight: 900;
  letter-spacing: 0.18em;
  color: var(--accent);
  line-height: 1;
  margin: 6px 0 12px;
  padding-left: 0.18em;
}
.room-code .link { font-size: 0.95rem; color: var(--muted); word-break: break-all; }

.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.players li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0, 0, 0, 0.18);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 1.1rem;
  font-weight: 650;
}
.players li.offline { opacity: 0.5; }
.players li.me { outline: 2px solid var(--primary); }
.players li.guesser { background: rgba(255, 210, 63, 0.18); }
.players .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.players .tag { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.players .score { font-weight: 900; font-size: 1.2rem; color: var(--accent); min-width: 2ch; text-align: right; }
.avatar {
  width: 34px; height: 34px; flex: none;
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 900; font-size: 1rem;
  color: #fff;
}

.setting { display: flex; flex-direction: column; gap: 6px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { flex: 1; min-width: 56px; padding: 10px 6px; font-size: 1rem; font-weight: 700; white-space: nowrap; }
.seg button.on { background: var(--primary); }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.toggle .switch {
  width: 56px; height: 32px; border-radius: 16px; padding: 3px; flex: none;
  background: rgba(255,255,255,0.2);
  position: relative;
}
.toggle .switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: left 0.15s;
}
.toggle .switch.on { background: var(--correct); }
.toggle .switch.on::after { left: 27px; }
.settings-summary { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { background: rgba(0, 0, 0, 0.25); padding: 6px 12px; border-radius: 999px; font-size: 0.95rem; font-weight: 600; }

/* Kategorival */
.decks { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.deck {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  padding: 16px 8px; min-height: 108px;
  background: var(--panel-strong);
  border-radius: 16px;
  text-align: center;
}
.deck .emoji { font-size: 2.2rem; line-height: 1; }
.deck .dname { font-size: 1rem; font-weight: 800; }
.deck .count { font-size: 0.8rem; color: var(--muted); font-weight: 600; }

/* Väntskärmar */
.waiting { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; text-align: center; padding: 24px 8px; }
.waiting .big-emoji { font-size: 4.5rem; line-height: 1; }
.pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
.dots::after { content: "…"; animation: dots 1.2s steps(3) infinite; display: inline-block; width: 1.2em; text-align: left; }

/* Nedräkning */
.countdown { flex: 1; display: grid; place-items: center; text-align: center; }
.countdown .num { font-size: 9rem; font-weight: 900; color: var(--accent); line-height: 1; animation: pop 0.9s ease-out; }
@keyframes pop { from { transform: scale(1.6); opacity: 0; } 30% { opacity: 1; } to { transform: scale(1); } }

/* Spelskärm */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.timer {
  font-size: 2.2rem; font-weight: 900; font-variant-numeric: tabular-nums;
  background: rgba(0,0,0,0.3); border-radius: 14px; padding: 4px 14px; min-width: 3.2ch; text-align: center;
}
.timer.low { color: #ff6b6b; animation: pulse 1s ease-in-out infinite; }
.tally { display: flex; gap: 8px; font-weight: 800; font-size: 1.1rem; }
.tally .c { color: var(--correct); }
.tally .p { color: var(--pass); }

.card {
  flex: 1;
  min-height: 220px;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 24px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 20px;
  text-align: center;
  box-shadow: 0 10px 0 rgba(0,0,0,0.25);
  gap: 10px;
}
.card .deck-label { font-size: 0.95rem; font-weight: 800; opacity: 0.7; text-transform: uppercase; letter-spacing: 0.05em; }
.card .word { font-weight: 900; line-height: 1.05; overflow-wrap: anywhere; hyphens: auto; }
.card .hint { font-size: 0.95rem; font-weight: 600; opacity: 0.75; }
.card.new { animation: cardin 0.22s ease-out; }
@keyframes cardin { from { transform: translateY(16px) scale(0.96); opacity: 0.2; } }

.answer { display: grid; grid-template-columns: 1fr 1.4fr; gap: 12px; }
.answer button { font-size: 1.6rem; padding: 26px 10px; border-radius: 20px; box-shadow: 0 6px 0 rgba(0,0,0,0.3); }
.answer .pass { background: var(--pass); }
.answer .correct { background: var(--correct); }

/* Pannläget: hela skärmen är kortet */
body.forehead { background: var(--accent); }
body.forehead main { max-width: none; padding: calc(10px + var(--safe-top)) 12px calc(12px + var(--safe-bottom)); gap: 10px; }
.forehead-view { flex: 1; display: flex; flex-direction: column; gap: 10px; color: var(--accent-ink); }
.forehead-view .topbar .timer { background: rgba(27, 20, 70, 0.12); color: var(--accent-ink); }
.forehead-view .topbar .timer.low { color: #c81e1e; }
.forehead-view .tally { color: var(--accent-ink); }
.forehead-view .fword {
  flex: 1; display: grid; place-items: center; text-align: center;
  font-weight: 900; line-height: 1.02; overflow: hidden; padding: 0 4px;
}
/* Bryt aldrig mitt i ett ord på pannan: fitText krymper i stället texten. */
.forehead-view .fword #fword { display: block; width: 100%; overflow-wrap: normal; word-break: normal; hyphens: manual; }
.forehead-view .answer button { padding: 18px 8px; font-size: 1.3rem; color: #fff; }

/* Gissaren utan pannläge */
.guess-view { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; text-align: center; }
.guess-view .bigtimer { font-size: 7rem; font-weight: 900; font-variant-numeric: tabular-nums; line-height: 1; }
.guess-view .bigtimer.low { color: #ff6b6b; }

/* Blixt vid rätt/pass */
.flash {
  position: fixed; inset: 0; pointer-events: none; opacity: 0;
  display: grid; place-items: center;
  font-size: clamp(2.2rem, 13vw, 4.5rem); font-weight: 900; color: #fff; text-shadow: 0 4px 0 rgba(0,0,0,0.2);
  text-align: center; line-height: 1.05; padding: 24px;
  z-index: 50;
}
.flash.correct { background: var(--correct); animation: flash 0.6s ease-out; }
.flash.pass { background: var(--pass); animation: flash 0.6s ease-out; }
.flash.end { background: var(--danger); animation: flash 1.2s ease-out; }
@keyframes flash { 0% { opacity: 0.97; } 55% { opacity: 0.9; } 100% { opacity: 0; } }
.flash.correct, .flash.pass, .flash.end { animation-fill-mode: forwards; }

/* Sammanfattning */
.result-head { text-align: center; display: flex; flex-direction: column; gap: 6px; padding-top: 8px; }
.result-head .score { font-size: 4.5rem; font-weight: 900; color: var(--accent); line-height: 1; }
.cards { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.cards li button {
  width: 100%; display: flex; align-items: center; gap: 12px; text-align: left;
  padding: 12px 14px; font-size: 1.1rem; border-radius: 12px; font-weight: 700;
}
.cards li button:disabled { opacity: 1; }
.cards .correct button { background: rgba(34, 197, 94, 0.25); }
.cards .pass button { background: rgba(249, 115, 22, 0.2); color: #ffd6b8; }
.cards .timeout button { background: rgba(255, 255, 255, 0.06); color: var(--timeout); }
.cards .mark { width: 1.6em; text-align: center; font-size: 1.2rem; flex: none; }
.cards .w { flex: 1; min-width: 0; }

/* Slutresultat */
.podium { display: flex; flex-direction: column; gap: 8px; }
.podium .winner { font-size: 4rem; text-align: center; }
.history { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 4px; font-size: 0.95rem; }
.history li { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); padding: 4px 2px; }
.history b { color: var(--text); }

.footer { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: auto; padding-top: 8px; }

.conn {
  position: fixed; top: calc(8px + var(--safe-top)); left: 50%; transform: translateX(-50%);
  background: var(--danger); color: #fff; font-weight: 700; padding: 8px 16px; border-radius: 999px; z-index: 60; font-size: 0.95rem;
}
.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-bottom)); transform: translate(-50%, 30px);
  background: #fff; color: #1b1446; font-weight: 700; padding: 12px 18px; border-radius: 14px;
  max-width: calc(100% - 32px); opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 70;
  box-shadow: 0 8px 30px rgba(0,0,0,0.35);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.rules { font-size: 0.98rem; color: var(--muted); }
.rules ol { margin: 6px 0 0; padding-left: 1.3em; display: flex; flex-direction: column; gap: 4px; }
details summary { cursor: pointer; font-weight: 700; color: var(--text); }

@media (min-height: 760px) {
  .answer button { padding: 32px 10px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  main { gap: 10px; padding-top: calc(8px + var(--safe-top)); }
  .card { min-height: 120px; }
  .answer button { padding: 14px 10px; }
  .countdown .num { font-size: 6rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .flash.correct, .flash.pass, .flash.end { opacity: 0.9; }
}
