* {
    box-sizing: border-box;
}

:root {
    --bg: #101820;
    --panel: #182632;
    --panel-2: #223444;
    --ink: #f7fafc;
    --muted: #aebbc8;
    --accent: #2fbf71;
    --accent-2: #4cc9f0;
    --warn: #f4b942;
    --danger: #df4f4f;
    --line: rgba(255, 255, 255, 0.16);
}

html,
body {
    min-height: 100vh;
    min-height: 100dvh;
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: Arial, "Microsoft JhengHei", sans-serif;
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    display: flex;
    justify-content: center;
}

button,
input,
select {
    font: inherit;
}

.game-shell {
    width: min(100%, 760px);
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
}

.topbar {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 8px;
}

.home-link,
.game-btn,
.select-field,
.text-field {
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-2);
    color: var(--ink);
    font-size: 14px;
    font-weight: 700;
}

.home-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 12px;
    text-decoration: none;
    white-space: nowrap;
}

.game-title {
    margin: 0;
    text-align: center;
    font-size: 22px;
    letter-spacing: 0;
}

.select-field,
.text-field {
    padding: 0 10px;
}

.text-field {
    font-size: 16px;
}

.game-btn {
    cursor: pointer;
    padding: 0 12px;
}

.game-btn.primary {
    background: var(--accent);
    color: #06140c;
}

.game-btn.warn {
    background: var(--warn);
    color: #191100;
}

.game-btn.active {
    background: var(--accent-2);
    color: #061017;
}

.stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.stat-card {
    min-height: 54px;
    padding: 7px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    text-align: center;
}

.stat-label {
    display: block;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.2;
}

.stat-value {
    display: block;
    margin-top: 3px;
    font-size: 22px;
    font-weight: 800;
    line-height: 1;
}

.toolbar {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.toolbar .game-btn:only-child {
    grid-column: 1 / -1;
}

body[data-game="snake"] .toolbar {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

.play-area {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.panel {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    padding: 10px;
}

.message {
    min-height: 36px;
    padding: 9px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--muted);
    text-align: center;
    font-size: 14px;
    line-height: 1.25;
}

.message.win {
    color: #b9f7d2;
}

.message.lose {
    color: #ffd1d1;
}

.canvas-game {
    width: min(100%, 520px);
    aspect-ratio: 1 / 1;
    max-height: calc(100dvh - 245px);
    display: block;
    border: 3px solid #506273;
    border-radius: 8px;
    background: #081118;
    touch-action: none;
}

.grid-game {
    width: min(100%, 540px);
    max-height: calc(100dvh - 240px);
    aspect-ratio: 1 / 1;
    display: grid;
    gap: 8px;
    touch-action: manipulation;
}

.tile-btn {
    border: 0;
    border-radius: 8px;
    background: #dce3ea;
    color: #13202b;
    font-weight: 900;
    font-size: clamp(18px, 5vw, 30px);
    box-shadow:
        inset 0 2px 0 rgba(255, 255, 255, 0.7),
        inset 0 -3px 0 rgba(0, 0, 0, 0.22);
    cursor: pointer;
}

.tile-btn:active {
    transform: translateY(1px);
}

.tile-btn.open,
.tile-btn.empty,
.tile-btn.matched {
    background: #2c3e4f;
    color: var(--ink);
    box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.24);
}

.tile-btn.empty {
    opacity: 0.35;
    cursor: default;
}

.tile-btn.matched {
    background: #2fbf71;
    color: #06140c;
}

.keyboard {
    display: grid;
    grid-template-columns: repeat(8, minmax(0, 1fr));
    gap: 6px;
}

.word-row {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    margin-bottom: 6px;
}

.word-cell {
    min-height: 54px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #203342;
    font-size: 26px;
    font-weight: 900;
}

.word-cell.hit {
    background: #2fbf71;
    color: #06140c;
}

.word-cell.near {
    background: #f4b942;
    color: #191100;
}

.word-cell.miss {
    background: #4b5965;
    color: white;
}

.cards-table {
    width: 100%;
    display: grid;
    gap: 10px;
}

.card-row {
    display: flex;
    justify-content: center;
    gap: 6px;
}

.card {
    width: min(12vw, 48px);
    height: min(17vw, 68px);
    min-width: 34px;
    min-height: 48px;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 7px;
    background: #f7f7f7;
    color: #1a1a1a;
    font-weight: 900;
    cursor: pointer;
}

.card.red {
    color: #c13d3d;
}

.card.back {
    background: repeating-linear-gradient(45deg, #244760 0 5px, #1a354a 5px 10px);
    color: transparent;
}

.card.selected {
    outline: 3px solid var(--warn);
}

.card.removed {
    visibility: hidden;
}

.inline-form {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
}

@media (max-width: 540px) {
    .game-shell {
        padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
        gap: 8px;
    }

    .topbar {
        grid-template-columns: 1fr 1fr;
    }

    .game-title {
        grid-column: 1 / -1;
        grid-row: 1;
        font-size: 20px;
    }

    .home-link {
        grid-column: 1;
        grid-row: 2;
    }

    .topbar > .select-field,
    .topbar > .game-btn {
        grid-column: 2;
        grid-row: 2;
    }

    .home-link,
    .game-btn,
    .select-field {
        min-height: 42px;
        font-size: 13px;
    }

    .text-field {
        min-height: 42px;
        font-size: 16px;
    }

    .stat-card {
        min-height: 48px;
        padding: 6px 4px;
    }

    .stat-value {
        font-size: 20px;
    }

    .toolbar {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .canvas-game {
        max-height: calc(100dvh - 260px);
        border-width: 2px;
    }

    .grid-game {
        gap: 5px;
        max-height: calc(100dvh - 250px);
    }

    .keyboard {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}
