:root {
    --bg: #fcfcf9;
    --card: #fffffd;
    --text: #414141;
    --muted: #77736d;
    --border: rgba(94, 82, 64, 0.14);
    --shadow: 0 10px 30px rgba(64, 49, 26, 0.08);
    --orange: #ff8a00;
    --orange-dark: #e87800;
    --blue: #5069d8;
    --violet: #7864c8;
    --board: #171a22;
    --board-grid: rgba(255, 255, 255, 0.055);
    --danger: #c84f4f;
    --radius: 14px;
}

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

html {
    min-height: 100%;
    background: var(--bg);
}

body {
    margin: 0;
    min-width: 320px;
    min-height: 100vh;
    padding: 22px 14px calc(22px + env(safe-area-inset-bottom));
    font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    color: var(--text);
    background:
        radial-gradient(circle at 15% 0%, rgba(255, 138, 0, 0.07), transparent 28rem),
        var(--bg);
    overscroll-behavior: none;
}

button,
input {
    font: inherit;
}

button {
    touch-action: manipulation;
}

[hidden] {
    display: none !important;
}

.app-shell {
    width: min(100%, 900px);
    margin: 0 auto;
    padding: 26px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card);
    box-shadow: var(--shadow);
}

.brand {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1px;
    width: 100%;
    transform: translateX(-6px);
}

.app-brand {
    margin-bottom: 20px;
}

.brand-logo {
    display: block;
    width: 80px;
    height: 80px;
    object-fit: contain;
}

h1,
h2,
h3,
p {
    margin-top: 0;
}

h1 {
    margin: 0 0 1px;
    font-size: 22px;
    line-height: 1.15;
    color: var(--text);
}

.brand-subtitle {
    margin: 5px 0 0;
    font-size: 12px;
    color: var(--muted);
}

.game-layout {
    display: grid;
    grid-template-columns: minmax(310px, 1fr) 245px;
    gap: 18px;
    align-items: start;
}

.game-card,
.panel {
    border: 1px solid var(--border);
    border-radius: 12px;
    background: #fff;
    box-shadow: 0 4px 14px rgba(64, 49, 26, 0.05);
}

.game-card {
    padding: 14px;
}

.status-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}

.status-item {
    min-width: 0;
    padding: 8px 7px;
    border: 1px solid rgba(80, 105, 216, 0.15);
    border-radius: 9px;
    background: linear-gradient(145deg, rgba(80, 105, 216, 0.065), rgba(120, 100, 200, 0.035));
    text-align: center;
}

.status-item span {
    display: block;
    margin-bottom: 2px;
    font-size: 10px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.status-item strong {
    display: block;
    overflow: hidden;
    font-size: 15px;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.board-wrap {
    position: relative;
    width: min(100%, 350px);
    margin: 0 auto;
    border: 5px solid #2d3039;
    border-radius: 10px;
    background: var(--board);
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.06), 0 8px 20px rgba(24, 27, 35, .18);
    overflow: hidden;
}

#gameCanvas {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 2;
    background: var(--board);
    image-rendering: pixelated;
    touch-action: none;
    cursor: default;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.pause-overlay {
    position: absolute;
    inset: 0;
    display: grid;
    place-content: center;
    gap: 6px;
    padding: 20px;
    color: #fff;
    text-align: center;
    background: rgba(17, 20, 28, 0.78);
    backdrop-filter: blur(3px);
    pointer-events: none;
}

.pause-overlay strong {
    font-size: 28px;
}

.pause-overlay span {
    color: rgba(255,255,255,.75);
    font-size: 12px;
}

.mobile-next-status {
    display: none;
}

.mobile-next-status canvas {
    display: block;
    width: 38px;
    height: 38px;
    margin: -2px auto -3px;
}

.side-column {
    display: grid;
    gap: 14px;
}

.panel {
    padding: 14px;
}

.panel-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 10px;
}

.panel h2 {
    margin: 0;
    font-size: 15px;
}

.next-panel {
    text-align: center;
}

.next-panel .panel-heading {
    justify-content: center;
}

#nextCanvas {
    display: block;
    width: 126px;
    height: 126px;
    margin: 0 auto;
    border-radius: 10px;
    background: #f2f2ef;
}

.key-list {
    display: grid;
    gap: 7px;
    margin: 0 0 13px;
}

.key-list div {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    font-size: 12px;
}

.key-list dt {
    min-width: 70px;
    font-weight: 700;
    color: var(--blue);
}

.key-list dd {
    margin: 0;
    color: var(--muted);
    text-align: right;
}

.btn {
    min-height: 44px;
    padding: 10px 14px;
    border: 0;
    border-radius: 8px;
    font-weight: 650;
    cursor: pointer;
    transition: transform .12s ease, filter .12s ease, box-shadow .12s ease;
}

.btn:hover {
    filter: brightness(.98);
}

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

.btn-orange {
    color: #fff;
    background: var(--orange);
    box-shadow: 0 4px 10px rgba(255, 138, 0, .22);
}

.btn-orange:hover {
    background: var(--orange-dark);
}

.btn-secondary {
    color: #373737;
    border: 1px solid rgba(65,65,65,.14);
    background: #ececea;
}

.btn-pause {
    color: #fff;
    background: var(--violet);
    box-shadow: 0 4px 10px rgba(120, 100, 200, .2);
}

.btn-pause:hover {
    filter: brightness(.96);
}

.side-actions {
    display: grid;
    gap: 8px;
}

.full-width {
    width: 100%;
}

.icon-btn {
    display: grid;
    place-items: center;
    width: 30px;
    height: 30px;
    padding: 0;
    border: 1px solid rgba(65,65,65,.13);
    border-radius: 8px;
    color: #343434;
    background: #ececea;
    font-size: 17px;
    cursor: pointer;
}

.leaderboard-list {
    overflow: hidden;
    border: 1px solid rgba(65,65,65,.09);
    border-radius: 9px;
}

.leaderboard-row {
    display: grid;
    grid-template-columns: 28px minmax(0, 1fr) auto;
    gap: 7px;
    align-items: center;
    min-height: 34px;
    padding: 6px 8px;
    border-bottom: 1px solid rgba(65,65,65,.075);
    font-size: 12px;
}

.leaderboard-row:last-child {
    border-bottom: 0;
}

.leaderboard-row:nth-child(odd) {
    background: rgba(80, 105, 216, .025);
}

.leaderboard-row.current-player {
    background: rgba(255, 138, 0, .11);
}

.leaderboard-rank {
    font-weight: 800;
    color: var(--violet);
}

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

.leaderboard-score {
    font-variant-numeric: tabular-nums;
    color: var(--muted);
}

.empty-state {
    margin: 0;
    padding: 16px 10px;
    color: var(--muted);
    font-size: 12px;
    text-align: center;
}

.app-footer {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-top: 18px;
    padding-top: 13px;
    border-top: 1px solid var(--border);
    color: var(--muted);
    font-size: 10px;
}

.modal-backdrop {
    position: fixed;
    z-index: 1000;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(31, 34, 40, .68);
    backdrop-filter: blur(5px);
}

.modal-card {
    width: min(100%, 420px);
    max-height: calc(100vh - 36px);
    overflow-y: auto;
    padding: 24px;
    border: 1px solid rgba(255,255,255,.4);
    border-radius: 15px;
    background: var(--card);
    box-shadow: 0 20px 55px rgba(10, 12, 18, .28);
}

.start-card {
    text-align: center;
}

.modal-logo {
    width: 92px;
    height: 92px;
    object-fit: contain;
}

.modal-card h2 {
    margin-bottom: 7px;
    font-size: 24px;
}

.modal-card > p {
    color: var(--muted);
    font-size: 13px;
}

.modal-card form {
    margin-top: 19px;
    text-align: left;
}

.modal-card label {
    display: block;
    margin: 0 4px 6px;
    color: #555;
    font-size: 13px;
    font-weight: 650;
}

.modal-card input {
    width: 100%;
    min-height: 46px;
    margin-bottom: 12px;
    padding: 11px 12px;
    border: 2px solid #ddd;
    border-radius: 8px;
    color: var(--text);
    background: #fff;
    font-size: 16px;
    outline: none;
}

.modal-card input:focus {
    border-color: var(--blue);
    box-shadow: 0 0 0 3px rgba(80, 105, 216, .1);
}

.modal-hint {
    margin: 14px 0 0 !important;
    font-size: 10px !important;
}

.gameover-card h2,
.gameover-card > p,
.gameover-card > h3 {
    text-align: center;
}

.gameover-icon {
    display: grid;
    place-items: center;
    width: 58px;
    height: 58px;
    margin: 0 auto 9px;
    border-radius: 14px;
    color: #fff;
    background: linear-gradient(135deg, var(--orange), #ffad45);
    font-size: 32px;
    box-shadow: 0 8px 18px rgba(255, 138, 0, .23);
}

.gameover-summary {
    margin-bottom: 15px;
}

.result-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 7px;
    margin-bottom: 18px;
}

.result-grid div {
    padding: 9px 5px;
    border: 1px solid rgba(80,105,216,.13);
    border-radius: 9px;
    background: rgba(80,105,216,.045);
    text-align: center;
}

.result-grid span {
    display: block;
    margin-bottom: 2px;
    color: var(--muted);
    font-size: 9px;
    text-transform: uppercase;
}

.result-grid strong {
    font-size: 16px;
}

.gameover-card h3 {
    margin-bottom: 9px;
    font-size: 15px;
}

.leaderboard-list.full {
    max-height: 250px;
    overflow-y: auto;
}

.modal-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 9px;
    margin-top: 15px;
}

.toast {
    position: fixed;
    z-index: 2000;
    top: 16px;
    left: 50%;
    max-width: calc(100vw - 28px);
    padding: 10px 14px;
    border-radius: 9px;
    color: #fff;
    background: #3d414a;
    box-shadow: 0 8px 22px rgba(0,0,0,.18);
    font-size: 12px;
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, -12px);
    transition: opacity .18s ease, transform .18s ease;
}

.toast.show {
    opacity: 1;
    transform: translate(-50%, 0);
}

.toast.error {
    background: var(--danger);
}

@media (max-width: 760px) {
    body {
        padding-top: 16px;
    }

    .app-shell {
        width: min(100%, 560px);
        padding: 19px;
    }

    .game-layout {
        grid-template-columns: 1fr;
    }

    .side-column {
        grid-template-columns: 1fr;
    }

    .leaderboard-panel {
        grid-column: auto;
    }

    .controls-panel {
        display: none;
    }

    .next-panel {
        display: none;
    }

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

    .mobile-next-status {
        display: block;
    }

}

@media (max-width: 500px) {
    body {
        padding: 6px 4px calc(8px + env(safe-area-inset-bottom));
    }

    .app-shell {
        width: 100%;
        padding: 8px;
        border-radius: 10px;
    }

    .brand {
        transform: translateX(-3px);
    }

    .app-brand {
        margin-bottom: 7px;
    }

    .brand-logo {
        width: 54px;
        height: 54px;
    }

    h1 {
        font-size: 18px;
    }

    .brand-subtitle {
        display: none;
    }

    .game-card {
        padding: 5px;
        border-radius: 10px;
    }

    .status-grid {
        grid-template-columns: repeat(5, minmax(0, 1fr));
        gap: 4px;
        margin-bottom: 5px;
    }

    .mobile-next-status {
        display: block;
        padding-block: 2px;
    }

    .status-item {
        padding: 3px 2px;
        border-radius: 7px;
    }

    .status-item span {
        margin-bottom: 1px;
        font-size: 7px;
    }

    .status-item strong {
        font-size: 12px;
        line-height: 1.15;
    }

    .board-wrap {
        /* Fallback; JavaScript passt die Breite zusätzlich an die sichtbare Höhe an. */
        width: min(100%, calc((100svh - 180px) / 2));
        min-width: 0;
        max-width: none;
        border-width: 4px;
        border-radius: 8px;
    }


    .side-column {
        grid-template-columns: 1fr;
        gap: 9px;
    }

    .panel {
        padding: 10px;
    }

    .panel h2 {
        font-size: 13px;
    }


    .leaderboard-row {
        min-height: 32px;
        font-size: 11px;
    }

    /* Im Game-Over-Fenster bleiben auf Smartphones genau fünf Plätze sichtbar.
       Weitere Einträge werden innerhalb der Bestenliste gescrollt. */
    .gameover-card .leaderboard-list.full {
        max-height: 162px;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        scrollbar-gutter: stable;
        touch-action: pan-y;
    }

    .app-footer {
        flex-direction: column;
        align-items: center;
        gap: 3px;
        text-align: center;
    }

    .modal-card {
        padding: 20px;
    }

    .modal-logo {
        width: 82px;
        height: 82px;
    }

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

    .modal-actions {
        grid-template-columns: 1fr;
    }
}

@media (max-height: 700px) and (max-width: 500px) {
    .brand-logo {
        width: 46px;
        height: 46px;
    }

    .app-brand {
        margin-bottom: 4px;
    }

    h1 {
        font-size: 17px;
    }

    .status-item {
        padding-block: 2px;
    }

    .side-column,
    .app-footer {
        display: none;
    }
}

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