:root {
    --bg: #14161b;
    --panel: rgba(24, 27, 34, 0.92);
    --panel-solid: #1c1f27;
    --line: rgba(255, 255, 255, 0.1);
    --text: #eceae4;
    --muted: #9a9ca4;
    --accent: #f2b84b;
    --accent-ink: #2a1d05;
    --good: #59c98a;
    --bad: #ef6461;
    --p0: #4fa3f7;
    --p1: #f2779a;
    --radius: 12px;
    --font: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: var(--font); }
canvas { display: block; width: 100%; height: 100%; outline: none; touch-action: none; }
[hidden] { display: none !important; }
kbd { font: inherit; font-size: .8em; padding: 1px 5px; border: 1px solid var(--line); border-radius: 4px; background: rgba(255,255,255,.06); }

/* ---- Knappar och fält ---------------------------------------------------------------- */
.btn {
    position: relative; overflow: hidden;
    font: inherit; font-weight: 600; font-size: 15px; color: var(--text);
    background: rgba(255,255,255,0.08); border: 1px solid var(--line); border-radius: 10px;
    padding: 11px 18px; cursor: pointer; transition: background .15s, transform .08s, opacity .15s;
    white-space: nowrap;
}
.btn:hover:not(:disabled) { background: rgba(255,255,255,0.14); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover:not(:disabled) { background: #ffc864; }
.btn.active { outline: 2px solid var(--accent); }
.btn.wide { width: 100%; }
.btn b { font-weight: 800; margin-left: 4px; }
.btn.icon { display: grid; place-items: center; padding-left: 10px; padding-right: 10px; }
.btn.icon svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

input {
    font: inherit; font-size: 16px; color: var(--text); background: rgba(0,0,0,.35);
    border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; width: 100%;
}
input:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- Paneler -------------------------------------------------------------------------- */
.panel-screen {
    position: fixed; inset: 0; display: grid; place-items: center; padding: 16px;
    background: radial-gradient(ellipse at center, rgba(10,12,16,.35), rgba(10,12,16,.8));
    z-index: 20;
}
.panel {
    width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line);
    border-radius: 18px; padding: 28px; box-shadow: 0 20px 60px rgba(0,0,0,.5);
    backdrop-filter: blur(8px); display: flex; flex-direction: column; gap: 14px;
}
.panel h2 { margin: 0; font-size: 22px; }
.panel p { margin: 0; color: var(--muted); line-height: 1.45; }
.logo {
    margin: 0; font-size: 44px; letter-spacing: 2px; font-weight: 900; text-align: center;
    color: var(--accent); text-shadow: 0 3px 0 #8a5a12;
}
.tagline { text-align: center; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.join-row { display: flex; gap: 8px; }
.join-row input { text-transform: uppercase; letter-spacing: 6px; text-align: center; font-weight: 700; }
.error { color: var(--bad) !important; font-size: 14px; }
.rules { font-size: 14px; color: var(--muted); }
.rules summary { cursor: pointer; color: var(--text); }
.rules ul { padding-left: 18px; margin: 8px 0 0; line-height: 1.5; }
.room-link { font-size: 16px; text-align: center; }
.room-code {
    font-size: 48px; font-weight: 900; letter-spacing: 12px; text-align: center; color: var(--accent);
    padding: 8px 0 8px 12px; background: rgba(0,0,0,.3); border-radius: 12px;
}
.sub { color: var(--muted); font-size: 13px; }

/* ---- HUD ------------------------------------------------------------------------------ */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#hud > * { pointer-events: auto; }

.topbar {
    position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between;
    align-items: flex-start; gap: 12px; padding: 12px 16px; pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.player-card {
    min-width: 180px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 10px 14px; border-left: 4px solid var(--pc, var(--p0));
}
.player-card.right { text-align: right; border-left: 1px solid var(--line); border-right: 4px solid var(--pc, var(--p1)); }
.player-card .name { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.player-card.right .name { justify-content: flex-end; }
.player-card .score { font-size: 30px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.player-card .score.bump { animation: bump .5s; }
.player-card .meta { font-size: 12px; color: var(--muted); display: flex; gap: 10px; }
.player-card.right .meta { justify-content: flex-end; }
.player-card .tag { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; padding: 1px 6px; border-radius: 4px; background: rgba(255,255,255,.1); }
.player-card .tag.off { background: var(--bad); color: #fff; }
.dots { letter-spacing: 1px; }
@keyframes bump { 30% { transform: scale(1.25); } }

.center-info { text-align: center; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 18px; }
.clock { font-size: 26px; font-weight: 800; font-variant-numeric: tabular-nums; }
.clock.low { color: var(--bad); }

.word-log {
    position: absolute; right: 16px; top: 120px; width: 210px; max-height: 45vh; overflow: hidden;
    display: flex; flex-direction: column; gap: 6px; pointer-events: none;
}
.word-log .entry {
    background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--pc); border-radius: 8px;
    padding: 6px 10px; font-size: 13px; display: flex; justify-content: space-between; gap: 8px;
    animation: slide-in .3s ease-out;
}
.word-log .entry .w { font-weight: 700; letter-spacing: .5px; overflow: hidden; text-overflow: ellipsis; }
.word-log .entry .p { font-weight: 800; font-variant-numeric: tabular-nums; }
.word-log .entry .p.neg { color: var(--bad); }
.word-log .entry .p.pos { color: var(--good); }
@keyframes slide-in { from { transform: translateX(30px); opacity: 0; } }

.mode-banner {
    position: absolute; left: 50%; bottom: 100px; transform: translateX(-50%);
    background: var(--accent); color: var(--accent-ink); font-weight: 700; padding: 8px 16px; border-radius: 20px;
}

.actionbar {
    position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%);
    display: flex; gap: 10px; align-items: stretch; padding: 10px; max-width: calc(100% - 32px);
    background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
}
.action-hint { align-self: center; padding: 0 8px; font-size: 13px; color: var(--muted); }

.word-panel {
    position: absolute; left: 0; top: 0; display: flex; gap: 10px; align-items: center; padding: 8px 8px 8px 10px;
    background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.35);
    will-change: transform; animation: fade-in .15s ease-out; transition: opacity .15s;
}
.word-panel.dimmed { opacity: .3; pointer-events: none; }
@keyframes fade-in { from { opacity: 0; } }
.word-preview { min-width: 130px; display: flex; flex-direction: column; justify-content: center; gap: 6px; padding: 0 6px; }
.preview-text { font-weight: 800; letter-spacing: 2px; font-size: 18px; min-height: 22px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.preview-text .hint { font-weight: 500; letter-spacing: 0; font-size: 12px; color: var(--muted); }
.preview-text .hint.bad { color: var(--bad); }
.preview-text .hint.bonus { color: var(--accent); font-weight: 700; }
.preview-text .hint.mult { color: var(--mc); font-weight: 800; }
.bar { height: 5px; border-radius: 3px; background: rgba(255,255,255,.1); overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--good); transition: width .1s linear; }
.bar.idle > div.warn { background: var(--bad); }

.btn.meter .meter-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: rgba(242,184,75,.25); transition: width .1s linear; }
.btn.meter .label { position: relative; }

/* ---- Nedräkning och toasts ------------------------------------------------------------ */
.countdown {
    position: fixed; inset: 0; display: grid; place-items: center; z-index: 25; pointer-events: none;
    font-size: 160px; font-weight: 900; color: var(--accent); text-shadow: 0 8px 30px rgba(0,0,0,.6);
}
.countdown.pop { animation: pop .9s ease-out; }
@keyframes pop { from { transform: scale(1.6); opacity: 0; } 30% { opacity: 1; } }
/* ---- Firande: stor siffra som svischar förbi -------------------------------------------- */
.celebrate { position: fixed; inset: 0; z-index: 40; pointer-events: none; display: grid; place-items: center; overflow: hidden; }
.celebration { grid-area: 1 / 1; text-align: center; animation: swish 1.9s cubic-bezier(.2, .8, .2, 1) forwards; }
.celebration .big {
    font-size: clamp(96px, 22vw, 210px); font-weight: 900; line-height: .9; color: var(--c);
    -webkit-text-stroke: 3px rgba(0, 0, 0, .25); text-shadow: 0 0 40px var(--c), 0 10px 30px rgba(0, 0, 0, .55);
}
.celebration .small {
    margin-top: 8px; font-size: clamp(16px, 3.4vw, 26px); font-weight: 800; letter-spacing: 3px;
    text-transform: uppercase; color: #fff; text-shadow: 0 2px 12px rgba(0, 0, 0, .7);
}
@keyframes swish {
    0%   { transform: translateX(-70vw) scale(.3) skewX(28deg); opacity: 0; filter: blur(10px); }
    18%  { transform: translateX(0) scale(1.25) skewX(-6deg); opacity: 1; filter: blur(0); }
    28%  { transform: translateX(0) scale(1) skewX(0); }
    72%  { transform: translateX(0) scale(1.05) skewX(0); opacity: 1; filter: blur(0); }
    100% { transform: translateX(70vw) scale(.6) skewX(-28deg); opacity: 0; filter: blur(12px); }
}
.sparks { grid-area: 1 / 1; position: relative; width: 0; height: 0; }
.sparks i {
    position: absolute; left: -6px; top: -6px; width: 12px; height: 12px; border-radius: 50%;
    background: var(--c); box-shadow: 0 0 12px var(--c); opacity: 0;
    animation: spark .9s ease-out .3s forwards;
}
@keyframes spark {
    0%   { transform: rotate(var(--a)) translateX(0) scale(1); opacity: 1; }
    100% { transform: rotate(var(--a)) translateX(var(--d)) scale(.2); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
    .celebration { animation: fade-out 1.9s ease forwards; }
    .sparks { display: none; }
}
@keyframes fade-out { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }

/* Nedräkning när en tärning vrids – placeras ovanför den upplyfta tärningen. */
.flip-timer {
    position: fixed; left: 50%; top: max(96px, calc(50% - 13vh - 136px)); transform: translateX(-50%);
    width: 120px; height: 120px; z-index: 20; pointer-events: none; display: grid; place-items: center;
}
.flip-timer svg { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.flip-timer .track { fill: rgba(20, 22, 27, .75); stroke: rgba(255, 255, 255, .12); stroke-width: 7; }
.flip-timer .fill {
    fill: none; stroke: var(--accent); stroke-width: 7; stroke-linecap: round;
    stroke-dasharray: 276.46; stroke-dashoffset: 0;
}
.flip-timer span {
    position: relative; font-size: 72px; font-weight: 900; line-height: 1; color: var(--accent);
    text-shadow: 0 4px 18px rgba(0, 0, 0, .5); font-variant-numeric: tabular-nums;
}
.flip-timer.urgent .fill { stroke: var(--bad); }
.flip-timer.urgent span { color: var(--bad); }
.flip-timer span.pop { animation: tick-pop .45s ease-out; }
@keyframes tick-pop { from { transform: scale(1.6); opacity: .3; } }

.toasts {
    position: fixed; left: 50%; top: 104px; transform: translateX(-50%); z-index: 30;
    display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none;
}
.toast {
    background: var(--panel-solid); border: 1px solid var(--line); border-radius: 10px; padding: 9px 16px;
    font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.4); animation: toast-in .25s ease-out;
    max-width: min(520px, 90vw); text-align: center;
}
.toast.good { border-color: var(--good); }
.toast.bad { border-color: var(--bad); }
.toast.fade { opacity: 0; transition: opacity .4s; }
@keyframes toast-in { from { transform: translateY(-10px); opacity: 0; } }

/* ---- Slut ----------------------------------------------------------------------------- */
.final-scores { display: flex; flex-direction: column; gap: 8px; }
.final-scores .row {
    display: flex; justify-content: space-between; align-items: center; padding: 10px 14px;
    background: rgba(0,0,0,.3); border-radius: 10px; border-left: 4px solid var(--pc);
}
.final-scores .row .s { font-size: 24px; font-weight: 800; }
.final-scores .row.win { background: rgba(242,184,75,.15); }

/* ---- Mobil ---------------------------------------------------------------------------- */
@media (max-width: 720px) {
    .topbar { padding: 8px; gap: 6px; }
    .player-card { min-width: 0; flex: 1; padding: 6px 10px; }
    .player-card .score { font-size: 22px; }
    .player-card .meta { display: none; }
    .center-info { padding: 6px 10px; }
    .clock { font-size: 20px; }
    .word-log { display: none; }
    .actionbar { bottom: 8px; padding: 8px; gap: 6px; flex-wrap: wrap; justify-content: center; width: calc(100% - 16px); }
    .actionbar .btn { flex: 1; }
    .word-preview { min-width: 90px; }
    .preview-text { max-width: 170px; }
    .action-hint { min-width: 100%; text-align: center; }
    .btn { padding: 10px 12px; font-size: 14px; }
    .toasts { top: 84px; }
    .mode-banner { bottom: 150px; }
}
