/**
 * STORMSCAPE — Application shell
 *
 * Layout philosophy: the map is full-bleed and edge to edge. Panels float over
 * it as inset glass surfaces rather than sitting in rigid columns, so the
 * scenery always reaches the edges of the screen and the app reads as a
 * viewport with instruments on it — not a dashboard with a map widget in it.
 *
 * Every floating surface is positioned with `position: fixed` against the
 * viewport and uses safe-area insets so it survives notches and home bars.
 */

.app {
  position: relative;
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
  background: var(--bg-void);
}

/* ========================================================================== */
/* STAGE — the map and its full-frame effect layers                           */
/* ========================================================================== */

.stage {
  position: fixed;
  inset: 0;
  z-index: var(--z-map);
  background:
    radial-gradient(120% 90% at 50% 8%, #16202c 0%, #0a0e13 55%, #06090d 100%);
}

.stage__map {
  position: absolute;
  inset: 0;
}

/* MapLibre paints its own background; we want ours to show through while
   tiles stream in. */
.stage__map .maplibregl-canvas {
  outline: none;
}

/* Colour grade. Driven by CSS custom properties that main.js writes each time
   the storm's darkness or the time of day changes — one style write per change
   rather than per frame. */
.stage__grade {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  /* Two stacked washes: a directional sky tint and an overall darkening. */
  background:
    linear-gradient(
      180deg,
      color-mix(in srgb, var(--grade-sky, #2a3644) var(--grade-sky-strength, 0%), transparent) 0%,
      transparent 45%
    ),
    color-mix(in srgb, #050810 var(--grade-darkness, 0%), transparent);
  transition:
    background var(--duration-slower) var(--ease-out-soft),
    opacity var(--duration-slower) var(--ease-out-soft);
  mix-blend-mode: multiply;
}

/* Lightning. Opacity is set imperatively at flash time and decays via
   transition, so no JS runs per frame for the falloff. */
.stage__flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
  opacity: 0;
  background:
    radial-gradient(
      60% 50% at var(--flash-x, 50%) var(--flash-y, 40%),
      rgba(226, 238, 255, 0.92) 0%,
      rgba(190, 214, 250, 0.34) 32%,
      transparent 68%
    );
  mix-blend-mode: screen;
  will-change: opacity;
}

.stage__vignette {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  background: radial-gradient(
    128% 108% at 50% 46%,
    transparent 56%,
    rgba(4, 7, 12, 0.22) 84%,
    rgba(3, 5, 9, 0.44) 100%
  );
}

/* Users who ask for reduced motion get no lightning at all — a full-frame
   white flash is precisely the kind of effect that triggers discomfort. */
@media (prefers-reduced-motion: reduce) {
  .stage__flash {
    display: none;
  }
}

/* ========================================================================== */
/* TOP BAR                                                                    */
/* ========================================================================== */

.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: calc(var(--topbar-height) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) var(--space-5) 0;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  z-index: var(--z-topbar);
  pointer-events: none;
  /* A soft scrim rather than a solid bar, so the map still reads behind it. */
  background: linear-gradient(
    180deg,
    color-mix(in srgb, var(--bg-void) 78%, transparent) 0%,
    color-mix(in srgb, var(--bg-void) 42%, transparent) 62%,
    transparent 100%
  );
}

.topbar > * {
  pointer-events: auto;
}

.topbar__brand {
  display: flex;
  align-items: center;
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  text-decoration: none;
  color: var(--text-primary);
  border-radius: var(--radius-md);
  padding: var(--space-1) var(--space-1);
  transition: opacity var(--duration-fast) var(--ease-out);
}

.brand:hover {
  opacity: 0.82;
}

.brand__mark {
  display: block;
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
}

.brand__word {
  display: block;
  height: 15px;
}

.topbar__context {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
  margin-left: var(--space-3);
  /* Fades in only once a simulation is running. */
  animation: chip-in var(--duration-slow) var(--ease-out) both;
}

@keyframes chip-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.context-chip {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
  padding: var(--space-2) var(--space-4) var(--space-2) var(--space-3);
  border-radius: var(--radius-full);
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg-panel-solid) 72%, transparent);
  backdrop-filter: blur(var(--panel-blur)) saturate(var(--panel-saturate));
  -webkit-backdrop-filter: blur(var(--panel-blur)) saturate(var(--panel-saturate));
  box-shadow: var(--shadow-sm), inset 0 1px 0 var(--edge-highlight);
  color: var(--text-primary);
  text-align: left;
  transition:
    background var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

.context-chip:hover {
  background: color-mix(in srgb, var(--bg-control-hover) 88%, transparent);
  border-color: var(--border-strong);
}

.context-chip:active {
  transform: scale(0.985);
}

.context-chip__icon {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  color: var(--text-tertiary);
}

.context-chip__body {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.25;
}

.context-chip__label {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  letter-spacing: var(--tracking-tight);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.context-chip__meta {
  font-size: var(--text-2xs);
  color: var(--text-tertiary);
  letter-spacing: var(--tracking-wide);
}

.topbar__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
  flex: 0 0 auto;
}

.icon-button {
  display: grid;
  place-items: center;
  position: relative;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  color: var(--text-secondary);
  transition:
    background var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}

.icon-button svg {
  width: 19px;
  height: 19px;
}

.icon-button:hover {
  background: color-mix(in srgb, var(--bg-control-hover) 80%, transparent);
  border-color: var(--border);
  color: var(--text-primary);
}

/* Theme toggle cross-fades its two glyphs rather than swapping them. */
.icon-button__glyph {
  grid-area: 1 / 1;
  transition:
    opacity var(--duration-normal) var(--ease-out),
    transform var(--duration-normal) var(--ease-out);
}

.icon-button__glyph[data-icon='sun'] {
  opacity: 0;
  transform: rotate(-70deg) scale(0.7);
}

:root[data-theme='light'] .icon-button__glyph[data-icon='moon'] {
  opacity: 0;
  transform: rotate(70deg) scale(0.7);
}

:root[data-theme='light'] .icon-button__glyph[data-icon='sun'] {
  opacity: 1;
  transform: none;
}

/* ========================================================================== */
/* FLOATING PANELS — left rail and right context panel                        */
/* ========================================================================== */

.rail,
.panel {
  position: fixed;
  top: calc(var(--topbar-height) + env(safe-area-inset-top) + var(--space-3));
  bottom: calc(var(--timeline-height) + env(safe-area-inset-bottom) + var(--space-3));
  z-index: var(--z-panel);
  display: flex;
  border-radius: var(--radius-xl);
  border: 1px solid var(--border);
  background: var(--bg-panel);
  backdrop-filter: blur(var(--panel-blur)) saturate(var(--panel-saturate));
  -webkit-backdrop-filter: blur(var(--panel-blur)) saturate(var(--panel-saturate));
  box-shadow: var(--shadow-lg), inset 0 1px 0 var(--edge-highlight);
  /* Transform, not width — animating width relayouts the map every frame. */
  transition:
    transform var(--duration-slow) var(--ease-out),
    opacity var(--duration-normal) var(--ease-out);
}

.rail {
  left: var(--space-4);
  width: var(--rail-width);
}

.panel {
  right: var(--space-4);
  width: var(--panel-width);
}

.rail[data-open='false'] {
  transform: translateX(calc(-100% - var(--space-4) - 2px));
  opacity: 0;
  pointer-events: none;
}

.panel[data-open='false'] {
  transform: translateX(calc(100% + var(--space-4) + 2px));
  opacity: 0;
  pointer-events: none;
}

.rail__inner,
.panel__inner {
  flex: 1;
  min-width: 0;
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

/* Collapse handles ride on the outer edge of each panel and stay put when the
   panel slides away, so there is always something to click to bring it back. */
.rail__handle,
.panel__handle {
  position: fixed;
  top: 50%;
  z-index: calc(var(--z-panel) + 1);
  display: grid;
  place-items: center;
  width: 22px;
  height: 62px;
  margin-top: -31px;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  backdrop-filter: blur(var(--panel-blur));
  -webkit-backdrop-filter: blur(var(--panel-blur));
  color: var(--text-tertiary);
  box-shadow: var(--shadow-md);
  transition:
    left var(--duration-slow) var(--ease-out),
    right var(--duration-slow) var(--ease-out),
    color var(--duration-fast) var(--ease-out),
    background var(--duration-fast) var(--ease-out);
}

.rail__handle svg,
.panel__handle svg {
  width: 11px;
  height: 18px;
  transition: transform var(--duration-slow) var(--ease-out);
}

.rail__handle:hover,
.panel__handle:hover {
  color: var(--text-primary);
  background: var(--bg-control-hover);
}

.rail__handle {
  left: calc(var(--space-4) + var(--rail-width) - 1px);
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  border-left: none;
}

.panel__handle {
  right: calc(var(--space-4) + var(--panel-width) - 1px);
  border-radius: var(--radius-md) 0 0 var(--radius-md);
  border-right: none;
}

.rail[data-open='false'] ~ .rail__handle,
.rail[data-open='false'] + .rail__handle {
  left: 0;
}

.panel[data-open='false'] + .panel__handle {
  right: 0;
}

.rail[data-open='false'] + .rail__handle svg,
.panel[data-open='false'] + .panel__handle svg {
  transform: scaleX(-1);
}

/* ========================================================================== */
/* VIEW DOCK — camera presets and map modes, bottom-left of the stage         */
/* ========================================================================== */

.viewdock {
  position: fixed;
  left: calc(var(--space-4) + var(--rail-width) + var(--space-4));
  /* Bounded on the right by the analysis panel so the dock can never run
     underneath it; it scrolls internally instead. */
  right: calc(var(--space-4) + var(--panel-width) + var(--space-4));
  bottom: calc(var(--timeline-height) + env(safe-area-inset-bottom) + var(--space-3));
  z-index: var(--z-map-overlay);
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  gap: var(--space-2);
  overflow-x: auto;
  scrollbar-width: none;
  transition:
    left var(--duration-slow) var(--ease-out),
    right var(--duration-slow) var(--ease-out);
  pointer-events: none;
}

.viewdock::-webkit-scrollbar {
  display: none;
}

.viewdock > * {
  flex: 0 0 auto;
}

.app[data-panel='closed'] .viewdock {
  right: var(--space-4);
}

.viewdock > * {
  pointer-events: auto;
}

.app[data-rail='closed'] .viewdock {
  left: var(--space-4);
}

/* ========================================================================== */
/* MINI DOCK — radar, wind and hazard readouts floating over the stage        */
/* ========================================================================== */

.minidock {
  position: fixed;
  right: calc(var(--space-4) + var(--panel-width) + var(--space-4));
  top: calc(var(--topbar-height) + env(safe-area-inset-top) + var(--space-3));
  z-index: var(--z-map-overlay);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-3);
  transition: right var(--duration-slow) var(--ease-out);
  pointer-events: none;
}

.minidock > * {
  pointer-events: auto;
}

.app[data-panel='closed'] .minidock {
  right: var(--space-4);
}

/* ========================================================================== */
/* TIMELINE                                                                   */
/* ========================================================================== */

.timeline {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + var(--space-4));
  transform: translateX(-50%);
  z-index: var(--z-panel);
  width: min(760px, calc(100vw - var(--space-8)));
  border-radius: var(--radius-xl);
  border: 1px solid var(--border);
  background: var(--bg-panel);
  backdrop-filter: blur(var(--panel-blur)) saturate(var(--panel-saturate));
  -webkit-backdrop-filter: blur(var(--panel-blur)) saturate(var(--panel-saturate));
  box-shadow: var(--shadow-lg), inset 0 1px 0 var(--edge-highlight);
  transition:
    transform var(--duration-slow) var(--ease-out),
    opacity var(--duration-normal) var(--ease-out);
}

/* ========================================================================== */
/* STATE TRANSITIONS                                                          */
/* ========================================================================== */
/* During onboarding the instruments are gone entirely — the first screen is
   scenery and a single question. They fly in when the simulation launches. */

.app[data-state='onboarding'] .topbar__context,
.app[data-state='booting'] .topbar__context {
  display: none;
}

.app[data-state='onboarding'] .rail,
.app[data-state='onboarding'] .panel,
.app[data-state='onboarding'] .timeline,
.app[data-state='onboarding'] .viewdock,
.app[data-state='onboarding'] .minidock,
.app[data-state='booting'] .rail,
.app[data-state='booting'] .panel,
.app[data-state='booting'] .timeline,
.app[data-state='booting'] .viewdock,
.app[data-state='booting'] .minidock,
.app[data-state='booting'] .rail__handle,
.app[data-state='booting'] .panel__handle,
.app[data-state='onboarding'] .rail__handle,
.app[data-state='onboarding'] .panel__handle {
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
}

.app[data-state='onboarding'] .rail,
.app[data-state='booting'] .rail {
  transform: translateX(-24px);
}

.app[data-state='onboarding'] .panel,
.app[data-state='booting'] .panel {
  transform: translateX(24px);
}

.app[data-state='onboarding'] .timeline,
.app[data-state='booting'] .timeline {
  transform: translateX(-50%) translateY(24px);
}

/* Staggered entrance when the simulation opens. Each surface arrives on its
   own beat so the interface assembles rather than appearing. */
.app[data-state='running'] .rail {
  transition-delay: 120ms;
}
.app[data-state='running'] .panel {
  transition-delay: 200ms;
}
.app[data-state='running'] .timeline {
  transition-delay: 280ms;
}
.app[data-state='running'] .viewdock,
.app[data-state='running'] .minidock {
  transition-delay: 340ms;
}

/* ========================================================================== */
/* RESPONSIVE                                                                 */
/* ========================================================================== */

/* Laptop — tighten the instruments so the stage keeps its breathing room. */
@media (max-width: 1360px) {
  :root {
    --rail-width: 292px;
    --panel-width: 336px;
  }
}

/* Small laptop — the two panels plus a usable stage no longer fit side by
   side, so the right panel becomes an overlay that the user opens on demand. */
@media (max-width: 1180px) {
  :root {
    --rail-width: 280px;
    --panel-width: 330px;
  }

  .minidock {
    right: var(--space-4);
    top: calc(var(--topbar-height) + env(safe-area-inset-top) + var(--space-3));
  }

  .panel {
    box-shadow: var(--shadow-xl), inset 0 1px 0 var(--edge-highlight);
  }
}

/* Tablet — both side panels become overlays; the stage runs full width. */
@media (max-width: 940px) {
  .viewdock {
    left: var(--space-4);
    right: var(--space-4);
  }

  .minidock {
    top: auto;
    bottom: calc(var(--timeline-height) + env(safe-area-inset-bottom) + var(--space-3));
    right: var(--space-4);
    flex-direction: row;
    align-items: flex-end;
  }

  .rail,
  .panel {
    box-shadow: var(--shadow-xl), inset 0 1px 0 var(--edge-highlight);
  }
}

/* ---------------------------------------------------------------------------
   Mobile — the shell reorganises rather than shrinking.

   Panels become a single bottom sheet with tabs, dragged by a grab handle. The
   stage keeps the top ~55% of the screen so the storm is always visible while
   the user reads or adjusts. This is the layout the mobile experience is
   designed around, not a squeezed desktop.
   --------------------------------------------------------------------------- */
@media (max-width: 720px) {
  :root {
    --topbar-height: 52px;
    --timeline-height: 56px;
    /* How much of the sheet stays on screen when collapsed. Declared here
       rather than relied on as a var() fallback so every consumer below shares
       one number — the timeline, the view dock and the toasts all stack
       against it, and a mismatch makes them overlap. */
    --sheet-peek: 132px;
  }

  .topbar {
    padding-left: var(--space-3);
    padding-right: var(--space-3);
    gap: var(--space-2);
  }

  .topbar__context {
    margin-left: var(--space-1);
    min-width: 0;
    overflow: hidden;
  }

  /* On phones the storm chip is redundant with the sheet header; the location
     matters more, so it gets the space. */
  #chip-storm {
    display: none;
  }

  .context-chip {
    padding: var(--space-1) var(--space-3) var(--space-1) var(--space-2);
  }

  .rail__handle,
  .panel__handle {
    display: none;
  }

  .viewdock {
    left: var(--space-3);
    right: var(--space-3);
    /* Clears the sheet *and* the timeline that sits on top of it. */
    bottom: calc(
      var(--sheet-peek) + var(--timeline-height) + env(safe-area-inset-bottom) + var(--space-4)
    );
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
    transition: bottom var(--duration-normal) var(--ease-out);
  }

  .viewdock::-webkit-scrollbar {
    display: none;
  }

  .minidock {
    top: calc(var(--topbar-height) + env(safe-area-inset-top) + var(--space-2));
    bottom: auto;
    left: var(--space-3);
    right: var(--space-3);
    flex-direction: row;
    align-items: flex-start;
    justify-content: flex-start;
    gap: var(--space-2);
    overflow-x: auto;
    scrollbar-width: none;
  }

  .minidock::-webkit-scrollbar {
    display: none;
  }

  /* Sheet: rail and panel stack into one surface, switched by tabs. */
  .rail,
  .panel {
    top: auto;
    left: 0;
    right: 0;
    width: 100%;
    bottom: 0;
    height: 62dvh;
    /* The desktop panel is a flex row (handle beside content). As a sheet it
       gains a grab handle and a tab bar above the content, so it has to stack —
       otherwise those two sit *beside* the panel body and shove it off screen. */
    flex-direction: column;
    max-height: 78dvh;
    border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
    border-bottom: none;
    border-left: none;
    border-right: none;
    transform: translateY(calc(100% - var(--sheet-peek)));
    transition: transform var(--duration-slow) var(--ease-out);
    box-shadow: var(--shadow-xl), inset 0 1px 0 var(--edge-highlight);
  }

  .rail[data-open='false'],
  .panel[data-open='false'] {
    transform: translateY(calc(100% - var(--sheet-peek)));
    opacity: 1;
    pointer-events: auto;
  }

  .rail[data-open='true'],
  .panel[data-open='true'] {
    transform: translateY(0);
  }

  /* Only the active tab's sheet is in the flow. */
  .app[data-sheet='controls'] .panel,
  .app[data-sheet='analysis'] .rail {
    display: none;
  }

  .rail__inner,
  .panel__inner {
    padding: var(--space-4) var(--space-4) calc(var(--space-16) + env(safe-area-inset-bottom));
    gap: var(--space-4);
  }

  .timeline {
    bottom: calc(var(--sheet-peek) + env(safe-area-inset-bottom) + var(--space-2));
    width: calc(100vw - var(--space-6));
    transition:
      bottom var(--duration-normal) var(--ease-out),
      transform var(--duration-slow) var(--ease-out);
  }

  .app[data-state='onboarding'] .timeline,
  .app[data-state='booting'] .timeline {
    transform: translateX(-50%) translateY(32px);
  }

  /* When a sheet is dragged fully open, the timeline and view dock get out of
     the way rather than being covered. */
  .app[data-sheet-open='true'] .timeline,
  .app[data-sheet-open='true'] .viewdock {
    opacity: 0;
    pointer-events: none;
    transform: translateX(-50%) translateY(16px);
  }

  .app[data-sheet-open='true'] .viewdock {
    transform: translateY(16px);
  }
}

/* Short landscape phones: there is no room for a peeking sheet plus a stage,
   so the sheet fully hides until summoned. */
@media (max-height: 480px) and (max-width: 940px) {
  :root {
    --sheet-peek: 0px;
  }

  .rail,
  .panel {
    height: 88dvh;
  }
}

/* ========================================================================== */
/* NOSCRIPT                                                                   */
/* ========================================================================== */

.noscript {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  gap: var(--space-4);
  padding: var(--space-8);
  text-align: center;
  background: var(--bg-base);
  z-index: var(--z-modal);
}

.noscript h1 {
  font-family: var(--font-display);
  font-size: var(--text-2xl);
  letter-spacing: var(--tracking-widest);
}

.noscript p {
  max-width: 46ch;
  color: var(--text-secondary);
}
