:root {
  color-scheme: dark;
  --sidebar-width: clamp(300px, 26vw, 400px);
  --bg: #0d1416;
  --panel: rgba(16, 28, 31, 0.78);
  --panel-border: rgba(177, 200, 194, 0.16);
  --ink: #e9efe6;
  --muted: #9cb0aa;
  --accent: #d9b36a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", serif;
}

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  height: 100vh;
}

.sidebar {
  padding: clamp(16px, 1.6vw, 24px);
  background: var(--panel);
  border-right: 1px solid var(--panel-border);
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
}

.eyebrow {
  margin: 0;
  color: var(--accent);
  font-size: 0.76rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 1.8rem;
  line-height: 1.1;
  font-weight: 600;
}

.lede {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
  font-size: 0.9rem;
}

.controls {
  display: flex;
  gap: 8px;
}

.scene-tabs {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 4px;
  padding: 3px;
  border-radius: 10px;
  border: 1px solid rgba(177, 200, 194, 0.16);
  background: rgba(9, 16, 19, 0.38);
}

.scene-tab {
  min-height: 32px;
  padding: 6px 8px;
  border-radius: 7px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  font-size: 0.75rem;
  letter-spacing: 0.04em;
}

.scene-tab.is-active {
  border-color: rgba(217, 179, 106, 0.42);
  background: rgba(217, 179, 106, 0.12);
  color: var(--ink);
}

.scene-panel[hidden] {
  display: none;
}

.debug-scene-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.debug-scene-grid button {
  min-height: 36px;
  line-height: 1.15;
}

.control-panel {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: 14px;
  background: rgba(195, 215, 219, 0.05);
  border: 1px solid var(--panel-border);
}

.timeseries-drawer {
  --drawer-tab-height: 38px;
  position: fixed;
  left: var(--sidebar-width);
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  justify-items: center;
  pointer-events: none;
  transform: translateY(calc(100% - var(--drawer-tab-height)));
  transition: transform 180ms ease;
}

.timeseries-drawer.is-open {
  transform: translateY(0);
}

.timeseries-tab {
  flex: none;
  width: min(220px, calc(100% - 32px));
  min-height: var(--drawer-tab-height);
  padding: 8px 14px;
  border-radius: 10px 10px 0 0;
  border-bottom-color: transparent;
  background: rgba(16, 28, 31, 0.92);
  box-shadow: 0 -10px 26px rgba(0, 0, 0, 0.28);
  pointer-events: auto;
}

.timeseries-drawer.is-open .timeseries-tab {
  border-color: rgba(217, 179, 106, 0.42);
  border-bottom-color: transparent;
  background: rgba(217, 179, 106, 0.14);
}

.timeseries-drawer-panel {
  width: 100%;
  max-height: min(46vh, 390px);
  display: grid;
  gap: 10px;
  padding: 12px clamp(14px, 2vw, 24px) 14px;
  overflow-y: auto;
  border-top: 1px solid var(--panel-border);
  background: rgba(16, 28, 31, 0.94);
  box-shadow: 0 -18px 44px rgba(0, 0, 0, 0.35);
  pointer-events: auto;
}

.timeseries-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.timeseries-actions {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
}

.timeseries-menu-wrap {
  position: relative;
}

.timeseries-icon-button {
  flex: none;
  width: 30px;
  min-width: 30px;
  height: 30px;
  min-height: 30px;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 8px;
  color: var(--muted);
}

.timeseries-icon-button:hover,
.timeseries-icon-button[aria-expanded="true"] {
  color: var(--ink);
}

.timeseries-menu-icon,
.timeseries-menu-icon::before,
.timeseries-menu-icon::after {
  width: 14px;
  height: 2px;
  display: block;
  border-radius: 999px;
  background: currentColor;
}

.timeseries-menu-icon {
  position: relative;
}

.timeseries-menu-icon::before,
.timeseries-menu-icon::after {
  content: "";
  position: absolute;
  left: 0;
}

.timeseries-menu-icon::before {
  top: -5px;
}

.timeseries-menu-icon::after {
  top: 5px;
}

.timeseries-minimize-icon {
  width: 14px;
  height: 2px;
  display: block;
  border-radius: 999px;
  background: currentColor;
}

.timeseries-menu-popover {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 12;
  width: min(260px, calc(100vw - 32px));
  max-height: min(52vh, 360px);
  padding: 8px;
  overflow-y: auto;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  background: rgba(9, 16, 19, 0.98);
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.42);
}

.timeseries-menu-popover[hidden] {
  display: none;
}

.timeseries-menu-option {
  min-height: 32px;
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  align-items: center;
  gap: 9px;
  padding: 6px 8px;
  border-radius: 6px;
  color: var(--ink);
  font-size: 0.75rem;
  line-height: 1.2;
  letter-spacing: 0;
  text-transform: none;
  cursor: pointer;
}

.timeseries-menu-option:hover {
  background: rgba(233, 239, 230, 0.07);
}

.timeseries-menu-option input {
  margin: 0;
  accent-color: var(--accent);
}

.timeseries-menu-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(9, 16, 19, 0.8);
}

.timeseries-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(142px, 1fr));
  gap: 8px;
}

.timeseries-card {
  min-width: 0;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid rgba(177, 200, 194, 0.14);
  background: rgba(9, 16, 19, 0.34);
}

.timeseries-card-header {
  min-height: 26px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.timeseries-title {
  min-width: 0;
  color: var(--muted);
  font-size: 0.66rem;
  line-height: 1.15;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  overflow-wrap: anywhere;
}

.timeseries-value {
  flex: 0 1 auto;
  max-width: 58%;
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0;
  text-align: right;
  overflow-wrap: anywhere;
}

.timeseries-chart {
  width: 100%;
  height: 58px;
  display: block;
  margin-top: 6px;
  border-radius: 6px;
  border: 1px solid rgba(233, 239, 230, 0.10);
}

.slider-field {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  color: var(--muted);
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.slider-field output {
  white-space: nowrap;
}

input[type="range"] {
  width: 100%;
  margin: 0;
  accent-color: var(--accent);
}

.spout-control {
  --spout-plane-size: auto;
  display: grid;
  grid-template-columns: minmax(118px, 1fr) minmax(54px, 68px);
  column-gap: clamp(18px, 5%, 30px);
  align-items: start;
}

.spout-plane {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  align-self: start;
  justify-self: stretch;
  overflow: hidden;
  touch-action: none;
  cursor: crosshair;
  border-radius: 8px;
  border: 1px solid rgba(177, 200, 194, 0.28);
  background:
    linear-gradient(rgba(233, 239, 230, 0.10) 1px, transparent 1px),
    linear-gradient(90deg, rgba(233, 239, 230, 0.10) 1px, transparent 1px),
    radial-gradient(circle at center, rgba(217, 179, 106, 0.12), transparent 62%),
    rgba(9, 16, 19, 0.42);
  background-size: 25% 25%, 25% 25%, 100% 100%, 100% 100%;
}

.spout-plane:focus-visible {
  outline: 2px solid rgba(217, 179, 106, 0.72);
  outline-offset: 2px;
}

.spout-plane-axis {
  position: absolute;
  pointer-events: none;
  background: rgba(233, 239, 230, 0.26);
}

.spout-plane-axis-x {
  left: 0;
  right: 0;
  top: 50%;
  height: 1px;
}

.spout-plane-axis-z {
  top: 0;
  bottom: 0;
  left: 50%;
  width: 1px;
}

.spout-plane-marker {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  border: 2px solid var(--accent);
  background: rgba(9, 16, 19, 0.62);
  box-shadow:
    0 0 0 2px rgba(9, 16, 19, 0.72),
    0 0 0 3px rgba(233, 239, 230, 0.48),
    0 4px 12px rgba(0, 0, 0, 0.32);
  pointer-events: none;
}

.spout-plane-marker::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: var(--accent);
}

.height-control {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  justify-items: center;
  align-items: center;
  gap: 12px;
  padding-left: clamp(10px, 3%, 14px);
  border-left: 1px solid rgba(177, 200, 194, 0.18);
  color: var(--muted);
  font-size: 0.70rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  height: var(--spout-plane-size);
  align-self: start;
  overflow: hidden;
}

.height-control span {
  max-width: 100%;
  overflow-wrap: anywhere;
  text-align: center;
}

.height-control input[type="range"] {
  width: 28px;
  height: 100%;
  min-height: 0;
  writing-mode: vertical-lr;
  direction: rtl;
  appearance: slider-vertical;
  -webkit-appearance: slider-vertical;
}

.height-control output {
  color: var(--ink);
  font-weight: 600;
  letter-spacing: 0;
}

@media (max-width: 760px) {
  .spout-control {
    grid-template-columns: minmax(112px, 1fr) 54px;
    column-gap: 16px;
  }

  .height-control {
    padding-left: 10px;
  }
}

button {
  flex: 1;
  min-width: 0;
  padding: 8px 6px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: rgba(9, 16, 19, 0.56);
  color: var(--ink);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color 160ms ease, background 160ms ease;
}

button:hover {
  border-color: rgba(217, 179, 106, 0.45);
  background: rgba(217, 179, 106, 0.08);
}

.stats {
  margin: 0;
  display: grid;
  gap: 8px;
}

.stats > div {
  display: flex;
  justify-content: space-between;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(195, 215, 219, 0.06);
  border: 1px solid var(--panel-border);
}

dt {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

dd {
  margin: 0;
  font-weight: 600;
}

.viewer {
  position: relative;
  overflow: hidden;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
}

body.sim-startup-failed .timeseries-drawer,
body.sim-startup-failed .cross-section-overlay,
body.sim-startup-failed .view-cube {
  display: none;
}

body.sim-startup-failed canvas {
  opacity: 0.12;
}

body.sim-startup-failed button,
body.sim-startup-failed input {
  cursor: not-allowed;
  opacity: 0.55;
}

.startup-error {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: clamp(18px, 4vw, 48px);
  background:
    linear-gradient(rgba(9, 16, 19, 0.84), rgba(9, 16, 19, 0.90)),
    radial-gradient(circle at 50% 20%, rgba(217, 179, 106, 0.13), transparent 42%);
}

.startup-error-card {
  width: min(560px, 100%);
  padding: clamp(20px, 3vw, 30px);
  border: 1px solid rgba(217, 179, 106, 0.28);
  border-radius: 8px;
  background: rgba(16, 28, 31, 0.92);
  box-shadow: 0 22px 52px rgba(0, 0, 0, 0.38);
}

.startup-error-eyebrow {
  margin: 0 0 12px;
  color: var(--accent);
  font-size: 0.74rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.startup-error-card h2 {
  margin: 0;
  font-size: clamp(1.45rem, 3vw, 2.1rem);
  line-height: 1.1;
}

.startup-error-card p {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.5;
}

.startup-error-card pre {
  max-height: 160px;
  margin: 16px 0 0;
  padding: 12px;
  overflow: auto;
  border-radius: 6px;
  border: 1px solid rgba(177, 200, 194, 0.16);
  background: rgba(9, 16, 19, 0.62);
  color: rgba(233, 239, 230, 0.78);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
  line-height: 1.45;
  white-space: pre-wrap;
}

.cross-section-overlay {
  position: absolute;
  top: 16px;
  right: 16px;
  width: clamp(150px, 24%, 280px);
  aspect-ratio: 1.38;
  pointer-events: none;
  border: 1px solid rgba(233, 239, 230, 0.68);
  background:
    linear-gradient(rgba(233, 239, 230, 0.12) 1px, transparent 1px),
    linear-gradient(90deg, rgba(233, 239, 230, 0.12) 1px, transparent 1px);
  background-size: 25% 25%;
  box-shadow:
    inset 0 0 0 1px rgba(9, 16, 19, 0.82),
    0 0 0 1px rgba(0, 0, 0, 0.52),
    0 10px 30px rgba(0, 0, 0, 0.22);
}

.cross-section-overlay span {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  color: rgba(233, 239, 230, 0.78);
  font-size: 0.68rem;
  letter-spacing: 0.10em;
  text-align: center;
  text-transform: uppercase;
  white-space: nowrap;
}

.view-cube {
  --cube-size: 56px;
  --cube-half: calc(var(--cube-size) / 2);
  position: absolute;
  right: 18px;
  bottom: 18px;
  width: 108px;
  height: 108px;
  display: grid;
  place-items: center;
  pointer-events: none;
  perspective: 420px;
  filter: drop-shadow(0 12px 24px rgba(0, 0, 0, 0.38));
}

.view-cube::before {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 8px;
  border: 1px solid rgba(233, 239, 230, 0.16);
  background:
    radial-gradient(circle at 50% 48%, rgba(233, 239, 230, 0.12), transparent 48%),
    rgba(9, 16, 19, 0.42);
}

.view-cube-stage {
  position: relative;
  width: var(--cube-size);
  height: var(--cube-size);
  transform-style: preserve-3d;
  transition: transform 80ms linear;
}

.view-cube-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  width: var(--cube-size);
  height: var(--cube-size);
  border: 1px solid rgba(233, 239, 230, 0.42);
  border-radius: 6px;
  color: rgba(255, 255, 255, 0.92);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0;
  line-height: 1;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.62);
  backface-visibility: hidden;
}

.view-cube-face-front,
.view-cube-face-back {
  background: linear-gradient(145deg, rgba(73, 128, 236, 0.82), rgba(28, 56, 122, 0.82));
}

.view-cube-face-right,
.view-cube-face-left {
  background: linear-gradient(145deg, rgba(226, 70, 62, 0.82), rgba(116, 33, 30, 0.82));
}

.view-cube-face-top,
.view-cube-face-bottom {
  background: linear-gradient(145deg, rgba(75, 185, 95, 0.82), rgba(34, 92, 46, 0.82));
}

.view-cube-face-back,
.view-cube-face-left,
.view-cube-face-bottom {
  opacity: 0.82;
}

.view-cube-face-front {
  transform: translateZ(var(--cube-half));
}

.view-cube-face-back {
  transform: rotateY(180deg) translateZ(var(--cube-half));
}

.view-cube-face-right {
  transform: rotateY(90deg) translateZ(var(--cube-half));
}

.view-cube-face-left {
  transform: rotateY(-90deg) translateZ(var(--cube-half));
}

.view-cube-face-top {
  transform: rotateX(90deg) translateZ(var(--cube-half));
}

.view-cube-face-bottom {
  transform: rotateX(-90deg) translateZ(var(--cube-half));
}

.debug-stats.hidden {
  display: none;
}

.toggle-debug {
  flex: none;
  padding: 6px 12px;
  font-size: 0.78rem;
  color: var(--muted);
  letter-spacing: 0.06em;
}

@media (max-width: 700px) {
  :root {
    --sidebar-width: 0px;
  }

  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100vh;
  }
  .sidebar {
    max-height: min(48vh, 360px);
    padding: 14px;
    gap: 10px;
    border-right: none;
    border-bottom: 1px solid var(--panel-border);
  }

  .timeseries-drawer {
    left: 0;
  }

  .timeseries-drawer-panel {
    max-height: min(44vh, 360px);
  }

  .timeseries-grid {
    grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  }
}
