:root {
    --paper: #f3e6cb;
    --paper-2: #ead9b6;
    --card: #fffaf0;
    --tile: #fdf6e6;
    --tile-line: #e4d5b6;
    --ink: #3a2f22;
    --ink-soft: #8a7862;
    --wall: #5f5040;
    --arrow: #2f6fed;
    --arrow-soft: #cfe0ff;
    --block: #e0762b;
    --block-soft: #ffe0c2;
    --goal: #2e9e6b;
    --critter: #d13f3a;
    --danger: #b23b3b;
    --shadow: 0 8px 24px rgba(74, 58, 38, 0.22);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
}

body {
    background:
        radial-gradient(circle at 50% 0%, #fdf6e6 0%, var(--paper) 45%, var(--paper-2) 100%);
    color: var(--ink);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 780px;
    margin: 0 auto;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hidden { display: none !important; }

/* ---------------------------------------------------------------- HUD */
#hud {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 12px 6px;
}

.hud-block { display: flex; flex-direction: column; gap: 4px; }
.hud-left { align-items: flex-start; }
.hud-center { flex: 1; align-items: center; }
.hud-right { align-items: flex-end; }

.hud-label {
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 1.4px;
    color: var(--ink-soft);
}

.pips { display: flex; gap: 5px; }
.pip {
    width: 22px; height: 8px;
    border-radius: 4px;
    background: #d8c7a4;
    border: 1px solid #c7b48d;
    transition: background .25s, transform .25s;
}
.pip.current { background: var(--ink); transform: scaleY(1.35); }
.pip.clear { background: var(--goal); border-color: #257a53; }
.pip.fail { background: var(--critter); border-color: #a82f2c; }

.phase-label {
    font-size: 12px;
    font-weight: 900;
    letter-spacing: 2px;
    color: var(--ink);
}
.timer-track {
    width: min(52vw, 220px);
    height: 9px;
    border-radius: 5px;
    background: #dccaa6;
    border: 1px solid #c9b48d;
    overflow: hidden;
}
.timer-bar {
    height: 100%;
    width: 100%;
    background: var(--goal);
    transition: background .3s;
}
.timer-bar.warn { background: #e0a02b; }
.timer-bar.crit { background: var(--critter); }
.timer-text {
    font-size: 11px;
    font-weight: 800;
    color: var(--ink-soft);
    font-variant-numeric: tabular-nums;
}

.partner {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 1px;
    color: var(--ink-soft);
}
.partner .dot {
    width: 9px; height: 9px; border-radius: 50%;
    background: #b9a884;
    box-shadow: 0 0 0 0 rgba(46, 158, 107, 0.5);
}
.partner.here .dot { background: var(--goal); animation: pulse 1.8s infinite; }
.partner.gone .dot { background: var(--critter); }
@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(46, 158, 107, 0.45); }
    70% { box-shadow: 0 0 0 8px rgba(46, 158, 107, 0); }
    100% { box-shadow: 0 0 0 0 rgba(46, 158, 107, 0); }
}

.role-chip {
    font-size: 9px;
    font-weight: 900;
    letter-spacing: 1.2px;
    padding: 3px 8px;
    border-radius: 999px;
    color: #fff;
    background: var(--arrow);
}
.role-chip.blocks { background: var(--block); }

/* -------------------------------------------------------------- STAGE */
#stage {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px 10px;
}

#board {
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
}

.flash {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    font-size: clamp(34px, 11vw, 74px);
    font-weight: 900;
    letter-spacing: 3px;
    color: var(--card);
    text-shadow: 0 4px 0 rgba(58, 47, 34, 0.35), 0 0 34px rgba(0, 0, 0, 0.2);
    opacity: 0;
    transform: scale(0.7);
    transition: opacity .18s, transform .28s cubic-bezier(.2, 1.6, .4, 1);
}
.flash.show { opacity: 1; transform: scale(1); }
.flash.clear { color: #d9ffe9; }
.flash.fail { color: #ffd9d5; }

/* ------------------------------------------------------------ OVERLAY */
.overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 14px;
    pointer-events: none;
}
.overlay .panel { pointer-events: auto; }

.panel {
    width: min(440px, 100%);
    max-height: 100%;
    overflow: auto;
    background: var(--card);
    border: 2px solid #d8c6a2;
    border-radius: 20px;
    box-shadow: var(--shadow);
    padding: 22px 20px 20px;
    text-align: center;
    animation: pop .22s ease-out;
}
@keyframes pop { from { transform: translateY(8px) scale(.97); opacity: 0; } to { transform: none; opacity: 1; } }

.brand {
    font-size: 34px;
    font-weight: 900;
    letter-spacing: 3px;
    color: var(--ink);
}
.brand span { color: var(--critter); }
.brand.small { font-size: 24px; }

.tagline {
    margin-top: 2px;
    font-size: 13px;
    font-weight: 700;
    color: var(--ink-soft);
    letter-spacing: .5px;
}

.howto {
    margin: 16px 0 12px;
    display: flex;
    flex-direction: column;
    gap: 9px;
    text-align: left;
}
.howto-row {
    display: flex;
    align-items: flex-start;
    gap: 9px;
    font-size: 13px;
    line-height: 1.35;
    color: #4c4030;
}
.howto-row b { color: var(--ink); }
.howto-row .crit-chip, .howto-row .arrow-chip,
.howto-row .block-chip, .howto-row .goal-chip {
    flex: 0 0 auto;
    width: 20px; height: 20px;
    border-radius: 6px;
    margin-top: 1px;
}
.crit-chip { background: radial-gradient(circle at 35% 32%, #ff8a7b, var(--critter)); border-radius: 50% !important; }
.arrow-chip { background: var(--arrow); position: relative; }
.arrow-chip::after {
    content: ""; position: absolute; left: 5px; top: 5px;
    width: 0; height: 0;
    border-left: 8px solid #fff;
    border-top: 5px solid transparent;
    border-bottom: 5px solid transparent;
}
.block-chip { background: linear-gradient(#ffb15c, var(--block)); }
.goal-chip { background: var(--goal); position: relative; }
.goal-chip::after {
    content: ""; position: absolute; left: 5px; top: 3px;
    border-left: 7px solid #fff; border-top: 5px solid transparent; border-bottom: 5px solid transparent;
}

.lobby-hint {
    font-size: 11px;
    color: var(--ink-soft);
    margin-bottom: 16px;
}

.btn {
    appearance: none;
    border: 2px solid #c7b48d;
    background: #f4e8cd;
    color: var(--ink);
    font: inherit;
    font-size: 14px;
    font-weight: 900;
    letter-spacing: 1px;
    padding: 12px 18px;
    border-radius: 12px;
    cursor: pointer;
    transition: transform .08s, background .15s, box-shadow .15s;
}
.btn:active { transform: translateY(1px) scale(.99); }
.btn.primary {
    background: var(--goal);
    border-color: #257a53;
    color: #fff;
    box-shadow: 0 4px 0 #1f6747;
}
.btn.primary:active { box-shadow: 0 1px 0 #1f6747; transform: translateY(3px); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.tiny { font-size: 11px; padding: 7px 10px; border-radius: 9px; letter-spacing: .6px; }

#playBtn { width: 100%; font-size: 16px; padding: 15px; }

/* ------------------------------------------------------------- RESULT */
.result-kicker {
    font-size: 11px; font-weight: 900; letter-spacing: 2.4px; color: var(--ink-soft);
}
.result-big {
    font-size: 54px; font-weight: 900; line-height: 1.05; color: var(--ink);
    margin-top: 2px;
}
.result-word {
    font-size: 11px; font-weight: 900; letter-spacing: 2px; color: var(--goal); margin-bottom: 12px;
}
.result-recap { display: flex; justify-content: center; gap: 8px; margin-bottom: 14px; }
.recap {
    width: 40px; height: 40px; border-radius: 10px;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    font-size: 17px; font-weight: 900;
    background: #efe0bf; border: 2px solid #d8c6a2; color: var(--ink-soft);
}
.recap small { font-size: 8px; letter-spacing: .5px; font-weight: 800; }
.recap.clear { background: #d9f4e4; border-color: var(--goal); color: #1f6747; }
.recap.pit { background: #ffdcd7; border-color: var(--critter); color: #a82f2c; }
.recap.timeout { background: #ffe9c9; border-color: var(--block); color: #a85a1c; }

.result-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}
.stat {
    background: #f4e8cd; border: 1px solid #dccaa6; border-radius: 12px; padding: 8px 4px;
}
.stat .v { font-size: 19px; font-weight: 900; color: var(--ink); }
.stat .k { font-size: 8.5px; font-weight: 800; letter-spacing: .8px; color: var(--ink-soft); }

.result-series {
    font-size: 12px; font-weight: 800; color: var(--ink-soft);
    margin-bottom: 12px; letter-spacing: .5px;
}
.result-series b { color: var(--goal); }

.result-actions { display: flex; flex-direction: column; gap: 8px; }
.result-wait { margin-top: 10px; font-size: 12px; font-weight: 700; color: var(--ink-soft); }
.dots i { animation: blink 1.2s infinite; font-style: normal; }
.dots i:nth-child(2) { animation-delay: .2s; }
.dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .2; } 30% { opacity: 1; } }

.goodbye-msg { margin: 12px 0 18px; font-size: 14px; color: var(--ink-soft); }

/* ---------------------------------------------------------- WAIT BANNER */
.wait-banner {
    position: absolute;
    left: 12px; right: 12px; bottom: 10px;
    display: flex; align-items: center; gap: 10px;
    background: rgba(255, 250, 240, 0.94);
    border: 2px solid #d8c6a2;
    border-radius: 14px;
    padding: 9px 12px;
    box-shadow: var(--shadow);
    font-size: 12px; font-weight: 700; color: #4c4030;
}
.wait-banner #waitText { flex: 1; }
.wb-dot {
    width: 10px; height: 10px; border-radius: 50%;
    background: var(--goal);
    animation: pulse 1.6s infinite;
}

/* ------------------------------------------------------------ CONTROLS */
#controls {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 5px;
    padding: 4px 12px calc(10px + env(safe-area-inset-bottom));
}

.controls-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
}

.role-hint {
    font-size: 11px;
    font-weight: 800;
    letter-spacing: .3px;
    line-height: 1.25;
    text-align: center;
    padding: 5px 10px;
    border-radius: 9px;
    background: var(--arrow-soft);
    color: #1f4db0;
}
.role-hint.blocks { background: var(--block-soft); color: #9a4c13; }

.toolbar { display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap; }

.tool-btn {
    appearance: none;
    border: 2px solid #c7b48d;
    background: #fdf6e6;
    color: var(--ink);
    border-radius: 12px;
    font: inherit;
    font-weight: 900;
    cursor: pointer;
    transition: transform .08s, background .12s, border-color .12s;
    display: flex; align-items: center; justify-content: center;
}
.tool-btn:active { transform: translateY(1px); }

.tool-dir {
    width: 46px; height: 46px;
    font-size: 20px;
}
.tool-dir.arrow-tool.selected { background: var(--arrow); border-color: #2455c0; color: #fff; }
.tool-block {
    height: 46px; padding: 0 16px; gap: 8px;
    background: linear-gradient(#ffc684, var(--block)); border-color: #b85c1c; color: #fff;
}
.tool-block.selected { box-shadow: inset 0 0 0 3px #fff; }
.tool-block .swatch { width: 14px; height: 14px; border-radius: 4px; background: #fff; }
.tool-erase {
    width: 46px; height: 46px; font-size: 18px;
    background: #f4e8cd;
}
.tool-erase.selected { background: var(--ink); color: #fff; border-color: var(--ink); }

.tool-count {
    font-size: 11px; font-weight: 900; color: var(--ink-soft);
    letter-spacing: .5px;
    min-width: 54px;
}
.tool-count b { color: var(--ink); font-size: 14px; }

.footer-side { display: flex; align-items: center; gap: 8px; }
.reactions { display: flex; gap: 5px; }
.reaction {
    appearance: none;
    border: 2px solid #d8c6a2;
    background: #fdf6e6;
    border-radius: 10px;
    width: 38px; height: 38px;
    font-size: 18px;
    cursor: pointer;
    line-height: 1;
    transition: transform .08s;
}
.reaction:active { transform: scale(.9); }

@media (max-width: 520px) {
    .tool-dir { width: 42px; height: 42px; font-size: 18px; }
    .tool-block { height: 42px; padding: 0 12px; }
    .tool-erase { width: 42px; height: 42px; }
    .reaction { width: 34px; height: 34px; font-size: 16px; }
    .brand { font-size: 28px; }
    .result-big { font-size: 46px; }
}

@media (min-width: 760px) {
    .result-actions { flex-direction: row; }
    .result-actions .btn { flex: 1; }
}
