/*
 * Spiderbug – Gestaltung.
 *
 * Die Bühne füllt immer genau den sichtbaren Bereich: Kopf- und Fußzeile sind
 * feste Zeilen, das Netz nimmt den Rest ein. Während des Spiels wird deshalb
 * nie gescrollt; längere Inhalte wie die Bestenliste liegen in einem eigenen
 * Overlay mit eigenem Scrollbereich.
 */

:root {
    --bg-deep: #0c0719;
    --bg-mid: #1a0f38;
    --bg-glow: #2b1a5c;
    --ink: #f4f1ff;
    --ink-dim: #b9b2d8;
    --accent: #ffd23f;
    --accent-strong: #ff9f1c;
    --coral: #ff5d8f;
    --violet: #6d28d9;
    --line: rgba(255, 255, 255, 0.14);
    --panel: rgba(20, 12, 43, 0.92);
    --topbar-h: 54px;
    --bottombar-h: 46px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    color: var(--ink);
    background:
        radial-gradient(circle at 50% 18%, var(--bg-glow) 0%, transparent 58%),
        linear-gradient(160deg, var(--bg-mid) 0%, var(--bg-deep) 100%);
    background-color: var(--bg-deep);
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    padding:
        max(6px, env(safe-area-inset-top))
        max(8px, env(safe-area-inset-right))
        max(4px, env(safe-area-inset-bottom))
        max(8px, env(safe-area-inset-left));
    gap: 6px;
}

/* ── Kopfzeile ──────────────────────────────────────────────────────────── */

.topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: var(--topbar-h);
    flex: 0 0 auto;
}

.eb-badge {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 5px 11px 5px 5px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(22, 9, 58, 0.82);
    color: var(--ink);
    text-decoration: none;
    font: 600 13px/1 system-ui, -apple-system, sans-serif;
    white-space: nowrap;
}

.eb-mark {
    display: inline-grid;
    place-items: center;
    width: 22px;
    height: 22px;
    border-radius: 7px;
    background: linear-gradient(135deg, var(--violet), var(--coral));
    font: 900 10px system-ui;
}

.eb-name span {
    color: var(--accent);
}

.topbar h1 {
    font-size: 1.15rem;
    font-weight: 800;
    letter-spacing: 0.01em;
    white-space: nowrap;
}

.stats {
    display: flex;
    gap: 14px;
    margin-left: auto;
}

.stat {
    display: grid;
    justify-items: center;
    line-height: 1.1;
}

.stat-label {
    color: var(--ink-dim);
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
}

.stat-value {
    color: var(--accent);
    font-size: 1.05rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
}

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

.icon-btn {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.07);
    color: var(--ink);
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.18s, transform 0.18s;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.16);
}

.icon-btn:active {
    transform: scale(0.94);
}

/* ── Bühne ──────────────────────────────────────────────────────────────── */

.stage {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    place-items: center;
    /* Verhindert, dass eine zu große Spielfläche die Bühne aufzieht und damit
       eine Endlosschleife aus Messen und Skalieren auslöst. */
    overflow: hidden;
}

.canvas-wrapper {
    position: relative;
    line-height: 0;
    border-radius: 18px;
    box-shadow: 0 22px 60px rgba(0, 0, 0, 0.55);

    /* Das Netz ist eine direkte Spieloberfläche: ein Wischer oder längerer
       Druck darf dort nie die Browser-Textauswahl, einen Bild-Drag oder das
       iOS-Callout auslösen. Die Regel bleibt bewusst innerhalb der Bühne;
       das Bestenlisten-Blatt außerhalb sowie Texteingaben darunter behalten
       ihr normales Browserverhalten. */
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

canvas {
    display: block;
    border-radius: 18px;
    border: 1px solid rgba(148, 163, 255, 0.22);
    touch-action: none;
    -webkit-user-drag: none;
    background: #120c26;
}

.canvas-wrapper img {
    -webkit-user-drag: none;
    user-drag: none;
}

/* Der Game-over-Name bleibt eine echte, gut bedienbare Texteingabe. */
.canvas-wrapper input,
.canvas-wrapper textarea,
.canvas-wrapper select,
.canvas-wrapper [contenteditable="true"] {
    -webkit-user-select: text;
    user-select: text;
    -webkit-touch-callout: default;
}

/* ── Fußzeile ───────────────────────────────────────────────────────────── */

.bottombar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    min-height: var(--bottombar-h);
    flex: 0 0 auto;
}

.bottombar .hint {
    flex: 1 1 auto;
    text-align: right;
}

.bottombar .build {
    flex: 1 1 auto;
}

.hint {
    color: var(--ink-dim);
    font-size: 0.76rem;
    text-align: center;
}

.build {
    color: rgba(185, 178, 216, 0.55);
    font-size: 0.66rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

/* Gestenhilfe: liegt über dem Netz, fängt aber keine Berührung ab, damit der
   erste Wischer sofort zählt. */
/* ── Laufende Extras ────────────────────────────────────────────────────── */

.effect-bar {
    position: absolute;
    top: 2.5%;
    left: 50%;
    z-index: 6;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
    max-width: 94%;
    pointer-events: none;
    transform: translateX(-50%);
}

.effect-chip {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    padding: 4px 9px;
    /* Der Netzbereich setzt line-height auf 0, damit der Canvas keinen
       Zeilenabstand erbt. Text darin braucht seinen Zeilenabstand zurück. */
    line-height: 1.35;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(8, 4, 20, 0.78);
    color: var(--ink-dim);
    font-size: clamp(0.68rem, 1.8vmin, 0.8rem);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.effect-chip b {
    color: var(--ink);
    font-size: 1.05em;
}

.effect-chip.is-combo {
    border-color: rgba(255, 210, 63, 0.5);
    color: var(--accent);
}

.effect-value {
    color: var(--ink);
    font-weight: 700;
}

/* Am Handy zählt jeder Millimeter Netz: Dort bleiben nur Symbol und Wert. */
@media (max-width: 560px) {
    .effect-name {
        display: none;
    }

    .effect-chip {
        padding: 3px 7px;
    }
}

.touch-hint {
    position: absolute;
    left: 50%;
    bottom: 8%;
    z-index: 6;
    display: grid;
    gap: 0.28rem;
    padding: 11px 15px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: rgba(8, 4, 20, 0.82);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    color: var(--ink-dim);
    max-width: 92%;
    font-size: clamp(0.72rem, 1.9vmin, 0.84rem);
    line-height: 1.35;
    pointer-events: none;
    opacity: 0;
    transform: translate(-50%, 12px);
    transition: opacity 0.35s ease, transform 0.35s ease;
}

.touch-hint.is-visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

.touch-hint-row b {
    display: inline-block;
    min-width: 1.2em;
    color: var(--accent);
    font-size: 1.05em;
}

/* ── Overlays im Netzbereich ────────────────────────────────────────────── */

.overlay {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 12px;
    padding: 22px;
    border-radius: 18px;
    background: rgba(8, 4, 20, 0.88);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    text-align: center;
    line-height: 1.45;
    overflow: auto;
}

.overlay h2 {
    font-size: clamp(1.5rem, 4.4vmin, 2.4rem);
    letter-spacing: 0.01em;
}

.overlay .lead {
    max-width: 44ch;
    font-size: clamp(0.85rem, 2.1vmin, 1rem);
    color: var(--ink);
}

.overlay p.hint {
    max-width: 44ch;
}

/* Kurzvorstellung der Spinnenarten im Startbildschirm. */
.legend {
    display: grid;
    gap: 0.3rem;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: clamp(0.74rem, 1.9vmin, 0.86rem);
    color: var(--ink-dim);
    text-align: left;
}

.legend li {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.legend-dot {
    width: 0.72rem;
    height: 0.72rem;
    flex: 0 0 auto;
    border-radius: 50%;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.legend-hunter { background: #b9c2ff; }
.legend-wanderer { background: #6fd8c8; }
.legend-lurker { background: #ffb27a; }

.legend-loot li span {
    width: 1.2rem;
    flex: 0 0 auto;
    text-align: center;
}

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

.primary,
.ghost {
    display: block;
    padding: 12px 22px;
    border: 1px solid transparent;
    border-radius: 12px;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 700;
    text-align: center;
    text-decoration: none;
    cursor: pointer;
    transition: filter 0.18s, background 0.18s;
}

.primary {
    background: linear-gradient(135deg, var(--accent-strong), var(--coral));
    color: #1b0a2a;
}

.primary:hover {
    filter: brightness(1.09);
}

.ghost {
    background: rgba(255, 255, 255, 0.08);
    border-color: var(--line);
    color: var(--ink);
}

.ghost:hover {
    background: rgba(255, 255, 255, 0.16);
}

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

.highlight {
    color: var(--accent);
}

.error-text {
    color: #ff8f9c;
}

.result-line {
    font-size: 1.05rem;
}

.name-field {
    display: grid;
    gap: 6px;
    justify-items: center;
    width: min(320px, 100%);
}

.name-field label {
    color: var(--ink-dim);
    font-size: 0.78rem;
}

input[type="text"] {
    width: 100%;
    padding: 11px 13px;
    border: 1px solid rgba(255, 210, 63, 0.55);
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--ink);
    font-size: 1rem;
    text-align: center;
}

input[type="text"]:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

/* ── Bestenliste als Blatt über dem Spiel ───────────────────────────────── */

.sheet {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: grid;
    place-items: center;
    padding: 16px;
    background: rgba(6, 3, 16, 0.78);
}

.sheet-inner {
    display: flex;
    flex-direction: column;
    width: min(560px, 100%);
    max-height: min(88dvh, 720px);
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--panel);
    box-shadow: 0 26px 70px rgba(0, 0, 0, 0.6);
    overflow: hidden;
}

.sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--line);
}

.sheet-head h2 {
    font-size: 1.1rem;
}

.tabs {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    padding: 10px 12px 0;
}

.tab {
    padding: 9px 4px;
    border: 1px solid transparent;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.06);
    color: var(--ink-dim);
    font-size: 0.76rem;
    font-weight: 700;
    cursor: pointer;
}

.tab.is-active {
    background: rgba(255, 210, 63, 0.18);
    border-color: rgba(255, 210, 63, 0.5);
    color: var(--accent);
}

.sheet-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    padding: 12px 14px;
    -webkit-overflow-scrolling: touch;
}

.sheet-note {
    margin-bottom: 10px;
    color: var(--ink-dim);
    font-size: 0.8rem;
    line-height: 1.5;
}

.sheet-note a {
    color: var(--accent);
}

.ranking {
    display: grid;
    gap: 6px;
}

.ranking-row {
    display: grid;
    grid-template-columns: 2.1rem 1fr auto;
    align-items: center;
    gap: 10px;
    padding: 9px 11px;
    border-radius: 11px;
    background: rgba(255, 255, 255, 0.05);
}

.ranking-row.is-me {
    background: rgba(255, 210, 63, 0.16);
}

.ranking-place {
    color: var(--ink-dim);
    font-weight: 800;
    text-align: center;
}

.ranking-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
}

.ranking-meta {
    display: grid;
    justify-items: end;
    line-height: 1.2;
}

.ranking-score {
    color: var(--accent);
    font-weight: 800;
    font-variant-numeric: tabular-nums;
}

.ranking-sub {
    color: var(--ink-dim);
    font-size: 0.68rem;
}

.empty {
    padding: 22px 8px;
    color: var(--ink-dim);
    text-align: center;
    line-height: 1.6;
}

.sheet-foot {
    display: grid;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid var(--line);
}

.sheet-foot .row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.sheet-foot .row > * {
    flex: 1 1 auto;
}

.sheet-foot input[type="text"] {
    text-align: left;
}

.sheet-foot .ghost,
.sheet-foot .primary {
    padding: 10px 14px;
    font-size: 0.88rem;
}

/* ── Musikanzeige ───────────────────────────────────────────────────────── */

.music-indicator {
    display: flex;
    align-items: center;
    gap: 9px;
    max-width: 290px;
    min-width: 0;
    min-height: 42px;
    padding: 6px 10px;
    border: 1px solid rgba(255, 210, 63, 0.45);
    border-radius: 16px;
    background: rgba(26, 15, 56, 0.94);
    color: var(--ink);
    text-align: left;
    flex: 0 1 auto;
}

.music-indicator-copy {
    display: grid;
    min-width: 0;
    flex: 1;
}

.music-indicator-copy strong,
.music-indicator-copy small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.music-indicator-copy strong {
    font-size: 0.8rem;
}

.music-indicator-copy small {
    color: var(--ink-dim);
    font-size: 0.68rem;
    font-weight: normal;
}

.music-btn {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    flex: 0 0 auto;
    border: 0;
    border-radius: 9px;
    background: rgba(255, 255, 255, 0.12);
    color: var(--ink);
    font-size: 0.9rem;
    cursor: pointer;
}

.music-btn:hover {
    background: rgba(255, 255, 255, 0.24);
}

#musicSkipBtn[hidden] {
    display: none;
}

.music-bars {
    display: flex;
    align-items: flex-end;
    gap: 2px;
    width: 21px;
    height: 22px;
}

.music-bars i {
    display: block;
    width: 5px;
    border-radius: 4px;
    background: linear-gradient(var(--accent), var(--accent-strong));
    animation: music-bar 0.72s ease-in-out infinite alternate;
}

.music-bars i:nth-child(1) { height: 42%; }
.music-bars i:nth-child(2) { height: 92%; animation-delay: -0.28s; }
.music-bars i:nth-child(3) { height: 65%; animation-delay: -0.48s; }
.music-indicator.is-idle .music-bars i { animation: none; height: 45%; opacity: 0.5; }

@keyframes music-bar {
    to { height: 24%; }
}

/* ── Effekte ────────────────────────────────────────────────────────────── */

.hidden {
    display: none !important;
}

@keyframes flash-red {
    0%, 100% { filter: brightness(1); }
    50% { filter: brightness(1.7) hue-rotate(-25deg); }
}

@keyframes flash-green {
    0%, 100% { filter: brightness(1); }
    50% { filter: brightness(1.45) hue-rotate(75deg); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-9px); }
    20%, 40%, 60%, 80% { transform: translateX(9px); }
}

.flash-red { animation: flash-red 0.3s ease-in-out 3; }
.flash-green { animation: flash-green 1s ease-in-out 1; }
.shake { animation: shake 0.5s ease-in-out 1; }

.floating-message {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 5;
    transform: translate(-50%, -50%);
    font-weight: 900;
    text-align: center;
    pointer-events: none;
    animation: pulse 0.45s ease-in-out 2;
}

/* Countdown vor dem Levelstart. */
.countdown {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 7;
    color: var(--accent);
    font-size: clamp(3rem, 18vmin, 7rem);
    font-weight: 900;
    line-height: 1;
    text-shadow: 0 0 24px rgba(255, 210, 63, 0.55);
    pointer-events: none;
    transform: translate(-50%, -50%);
    animation: countdown-step 0.9s ease-out 1 forwards;
}

.countdown.is-go {
    color: #6bff9e;
    font-size: clamp(2.2rem, 12vmin, 4.6rem);
    text-shadow: 0 0 24px rgba(107, 255, 158, 0.5);
    animation-duration: 0.7s;
}

@keyframes countdown-step {
    0% { opacity: 0; transform: translate(-50%, -50%) scale(1.6); }
    22% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -50%) scale(0.82); }
}

.floating-message.is-good {
    color: #6bff9e;
    font-size: clamp(1.3rem, 5vmin, 2.6rem);
    text-shadow: 0 3px 8px #000, 0 0 22px rgba(107, 255, 158, 0.75);
}

.floating-message.is-bad {
    color: #ff6b6b;
    font-size: clamp(1.2rem, 4.4vmin, 2.2rem);
    text-shadow: 0 3px 8px #000, 0 0 22px rgba(255, 107, 107, 0.75);
}

@keyframes pulse {
    0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
    50% { transform: translate(-50%, -50%) scale(1.16); opacity: 0.82; }
}

/* ── Enge Ansichten ─────────────────────────────────────────────────────── */

@media (max-width: 560px) {
    :root {
        --topbar-h: 46px;
    }

    .topbar h1,
    .eb-name,
    .bottombar .hint {
        display: none;
    }

    .stats {
        gap: 10px;
        min-width: 0;
    }

    .icon-btn {
        width: 34px;
        height: 34px;
    }

    .tabs {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Stehendes Handy: Kopfzeile muss auch mit sichtbarer Pausetaste passen. */
@media (max-width: 430px) {
    .topbar {
        gap: 6px;
    }

    .stats {
        gap: 7px;
    }

    .stat-label {
        font-size: 0.52rem;
        letter-spacing: 0.04em;
    }

    .stat-value {
        font-size: 0.92rem;
    }

    .topbar-actions {
        gap: 4px;
    }

    .icon-btn {
        width: 32px;
        height: 32px;
        font-size: 0.92rem;
    }
}

/* Liegendes Handy: die Fußzeile schrumpft auf die Musikschaltfläche. */
@media (max-height: 430px) {
    .bottombar .hint,
    .bottombar .build,
    .music-indicator-copy small {
        display: none;
    }

    .music-indicator {
        width: auto;
        min-height: 32px;
        padding: 3px 8px;
    }
}
