* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Inter, sans-serif;
  background: #0a0a0a;
  color: #eaeaea;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow: hidden;
}

/* --- Header --- */
header {
  padding: 10px 16px;
  border-bottom: 1px solid #1f1f1f;
  background: #0d0d0d;
  flex-shrink: 0;
}
.row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.spacer { flex: 1; }

.muted { color: #777; font-size: 13px; }
.muted.small { font-size: 12px; }
.muted kbd {
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  border-radius: 3px;
  padding: 1px 6px;
  font-family: ui-monospace, monospace;
  font-size: 11px;
}

label { color: #aaa; font-size: 13px; }

select, button, input[type="number"], input[type="range"] {
  background: #1a1a1a;
  color: #eee;
  border: 1px solid #2a2a2a;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}
select { min-width: 220px; }
input[type="range"] { padding: 0; width: 100%; }

button { cursor: pointer; }
button:hover:not(:disabled) { background: #242424; border-color: #3a3a3a; }
button:disabled { opacity: 0.4; cursor: not-allowed; }

button.primary {
  background: #2563eb;
  border-color: #2563eb;
  font-weight: 600;
  padding: 8px 22px;
}
button.primary:hover:not(:disabled) {
  background: #1d4ed8;
  border-color: #1d4ed8;
}

.icon-btn {
  background: transparent;
  border-color: #2a2a2a;
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.muted-btn { background: transparent; border-color: #333; color: #aaa; }

/* --- Main layout --- */
main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 16px;
  padding: 16px;
}

#play-area {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  min-height: 0;
}

/* --- Lights --- */
#lights-wrap {
  flex: 1;
  position: relative;
  display: flex;
  min-height: 180px;
  touch-action: manipulation;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#lights {
  flex: 1;
  background: #000;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 16px;
  border: 1px solid #1a1a1a;
  overflow: hidden;
}

.lights.layout-horizontal { flex-direction: row; }
.lights.layout-stack { flex-direction: column; }
.lights.layout-single { }

.light {
  /* width/height are set inline by layoutLights() so the lights always
     fit whatever space the container actually has.

     The LED-matrix look is built up in layers:
       1. `background-color` is the dark housing behind the LEDs.
       2. `background-image` is a fine radial-gradient dot pattern that
          repeats across the surface, simulating individual LEDs. The
          --led custom property colours every dot in one shot, and
          `background-size` is expressed as a percentage so the dot
          density scales with the light's pixel size.
       3. `box-shadow` provides the inset rim shadow (housing depth)
          and the outer bloom when lit. */
  --led: #1c1c1c;
  --led-hot: rgba(255, 255, 255, 0);
  border-radius: 50%;
  background-color: #050505;
  background-image:
    radial-gradient(circle, var(--led-hot) 10%, transparent 14%),
    radial-gradient(circle, var(--led) 36%, transparent 40%);
  background-size: 5% 5%, 5% 5%;
  background-position: center, center;
  border: 4px solid #0a0a0a;
  box-shadow:
    inset 0 6px 18px rgba(0, 0, 0, 0.85),
    inset 0 -2px 6px rgba(255, 255, 255, 0.03),
    0 4px 14px rgba(0, 0, 0, 0.6);
  flex-shrink: 0;
}

.light.red {
  --led: #ff5060;
  --led-hot: rgba(255, 220, 220, 0.85);
  background-color: #240008;
  box-shadow:
    inset 0 0 60px rgba(255, 80, 100, 0.7),
    0 0 20px rgba(255, 30, 50, 0.5);
}
.light.amber {
  --led: #ffc040;
  --led-hot: rgba(255, 240, 200, 0.85);
  background-color: #2a1500;
  box-shadow:
    inset 0 0 60px rgba(255, 190, 60, 0.7),
    0 0 20px rgba(255, 160, 30, 0.5);
}
.light.green {
  --led: #60ff80;
  --led-hot: rgba(220, 255, 230, 0.85);
  background-color: #002b0c;
  box-shadow:
    inset 0 0 60px rgba(70, 255, 120, 0.7),
    0 0 22px rgba(0, 255, 80, 0.5);
}
.light.blue {
  --led: #80c8ff;
  --led-hot: rgba(220, 240, 255, 0.85);
  background-color: #00203a;
  box-shadow:
    inset 0 0 60px rgba(120, 200, 255, 0.7),
    0 0 20px rgba(80, 170, 255, 0.5);
}
.light.white {
  --led: #ffffff;
  --led-hot: rgba(255, 255, 255, 1);
  background-color: #2a2a2a;
  box-shadow:
    inset 0 0 60px rgba(255, 255, 255, 0.6),
    0 0 20px rgba(255, 255, 255, 0.5);
}

/* --- Calibration overlay --- */
#calibration-overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 5, 5, 0.92);
  backdrop-filter: blur(4px);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
#calibration-overlay[data-state="recording"] { background: rgba(40, 12, 12, 0.94); }
#calibration-overlay[data-state="active"]    { background: rgba(10, 30, 50, 0.94); }
#calibration-overlay[data-state="error"]     { background: rgba(40, 10, 10, 0.94); }

.cal-card { text-align: center; max-width: 560px; padding: 24px; }
#cal-title { font-size: 28px; font-weight: 700; margin-bottom: 12px; }
#cal-sub { font-size: 15px; margin-bottom: 20px; min-height: 22px; }
.cal-actions { display: flex; gap: 10px; justify-content: center; }

/* --- Status --- */
#status {
  text-align: center;
  font-size: 16px;
  min-height: 22px;
  color: #bbb;
}

/* --- Stats panel (right sidebar) --- */
#stats-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  padding-right: 4px;
}
.stat {
  background: #111;
  border: 1px solid #1f1f1f;
  border-radius: 8px;
  padding: 8px 12px;
}
.stat-label {
  color: #888;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.stat-value {
  font-size: 24px;
  font-weight: 700;
  line-height: 1.15;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}
.stat-value.warn { color: #ff6b35; }
.stat-value.bad  { color: #ef4444; }
.stat-value.live { color: #ffaa30; }
#best-card .stat-value { color: #5af; }
.stat-sub { font-size: 10px; margin-top: 2px; }
#jumpstarts-card .stat-value.warn { color: #ff6b35; }
#jumpstarts-card .stat-value.bad { color: #ef4444; }
#jumpstarts-card.just-cleared {
  animation: clear-flash 0.6s ease;
}
@keyframes clear-flash {
  0%   { background: #5a1d1d; border-color: #ef4444; }
  100% { background: #111; border-color: #1f1f1f; }
}

.top-stat ol {
  margin: 4px 0 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.top-stat li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 4px 6px;
  background: #0d0d0d;
  border: 1px solid #1a1a1a;
  border-radius: 4px;
  font-variant-numeric: tabular-nums;
}
.top-stat li:first-child { border-color: #2a4a7a; background: #0e1626; }
.top-stat li.empty { justify-content: center; padding: 8px; }
.top-stat .ms { font-weight: 700; font-size: 15px; }
.top-stat .ctx { color: #888; font-size: 11px; }

/* --- Mobile: stats below play area --- */
@media (max-width: 800px) {
  main {
    grid-template-columns: 1fr;
    overflow-y: auto;
  }
  body { overflow: auto; }
  #stats-panel {
    flex-direction: row;
    flex-wrap: wrap;
    overflow: visible;
  }
  #stats-panel .stat { flex: 1 1 130px; }
  #stats-panel .top-stat { flex: 1 1 100%; }
  #about-link { flex: 1 1 100%; margin-top: 0; }
}

/* --- Drawer --- */
#drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 380px;
  max-width: 90vw;
  background: #0d0d0d;
  border-left: 1px solid #1f1f1f;
  transform: translateX(100%);
  transition: transform 0.22s ease;
  z-index: 100;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 24px rgba(0,0,0,0.5);
}
#drawer.open {
  transform: translateX(0);
}
#drawer-scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}
#drawer-scrim.visible {
  opacity: 1;
  pointer-events: auto;
}

.drawer-head {
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #1f1f1f;
}
.drawer-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
}
.drawer-section {
  margin-bottom: 22px;
}
.drawer-section h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #888;
  margin: 0 0 8px;
  font-weight: 500;
}
.drawer-section .btn-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.drawer-section .threshold-row {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.checkbox {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

/* --- First-open settings hint --- */
#settings-hint {
  background: #0e1a2e;
  border: 1px solid #2a4a7a;
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 18px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
#settings-hint:hover { background: #122339; border-color: #3a5a8a; }
#settings-hint h3 {
  font-size: 13px;
  margin: 0 0 8px;
  color: #5af;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 600;
}
#settings-hint ol {
  margin: 0 0 4px;
  padding: 0 0 0 20px;
  font-size: 13px;
  color: #ddd;
  line-height: 1.55;
}
#settings-hint ol li { margin-bottom: 4px; }
#settings-hint img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  border: 1px solid #1f1f1f;
  margin: 10px 0 6px;
}
#settings-hint .hint-footer { text-align: center; margin-top: 4px; }

/* --- Throttle gauge (in drawer) --- */
#gauge {
  background: #0e0e0e;
  border: 1px solid #1f1f1f;
  border-radius: 8px;
  padding: 8px 12px;
  margin: 8px 0;
}
.gauge-top {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 6px;
}
#gauge-track {
  position: relative;
  height: 18px;
  background: #1a1a1a;
  border-radius: 9px;
  overflow: hidden;
}
#gauge-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #2563eb, #5af);
  transition: width 0.05s linear;
}
#gauge-fill.above {
  background: linear-gradient(90deg, #00b840, #80ff90);
  box-shadow: inset 0 0 14px #80ff80aa;
}
#gauge-threshold {
  position: absolute;
  top: -2px;
  bottom: -2px;
  width: 2px;
  background: #fff;
  box-shadow: 0 0 4px #fff;
  pointer-events: none;
}
#gauge-value { font-family: ui-monospace, monospace; }

/* --- Debug picker (in drawer) --- */
#picker { margin-top: 10px; }
.picker-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}
.picker-head h4 {
  font-size: 11px;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0;
  font-weight: 500;
  flex: 1;
}
.live { display: flex; align-items: center; gap: 8px; }

#activity-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #333;
  display: inline-block;
  transition: background 0.1s;
}
#activity-dot[data-state="idle"]  { background: #444; }
#activity-dot[data-state="stale"] { background: #ef4444; box-shadow: 0 0 8px #ef444480; }
#activity-dot[data-state="live"]  { background: #22c55e; animation: pulse 0.8s ease-in-out infinite; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 #22c55e80; }
  50%      { box-shadow: 0 0 0 6px #22c55e00; }
}

.report-section {
  margin-top: 10px;
  padding: 8px;
  background: #0d0d0d;
  border: 1px solid #1a1a1a;
  border-radius: 8px;
}
.report-header { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.report-id { font-size: 12px; font-weight: 600; color: #ccc; font-family: ui-monospace, monospace; }
.report-count { font-size: 11px; }
.byte-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 6px;
}
.byte-cell {
  position: relative;
  text-align: left;
  padding: 5px 7px;
  font-family: ui-monospace, monospace;
  font-size: 11px;
  background: #111;
  border: 1px solid #232323;
  border-radius: 6px;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
}
.byte-cell.active {
  border-color: #5af;
  box-shadow: 0 0 0 1px #5af80, 0 0 14px #2563eb55;
  background: #0f1a2e;
}
.byte-cell .bi { color: #777; font-size: 10px; }
.byte-cell .bv { font-size: 14px; font-weight: 600; color: #eaeaea; }
.byte-cell .bar {
  height: 4px;
  background: #2a2a2a;
  border-radius: 2px;
  margin-top: 4px;
  overflow: hidden;
}
.byte-cell .bar .fill {
  height: 100%;
  background: #5af;
  width: 0;
  transition: width 0.05s linear;
}
.byte-cell .range { font-size: 10px; margin-top: 2px; font-family: ui-monospace, monospace; }

/* --- About link + modal --- */
#about-link {
  display: block;
  text-align: center;
  padding: 8px 0;
  margin-top: auto;
  text-decoration: none;
  color: #777;
  font-size: 12px;
  cursor: pointer;
  border-radius: 6px;
}
#about-link:hover { color: #ccc; background: #111; }

#share-top {
  width: 100%;
  margin-top: 8px;
  padding: 6px;
  font-size: 12px;
  background: #0e1626;
  border: 1px solid #2a4a7a;
  color: #5af;
}
#share-top:hover:not(:disabled) {
  background: #122339;
  border-color: #3a5a8a;
}

#about-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.about-card {
  background: #0d0d0d;
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  padding: 28px 30px;
  max-width: 560px;
  width: 100%;
  position: relative;
  box-shadow: 0 20px 60px rgba(0,0,0,0.7);
  max-height: calc(100vh - 40px);
  overflow-y: auto;
}
.about-card h2 {
  margin: 0 0 14px;
  font-size: 22px;
  font-weight: 700;
  padding-right: 32px;
}
.about-card h3 {
  margin: 18px 0 6px;
  font-size: 13px;
  color: #5af;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.about-card p {
  margin: 0 0 10px;
  color: #ccc;
  line-height: 1.55;
  font-size: 14px;
}
.about-card strong { color: #eee; }
.about-close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
}

/* --- Welcome modal (first visit) --- */
#welcome-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(6px);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.welcome-card {
  background: #0d0d0d;
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  padding: 26px 28px;
  max-width: 440px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.7);
}
.welcome-card h2 {
  margin: 0 0 8px;
  font-size: 22px;
  font-weight: 700;
}
.welcome-card > p {
  margin: 0 0 16px;
  color: #ccc;
  line-height: 1.5;
  font-size: 14px;
}
.welcome-card ul {
  margin: 0 0 22px;
  padding: 0;
  list-style: none;
}
.welcome-card li {
  padding: 10px 0;
  border-top: 1px solid #1f1f1f;
  color: #ddd;
  font-size: 14px;
  line-height: 1.45;
}
.welcome-card li:first-child { border-top: none; padding-top: 4px; }
.welcome-card li strong { color: #eee; font-weight: 600; }
.welcome-card kbd {
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  border-radius: 3px;
  padding: 1px 6px;
  font-family: ui-monospace, monospace;
  font-size: 12px;
}
.welcome-card .welcome-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
}
.welcome-card button { width: 100%; padding: 10px; font-size: 15px; }
.welcome-card button kbd {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.2);
  color: inherit;
}

/* --- Cookie / storage notice --- */
#cookie-banner {
  position: fixed;
  bottom: 16px;
  left: 16px;
  right: 16px;
  max-width: 640px;
  margin: 0 auto;
  background: #0e0e0e;
  border: 1px solid #2a2a2a;
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  gap: 12px;
  align-items: center;
  z-index: 200;
  box-shadow: 0 12px 32px rgba(0,0,0,0.6);
  transform: translateY(140%);
  transition: transform 0.32s ease;
  font-size: 13px;
  line-height: 1.45;
}
#cookie-banner.show { transform: translateY(0); }
#cookie-banner p { margin: 0; flex: 1; color: #ccc; }
#cookie-banner button { flex-shrink: 0; padding: 6px 16px; }
