:root {
    --bg: #0f172a;
    --fg: #f8fafc;
    --muted: #1e293b;
    --border: #334155;
    --subtle: #94a3b8;
    --accent: #ef4444;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overscroll-behavior: none; }
body {
    background: var(--bg);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
}

#topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    padding-top: max(12px, env(safe-area-inset-top));
    background: var(--muted);
    border-bottom: 1px solid var(--border);
}
#topbar h1 {
    font-size: 18px;
    margin: 0;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
#back-btn {
    background: transparent;
    color: var(--fg);
    border: none;
    font-size: 16px;
    padding: 6px 10px;
    cursor: pointer;
    border-radius: 6px;
}
#back-btn:active { background: rgba(255,255,255,0.08); }
#sort-wrap {
    position: relative;
}
#sort-trigger {
    background: var(--bg);
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
    font: inherit;
    font-size: 13px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    max-width: 150px;
    white-space: nowrap;
}
#sort-trigger #sort-label {
    overflow: hidden;
    text-overflow: ellipsis;
}
#sort-menu {
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    background: var(--muted);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 4px;
    min-width: 170px;
    z-index: 100;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
    display: flex;
    flex-direction: column;
    gap: 2px;
}
#sort-menu[hidden] { display: none; }
.sort-option {
    background: transparent;
    color: var(--fg);
    border: none;
    text-align: left;
    padding: 10px 12px;
    border-radius: 6px;
    font: inherit;
    font-size: 14px;
    cursor: pointer;
    width: 100%;
}
.sort-option:active { background: rgba(255, 255, 255, 0.06); }
.sort-option.active {
    background: rgba(253, 224, 71, 0.12);
    color: #fde047;
}
#sort-wrap[hidden] { display: none; }

#stage {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

#ad-banner {
    min-height: 60px;
    padding: 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    background: var(--muted);
    border-top: 1px dashed var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: var(--subtle);
    text-align: center;
}

.menu-stats {
    text-align: center;
    color: var(--subtle);
    font-size: 13px;
    padding: 14px 16px 0;
}

.menu {
    display: grid;
    gap: 12px;
    padding: 16px;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    align-content: start;
}
.menu-item {
    background: var(--muted);
    border: 1px solid var(--border);
    padding: 14px 16px;
    border-radius: 12px;
    color: var(--fg);
    text-align: left;
    cursor: pointer;
    font: inherit;
    display: flex;
    align-items: center;
    gap: 14px;
}
.menu-item:active { transform: scale(0.98); }
.menu-item .icon {
    font-size: 32px;
    line-height: 1;
    flex-shrink: 0;
    width: 44px;
    text-align: center;
}
.menu-item .body {
    flex: 1;
    min-width: 0;
}
.menu-item .row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 4px;
}
.menu-item .name {
    font-weight: 600;
    font-size: 18px;
}
.menu-item .best {
    color: #fde047;
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
}
.menu-item .desc {
    color: var(--subtle);
    font-size: 13px;
    display: block;
}
.menu-item .plays {
    color: var(--subtle);
    font-size: 12px;
    display: block;
    margin-top: 4px;
    opacity: 0.8;
}
.menu-item .plays:empty { display: none; }
.menu-item .best:empty { display: none; }

.random-tile {
    background: linear-gradient(135deg, #6d28d9, #db2777);
    border-color: #a855f7;
}
.random-tile .name { color: #fff; }
.random-tile .desc { color: rgba(255, 255, 255, 0.85); }

.menu-item.dragging {
    z-index: 10;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.6);
    opacity: 0.95;
    transition: none;
}
.menu-item:not(.dragging) {
    transition: transform 80ms;
}
.drag-handle {
    flex-shrink: 0;
    align-self: stretch;
    width: 22px;
    margin-right: -10px;
    margin-left: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--subtle);
    font-size: 18px;
    line-height: 1;
    cursor: grab;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    border: none;
    background: transparent;
    opacity: 0.7;
}
.drag-handle:active { cursor: grabbing; opacity: 1; }

.menu-hint {
    color: var(--subtle);
    font-size: 12px;
    text-align: center;
    padding: 4px 8px;
    opacity: 0.7;
}

.reset-btn {
    background: transparent;
    color: var(--subtle);
    border: 1px solid var(--border);
    padding: 12px 16px;
    border-radius: 12px;
    font: inherit;
    font-size: 14px;
    cursor: pointer;
    margin-top: 8px;
}
.reset-btn:active { transform: scale(0.98); }

.menu-version {
    text-align: center;
    color: var(--subtle);
    font-size: 11px;
    padding: 12px 8px 4px;
    opacity: 0.6;
    letter-spacing: 0.5px;
}

.game-hud {
    display: flex;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--muted);
    font-size: 16px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
}

.game-canvas {
    flex: 1;
    background: #020617;
    touch-action: manipulation;
    display: block;
    width: 100%;
}

.game-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    background: rgba(15, 23, 42, 0.9);
    padding: 24px;
    text-align: center;
    z-index: 1;
}
.game-overlay h2 {
    font-size: 22px;
    margin: 0 0 16px;
    line-height: 1.4;
    white-space: pre-line;
}
.game-overlay button {
    background: var(--accent);
    color: white;
    border: none;
    padding: 12px 28px;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    font-weight: 600;
}
.game-overlay button:active { transform: scale(0.97); }
