* { margin: 0; padding: 0; box-sizing: border-box; touch-action: none; }
:root { --sat: env(safe-area-inset-top, 0px); }
html, body {
    width: 100%; height: 100%;
    overflow: hidden;
    background: #EDF2F8;
    font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}
#wrap {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    overflow: hidden;
}
canvas { display: block; width: 100%; height: 100%; will-change: transform; }

/* ── Modal overlay for Game Over / Completion popups ── */
#modal {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    z-index: 10;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}
#modal.show { opacity: 1; pointer-events: auto; }
.card {
    background: #fff;
    border-radius: 20px;
    border: 1px solid rgba(99, 102, 241, 0.16);
    padding: 36px 28px 28px;
    text-align: center;
    max-width: 340px; width: 85%;
    box-shadow: 0 28px 60px rgba(15, 23, 42, 0.22), 0 0 0 8px rgba(255,255,255,0.55);
    transform: translateY(16px) scale(0.97);
    transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
#modal.show .card { transform: translateY(0) scale(1); }
.card h2 {
    font-size: 22px; font-weight: 800;
    color: #0f172a; margin-bottom: 14px;
    letter-spacing: -0.5px;
    white-space: nowrap;
}
.card p {
    font-size: 22px; color: #64748b;
    font-weight: 600;
    margin-bottom: 24px; line-height: 1.5;
}
.btn {
    display: block; width: 100%;
    padding: 14px 0; border: none; border-radius: 14px;
    font-size: 16px; font-weight: 700;
    cursor: pointer; outline: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.1s;
}
.btn:active { transform: scale(0.96); }
.btn-main { background: #6366f1; color: #fff; }
.btn-ghost { background: transparent; color: #6366f1; margin-top: 4px; }
.modal-brand {
    font-size: 11px; font-weight: 700; letter-spacing: 0.5px;
    text-transform: uppercase; color: #6366f1;
    margin-bottom: 6px !important; opacity: 0.7;
}
.modal-detail {
    font-size: 13px; color: #7C8AA5;
    margin-bottom: 20px !important; line-height: 1.4;
}
.modal-detail span {
    display: block; font-variant-numeric: tabular-nums;
}
.modal-avatar {
    width: 68px; height: 68px;
    margin: -52px auto 10px;
    display: none;
}
.modal-avatar svg {
    width: 100%; height: 100%;
    filter: drop-shadow(0 4px 10px rgba(99,102,241,0.25));
}

/* ── Settings cog button ── */
#settings-btn {
    position: absolute;
    top: max(22px, env(safe-area-inset-top, 0px) + 22px);
    right: max(12px, env(safe-area-inset-right, 0px) + 4px, calc(50% - 300px));
    z-index: 5;
    width: 48px; height: 48px;
    background: none;
    border: none;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.1s, opacity 0.2s;
}
#settings-btn:active { transform: scale(0.88); }
#settings-btn svg { width: 32px; height: 32px; fill: #7C8AA5; }

/* ── Settings modal ── */
#settings-overlay {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    z-index: 20;
    background: transparent;
    opacity: 0;
    transition: opacity 0.25s ease;
    pointer-events: none;
}
#settings-overlay.show { opacity: 1; pointer-events: auto; }
#settings-overlay .card { padding: 28px 24px 20px; }
#settings-overlay.show .card { transform: translateY(0) scale(1); }

.toggle-row {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 0;
}
.toggle-row + .toggle-row { border-top: 1px solid #f1f5f9; }
.toggle-label { font-size: 15px; font-weight: 600; color: #334155; }
.toggle {
    position: relative;
    width: 48px; height: 28px;
    background: #cbd5e1;
    border: none; border-radius: 14px;
    cursor: pointer;
    transition: background 0.2s;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
}
.toggle.on { background: #6366f1; }
.toggle::after {
    content: '';
    position: absolute;
    top: 3px; left: 3px;
    width: 22px; height: 22px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.2s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.toggle.on::after { transform: translateX(20px); }

/* ── Volume slider ── */
.slider-row {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 0 14px;
    border-top: 1px solid #f1f5f9;
}
.vol-slider {
    -webkit-appearance: none; appearance: none;
    width: 120px; height: 6px;
    background: #e2e8f0; border-radius: 3px; outline: none;
}
.vol-slider::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none;
    width: 22px; height: 22px;
    background: #6366f1; border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(0,0,0,0.15);
}
.vol-slider::-moz-range-thumb {
    width: 22px; height: 22px;
    background: #6366f1; border: none; border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(0,0,0,0.15);
}

/* ── Stats / Trophy button ── */
#stats-btn {
    position: absolute;
    top: max(22px, env(safe-area-inset-top, 0px) + 22px);
    left: max(12px, env(safe-area-inset-left, 0px) + 4px, calc(50% - 300px));
    z-index: 5;
    width: 48px; height: 48px;
    background: none;
    border: none;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.1s, opacity 0.2s;
}
#stats-btn:active { transform: scale(0.88); }
#stats-btn svg { width: 32px; height: 32px; fill: #7C8AA5; }

/* ── Stats modal ── */
#stats-overlay {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    z-index: 20;
    background: transparent;
    opacity: 0;
    transition: opacity 0.25s ease;
    pointer-events: none;
}
#stats-overlay.show { opacity: 1; pointer-events: auto; }
#stats-overlay .card {
    padding: 28px 24px 20px;
    max-width: 320px;
    max-height: 80vh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
}
#stats-overlay.show .card { transform: translateY(0) scale(1); }
#stats-overlay .card * { touch-action: pan-y; }
.stats-section {
    text-align: left;
    padding: 14px 0;
    border-bottom: 1px solid #f1f5f9;
}
.stats-section-title {
    font-size: 11px; font-weight: 700;
    text-transform: uppercase; letter-spacing: 0.5px;
    color: #7C8AA5; margin-bottom: 10px;
    text-align: center;
}
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.stat-item { text-align: center; }
.stat-value {
    font-size: 22px; font-weight: 800;
    color: #0f172a; letter-spacing: -0.5px;
    font-variant-numeric: tabular-nums;
}
.stat-label {
    font-size: 11px; font-weight: 600;
    color: #7C8AA5; margin-top: 2px;
}
.trophy-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
}
.trophy-item {
    display: flex; flex-direction: column;
    align-items: center; text-align: center;
    padding: 12px 4px;
    border-radius: 12px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    position: relative;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.trophy-item.unlocked {
    background: linear-gradient(135deg, #fef9c3, #fef3c7);
    border-color: #fbbf24;
}
.trophy-icon { font-size: 24px; margin-bottom: 4px; }
.trophy-item:not(.unlocked) .trophy-icon { filter: grayscale(1); opacity: 0.3; }
.trophy-name {
    font-size: 10px; font-weight: 700;
    color: #64748b; line-height: 1.2;
}
.trophy-item.unlocked .trophy-name { color: #92400e; }
.trophy-tooltip {
    position: absolute;
    left: 50%; transform: translateX(-50%);
    bottom: calc(100% + 8px);
    background: #1e293b;
    color: #f8fafc;
    font-size: 11px; font-weight: 600;
    padding: 6px 12px;
    border-radius: 8px;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s ease;
    z-index: 30;
}
.trophy-tooltip.visible { opacity: 1; }
.trophy-tooltip::after {
    content: '';
    position: absolute;
    top: 100%; left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: #1e293b;
}
.stat-best-sub {
    font-size: 10px; font-weight: 600;
    color: #7C8AA5; margin-top: 1px;
}

/* ── Privacy & Terms link ── */
.legal-row {
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

/* ── Level select modal ── */
#level-select {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    z-index: 25;
    background: rgba(15, 23, 42, 0.45);
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}
#level-select.show { opacity: 1; pointer-events: auto; }
#level-select .card {
    padding: 28px 24px 24px;
    max-width: 320px;
    position: relative;
}
#level-select.show .card { transform: translateY(0) scale(1); }
.ls-wrap {
    display: flex; align-items: center; gap: 10px;
}
.ls-arrow {
    width: 40px; height: 40px; flex-shrink: 0;
    background: none; border: none; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    border-radius: 50%;
    transition: background 0.15s, opacity 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.ls-arrow:active { background: rgba(99,102,241,0.1); }
.ls-arrow svg { width: 22px; height: 22px; fill: #6366f1; }
.ls-arrow:disabled { opacity: 0.2; cursor: default; }
.ls-arrow:disabled:active { background: none; }
.ls-card {
    flex: 1; text-align: center;
}
.ls-image {
    width: 100%; aspect-ratio: 16/10;
    border-radius: 14px;
    display: flex; align-items: center; justify-content: center;
    position: relative;
    overflow: hidden;
}
#ls-char { filter: drop-shadow(0 4px 12px rgba(0,0,0,0.2)); }
.ls-lock-overlay {
    position: absolute; inset: 0;
    background: rgba(15, 23, 42, 0.65);
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    border-radius: 14px;
}
.ls-lock-icon { font-size: 28px; margin-bottom: 4px; }
.ls-lock-text {
    font-size: 11px; font-weight: 700;
    color: rgba(255,255,255,0.8);
    text-transform: uppercase; letter-spacing: 0.3px;
}
.ls-title {
    font-size: 20px; font-weight: 800;
    color: #0f172a; margin-top: 14px;
    letter-spacing: -0.5px;
}
.ls-dots {
    display: flex; justify-content: center;
    gap: 6px; margin-top: 16px;
}
.ls-dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: #cbd5e1;
    transition: background 0.2s;
}
.ls-dot.active { background: #6366f1; }
#ls-play { margin-top: 20px; }

/* ── Dark mode overrides ── */
body.dark { background: #111827; }
.dark .card {
    background: #1e293b;
    border-color: rgba(99, 102, 241, 0.25);
    box-shadow: 0 28px 60px rgba(0, 0, 0, 0.5), 0 0 0 8px rgba(30,41,59,0.55);
}
.dark .card h2 { color: #f1f5f9; }
.dark .card p { color: #94a3b8; }
.dark .modal-detail { color: #64748b; }
.dark .toggle-label { color: #cbd5e1; }
.dark .toggle { background: #374151; }
.dark .toggle.on { background: #6366f1; }
.dark .toggle::after { background: #cbd5e1; }
.dark .toggle.on::after { background: #fff; }
.dark .toggle-row + .toggle-row,
.dark .slider-row { border-top-color: #334155; }
.dark .vol-slider { background: #374151; }
.dark #settings-btn svg,
.dark #stats-btn svg { fill: #64748b; }
.dark .stats-section { border-bottom-color: #334155; }
.dark .stats-section-title { color: #64748b; }
.dark .stat-value { color: #f1f5f9; }
.dark .stat-label { color: #64748b; }
.dark .stat-best-sub { color: #64748b; }
.dark .trophy-item {
    background: #111827;
    border-color: #334155;
}
.dark .trophy-item.unlocked {
    background: linear-gradient(135deg, #422006, #3b1f06);
    border-color: #b45309;
}
.dark .trophy-name { color: #94a3b8; }
.dark .trophy-item.unlocked .trophy-name { color: #fbbf24; }
.dark .trophy-tooltip {
    background: #f1f5f9;
    color: #0f172a;
}
.dark .trophy-tooltip::after { border-top-color: #f1f5f9; }
.dark .ls-title { color: #f1f5f9; }
.dark .ls-arrow svg { fill: #818cf8; }
.dark .ls-arrow:active { background: rgba(129,140,248,0.12); }
.dark .ls-dot { background: #374151; }
.dark .ls-dot.active { background: #818cf8; }
.dark #level-select { background: rgba(0, 0, 0, 0.55); }

/* ── Footer / Credits ── */
#bz-footer {
    position: absolute;
    bottom: 0; left: 0; right: 0;
    text-align: center;
    padding: 7px 0 22px;
    font-size: 13px;
    font-weight: 600;
    color: #7C8AA5;
    z-index: 3;
    display: none;
    pointer-events: auto;
    letter-spacing: 0.1px;
}
#bz-footer a {
    color: #6366f1;
    text-decoration: none;
}
#bz-footer a:hover { color: #4f46e5; }
.dark #bz-footer { color: #4a5568; }
.dark #bz-footer a { color: #818cf8; }
.dark #bz-footer a:hover { color: #a5b4fc; }

/* ── Migration banner (stats modal) ── */
.migrate-notice {
    background: linear-gradient(135deg, #eff6ff, #eef2ff);
    border: 1px solid #c7d2fe;
    border-radius: 12px;
    padding: 14px 16px;
    margin-bottom: 16px;
    text-align: center;
}
.migrate-notice p {
    font-size: 12px; font-weight: 600;
    color: #4338ca; line-height: 1.4;
    margin-bottom: 10px;
}
.btn-migrate {
    display: block; width: 100%;
    padding: 10px 0; border: none; border-radius: 10px;
    font-size: 13px; font-weight: 700;
    cursor: pointer; background: #4f46e5; color: #fff;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.1s;
}
.btn-migrate:active { transform: scale(0.96); }
.migrate-dismiss {
    display: inline-block; margin-top: 8px;
    font-size: 11px; font-weight: 600;
    color: #818cf8; background: none; border: none;
    cursor: pointer; padding: 4px 8px;
    -webkit-tap-highlight-color: transparent;
}
.dark .migrate-notice {
    background: linear-gradient(135deg, #1e1b4b, #312e81);
    border-color: #4338ca;
}
.dark .migrate-notice p { color: #a5b4fc; }
.dark .migrate-dismiss { color: #6366f1; }
