/* Shared design tokens for every surface (must precede all rules). */
@import "./src/ui/theme.css";

:root {
  --ink: #1c1f24;
  --tray-bg: rgba(255, 255, 255, 0.82);
  --line: rgba(20, 24, 30, 0.12);
  --accent: #2dd4ff;
  --pixel-font: ui-monospace, "Cascadia Mono", "Consolas", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #000000;
  font-family: var(--cb-font-ui, system-ui, sans-serif);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  touch-action: none;
  cursor: grab;
}
#scene.grabbing { cursor: grabbing; }
#scene.over-part { cursor: pointer; }

/* ---- overlays ---- */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(6, 6, 8, 0.94);
  z-index: 50;
  transition: opacity .45s ease;
}
.overlay.hidden, .hidden { display: none !important; }
/* The loader fades out over the freshly-revealed bench (see playLabIntro). */
.overlay.leaving { opacity: 0; pointer-events: none; }

/* ---- loading screen ---- */
#loading {
  background:
    radial-gradient(circle at 50% 42%, rgba(255, 255, 255, 0.06), transparent 58%),
    rgba(6, 6, 8, 0.97);
}
.loading-card { text-align: center; color: #e8e8ea; position: relative; }
/* The loader IS a 2D resistor with current marching through it. Two overlaid
   copies of the same conductor path (leads + zig-zag body): a dim track and a
   bright cyan "current" whose dashes flow left→right via stroke-dashoffset —
   that flow is the loading indicator (no separate bar). */
.load-resistor { display: block; width: 200px; height: 44px; margin: 0 auto 18px; overflow: visible; }
.load-resistor path { fill: none; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.load-resistor .lr-track { stroke: rgba(255, 255, 255, 0.22); }
.load-resistor .lr-flow {
  stroke: #ffffff;
  stroke-dasharray: 9 15;                 /* dash + gap = 24; matches the keyframe loop */
  filter: drop-shadow(0 0 4px rgba(255, 255, 255, 0.85));
  animation: lrFlow .7s linear infinite;
}
@keyframes lrFlow { to { stroke-dashoffset: -24; } }
/* ---- activity spinner (src/ui/Spinner.js + the static loader markup) ----
   N rounded bars on a ring; each fades on a stagger so the bright one chases
   around. Bars inherit currentColor; --invert forces a light tone for dark
   backdrops. Geometry comes in via the --cb-spin-* custom properties. */
.cb-spinner {
  position: relative; display: inline-block;
  width: var(--cb-spin-size, 16px); height: var(--cb-spin-size, 16px);
  color: inherit; flex: 0 0 auto;
}
.cb-spinner--invert { color: #eef2f8; }
.cb-spinner > span {
  position: absolute; left: 50%; top: 50%;
  width: var(--cb-spin-bw, 4px); height: var(--cb-spin-bh, 2px);
  margin-left: calc(var(--cb-spin-bw, 4px) / -2);
  margin-top: calc(var(--cb-spin-bh, 2px) / -2);
  background: currentColor; border-radius: 999px;
  opacity: .15; animation: cbSpin 1s linear infinite;
}
@keyframes cbSpin { 0% { opacity: 1; } 100% { opacity: .15; } }
.loading-spinner { margin: 0 auto 18px; color: #e8e8ea; }
@media (prefers-reduced-motion: reduce) { .cb-spinner > span { animation-duration: 2.6s; } }

.loading-text { letter-spacing: .14em; text-transform: uppercase; font-size: 12px; }
.loading-sub { margin-top: 6px; font-size: 11px; opacity: .5; min-height: 14px; }

/* ---- lab entrance: staggered reveal once the loader clears (body.booted) ---- */
@keyframes benchReveal { from { opacity: 0; } to { opacity: 1; } }
@keyframes chromeDropIn {
  from { opacity: 0; transform: translateY(-14px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes chromePopIn {
  from { opacity: 0; transform: scale(.55); }
  to   { opacity: 1; transform: scale(1); }
}
/* The tray stands up from fallen — a callback to its falling-wall fold. */
@keyframes trayStandUp {
  0%   { opacity: 0; transform: translateX(-50%) perspective(900px) rotateX(-90deg); }
  55%  { opacity: 1; }
  100% { opacity: 1; transform: translateX(-50%) perspective(900px) rotateX(0deg); }
}
body.booted #scene       { animation: benchReveal 1.1s ease both; }
body.booted #labbar      { animation: chromeDropIn .55s cubic-bezier(.22, .61, .36, 1) .30s backwards; }
body.booted #help-toggle { animation: chromePopIn .55s cubic-bezier(.22, 1, .36, 1) .46s backwards; }
body.booted #tray        { animation: trayStandUp .9s cubic-bezier(.22, 1, .36, 1) .6s backwards; }
@media (prefers-reduced-motion: reduce) {
  body.booted #scene, body.booted #labbar,
  body.booted #help-toggle, body.booted #tray { animation: none; }
  .load-resistor .lr-flow { animation: none; }
}

.error-card {
  max-width: 520px; padding: 24px 28px; border: 2px solid #ff5a5a;
  background: #fff; border-radius: 10px;
}
.error-card h2 { margin: 0 0 10px; color: #d0322c; }
#error-text {
  white-space: pre-wrap; font-size: 11px; background: #faf2f2;
  padding: 10px; border-radius: 6px; max-height: 180px; overflow: auto;
}
.error-hint { font-size: 12px; opacity: .7; line-height: 1.5; }
.error-hint code { background: #eef0f3; padding: 1px 5px; border-radius: 4px; }

/* ---- parts tray ---- */
.tray {
  position: fixed;
  left: 50%;
  bottom: 18px;
  /* Upright resting (open) state: the tray stands facing you like a wall.
     .tray.folded tips it forward on its bottom-edge hinge — a "falling wall".
     perspective() is held constant in both states so only the angle animates. */
  transform: translateX(-50%) perspective(900px) rotateX(0deg);
  transform-origin: 50% 100%;        /* hinge runs along the bottom edge */
  will-change: transform;
  /* Falls down out of the way when the cursor drifts off, stands back up on
     approach — driven by installTrayAutoFold + the .tray.folded rule below.
     The slight overshoot ease gives the fall / stand-up a springy settle. */
  transition: transform .52s cubic-bezier(.22, 1, .36, 1),
              opacity .4s ease, box-shadow .45s ease;
  display: flex;
  gap: 8px;
  padding: 8px 10px;
  max-width: 94vw;
  overflow-x: auto;
  background: var(--tray-bg);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 6px 14px rgba(20, 24, 30, 0.14);
  z-index: 20;
}
/* Folded "fallen wall": the tray tips down on its bottom hinge to a steep angle,
   left open by a small angle so it stays visible + reachable along the bottom.
   Negative rotateX topples it *toward* the viewer (top edge arcs forward as it
   falls). Toggled by installTrayAutoFold from cursor proximity (mouse only). */
.tray.folded {
  transform: translateX(-50%) perspective(900px) rotateX(-72deg);
  opacity: .92;
  box-shadow: 0 6px 14px rgba(20, 24, 30, 0.22);
}
/* While folded the peeking lip is only a reveal target — approaching/hovering
   it opens the tray (events fall through to the container), so the buttons in
   the strip shouldn't spawn parts or scroll on a stray click. */
.tray.folded > * { pointer-events: none; }
/* Keyboard reveal: focusing any control inside the tray (Tab) force-opens it,
   independent of the JS focus latch — so keyboard users are never left with the
   focused button tucked off-screen. Placed after .folded so equal specificity
   resolves in its favour. */
.tray:focus-within {
  transform: translateX(-50%) perspective(900px) rotateX(0deg);
  opacity: 1;
}
.tray:focus-within > * { pointer-events: auto; }
@media (prefers-reduced-motion: reduce) {
  /* Auto-fold is disabled in JS here (see installTrayAutoFold), so the tray
     simply stays open. This rule is a belt-and-suspenders fallback: should the
     folded class ever apply, keep the tray in place and dim it rather than
     teleporting it off-screen. */
  .tray { transition: opacity .2s ease; }
  .tray.folded { transform: translateX(-50%) perspective(900px) rotateX(0deg); opacity: .6; }
}
.tray-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  flex: 0 0 auto;
  width: 64px; padding: 8px 4px 6px;
  border: 1px solid transparent; border-radius: 10px;
  background: transparent; color: var(--ink);
  cursor: pointer; font-family: inherit; font-size: 9px;
  letter-spacing: .04em; text-transform: uppercase;
  transition: background .12s, border-color .12s, transform .08s;
}
.tray-item:hover { background: #fff; border-color: var(--line); transform: translateY(-2px); }
.tray-item:active { transform: translateY(0); }
.tray-glyph { font-size: 22px; line-height: 1; height: 24px; display: flex; align-items: center; justify-content: center; }
.tray-glyph svg { width: 24px; height: 24px; display: block; }
.tray-divider { width: 1px; align-self: stretch; background: var(--line); margin: 4px 2px; }
.tray-clear:hover { border-color: #ff7a7a; color: #d0322c; }
.tray-clear .tray-glyph { filter: saturate(1.2); }
/* Armed state of the two-click Clear All confirm. */
.tray-clear.armed {
  border-color: #ff7a7a; color: #d0322c;
  background: rgba(255, 90, 90, 0.12);
}
.tray-clear.armed .tray-label { font-weight: 700; }

/* ---- help ---- */
.help-toggle {
  position: fixed; top: 16px; right: 16px; z-index: 25;
  width: 34px; height: 34px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--tray-bg);
  color: var(--ink); font-family: inherit; font-size: 16px; cursor: pointer;
  backdrop-filter: blur(8px);
}
.help {
  position: fixed; top: 58px; right: 16px; z-index: 25;
  width: 290px; padding: 14px 16px;
  background: var(--tray-bg); backdrop-filter: blur(8px);
  border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 6px 14px rgba(20, 24, 30, 0.14);
  font-size: 12px; line-height: 1.7;
}
.help h3 { margin: 0 0 8px; font-size: 13px; letter-spacing: .08em; text-transform: uppercase; }
.help ul { margin: 0; padding-left: 16px; }
.help b { color: #0b6; }

/* ---- hover readout: an animated 3D tooltip that anchors next to the part ---- */
.readout {
  position: fixed; top: 0; left: 0; z-index: 22;
  padding: 8px 12px; min-width: 120px; max-width: 260px;
  background: var(--tray-bg); backdrop-filter: blur(8px);
  border: 1px solid var(--line); border-radius: 10px;
  font-family: var(--cb-font-mono);   /* it shows live values — data stays monospace */
  font-size: 12px; line-height: 1.5;
  pointer-events: none;          /* it follows the cursor — must never block picking */
  transform-origin: center bottom;
  will-change: transform, opacity, left, top;
  box-shadow: 0 8px 14px rgba(8, 12, 20, 0.30);
  opacity: 0;                    /* JS fades/positions/tilts it each frame */
}
.readout .rk { opacity: .5; }
.readout b { font-size: 13px; }

/* ---- past-projects load menu (the tray's Load button → dropdown) ---- */
.proj-menu {
  position: fixed; z-index: 40;
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px;
  background: var(--tray-bg); backdrop-filter: blur(10px);
  border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 10px 14px rgba(20, 24, 30, 0.26);
  font-family: var(--cb-font-ui); color: var(--ink);
  animation: pmIn .16s ease both;
}
@keyframes pmIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.proj-menu-head {
  font-size: 10px; letter-spacing: .09em; text-transform: uppercase;
  opacity: .55; padding: 2px 6px 0;
}
.proj-menu-list { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; }
.proj-menu-empty { font-size: 12px; opacity: .6; padding: 14px 8px; text-align: center; }
.proj-menu-item { display: flex; align-items: stretch; gap: 4px; border-radius: 9px; }
.proj-menu-item.current { background: rgba(45, 212, 255, 0.14); }
.proj-open {
  flex: 1 1 auto; min-width: 0;
  display: flex; flex-direction: column; gap: 2px; text-align: left;
  padding: 8px 10px; border: 1px solid transparent; border-radius: 9px;
  background: transparent; color: inherit; cursor: pointer; font-family: inherit;
}
.proj-open:hover { background: #fff; border-color: var(--line); }
.pm-name {
  font-size: 12.5px; font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.pm-meta { font-size: 10.5px; opacity: .6; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.proj-del {
  flex: 0 0 auto; align-self: center;
  padding: 6px 8px; border: 1px solid transparent; border-radius: 8px;
  background: transparent; color: var(--ink); opacity: .45; cursor: pointer;
  font-family: inherit; font-size: 11px;
}
.proj-del:hover { opacity: 1; color: #d0322c; border-color: #ff7a7a; background: #fff; }
.proj-del.armed {
  opacity: 1; color: #d0322c; border-color: #ff7a7a; background: #fff;
  font-size: 10px; text-transform: uppercase; letter-spacing: .03em;
}

