:root {
    --clr-bg: #101214;
    --clr-panel: #181b1f;
    --clr-text: #eef2f5;
    --clr-muted: #9aa3ad;
    --clr-border: #2a3037;
    --clr-control: #262d34;
    --green: #5fa85a;
    --yellow: #c7ad45;
    --gray: #6f767d;
    --unknown: #30363d;
    --tile-size: clamp(24px, 3.55vh, 34px);
    --tile-gap: 3px;
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
}

html {
    overflow-y: auto;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    overflow-x: hidden;
    overscroll-behavior-y: auto;
    background: var(--clr-bg);
    color: var(--clr-text);
    font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Helvetica, Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
    -webkit-overflow-scrolling: touch;
}

.page {
    width: min(100%, 1120px);
    min-height: 100vh;
    min-height: 100dvh;
    margin: 0 auto;
    padding: 8px 12px 10px;
    display: grid;
    grid-template-rows: auto 1fr auto;
    gap: 8px;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.brand {
    display: flex;
    align-items: baseline;
    gap: 10px;
    min-width: 0;
}

.brand h1 {
    margin: 0;
    font-size: 22px;
    line-height: 1;
    white-space: nowrap;
}

#mode-label {
    color: var(--clr-muted);
    font-size: 13px;
    white-space: nowrap;
}

.actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.mode-switch {
    display: inline-flex;
    padding: 2px;
    border: 1px solid var(--clr-border);
    border-radius: 8px;
    background: #14171a;
}

.mode-btn,
.btn {
    border: 1px solid var(--clr-border);
    border-radius: 7px;
    background: var(--clr-control);
    color: var(--clr-text);
    cursor: pointer;
    font: inherit;
}

.mode-btn {
    min-width: 76px;
    padding: 6px 8px;
    border-color: transparent;
    background: transparent;
    color: var(--clr-muted);
}

.mode-btn.active {
    background: #33404a;
    color: var(--clr-text);
}

.btn {
    padding: 7px 10px;
}

.mode-btn:hover,
.btn:hover {
    filter: brightness(1.08);
}

.attempts {
    min-width: 68px;
    color: var(--clr-muted);
    font-size: 12px;
    text-align: right;
}

.attempts span,
.attempts strong {
    display: block;
}

.attempts strong {
    color: var(--clr-text);
    font-size: 14px;
}

.boards {
    align-self: center;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
}

.board {
    min-width: 0;
    padding: 8px;
    border: 1px solid var(--clr-border);
    border-radius: 8px;
    background: var(--clr-panel);
}

.board h2 {
    margin: 0 0 5px;
    color: var(--clr-muted);
    font-size: 12px;
    line-height: 1;
}

.grid {
    display: grid;
    grid-template-columns: repeat(5, var(--tile-size));
    grid-template-rows: repeat(9, var(--tile-size));
    justify-content: center;
    gap: var(--tile-gap);
}

.tile {
    width: var(--tile-size);
    height: var(--tile-size);
    display: grid;
    place-items: center;
    border: 1px solid var(--clr-border);
    border-radius: 5px;
    background: #20252a;
    color: var(--clr-text);
    font-size: clamp(14px, 2.2vh, 19px);
    font-weight: 800;
    line-height: 1;
    text-transform: uppercase;
}

.tile.absent {
    background: var(--gray);
    border-color: var(--gray);
}

.tile.present {
    background: var(--yellow);
    border-color: var(--yellow);
}

.tile.correct {
    background: var(--green);
    border-color: var(--green);
}

.tile.filled {
    border-color: #59636e;
}

.keyboard {
    display: grid;
    justify-content: center;
    gap: 5px;
}

.kb-row {
    display: flex;
    justify-content: center;
    gap: 5px;
}

.key {
    position: relative;
    width: clamp(27px, 5.1vw, 42px);
    height: 45px;
    padding: 0;
    border: 1px solid var(--clr-border);
    border-radius: 7px;
    background: var(--clr-control);
    color: var(--clr-text);
    cursor: pointer;
    touch-action: pan-y manipulation;
    user-select: none;
}

.key.wide {
    width: clamp(56px, 10vw, 78px);
}

.key:active {
    transform: translateY(1px);
}

.key-label {
    position: absolute;
    top: 5px;
    left: 3px;
    right: 3px;
    overflow: hidden;
    font-size: 11px;
    font-weight: 800;
    line-height: 1;
    pointer-events: none;
    text-align: center;
    text-overflow: ellipsis;
}

.key-quarters {
    position: absolute;
    right: 5px;
    bottom: 5px;
    left: 5px;
    height: 20px;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(2, 1fr);
    gap: 2px;
}

.q {
    border: 1px solid #3e4650;
    border-radius: 2px;
    background: var(--unknown);
}

.q.absent {
    background: var(--gray);
    border-color: var(--gray);
}

.q.present {
    background: var(--yellow);
    border-color: var(--yellow);
}

.q.correct {
    background: var(--green);
    border-color: var(--green);
}

.toast {
    position: fixed;
    bottom: 18px;
    left: 50%;
    z-index: 3;
    max-width: min(90vw, 420px);
    padding: 10px 14px;
    transform: translateX(-50%);
    border: 1px solid var(--clr-border);
    border-radius: 8px;
    background: #293039;
    color: var(--clr-text);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
}

.toast.show {
    opacity: 1;
}

.overlay {
    position: fixed;
    inset: 0;
    z-index: 2;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(0, 0, 0, .56);
}

.hidden {
    display: none;
}

.modal {
    width: min(420px, 100%);
    padding: 18px;
    border: 1px solid var(--clr-border);
    border-radius: 8px;
    background: var(--clr-panel);
}

.modal h3 {
    margin: 0 0 8px;
}

.modal p {
    margin: 0 0 14px;
    color: var(--clr-muted);
}

@media (max-width: 760px) {
    :root {
        --tile-size: min(21px, calc((100vw - 42px) / 10));
        --tile-gap: 2px;
    }

    html,
    body {
        width: 100%;
        min-height: 100%;
        overflow-x: hidden;
        overflow-y: auto;
    }

    .page {
        width: 100%;
        min-height: 100svh;
        padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) 10px max(6px, env(safe-area-inset-left));
        grid-template-rows: auto auto auto;
        gap: 5px;
        overflow: visible;
    }

    .topbar {
        position: relative;
        z-index: 1;
        min-height: 38px;
        align-items: stretch;
        gap: 6px;
    }

    .brand {
        flex-direction: column;
        justify-content: center;
        gap: 2px;
        flex: 1 1 72px;
    }

    .brand h1 {
        font-size: clamp(15px, 4.2vw, 17px);
    }

    #mode-label {
        max-width: 96px;
        overflow: hidden;
        font-size: 11px;
        text-overflow: ellipsis;
    }

    .actions {
        flex: 0 1 auto;
        gap: 5px;
        justify-content: flex-end;
        min-width: 0;
    }

    .mode-switch {
        align-self: center;
        border-radius: 7px;
    }

    .mode-btn {
        min-width: 0;
        width: clamp(49px, 14vw, 61px);
        padding: 6px 3px;
        font-size: 11px;
    }

    .btn {
        align-self: center;
        min-width: 50px;
        padding: 7px 6px;
        font-size: 11px;
    }

    .attempts {
        align-self: center;
        min-width: 42px;
        font-size: 10px;
    }

    .attempts strong {
        font-size: 13px;
    }

    .boards {
        align-self: stretch;
        align-content: start;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 5px;
        min-height: auto;
        overflow: visible;
    }

    .board {
        display: grid;
        grid-template-rows: auto 1fr;
        align-content: center;
        padding: 3px;
    }

    .board h2 {
        margin-bottom: 2px;
        font-size: 10px;
    }

    .grid {
        align-self: center;
    }

    .tile {
        border-radius: 4px;
        font-size: clamp(12px, calc(var(--tile-size) * .62), 17px);
    }

    .keyboard {
        width: 100%;
        max-width: 460px;
        margin: 2px auto 92px;
        padding-top: 2px;
        display: flex;
        flex-direction: column;
        gap: 5px;
    }

    .kb-row {
        width: 100%;
        display: grid;
        justify-content: stretch;
        gap: 4px;
    }

    .kb-row:nth-child(1),
    .kb-row:nth-child(2) {
        grid-template-columns: repeat(10, minmax(0, 1fr));
    }

    .kb-row:nth-child(2) {
        padding-inline: 10px;
    }

    .kb-row:nth-child(3) {
        grid-template-columns: 1.55fr repeat(7, minmax(0, 1fr)) 1.55fr;
    }

    .key {
        width: 100%;
        min-width: 0;
        height: clamp(34px, 5svh, 42px);
        border-radius: 7px;
    }

    .key.wide {
        width: 100%;
    }

    .key-label {
        top: 5px;
        font-size: clamp(9px, 2.55vw, 11px);
    }

    .key-quarters {
        right: 4px;
        bottom: 5px;
        left: 4px;
        height: clamp(13px, 2.25svh, 17px);
    }

    .toast {
        bottom: calc(env(safe-area-inset-bottom) + 92px);
    }
}

@media (max-height: 700px) {
    :root {
        --tile-size: clamp(20px, 3.05vh, 28px);
        --tile-gap: 2px;
    }

    .page {
        gap: 5px;
    }

    .keyboard {
        gap: 4px;
    }

    .kb-row {
        gap: 4px;
    }

    .key {
        height: 36px;
    }

    .key-quarters {
        height: 14px;
    }
}

@media (max-width: 380px) {
    :root {
        --tile-size: min(20px, calc((100vw - 38px) / 10));
    }

    .page {
        gap: 5px;
    }

    .topbar {
        min-height: 39px;
    }

    .brand h1 {
        font-size: 15px;
    }

    #mode-label {
        display: none;
    }

    .mode-btn {
        width: clamp(47px, 14vw, 56px);
        padding-inline: 2px;
        font-size: 11px;
    }

    .btn {
        min-width: 48px;
        padding-inline: 5px;
    }

    .attempts {
        min-width: 36px;
    }

    .key {
        height: clamp(32px, 4.8svh, 39px);
    }

    .key-label {
        font-size: 10px;
    }

    .key-quarters {
        right: 4px;
        left: 4px;
    }
}

@media (max-width: 760px) and (max-height: 640px) {
    :root {
        --tile-size: min(19px, calc((100vw - 38px) / 10));
    }

    .topbar {
        min-height: 36px;
    }

    .brand h1 {
        font-size: 14px;
    }

    #mode-label,
    .attempts span {
        display: none;
    }

    .mode-btn,
    .btn {
        padding-block: 5px;
    }

    .board {
        padding: 3px;
    }

    .key {
        height: 32px;
    }

    .key-label {
        top: 5px;
    }

    .key-quarters {
        bottom: 5px;
        height: 14px;
    }
}
