/* Axion Valley — minimal cross-device valley showcase.
   Visual language adapted from vikaskumarsingh.com: crisp white floating plates over the live scene,
   charcoal instrument popovers with tabular figures, and a floating lower-left touch thumb-stick. */

:root {
    --ink: #1a1a1f;
    --ink-2: #5a5a62;
    --rule: #d5d5da;
    --white: #ffffff;
    --plate: rgba(255, 255, 255, 0.94);
    --charcoal: rgba(28, 30, 35, 0.94);
    --charcoal-border: rgba(255, 255, 255, 0.14);
    --accent: #c2416c;
    --fps-good: #26c45a;
    --fps-warn: #f0a500;
    --fps-bad: #e54338;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.22);
    --edge-t: max(14px, env(safe-area-inset-top));
    --edge-r: max(14px, env(safe-area-inset-right));
    --edge-b: max(14px, env(safe-area-inset-bottom));
    --edge-l: max(14px, env(safe-area-inset-left));
    --radius: 3px;
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
    --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    background: #101216;
    color: var(--ink);
    font: 500 14px/1.3 var(--sans);
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#engine-canvas {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    display: block;
    z-index: 0;
    touch-action: none;
    cursor: grab;
}

html[data-dragging="1"] #engine-canvas { cursor: grabbing; }

/* Shared white plate button */
.plate-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 36px;
    padding: 0 12px;
    border: 1px solid rgba(26, 26, 31, 0.14);
    border-radius: var(--radius);
    background: var(--plate);
    color: var(--ink);
    font: 700 13px/1 var(--sans);
    letter-spacing: 0.01em;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: background-color 0.15s var(--ease), color 0.15s var(--ease), border-color 0.15s var(--ease);
    -webkit-tap-highlight-color: transparent;
}

.plate-btn:hover,
.plate-btn[aria-expanded="true"] {
    background: var(--ink);
    color: var(--white);
    border-color: var(--ink);
}

/* -------------------------------------------------------------------------
   Top-left: valley scene switcher
   ------------------------------------------------------------------------- */
.scene-switcher {
    position: fixed;
    top: var(--edge-t);
    left: var(--edge-l);
    z-index: 20;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
}

.scene-mark {
    width: 8px;
    height: 8px;
    border-radius: 1px;
    background: var(--accent);
    flex: none;
}

.chevron {
    transition: transform 0.16s var(--ease);
    opacity: 0.75;
    flex: none;
}

#scene-trigger[aria-expanded="true"] .chevron {
    transform: rotate(180deg);
}

.scene-menu {
    display: flex;
    flex-direction: column;
    min-width: 176px;
    max-height: calc(100dvh - 76px);
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    padding: 4px;
    border: 1px solid var(--charcoal-border);
    border-radius: var(--radius);
    background: var(--charcoal);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.42);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.scene-opt {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    width: 100%;
    min-height: 36px;
    padding: 0 10px;
    border: 0;
    border-radius: 2px;
    background: transparent;
    color: rgba(255, 255, 255, 0.88);
    font: 600 13px/1.2 var(--sans);
    text-align: left;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.12s var(--ease), color 0.12s var(--ease);
}

.scene-opt:hover {
    background: rgba(255, 255, 255, 0.11);
    color: var(--white);
}

.scene-opt.is-active {
    background: var(--accent);
    color: var(--white);
}

.scene-opt-tag {
    font: 500 10px/1 var(--mono);
    letter-spacing: 0.04em;
    opacity: 0.72;
    text-transform: uppercase;
}

/* -------------------------------------------------------------------------
   Top-right: community links + FPS counter
   ------------------------------------------------------------------------- */
.top-right {
    position: fixed;
    top: var(--edge-t);
    right: var(--edge-r);
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 8px;
    pointer-events: none;
}

.link-plate {
    display: inline-flex;
    align-items: stretch;
    gap: 1px;
    border: 1px solid rgba(26, 26, 31, 0.14);
    border-radius: var(--radius);
    background: var(--rule);
    box-shadow: var(--shadow);
    overflow: hidden;
    pointer-events: auto;
}

.link-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    padding: 0;
    background: var(--plate);
    color: var(--ink);
    text-decoration: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.15s var(--ease), color 0.15s var(--ease);
}

.link-btn:hover {
    background: var(--ink);
    color: var(--white);
}

.link-btn:focus-visible {
    outline-offset: -4px;
}

.fps-badge {
    pointer-events: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 36px;
    padding: 0 11px;
    border: 1px solid rgba(26, 26, 31, 0.14);
    border-radius: var(--radius);
    background: var(--plate);
    color: var(--ink);
    box-shadow: var(--shadow);
    font: 700 13px/1 var(--mono);
    font-variant-numeric: tabular-nums;
}

.fps-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--ink-2);
    transition: background-color 0.25s ease;
}

.fps-dot.is-good { background: var(--fps-good); }
.fps-dot.is-warn { background: var(--fps-warn); }
.fps-dot.is-bad { background: var(--fps-bad); }

.fps-unit {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.06em;
    color: var(--ink-2);
}

/* -------------------------------------------------------------------------
   Floating touch thumb-stick (directly matching vikaskumarsingh.com)
   ------------------------------------------------------------------------- */
.walk-controls {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 14;
    pointer-events: none;
    overflow: hidden;
}

html[data-touch="1"][data-scene="running"] .walk-controls {
    display: block;
}

.stick {
    position: absolute;
    width: 0;
    height: 0;
    left: calc(82px + env(safe-area-inset-left));
    top: calc(100% - 86px - env(safe-area-inset-bottom));
    opacity: 0.56;
    transition: opacity 0.18s ease;
}

.stick.is-held {
    opacity: 1;
    transition: none;
}

.stick-ring,
.stick-thumb {
    position: absolute;
    border-radius: 50%;
}

.stick-ring {
    left: calc(-1 * var(--stick));
    top: calc(-1 * var(--stick));
    width: calc(2 * var(--stick));
    height: calc(2 * var(--stick));
    border: 3px solid var(--white);
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.28), inset 0 0 0 1px rgba(0, 0, 0, 0.22);
    transition: border-color 0.12s ease;
}

.stick-thumb {
    left: -23px;
    top: -23px;
    width: 46px;
    height: 46px;
    border: 2px solid var(--white);
    background: rgba(42, 44, 49, 0.88);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
    transition: background-color 0.12s ease;
}

.stick.is-run .stick-ring { border-color: var(--accent); }
.stick.is-run .stick-thumb { background: var(--accent); }

.stick-run {
    position: absolute;
    left: 0;
    top: calc(-1 * var(--stick) - 48px);
    transform: translateX(-50%);
    padding: 3px 8px;
    border-radius: 2px;
    background: var(--accent);
    color: var(--white);
    font: 700 11px/1.2 var(--sans);
    letter-spacing: 0.04em;
    white-space: nowrap;
    opacity: 0;
    transition: opacity 0.12s ease;
}

.stick.is-run .stick-run { opacity: 1; }

/* -------------------------------------------------------------------------
   Touch altitude buttons (Up / Down, matching the thumb-stick style)
   ------------------------------------------------------------------------- */
.alt-controls {
    display: none;
    position: fixed;
    right: var(--edge-r);
    bottom: calc(var(--edge-b) + 52px);
    z-index: 15;
    flex-direction: column;
    gap: 10px;
    transition: opacity 0.16s var(--ease);
}

html[data-touch="1"][data-scene="running"] .alt-controls {
    display: flex;
}

html[data-tune="1"] .alt-controls {
    opacity: 0;
    pointer-events: none;
}

.alt-btn {
    display: grid;
    place-items: center;
    width: 48px;
    height: 48px;
    padding: 0;
    border: 2px solid var(--white);
    border-radius: 50%;
    background: rgba(42, 44, 49, 0.78);
    color: var(--white);
    opacity: 0.62;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.34);
    cursor: pointer;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    transition: opacity 0.12s ease, background-color 0.12s ease, border-color 0.12s ease, transform 0.1s ease;
}

.alt-btn.is-held {
    opacity: 1;
    background: var(--accent);
    border-color: var(--white);
    transform: scale(0.95);
}

/* -------------------------------------------------------------------------
   Persistent translucent keyboard keyguide (desktop) + touch hint (mobile)
   ------------------------------------------------------------------------- */
.key-guide {
    display: none;
    position: fixed;
    left: var(--edge-l);
    bottom: var(--edge-b);
    z-index: 15;
    align-items: flex-end;
    gap: 6px;
    pointer-events: none;
}

html[data-scene="running"]:not([data-touch="1"]) .key-guide {
    display: flex;
}

.key-grid {
    display: grid;
    grid-template-columns: repeat(3, 28px);
    gap: 4px;
}

.key-meta {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.key-cap {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    height: 26px;
    border: 1px solid rgba(255, 255, 255, 0.28);
    border-radius: var(--radius);
    background: rgba(18, 20, 26, 0.36);
    color: rgba(255, 255, 255, 0.82);
    font: 700 10px/1 var(--mono);
    letter-spacing: 0.02em;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    transition: background-color 0.08s ease, border-color 0.08s ease, color 0.08s ease, box-shadow 0.08s ease, transform 0.06s ease;
}

.key-grid .key-cap small {
    position: absolute;
    right: 2px;
    bottom: 1px;
    font-size: 7px;
    line-height: 1;
    opacity: 0.65;
}

.key-wide {
    justify-content: flex-start;
    padding: 0 8px;
    white-space: nowrap;
}

.key-wide small {
    font-weight: 500;
    font-size: 9px;
    opacity: 0.65;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.key-cap.is-down {
    background: rgba(194, 65, 108, 0.82);
    border-color: rgba(255, 255, 255, 0.92);
    color: var(--white);
    box-shadow: 0 0 12px rgba(194, 65, 108, 0.45);
    transform: translateY(1px) scale(0.95);
}

.key-cap.is-down small {
    opacity: 0.95;
}

.controls-hint {
    display: none;
    position: fixed;
    left: 50%;
    bottom: var(--edge-b);
    transform: translateX(-50%);
    z-index: 15;
    margin: 0;
    max-width: calc(100vw - 260px);
    padding: 5px 11px;
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: var(--radius);
    background: rgba(18, 20, 26, 0.36);
    color: rgba(255, 255, 255, 0.78);
    font: 500 11px/1.3 var(--sans);
    text-align: center;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

html[data-touch="1"][data-scene="running"] .controls-hint {
    display: block;
}

@media (max-width: 540px) {
    .controls-hint {
        display: none !important;
    }
}

/* -------------------------------------------------------------------------
   Bottom-right: compact Speed / Time / Fog switch & popover
   ------------------------------------------------------------------------- */
.tune {
    position: fixed;
    right: var(--edge-r);
    bottom: var(--edge-b);
    z-index: 20;
    display: flex;
    flex-direction: column-reverse;
    align-items: flex-end;
    gap: 8px;
}

.tune-chip kbd {
    padding: 2px 5px;
    border: 1px solid currentColor;
    border-radius: 2px;
    font: 700 10px/1 var(--sans);
    opacity: 0.75;
}

html[data-touch="1"] .tune-chip kbd {
    display: none;
}

.tune-panel {
    display: grid;
    gap: 12px;
    width: min(276px, calc(100vw - 28px));
    max-height: calc(100dvh - 80px);
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    padding: 12px;
    border: 1px solid var(--charcoal-border);
    border-radius: var(--radius);
    background: var(--charcoal);
    color: var(--white);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.tune-group {
    display: grid;
    gap: 6px;
}

.tune-group + .tune-group {
    padding-top: 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.tune-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.tune-row label {
    font: 700 12px/1 var(--sans);
    letter-spacing: 0.02em;
    color: var(--white);
}

.tune-right {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
}

.tune-right output {
    font: 600 11px/1 var(--mono);
    font-variant-numeric: tabular-nums;
    color: rgba(255, 255, 255, 0.8);
    white-space: nowrap;
}

.tune-tag {
    min-height: 24px;
    padding: 0 7px;
    border: 1px solid rgba(255, 255, 255, 0.24);
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.06);
    color: rgba(255, 255, 255, 0.9);
    font: 700 10px/1 var(--sans);
    letter-spacing: 0.03em;
    text-transform: uppercase;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.12s var(--ease), border-color 0.12s var(--ease), color 0.12s var(--ease);
}

.tune-tag:hover {
    background: rgba(255, 255, 255, 0.16);
    border-color: rgba(255, 255, 255, 0.5);
}

.tune-tag[aria-pressed="true"] {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--white);
}

/* Touch-friendly charcoal range slider with progress fill */
.tune-slider {
    --fill: 0%;
    appearance: none;
    -webkit-appearance: none;
    width: 100%;
    height: 28px;
    margin: 0;
    padding: 0;
    background: transparent;
    cursor: ew-resize;
    touch-action: pan-x;
}

.tune-slider::-webkit-slider-runnable-track {
    height: 6px;
    border-radius: 2px;
    background: linear-gradient(
        to right,
        var(--accent) 0%,
        var(--accent) var(--fill),
        rgba(255, 255, 255, 0.18) var(--fill),
        rgba(255, 255, 255, 0.18) 100%
    );
}

.tune-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    margin-top: -5px;
    border: 2px solid var(--ink);
    border-radius: 50%;
    background: var(--white);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
}

.tune-slider::-moz-range-track {
    height: 6px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.18);
}

.tune-slider::-moz-range-progress {
    height: 6px;
    border-radius: 2px;
    background: var(--accent);
}

.tune-slider::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border: 2px solid var(--ink);
    border-radius: 50%;
    background: var(--white);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
}

/* Larger thumb on touch devices */
html[data-touch="1"] .tune-slider {
    height: 34px;
}

html[data-touch="1"] .tune-slider::-webkit-slider-thumb {
    width: 22px;
    height: 22px;
    margin-top: -8px;
}

html[data-touch="1"] .tune-slider::-moz-range-thumb {
    width: 20px;
    height: 20px;
}

@media (max-width: 420px) {
    .top-right {
        gap: 6px;
    }
    .link-btn {
        width: 31px;
    }
}

/* Compact spacing for short landscape phones */
@media (max-height: 440px) {
    :root {
        --edge-t: max(10px, env(safe-area-inset-top));
        --edge-b: max(10px, env(safe-area-inset-bottom));
    }
    .stick {
        top: calc(100% - 76px - env(safe-area-inset-bottom));
    }
    .alt-controls {
        bottom: calc(var(--edge-b) + 46px);
        gap: 8px;
    }
    .alt-btn {
        width: 44px;
        height: 44px;
    }
    .tune-panel {
        gap: 8px;
        padding: 10px;
        max-height: calc(100dvh - 64px);
    }
    .tune-group + .tune-group {
        padding-top: 8px;
    }
    .controls-hint {
        font-size: 11px;
        padding: 5px 10px;
    }
}

/* -------------------------------------------------------------------------
   Mobile portrait → urge landscape overlay
   ------------------------------------------------------------------------- */
.rotate-overlay {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: grid;
    place-items: center;
    padding: 24px;
    background: rgba(14, 16, 20, 0.92);
    color: var(--white);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.rotate-card {
    display: grid;
    justify-items: center;
    gap: 12px;
    max-width: 290px;
    text-align: center;
}

.rotate-icon {
    color: var(--white);
    animation: phone-turn 2.4s var(--ease) infinite;
}

@keyframes phone-turn {
    0%, 20% { transform: rotate(0deg); opacity: 0.95; }
    50%, 75% { transform: rotate(-90deg); opacity: 1; }
    100% { transform: rotate(0deg); opacity: 0.95; }
}

.rotate-card h1 {
    margin: 4px 0 0;
    font: 700 20px/1.2 var(--sans);
    letter-spacing: -0.01em;
}

.rotate-card p {
    margin: 0;
    font: 400 14px/1.5 var(--sans);
    color: rgba(255, 255, 255, 0.72);
}

.rotate-dismiss {
    margin-top: 8px;
    min-height: 40px;
    padding: 0 16px;
    border: 1px solid rgba(255, 255, 255, 0.28);
    border-radius: var(--radius);
    background: transparent;
    color: rgba(255, 255, 255, 0.86);
    font: 600 13px/1 var(--sans);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.rotate-dismiss:hover {
    background: rgba(255, 255, 255, 0.12);
    color: var(--white);
}

/* -------------------------------------------------------------------------
   Clean loading / WebGPU-error screen
   ------------------------------------------------------------------------- */
.loader {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: grid;
    place-items: center;
    padding: 24px;
    background: radial-gradient(circle at 50% 45%, #1c1f26 0%, #0f1115 100%);
    color: var(--white);
    transition: opacity 0.4s var(--ease), visibility 0s 0.4s;
}

.loader.is-done {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.loader-body {
    display: grid;
    gap: 10px;
    width: min(260px, 100%);
    text-align: center;
}

.loader-brand {
    font: 700 11px/1 var(--mono);
    letter-spacing: 0.36em;
    padding-left: 0.36em;
    color: rgba(255, 255, 255, 0.55);
}

.loader-scene {
    font: 700 22px/1.2 var(--sans);
    letter-spacing: -0.01em;
}

.loader-track {
    position: relative;
    height: 3px;
    margin-top: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.14);
    overflow: hidden;
}

.loader-bar {
    height: 100%;
    width: 18%;
    background: var(--accent);
    border-radius: 2px;
    transition: width 0.35s var(--ease);
}

.loader-status {
    font: 500 12px/1.3 var(--mono);
    color: rgba(255, 255, 255, 0.62);
}

.error-box {
    display: grid;
    justify-items: center;
    gap: 10px;
    max-width: 380px;
    padding: 24px;
    border: 1px solid var(--charcoal-border);
    border-radius: var(--radius);
    background: rgba(24, 26, 31, 0.96);
    text-align: center;
}

.error-badge {
    padding: 3px 8px;
    border-radius: 2px;
    background: var(--accent);
    color: var(--white);
    font: 700 11px/1.2 var(--mono);
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.error-box h2 {
    margin: 4px 0 0;
    font: 700 18px/1.25 var(--sans);
}

.error-box p {
    margin: 0;
    font: 400 13px/1.5 var(--sans);
    color: rgba(255, 255, 255, 0.78);
    overflow-wrap: anywhere;
}

#error-message {
    font-family: var(--mono);
    font-size: 12px;
    color: rgba(255, 255, 255, 0.92);
}

.error-hint {
    color: rgba(255, 255, 255, 0.56);
    font-size: 12px;
}

.error-reload {
    margin-top: 6px;
    min-height: 38px;
    padding: 0 18px;
    border: 0;
    border-radius: var(--radius);
    background: var(--white);
    color: var(--ink);
    font: 700 13px/1 var(--sans);
    cursor: pointer;
}

.error-reload:hover {
    background: var(--accent);
    color: var(--white);
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
