/* Base: reset, typography, app grid — owner: shell. */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: var(--lh);
  color: var(--text);
  background-color: var(--bg);
  background-image:
    radial-gradient(1100px 620px at 12% -8%, var(--bg-glow-1), transparent 62%),
    radial-gradient(900px 520px at 100% 110%, var(--bg-glow-2), transparent 60%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: "palt" 1;
  overflow: hidden;
}

/* Faint terminal grid texture behind everything. */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 48px 48px;
  mask-image: radial-gradient(ellipse at 40% 30%, #000 0%, transparent 75%);
  -webkit-mask-image: radial-gradient(ellipse at 40% 30%, #000 0%, transparent 75%);
  opacity: 0.55;
  z-index: 0;
}

h1, h2, h3, h4, h5, h6 {
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.01em;
  line-height: 1.25;
}

p {
  margin: 0;
}

a {
  color: var(--accent-strong);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

img,
svg,
canvas {
  display: block;
}

[hidden] {
  display: none !important;
}

::selection {
  background: var(--accent-soft);
  color: var(--text);
}

:focus-visible {
  outline: none;
  box-shadow: var(--ring);
  border-radius: var(--r-sm);
}

/* Numbers everywhere: tabular, monospaced. */
.num,
.mono,
kbd,
code {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1, "zero" 0;
}

.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--flat); }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.accent { color: var(--accent); }
.warn { color: var(--warn); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Scrollbars */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 10px;
  border: 3px solid transparent;
  background-clip: padding-box;
}
*::-webkit-scrollbar-track { background: transparent; }

/* ------------------------------------------------------------------
   App grid
   ------------------------------------------------------------------ */

.app {
  position: relative;
  z-index: 1;
  display: grid;
  height: 100vh;
  height: 100dvh;
  grid-template-columns: var(--lab-w) minmax(0, 1fr);
  grid-template-rows: var(--topbar-h) minmax(0, 1fr) var(--timeline-h);
  grid-template-areas:
    "top top"
    "lab stage"
    "time time";
  transition: grid-template-columns var(--dur-3) var(--ease-out);
}

.app.lab-collapsed {
  grid-template-columns: 0 minmax(0, 1fr);
}

.app > .topbar { grid-area: top; }
.app > .lab-shell { grid-area: lab; }
.app > .stage { grid-area: stage; }
.app > .timeline-dock { grid-area: time; }

.lab-shell {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: linear-gradient(180deg, var(--panel) 0%, transparent 100%);
  backdrop-filter: blur(var(--blur));
  -webkit-backdrop-filter: blur(var(--blur));
}
.lab-shell > #lab {
  height: 100%;
  width: var(--lab-w);
  overflow-y: auto;
  overflow-x: hidden;
}
.lab-close { display: none !important; }

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.views {
  position: relative;
  flex: 1;
  min-height: 0;
}

/* Higher specificity on purpose: view CSS may add classes to its mount element. */
.views > .view {
  position: absolute;
  inset: 0;
  overflow: auto;
  opacity: 0;
  visibility: hidden;
  transform: translateY(6px);
  transition:
    opacity var(--dur-2) var(--ease-out),
    transform var(--dur-3) var(--ease-out),
    visibility 0s linear var(--dur-2);
}
.views > .view.is-active {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition:
    opacity var(--dur-2) var(--ease-out),
    transform var(--dur-3) var(--ease-out),
    visibility 0s;
}

.timeline-dock {
  position: relative;
  min-width: 0;
  border-top: 1px solid var(--border);
  background: var(--panel);
  backdrop-filter: blur(var(--blur));
  -webkit-backdrop-filter: blur(var(--blur));
  z-index: 2;
}

/* Right slide-over drawer (shell owns the frame; ui/drawer.js owns #drawer content). */
.drawer-shell {
  position: fixed;
  top: var(--topbar-h);
  right: 0;
  bottom: var(--timeline-h);
  width: var(--drawer-w);
  z-index: var(--z-drawer);
  display: flex;
  flex-direction: column;
  background: var(--panel-solid);
  background: color-mix(in srgb, var(--panel-solid) 90%, transparent);
  backdrop-filter: blur(24px) saturate(1.2);
  -webkit-backdrop-filter: blur(24px) saturate(1.2);
  border-left: 1px solid var(--border-strong);
  box-shadow: -30px 0 80px -30px rgba(0, 0, 0, 0.6);
  transform: translateX(104%);
  transition: transform var(--dur-3) var(--ease-out);
  will-change: transform;
}
.app.drawer-open .drawer-shell { transform: none; }
.drawer-shell > #drawer {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
}
.float-close {
  position: absolute;
  z-index: 3;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--panel-solid);
  color: var(--text-2);
  box-shadow: var(--shadow-1);
}
.float-close:hover { background: var(--panel-solid); color: var(--text); border-color: var(--accent); }
.drawer-close {
  /* inside the drawer, next to the header's ☆ (never floating over the stage / tab bar) */
  top: 10px;
  right: 12px;
  left: auto;
  width: 30px;
  height: 30px;
  z-index: 6; /* above the drawer's sticky header (z-index 4) */
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-2) ease;
}
.app.drawer-open .drawer-close { opacity: 1; pointer-events: auto; }

.scrim {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-drawer) - 1);
  background: var(--overlay);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-3) var(--ease-out);
}

/* ------------------------------------------------------------------
   Responsive: ≤ 900px → lab & drawer become sheets
   ------------------------------------------------------------------ */
@media (max-width: 900px) {
  .app,
  .app.lab-collapsed {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "top"
      "stage"
      "time";
  }

  .lab-shell {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: min(78dvh, 720px);
    z-index: var(--z-sheet);
    border-right: 0;
    border-top: 1px solid var(--border-strong);
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    background: var(--panel-solid);
    box-shadow: 0 -30px 80px -20px rgba(0, 0, 0, 0.6);
    transform: translateY(104%);
    transition: transform var(--dur-3) var(--ease-out);
    display: flex;
    flex-direction: column;
  }
  .app.lab-open .lab-shell { transform: none; }
  .lab-shell { overflow: visible; }
  .lab-shell > #lab { border-radius: var(--r-xl) var(--r-xl) 0 0; }
  .lab-close {
    display: inline-grid !important;
    top: -46px;
    right: 12px;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dur-2) ease;
  }
  .app.lab-open .lab-close { opacity: 1; pointer-events: auto; }
  .lab-shell > #lab {
    width: 100%;
    flex: 1;
  }

  .drawer-shell {
    top: auto;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 92dvh;
    border-left: 0;
    border-top: 1px solid var(--border-strong);
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    transform: translateY(104%);
    z-index: var(--z-sheet);
  }
  .app.drawer-open .drawer-shell { transform: none; }
  .drawer-shell > #drawer { border-radius: var(--r-xl) var(--r-xl) 0 0; }
  .drawer-close { top: 12px; left: auto; right: 10px; }
  /* grab handle */
  .drawer-shell::before {
    content: "";
    position: absolute;
    top: 5px;
    left: 50%;
    width: 38px;
    height: 4px;
    margin-left: -19px;
    border-radius: 2px;
    background: color-mix(in srgb, var(--text) 25%, transparent);
    z-index: 4;
  }

  .app.lab-open .scrim,
  .app.drawer-open .scrim {
    opacity: 1;
    pointer-events: auto;
    z-index: calc(var(--z-sheet) - 1);
  }
}

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

/* ≥1280px: the drawer pushes the stage instead of overlaying it, so the ranking re-fits its columns
   (ResizeObserver → fitColumns) into a clean compact table rather than being cut mid-column. */
@media (min-width: 1280px) {
  .app > .stage { transition: margin-right var(--dur-3) var(--ease-out); }
  .app.drawer-open > .stage { margin-right: var(--drawer-w); }
  /* side-by-side, not floating: no big drop shadow tinting the ranking */
  .drawer-shell { box-shadow: -12px 0 30px -24px rgba(0, 0, 0, 0.35); }
}

/* While the time machine is scrubbed / played, blurred glass over changing content is too expensive. */
.tm-fast .drawer-shell,
.tm-fast .ins-hd,
.tm-fast .tl-banner {
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}
.tm-fast .drawer-shell { background: var(--panel-solid); }
