/* === Time Loop — puzzle platformer with time echoes === */

:root {
    --tl-night: hsl(228, 45%, 8%);
    --tl-night-deep: hsl(231, 55%, 4%);
    --tl-cyan: hsl(174, 85%, 55%);
    --tl-violet: hsl(262, 85%, 70%);
    --tl-gold: hsl(42, 93%, 54%);
    --tl-coral: hsl(10, 90%, 62%);
    --tl-text: hsl(220, 40%, 92%);
    --tl-text-dim: hsl(220, 25%, 68%);
    --tl-panel: rgba(12, 14, 30, 0.88);
    --tl-border: hsla(174, 70%, 60%, 0.22);

    --tl-font-display: 'Orbitron', 'Bahnschrift', 'Trebuchet MS', sans-serif;
    --tl-font-body: 'Outfit', 'Segoe UI', system-ui, sans-serif;
}

html,
body {
    margin: 0;
    padding: 0;
    background:
        radial-gradient(120% 70% at 50% -10%, hsl(228, 45%, 12%) 0%, transparent 60%),
        var(--tl-night-deep);
    font-family: var(--tl-font-body);
    color: var(--tl-text);
}

.tl-container {
    max-width: 980px;
    margin: 0 auto;
    padding: 12px 12px calc(20px + env(safe-area-inset-bottom));
    box-sizing: border-box;
}

/* === Header === */
.tl-header {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
}

.tl-header-side {
    display: flex;
    min-width: 0;
}

.tl-header-right {
    justify-content: flex-end;
    gap: 8px;
}

.tl-title {
    margin: 0;
    font-family: var(--tl-font-display);
    font-size: clamp(1.05rem, 4vw, 1.4rem);
    font-weight: 700;
    letter-spacing: 0.14em;
    text-align: center;
    background: linear-gradient(100deg, var(--tl-cyan) 20%, var(--tl-violet) 80%);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    text-transform: uppercase;
}

.tl-back-btn {
    flex-shrink: 0;
    min-height: 42px;
    display: inline-flex;
    align-items: center;
    padding: 0 12px;
    border-radius: 999px;
    border: 1px solid var(--tl-border);
    color: var(--tl-text-dim);
    text-decoration: none;
    font-size: 0.85rem;
    transition: color 0.2s, border-color 0.2s;
}

.tl-back-btn:hover {
    color: var(--tl-text);
    border-color: var(--tl-cyan);
}

.tl-icon-btn {
    width: 42px;
    height: 42px;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    border: 1px solid var(--tl-border);
    background: rgba(13, 16, 34, 0.6);
    color: var(--tl-text);
    cursor: pointer;
    transition: border-color 0.2s, transform 0.15s;
}

.tl-icon-btn:hover {
    border-color: var(--tl-cyan);
}

.tl-icon-btn:active {
    transform: scale(0.94);
}

.tl-pause-glyph,
.tl-pause-glyph::after {
    display: block;
    width: 4px;
    height: 14px;
    border-radius: 2px;
    background: currentColor;
}

.tl-pause-glyph {
    position: relative;
    margin-right: 8px;
}

.tl-pause-glyph::after {
    content: "";
    position: absolute;
    left: 8px;
    top: 0;
}

.tl-sound-off {
    display: none;
}

.tl-icon-btn.is-muted .tl-sound-on {
    display: none;
}

.tl-icon-btn.is-muted .tl-sound-off {
    display: block;
}

/* === HUD === */
.tl-hud {
    display: flex;
    gap: 8px;
    justify-content: center;
    flex-wrap: wrap;
    margin-bottom: 10px;
}

.tl-hud-chip {
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding: 6px 14px;
    border-radius: 999px;
    border: 1px solid var(--tl-border);
    background: rgba(13, 16, 34, 0.55);
}

.tl-hud-label {
    font-family: var(--tl-font-display);
    font-size: 0.75rem;
    letter-spacing: 0.14em;
    color: var(--tl-text-dim);
}

.tl-hud-value {
    font-family: var(--tl-font-display);
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--tl-cyan);
    min-width: 3.4em;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.tl-hud-cycle .tl-hud-value {
    color: var(--tl-violet);
    min-width: 2em;
}

.tl-hud-value.time-low {
    color: var(--tl-coral);
}

/* === Stage === */
.tl-stage {
    --stage-w: min(calc(100vw - 24px), calc((100dvh - 180px) * (800 / 450)));
    position: relative;
    width: min(100%, var(--stage-w));
    aspect-ratio: 800 / 450;
    margin: 0 auto;
    border-radius: 18px;
    overflow: hidden;
    border: 1px solid var(--tl-border);
    box-shadow:
        0 0 0 1px rgba(0, 0, 0, 0.35),
        0 18px 48px rgba(0, 0, 0, 0.5),
        0 0 64px hsla(174, 82%, 50%, 0.07);
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    background: #0a0e1e;
}

@supports not (height: 100dvh) {
    .tl-stage {
        --stage-w: min(calc(100vw - 24px), calc((100vh - 180px) * (800 / 450)));
    }
}

/* Short viewports = landscape phones: reclaim chrome space. */
@media (max-height: 540px) {
    .tl-stage {
        --stage-w: min(calc(100vw - 16px), calc((100dvh - 108px) * (800 / 450)));
    }

    @supports not (height: 100dvh) {
        .tl-stage {
            --stage-w: min(calc(100vw - 16px), calc((100vh - 108px) * (800 / 450)));
        }
    }
}

/* Rotate hint is for phones only; a narrow desktop window stays playable. */
@media (orientation: portrait) and (min-width: 769px) {
    .sbg-rotate-hint.force-landscape {
        display: none !important;
    }
}

@media (max-width: 480px) {
    .tl-stage {
        border-radius: 12px;
    }
}

#gameCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
}

/* === Overlays === */
.tl-overlay {
    position: absolute;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: center;
    padding: 14px;
    background: linear-gradient(to bottom, hsla(231, 55%, 5%, 0.5), hsla(231, 55%, 5%, 0.8));
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
}

.hidden {
    display: none !important;
}

.tl-panel {
    width: min(340px, 100%);
    max-height: 100%;
    overflow-y: auto;
    padding: 22px 20px;
    text-align: center;
    border-radius: 20px;
    border: 1px solid var(--tl-border);
    background: var(--tl-panel);
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.55), inset 0 1px 0 hsla(220, 60%, 90%, 0.06);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 13px;
    animation: tl-rise 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.tl-panel-wide {
    width: min(430px, 100%);
}

@media (prefers-reduced-motion: reduce) {
    .tl-panel {
        animation: none;
    }
}

@keyframes tl-rise {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.97);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

.tl-logo-badge {
    position: relative;
    width: 58px;
    height: 58px;
    border-radius: 18px;
    background: radial-gradient(circle at 38% 32%, hsl(232, 50%, 22%), hsl(235, 60%, 10%) 75%);
    box-shadow: 0 8px 22px rgba(0, 0, 0, 0.45), inset 0 1px 0 hsla(220, 60%, 90%, 0.08);
    overflow: hidden;
}

.tl-logo-ring {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border-radius: 50%;
    border: 2px dashed hsla(174, 80%, 60%, 0.6);
    animation: tl-spin 9s linear infinite;
}

@keyframes tl-spin {
    to {
        transform: rotate(360deg);
    }
}

@media (prefers-reduced-motion: reduce) {
    .tl-logo-ring {
        animation: none;
    }
}

.tl-logo-ghost {
    position: absolute;
    left: 22%;
    top: 32%;
    width: 11px;
    height: 13px;
    border-radius: 5px 5px 2px 2px;
    background: var(--tl-violet);
    opacity: 0.65;
}

.tl-logo-hero {
    position: absolute;
    right: 21%;
    bottom: 26%;
    width: 11px;
    height: 13px;
    border-radius: 3px;
    background: var(--tl-cyan);
    box-shadow: 0 0 10px hsla(174, 90%, 60%, 0.9);
}

.tl-panel-title {
    margin: 0;
    font-family: var(--tl-font-display);
    font-size: 1.3rem;
    font-weight: 900;
    letter-spacing: 0.12em;
    color: var(--tl-text);
}

.tl-panel-subtitle {
    margin: -6px 0 0;
    font-size: 0.85rem;
    color: var(--tl-text-dim);
}

/* === Buttons === */
.tl-btn {
    display: block;
    width: 100%;
    min-height: 48px;
    padding: 11px 18px;
    border-radius: 14px;
    font-family: var(--tl-font-display);
    font-size: 0.9rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-decoration: none;
    text-align: center;
    cursor: pointer;
    transition: transform 0.15s, filter 0.15s;
    box-sizing: border-box;
}

.tl-btn:active {
    transform: translateY(1px) scale(0.98);
}

.tl-btn-primary {
    border: none;
    color: #04191a;
    background: linear-gradient(160deg, hsl(174, 90%, 62%), hsl(180, 85%, 45%));
    box-shadow: 0 8px 24px hsla(174, 90%, 50%, 0.35), inset 0 1px 0 hsla(180, 100%, 90%, 0.55);
}

.tl-btn-primary:hover {
    filter: brightness(1.08);
}

.tl-btn-ghost {
    border: 1px solid var(--tl-border);
    color: var(--tl-text-dim);
    background: transparent;
}

.tl-btn-ghost:hover {
    color: var(--tl-text);
    border-color: var(--tl-cyan);
}

.tl-hint {
    margin: 2px 0 0;
    font-size: 0.78rem;
    line-height: 1.6;
    color: var(--tl-text-dim);
}

.tl-hint kbd {
    display: inline-block;
    min-width: 20px;
    padding: 2px 5px;
    margin: 0 2px;
    border-radius: 6px;
    border: 1px solid var(--tl-border);
    border-bottom-width: 2px;
    font-family: var(--tl-font-body);
    font-size: 0.78rem;
    color: var(--tl-text);
    background: rgba(255, 255, 255, 0.05);
}

/* === Level select grid === */
.tl-level-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
    gap: 10px;
    width: 100%;
}

.tl-level-cell {
    min-height: 56px;
    border-radius: 14px;
    border: 1px solid var(--tl-border);
    background: rgba(255, 255, 255, 0.03);
    color: var(--tl-text);
    font-family: var(--tl-font-display);
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 8px 4px;
    transition: border-color 0.2s, transform 0.15s;
}

.tl-level-cell:hover:not(:disabled) {
    border-color: var(--tl-cyan);
    transform: translateY(-2px);
}

.tl-level-cell:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

.tl-level-cell.completed {
    border-color: hsla(174, 80%, 55%, 0.6);
}

.tl-level-cell .cell-best {
    font-family: var(--tl-font-body);
    font-size: 0.68rem;
    color: var(--tl-violet);
}

/* === Results === */
.tl-record-badge {
    padding: 6px 14px;
    border-radius: 999px;
    font-family: var(--tl-font-display);
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.16em;
    color: #04191a;
    background: linear-gradient(120deg, hsl(174, 95%, 62%), hsl(160, 90%, 50%));
    box-shadow: 0 0 22px hsla(174, 93%, 54%, 0.55);
}

.tl-results {
    display: flex;
    gap: 8px;
    width: 100%;
}

.tl-result-box {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 12px 6px;
    border-radius: 14px;
    border: 1px solid var(--tl-border);
    background: rgba(255, 255, 255, 0.03);
}

.tl-result-label {
    font-family: var(--tl-font-display);
    font-size: 0.75rem;
    letter-spacing: 0.12em;
    color: var(--tl-text-dim);
}

.tl-result-value {
    font-family: var(--tl-font-display);
    font-size: 1.02rem;
    font-weight: 700;
    color: var(--tl-cyan);
    font-variant-numeric: tabular-nums;
}

.tl-result-box:nth-child(2) .tl-result-value {
    color: var(--tl-violet);
}

/* === Touch controls === */
.tl-touch-controls {
    position: absolute;
    left: 0;
    right: 0;
    bottom: max(10px, env(safe-area-inset-bottom));
    z-index: 110;
    display: none;
    justify-content: space-between;
    align-items: flex-end;
    padding: 0 14px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.25s ease;
}

body.tl-playing .tl-touch-controls {
    opacity: 1;
}

.sc-stage.touch-detected .tl-touch-controls,
.tl-stage.touch-detected .tl-touch-controls {
    display: flex;
}

@media (pointer: coarse) {
    .tl-touch-controls {
        display: flex;
    }
}

.tl-touch-group {
    display: flex;
    gap: 10px;
}

.tl-touch-btn {
    width: 66px;
    height: 66px;
    border-radius: 50%;
    border: 1px solid hsla(174, 70%, 60%, 0.4);
    background: hsla(228, 45%, 12%, 0.55);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    color: var(--tl-text);
    font-family: var(--tl-font-display);
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    cursor: pointer;
    pointer-events: auto;
    touch-action: none;
    display: grid;
    place-items: center;
    transition: transform 0.08s, background 0.08s, border-color 0.08s;
    -webkit-tap-highlight-color: transparent;
}

.tl-touch-btn.pressed {
    transform: scale(0.92);
    background: hsla(174, 82%, 40%, 0.45);
    border-color: var(--tl-cyan);
}

.tl-touch-jump {
    width: 76px;
    height: 76px;
}

.tl-arrow-left,
.tl-arrow-right {
    width: 20px;
    height: 20px;
    background: currentColor;
    opacity: 0.9;
}

.tl-arrow-left {
    clip-path: polygon(100% 0, 100% 100%, 0 50%);
}

.tl-arrow-right {
    clip-path: polygon(0 0, 0 100%, 100% 50%);
}

/* === Ads helpers === */
.ad-mobile-banner {
    margin-top: 14px;
}

@media (min-width: 1025px) {
    .ad-mobile-banner {
        display: none;
    }
}

/* === Focus visibility === */
.tl-btn:focus-visible,
.tl-icon-btn:focus-visible,
.tl-back-btn:focus-visible,
.tl-level-cell:focus-visible,
.tl-touch-btn:focus-visible {
    outline: 2px solid var(--tl-cyan);
    outline-offset: 2px;
}
