/* Poincaré Sphere Lab — standalone page styles */

* { box-sizing: border-box; }

/* the `hidden` attribute must win even over `display:flex` elements
   (.ml-row, .legend-ticks) that we toggle from JS */
[hidden] { display: none !important; }

:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --line: #dfe4ea;
  --ink: #1c2330;
  --ink-soft: #5b6677;
  --accent: #4f46e5;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --pad-x: 1.4rem;
  --maxw: 1500px;
}

html, body { height: 100%; }
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 ui-sans-serif, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  /* whole page is a viewport-height column so the bench never needs scrolling */
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ------------------------------- header -------------------------------- */

.top {
  flex: 0 0 auto;
  display: flex;
  align-items: baseline;
  gap: 1.2rem;
  padding: 0.6rem var(--pad-x) 0.35rem;
  max-width: var(--maxw);
  width: 100%;
  margin: 0 auto;
  flex-wrap: wrap;
}
.top h1 {
  margin: 0;
  font-family: Charter, Georgia, "Times New Roman", serif;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}
.top .sub { margin: 0.05rem 0 0; color: var(--ink-soft); font-size: 0.88rem; }

/* ------------------------------- layout -------------------------------- */

.layout {
  flex: 1 1 auto;
  min-height: 0;                /* allow the grid to shrink within the column */
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(300px, 380px);
  grid-template-rows: minmax(0, 1fr);   /* the single row fills the column height */
  gap: 0.9rem;
  padding: 0.35rem var(--pad-x);
  max-width: var(--maxw);
  width: 100%;
  margin: 0 auto;
  align-items: stretch;
}

/* ----------------------------- sphere pane ------------------------------ */

.sphere-pane {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  min-height: 320px;            /* fallback; normally stretches to fill the row */
  overflow: hidden;
}
#sphere {
  position: absolute;
  inset: 0;
  touch-action: none;
  cursor: grab;
}
#sphere.grabbing { cursor: grabbing; }
.sphere-hint {
  position: absolute;
  top: 0.6rem;
  right: 0.9rem;
  font-size: 0.78rem;
  color: #9aa4b2;
  pointer-events: none;
}

/* scan colormap legend — pinned to the bottom-right so it never covers the
   south pole / the lower hemisphere of the sphere */
.scan-legend {
  position: absolute;
  right: 0.85rem;
  bottom: 0.85rem;
  left: auto;
  width: min(290px, calc(100% - 1.7rem));
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.5rem 0.7rem 0.55rem;
  box-shadow: 0 6px 18px rgba(25, 35, 55, 0.12);
}
.legend-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 0.3rem;
}
#legendClose {
  border: none;
  background: none;
  font-size: 1.1rem;
  line-height: 1;
  color: var(--ink-soft);
  cursor: pointer;
  padding: 0 0.15rem;
}
#legendClose:hover { color: var(--ink); }
#legendBar {
  display: block;
  width: 100%;
  height: 12px;
  border-radius: 4px;
  border: 1px solid var(--line);
}
.legend-ticks {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--ink-soft);
  margin-top: 0.12rem;
}
.legend-status { font-size: 0.74rem; color: var(--ink-soft); margin-top: 0.28rem; }

#legendGraph { margin: 0.1rem 0 0; }
#legendPlot { display: block; width: 100%; height: auto; }

/* state-vector / rotation-axis key — bottom-left, mirrors the scan legend.
   It is purely informational, so it never intercepts sphere drags. */
.marker-legend {
  position: absolute;
  left: 0.85rem;
  bottom: 0.85rem;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.42rem 0.6rem;
  box-shadow: 0 6px 18px rgba(25, 35, 55, 0.1);
  font-size: 0.76rem;
  color: var(--ink-soft);
  pointer-events: none;
}
.ml-row { display: flex; align-items: center; gap: 0.45rem; }
.ml-row + .ml-row { margin-top: 0.22rem; }
.ml-swatch { width: 28px; height: 12px; flex: 0 0 auto; }

/* ------------------------------ side panels ----------------------------- */

.side {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;             /* scroll inside, not the page, on short screens */
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.7rem 0.9rem;
}
.panel h2 {
  margin: 0 0 0.5rem;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-soft);
}

select, input[type="number"] {
  font: inherit;
  color: var(--ink);
  background: #fbfcfe;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.32rem 0.45rem;
}
select:focus, input[type="number"]:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
#presetSel { width: 100%; }

.preset-desc {
  margin: 0.55rem 0 0;
  font-size: 0.84rem;
  color: #3d4757;
  background: #f6f7fb;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  padding: 0.45rem 0.65rem;
}

.readout { margin: 0; }
.readout dt {
  font-size: 0.74rem;
  color: var(--ink-soft);
  margin-top: 0.45rem;
}
.readout dt:first-child { margin-top: 0; }
.readout dd { margin: 0.08rem 0 0; }
.mono { font-family: var(--mono); font-size: 0.88rem; }
.mark-key { color: #303848; font-size: 0.82rem; }

.chip {
  display: inline-block;
  font: 600 0.72rem/1.4 ui-sans-serif, system-ui, sans-serif;
  background: #eef0fd;
  color: #3f3dc0;
  border-radius: 99px;
  padding: 0 0.5rem;
  margin-left: 0.3rem;
  vertical-align: 1px;
}
.chip.warn { background: #fdeeee; color: #b4232a; }

.ibar-wrap {
  height: 9px;
  border-radius: 99px;
  background: #e9edf2;
  overflow: hidden;
  margin: 0.22rem 0 0.18rem;
}
.ibar {
  height: 100%;
  width: 100%;
  border-radius: 99px;
  background: linear-gradient(90deg, #f59e0b, #fbbf24);
  transition: width 80ms linear;
}

.conventions summary {
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-soft);
}
.conventions ul { margin: 0.55rem 0 0.2rem; padding-left: 1.1rem; }
.conventions li { font-size: 0.83rem; color: #3d4757; margin-bottom: 0.4rem; }

/* -------------------------------- bench --------------------------------- */

.bench {
  flex: 0 0 auto;
  display: flex;
  align-items: stretch;
  gap: 0.9rem;
  max-width: var(--maxw);
  width: 100%;
  margin: 0 auto;
  padding: 0.25rem var(--pad-x) 0.3rem;
}

/* left: the squeezed optical axis */
.bench-axis {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.bench-head {
  display: flex;
  align-items: baseline;
  gap: 0.9rem;
  flex-wrap: wrap;
  margin-bottom: 0.25rem;
}
.bench-head h2 {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-soft);
}
.bench-hint { font-size: 0.76rem; color: #9aa4b2; }

.bench-row {
  flex: 1 1 auto;
  display: flex;
  align-items: stretch;
  gap: 0.7rem;
  background:
    linear-gradient(to bottom,
      transparent calc(50% - 1px),
      #c3ccd8 calc(50% - 1px),
      #c3ccd8 calc(50% + 1px),
      transparent calc(50% + 1px)),
    var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.55rem 0.7rem;
  overflow-x: auto;
  min-height: 118px;
}

.source-card, .detector-card {
  flex: 0 0 auto;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.42rem 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.22rem;
  justify-content: center;
  min-width: 132px;
}
.src-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-soft);
}
.src-label { font-size: 0.76rem; color: var(--ink-soft); }
.src-controls { display: flex; gap: 0.3rem; align-items: center; }
.src-controls select { max-width: 150px; }
#dice {
  font-size: 1rem;
  background: #fbfcfe;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.2rem 0.36rem;
  cursor: pointer;
}
#dice:hover { border-color: var(--accent); }
.beam-arrow { color: #97a1af; font-size: 1.05rem; text-align: right; }

.det-val { font-size: 1.15rem; font-weight: 700; }
.det-sub { font-size: 0.7rem; color: var(--ink-soft); }

.chain {
  display: flex;
  align-items: stretch;
  gap: 0.6rem;
  flex: 1 1 auto;
  min-height: 100px;
  padding: 0.1rem;
}
.chain.empty {
  align-items: center;
  justify-content: center;
  border: 2px dashed #ccd4de;
  border-radius: 10px;
}
.chain.empty::after {
  content: attr(data-empty-hint);
  color: #9aa4b2;
  font-size: 0.86rem;
  background: var(--card);
  padding: 0 0.6rem;
}

.comp-card {
  flex: 0 0 auto;
  width: 150px;
  background: var(--card);
  border: 1px solid var(--line);
  border-top: 3px solid var(--accent);
  border-radius: 10px;
  padding: 0 0.5rem 0.4rem;
  box-shadow: 0 2px 6px rgba(25, 35, 55, 0.06);
}
.comp-card.dragging { opacity: 0.45; }
.comp-card.scanning { box-shadow: 0 0 0 2px var(--accent); }

.comp-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.3rem 0 0.1rem;
  cursor: grab;
}
.comp-head:active { cursor: grabbing; }
.rm-btn {
  border: none;
  background: none;
  color: #9aa4b2;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.1rem;
}
.rm-btn:hover { color: #b4232a; }

.comp-icon {
  color: var(--accent);
  display: flex;
  justify-content: center;
  padding: 0 0 0.1rem;
}
.comp-icon svg { width: 34px; height: 34px; }
.comp-icon .axis-rot { transition: transform 120ms linear; }

.param-row {
  display: flex;
  align-items: center;
  gap: 0.28rem;
  margin-top: 0.2rem;
}
.param-row label { font-size: 0.82rem; width: 0.8rem; color: var(--ink-soft); }
.param-row input {
  width: 56px;
  padding: 0.2rem 0.28rem;
  font-family: var(--mono);
  font-size: 0.82rem;
}
.param-row input.scanning { color: var(--accent); font-weight: 700; }
.param-row .unit { color: var(--ink-soft); font-size: 0.82rem; }
.scan-btn {
  margin-left: auto;
  font: 600 0.72rem/1 ui-sans-serif, system-ui, sans-serif;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 7px;
  padding: 0.32rem 0.5rem;
  cursor: pointer;
}
.scan-btn:hover { filter: brightness(1.1); }
.scan-btn:disabled { opacity: 0.4; cursor: not-allowed; filter: none; }

/* per-component Hide/Show toggle, pinned to the bottom of the card */
.comp-foot { margin-top: 0.4rem; }
.hide-btn {
  display: block;
  width: 100%;
  font: 600 0.72rem/1 ui-sans-serif, system-ui, sans-serif;
  color: var(--ink-soft);
  background: #f1f4f8;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0.34rem 0.5rem;
  cursor: pointer;
}
.hide-btn:hover { border-color: var(--accent); color: var(--ink); }

/* a bypassed element: greyed out and dashed, but its Show button stays bright */
.comp-card.bypassed {
  border-top-color: #c3ccd8;
  border-top-style: dashed;
  background: #fbfcfd;
}
.comp-card.bypassed .comp-head,
.comp-card.bypassed .comp-icon,
.comp-card.bypassed .param-row { opacity: 0.5; }
.comp-card.bypassed .comp-icon { color: #9aa4b2; }
.comp-card.bypassed .hide-btn {
  color: var(--accent);
  border-color: var(--accent);
  background: #eef0fd;
}

.drop-caret {
  flex: 0 0 4px;
  border-radius: 99px;
  background: var(--accent);
  align-self: stretch;
}

/* right: the component palette (the "list" sits beside the axis) */
.palette-wrap {
  flex: 0 0 212px;          /* wide enough for "Quarter-wave plate" on one line */
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.palette-wrap h3 {
  margin: 0 0 0.28rem;
  font-size: 0.76rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-soft);
}
.palette {
  display: flex;
  flex-direction: column;
  gap: 0.34rem;
}
.pal-item {
  display: grid;
  grid-template-columns: 38px auto;
  grid-template-rows: auto auto;
  column-gap: 0.5rem;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 0.3rem 0.55rem 0.3rem 0.42rem;
  cursor: grab;
  user-select: none;
}
.pal-item:hover { border-color: var(--accent); }
.pal-item:active { cursor: grabbing; }
.pal-item .comp-icon { grid-row: 1 / 3; padding: 0; }
.pal-item .comp-icon svg { width: 32px; height: 32px; }
.pal-name { font-size: 0.82rem; font-weight: 600; }
.pal-hint { font-size: 0.68rem; color: #9aa4b2; }

/* -------------------------------- footer -------------------------------- */

.noscript { text-align: center; color: #b4232a; }

/* ------------------------------ responsive ------------------------------ */

/* On narrow screens fall back to a natural, scrollable stacked layout. */
@media (max-width: 1040px) {
  body { min-height: 0; }
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto;     /* natural flow when stacked */
    min-height: 0;
  }
  .side { overflow: visible; }
  .sphere-pane { min-height: min(82vw, 520px); }
  .marker-legend { display: none; }   /* avoid clashing with the scan legend when narrow */
  .bench { flex-direction: column; }
  .palette-wrap { flex: 0 0 auto; }
  .palette { flex-direction: row; flex-wrap: wrap; }
  .pal-item { flex: 1 1 180px; }
}
