:root {
  --bg: #04070f;
  --panel: rgba(13, 20, 40, 0.58);
  --panel-inner: rgba(255, 255, 255, 0.035);
  --border: rgba(150, 175, 255, 0.12);
  --border-strong: rgba(150, 175, 255, 0.22);
  --text: #e9eeff;
  --muted: #8f9bbd;
  --faint: #5d6888;
  --warm: #ffb45e;
  --warm-2: #ff7a8a;
  --cool: #b69cff;
  --cool-2: #6f8cff;
  --earth: #62c8ff;
  --earth-2: #3b82f6;
  --radius: 18px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

h1, h2, h3, p { margin: 0; }

#sky {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  height: 100vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) 232px;
  gap: 14px;
  padding: 16px 18px 18px;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 30%, rgba(255, 255, 255, 0.55), transparent 42%),
    radial-gradient(circle at 60% 65%, #1f8a5a 0 18%, transparent 19%),
    radial-gradient(circle at 30% 60%, #2e9a62 0 14%, transparent 15%),
    radial-gradient(circle at 50% 50%, #2f7fe0, #0b2a66 72%);
  box-shadow: 0 0 0 1px rgba(120, 190, 255, 0.35), 0 0 22px rgba(80, 160, 255, 0.45);
}
h1 { font-size: 20px; font-weight: 650; letter-spacing: -0.01em; }
.brand p { color: var(--muted); font-size: 13px; margin-top: 1px; }

.stats { display: flex; gap: 10px; }
.stat {
  min-width: 118px;
  padding: 8px 14px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(16px) saturate(140%);
}
.stat .label {
  display: block;
  font-size: 10.5px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.stat .value {
  display: block;
  font-size: 18px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.stat .value.warm { color: var(--warm); }
.stat .value.cool { color: var(--cool); }
.stat .value.up { color: #6ee7b7; }
.stat .value.down { color: #fda4af; }

/* ---------------------------------------------------------------- stage */

.stage {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 300px;
  gap: 14px;
  min-height: 0;
}

.panel {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  backdrop-filter: blur(18px) saturate(140%);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.births { --accent: var(--warm); --accent-2: var(--warm-2); }
.deaths { --accent: var(--cool); --accent-2: var(--cool-2); }

.births, .deaths {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  overflow-y: auto;
  scrollbar-width: none;
}
.births::-webkit-scrollbar, .deaths::-webkit-scrollbar { display: none; }
.births::before, .deaths::before {
  content: "";
  position: absolute;
  left: 18px;
  right: 18px;
  top: -1px;
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), var(--accent-2), transparent);
  opacity: 0.8;
}

.panel-head { display: flex; align-items: center; gap: 10px; }
.panel-head h2 { font-size: 17px; font-weight: 650; }
.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
}
.tag {
  margin-left: auto;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
}

.flow-value { display: flex; align-items: baseline; gap: 8px; }
.flow-value span {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  background: linear-gradient(120deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.flow-value small { color: var(--muted); font-size: 13px; }
.flow-sub { color: var(--muted); font-size: 12.5px; margin-top: -10px; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- controls */

.control { display: flex; flex-direction: column; gap: 8px; }
.control-row { display: flex; justify-content: space-between; align-items: baseline; }
.control label { font-size: 13px; font-weight: 600; }
.control output {
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--accent, var(--earth));
}
.hint { font-size: 11.5px; color: var(--faint); margin-top: -2px; }

input[type="range"] {
  --fill: 50%;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  margin: 6px 0;
  border-radius: 999px;
  background:
    linear-gradient(90deg, var(--accent-2, var(--earth-2)), var(--accent, var(--earth))) 0 / var(--fill) 100% no-repeat,
    rgba(255, 255, 255, 0.08);
  outline: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent, var(--earth));
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent, var(--earth)) 20%, transparent), 0 2px 8px rgba(0, 0, 0, 0.4);
  transition: transform 0.15s ease;
}
input[type="range"]:hover::-webkit-slider-thumb { transform: scale(1.1); }
input[type="range"]:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent, var(--earth)) 35%, transparent);
}

/* ---------------------------------------------------------------- loop cards */

.loop-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  border-radius: 14px;
  background: var(--panel-inner);
  border: 1px solid var(--border);
}
.loop-head { display: flex; align-items: center; gap: 12px; }
.loop-head h3 { font-size: 14px; font-weight: 650; }
.loop-head p { font-size: 12px; color: var(--muted); }
.loop-badge {
  flex: none;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-weight: 800;
  font-size: 16px;
  color: #0b1020;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 18px color-mix(in srgb, var(--accent) 45%, transparent);
}

.chain {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 3px;
  font-size: 11.5px;
}
.chain span {
  padding: 3px 6px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
}
.chain i {
  font-style: normal;
  font-weight: 700;
  font-size: 11px;
  color: var(--accent);
  display: inline-flex;
  align-items: center;
}
.chain i::after { content: "→"; margin-left: 1px; font-weight: 400; color: var(--muted); }
.chain i.neg { color: #fda4af; }

.explain { font-size: 12.5px; color: var(--muted); line-height: 1.55; }

.strength-row { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); margin-bottom: 6px; }
.strength-row span:last-child { color: var(--text); font-variant-numeric: tabular-nums; }
.bar { height: 6px; border-radius: 999px; background: rgba(255, 255, 255, 0.07); overflow: hidden; }
.bar-fill {
  height: 100%;
  width: 50%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  box-shadow: 0 0 10px var(--accent);
  transition: width 0.25s ease;
}

/* ---------------------------------------------------------------- scene */

.scene { position: relative; min-width: 0; min-height: 0; }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; }

.pill {
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
  background: rgba(13, 20, 40, 0.7);
  border: 1px solid var(--border-strong);
  backdrop-filter: blur(12px);
  --pill: #6ee7b7;
}
.pill.down { --pill: #fda4af; }
.pill.flat { --pill: var(--earth); }
.pill.over { --pill: #fbbf24; }
.pill-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--pill);
  box-shadow: 0 0 10px var(--pill);
  animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.35; } }

.stock {
  position: absolute;
  left: 50%;
  bottom: 4px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  text-align: center;
}
.eyebrow {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--earth);
}
.count {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 24px rgba(98, 200, 255, 0.35);
  white-space: nowrap;
}
.compact { font-size: 12.5px; color: var(--muted); }

/* ---------------------------------------------------------------- dock */

.dock {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 14px;
  min-height: 0;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
}
.buttons { display: grid; grid-template-columns: 1.4fr 1fr; gap: 8px; }

button { font: inherit; color: inherit; cursor: pointer; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 38px;
  border-radius: 11px;
  border: 1px solid var(--border-strong);
  background: rgba(255, 255, 255, 0.05);
  font-weight: 600;
  transition: background 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}
.btn:hover { background: rgba(255, 255, 255, 0.1); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .chip:focus-visible { outline: 2px solid var(--earth); outline-offset: 2px; }
.btn svg { width: 16px; height: 16px; fill: currentColor; }
.btn.primary {
  border-color: transparent;
  color: #04101f;
  background: linear-gradient(135deg, #8fe3ff, var(--earth) 45%, #4f8dff);
  box-shadow: 0 6px 20px rgba(80, 170, 255, 0.35);
}
.btn.primary:hover { box-shadow: 0 8px 26px rgba(80, 170, 255, 0.5); background: linear-gradient(135deg, #a8eaff, #7bd2ff 45%, #6a9fff); }
.btn .icon-pause { display: block; }
.btn .icon-play { display: none; }
.btn.paused .icon-pause { display: none; }
.btn.paused .icon-play { display: block; }

.presets { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  transition: all 0.15s ease;
}
.chip:hover { color: var(--text); border-color: var(--border-strong); background: rgba(255, 255, 255, 0.08); }
.chip.active { color: var(--text); border-color: rgba(98, 200, 255, 0.5); background: rgba(98, 200, 255, 0.14); }

.chart-card {
  display: flex;
  flex-direction: column;
  padding: 14px 16px 10px;
  min-width: 0;
}
.chart-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.chart-head h2 { font-size: 14px; font-weight: 650; }
.legend { display: flex; gap: 16px; margin: 0; padding: 0; list-style: none; font-size: 12px; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.swatch { width: 14px; height: 3px; border-radius: 2px; }
.swatch.s-pop { background: var(--earth); box-shadow: 0 0 6px var(--earth); }
.swatch.s-births { background: var(--warm); }
.swatch.s-deaths { background: var(--cool); }
.chart-body { position: relative; flex: 1; min-height: 0; margin-top: 6px; }
#chart { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; }

/* ---------------------------------------------------------------- web: smaller screens */

@media (max-width: 1100px) {
  html, body { height: auto; overflow: auto; }
  .app {
    height: auto;
    min-height: 100vh;
    grid-template-rows: auto;
    padding: 14px 16px 18px;
  }
  .topbar { flex-direction: column; align-items: stretch; gap: 12px; }
  .stats { flex-wrap: wrap; }
  .stat { flex: 1 1 110px; min-width: 0; }

  .app, .topbar, .stats, .panel { min-width: 0; }
  .stage { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .scene { grid-column: 1 / -1; grid-row: 1; height: 420px; }
  .births, .deaths { overflow: visible; }

  .dock { grid-template-columns: minmax(0, 1fr); }
  .chart-card { height: 280px; }
}

@media (max-width: 640px) {
  .stage { grid-template-columns: minmax(0, 1fr); }
  .scene { height: 340px; }
  .count { font-size: 26px; }
  .chart-head { flex-direction: column; align-items: flex-start; gap: 6px; }
  .legend { flex-wrap: wrap; gap: 10px; }
}
