:root {
    --bg: #e8edf1;
    --surface: #ffffff;
    --ink: #14212b;
    --muted: #5d6b77;
    --line: #c5cfd8;
    --key: #d3dce4;
    --correct: #12806a;
    --present: #e0a526;
    --absent: #66737f;
    --on-color: #ffffff;
    --focus: #1d5fd1;
    --gap: 6px;
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #121a21;
        --surface: #1b262f;
        --ink: #eef3f6;
        --muted: #93a2ae;
        --line: #35444f;
        --key: #34444f;
        --correct: #1fa387;
        --present: #c99416;
        --absent: #4a5863;
        --focus: #6ea2ff;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: "Bricolage Grotesque", system-ui, -apple-system, "Segoe UI", sans-serif;
    padding: env(safe-area-inset-top, 0) 0 env(safe-area-inset-bottom, 0);
}

#game {
    min-height: 100%;
    max-width: 520px;
    margin: 0 auto;
    padding: 20px 12px 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

header { text-align: center; }

h1 {
    margin: 0;
    font-size: clamp(2rem, 8vw, 2.75rem);
    font-weight: 800;
    letter-spacing: -0.03em;
    line-height: 1;
}

#subtitle {
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 0.95rem;
}

/* ---------- Grid ---------- */
#grid {
    --n: 5;
    --tile: clamp(30px, calc((min(100vw, 520px) - 40px - (var(--n) - 1) * var(--gap)) / var(--n)), 60px);
    display: grid;
    gap: var(--gap);
}

.row {
    display: grid;
    grid-template-columns: repeat(var(--n), var(--tile));
    gap: var(--gap);
}

.tile {
    width: var(--tile);
    height: var(--tile);
    display: grid;
    place-items: center;
    font-size: calc(var(--tile) * 0.55);
    font-weight: 800;
    text-transform: uppercase;
    background: var(--surface);
    border: 2px solid var(--line);
    border-radius: 8px;
    user-select: none;
    transition: border-color 0.12s;
}

.tile.filled { border-color: var(--muted); animation: pop 0.12s ease-out; }

.tile.correct, .tile.present, .tile.absent {
    color: var(--on-color);
    border-color: transparent;
}
.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent  { background: var(--absent); }

.tile.flip { animation: flip 0.5s ease-in-out; }
.row.shake { animation: shake 0.4s; }

@keyframes pop   { 50% { transform: scale(1.08); } }
@keyframes flip  { 50% { transform: scaleY(0); } }
@keyframes shake {
    20%, 60% { transform: translateX(-6px); }
    40%, 80% { transform: translateX(6px); }
}

/* ---------- Result ---------- */
#result {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 14px 12px 18px;
    background: var(--surface);
    border: 2px solid var(--line);
    border-radius: 12px;
}
#result[hidden] { display: none; }
#result p { margin: 0; font-weight: 700; }

button {
    font: inherit;
    cursor: pointer;
    border: 0;
    border-radius: 8px;
    color: inherit;
}
button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

#restart-btn {
    padding: 10px 16px;
    background: var(--ink);
    color: var(--bg);
    font-weight: 700;
    white-space: nowrap;
}

/* ---------- Keyboard ---------- */
#keyboard {
    margin-top: auto;
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.kb-row { display: flex; justify-content: center; gap: 5px; }

.key {
    flex: 1 1 0;
    max-width: 44px;
    height: 54px;
    background: var(--key);
    font-weight: 700;
    font-size: 1rem;
    text-transform: uppercase;
    touch-action: manipulation;
    transition: background 0.2s;
}
.key.wide { flex: 1.6 1 0; max-width: 68px; font-size: 0.8rem; }
.key:active { filter: brightness(0.92); }
.key.correct { background: var(--correct); color: var(--on-color); }
.key.present { background: var(--present); color: var(--on-color); }
.key.absent  { background: var(--absent);  color: var(--on-color); }

/* ---------- Toast ---------- */
#toast {
    position: fixed;
    top: calc(16px + env(safe-area-inset-top, 0px));
    left: 50%;
    transform: translate(-50%, -12px);
    padding: 10px 16px;
    background: var(--ink);
    color: var(--bg);
    font-weight: 700;
    border-radius: 8px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s, transform 0.15s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}