/* Ladder logic playground. Scoped under .ll-wrap; uses Dusk Harbor tokens. */

.ll-wrap {
  --ll-panel: #071830;
  --ll-panel-2: #0d2444;
  --ll-dim: #3d5878;
  --ll-text: #dbe8f8;
  --ll-sub: #9fb6d2;
  --ll-live: #8fc7ff;
  --ll-glow: 0 0 6px rgba(143, 199, 255, 0.85), 0 0 14px rgba(143, 199, 255, 0.45);
  --ll-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --cw: 58px;
  --ow: 84px;
  --rh: 76px;
  --wire: 3px;
  width: min(72rem, calc(100% - 2rem));
  margin-inline: auto;
  padding: 1.4rem 0 2.4rem;
  min-width: 0;
  max-width: 100%;
}

.ll-wrap h1 { font-size: clamp(2.4rem, 7vw, 3.6rem); }
.ll-wrap h2 { margin-top: 2.2rem; font-size: clamp(1.6rem, 4vw, 2.15rem); }
.ll-wrap > p,
.ll-notes p,
.ll-notes li,
.ll-notes dd { color: var(--muted); }
.ll-wrap .lede { max-width: 46rem; }
.ll-wrap .sim-notice { max-width: 46rem; }

.ll-noscript {
  margin: 1rem 0;
  padding: 1rem 1.1rem;
  border-radius: 14px;
  border: 1.5px dashed var(--line);
  background: var(--card);
  color: var(--ink);
  font-weight: 600;
}

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

.ll-app {
  display: grid;
  gap: 1rem;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "toolbar" "controls" "inputs" "ladder" "outputs";
  margin-top: 1.2rem;
  min-width: 0;
}
.ll-toolbar { grid-area: toolbar; min-width: 0; }
.ll-controls { grid-area: controls; }
.ll-inputs { grid-area: inputs; }
.ll-ladder-wrap { grid-area: ladder; min-width: 0; }
.ll-outputs { grid-area: outputs; }

@media (min-width: 960px) {
  .ll-app {
    grid-template-columns: minmax(0, 1fr) 21rem;
    grid-template-areas: "toolbar toolbar" "controls controls" "ladder inputs" "ladder outputs";
    grid-template-rows: auto auto auto 1fr;
    align-items: start;
  }
  .ll-outputs { align-self: start; }
}

.ll-app h3 {
  margin: 0 0 0.6rem;
  font-family: var(--sans);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--blue);
}

.ll-field { display: grid; gap: 0.3rem; min-width: 0; }
.ll-field > span { font-size: 0.85rem; font-weight: 700; color: var(--blue-deep); }
.ll-field > span.ll-preset-label { font-weight: 500; color: var(--muted); overflow-wrap: anywhere; }
.ll-preset-kicker { font-weight: 700; color: var(--blue-deep); }
.ll-field select,
.ll-field input {
  min-height: 48px;
  width: 100%;
  max-width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background-color: var(--card);
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
}
.ll-field select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 2.85rem;
  background-image: url("select-chevron.svg");
  background-repeat: no-repeat;
  background-position: right 0.7rem center;
  background-size: 22px 22px;
  cursor: pointer;
}
.ll-preset { max-width: 26rem; }
.ll-blurb { margin: 0.6rem 0 0; color: var(--muted); max-width: 46rem; }
.ll-saved { margin: 0.4rem 0 0; color: var(--blue-deep); font-weight: 650; font-size: 0.95rem; }

/* ---------- Buttons ---------- */

.ll-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 48px;
  min-width: 48px;
  padding: 0.5rem 1rem;
  border: 1.5px solid var(--line);
  border-radius: 999px;
  background: var(--card);
  color: var(--blue-deep);
  font: inherit;
  font-weight: 650;
  cursor: pointer;
  touch-action: manipulation;
}
.ll-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.ll-btn[aria-pressed="true"] { background: var(--blue-deep); border-color: var(--blue-deep); color: #fff; }
.ll-btn-sm { min-height: 44px; padding: 0.35rem 0.85rem; font-size: 0.92rem; }
.ll-btn-warn { color: #7a1f1f; border-color: #b06a6a; }
.ll-btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }

.ll-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem 0.75rem;
}
.ll-run .ll-dot {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  background: #b06a6a;
}
.ll-run.on .ll-dot { background: #6fe3a0; }
.ll-scans { margin: 0; color: var(--muted); font-variant-numeric: tabular-nums; }
.ll-scans-n { font-weight: 700; color: var(--ink); font-family: var(--ll-mono); }

/* ---------- Inputs / outputs panels ---------- */

.ll-io {
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
  min-width: 0;
}

.ll-in-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
}

.ll-input {
  display: grid;
  align-content: center;
  gap: 0.1rem;
  min-height: 76px;
  padding: 0.6rem 0.8rem;
  border: 2px solid var(--line);
  border-radius: 16px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  min-width: 0;
}
.ll-input.on { background: var(--blue-deep); border-color: var(--blue-deep); color: #fff; }
.ll-momentary { border-bottom-width: 5px; }
.ll-momentary.on { transform: translateY(2px); border-bottom-width: 3px; }
.ll-sensor { cursor: default; background: var(--soft); border-style: dashed; }
.ll-sensor.on { background: #0c3d2a; border-color: #0c3d2a; color: #fff; }
.ll-in-tag { font-family: var(--ll-mono); font-size: 0.8rem; font-weight: 700; opacity: 0.85; }
.ll-in-name { font-size: 1.1rem; font-weight: 700; overflow-wrap: anywhere; }
.ll-in-state { font-size: 0.85rem; opacity: 0.85; }

.ll-lamps { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.45rem; }
.ll-lamp {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.7rem;
  min-height: 48px;
  padding: 0.35rem 0.75rem;
  border-radius: 14px;
  background: #fff;
  border: 1px solid var(--line);
}
.ll-lamp-text { color: var(--ink); font-weight: 600; overflow-wrap: anywhere; }
.ll-lamp-state { font-family: var(--ll-mono); font-weight: 700; color: var(--muted); }
.ll-lamp.on .ll-lamp-state { color: var(--ink); }
.ll-bulb {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  background: #8a9bb0;
  border: 2px solid #5c6f86;
}
.ll-lamp.on .ll-bulb { background: #3fa9ff; border-color: #0c49a8; box-shadow: 0 0 10px #3fa9ff; }
.ll-lamp.ll-green.on .ll-bulb { background: #3ddc84; border-color: #137a42; box-shadow: 0 0 12px #3ddc84; }
.ll-lamp.ll-amber.on .ll-bulb { background: #ffc233; border-color: #9a6a00; box-shadow: 0 0 12px #ffc233; }
.ll-lamp.ll-red.on .ll-bulb { background: #ff5a4e; border-color: #8f1c14; box-shadow: 0 0 12px #ff5a4e; }

.ll-readouts { list-style: none; margin: 0.9rem 0 0; padding: 0; display: grid; gap: 0.6rem; }
.ll-readout { padding: 0.6rem 0.75rem; border-radius: 14px; background: #fff; border: 1px solid var(--line); }
.ll-ro-head { margin: 0 0 0.4rem; font-weight: 700; color: var(--ink); }
.ll-ro-nums {
  display: flex;
  flex-wrap: wrap;
  gap: 0.2rem 0.9rem;
  margin: 0.4rem 0 0;
  font-family: var(--ll-mono);
  font-size: 0.85rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.ll-bit { font-weight: 700; }
.ll-bit.on { color: #0b6b39; }
.ll-bar {
  display: block;
  height: 10px;
  border-radius: 999px;
  background: var(--soft);
  position: relative;
  overflow: hidden;
}
.ll-bar::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--blue);
  transform-origin: left center;
  transform: scaleX(var(--p, 0));
}
.ll-readout.done .ll-bar::after { background: #1f9d5a; }

.ll-tank-row { display: flex; gap: 1rem; align-items: center; margin-top: 0.9rem; }
.ll-tank {
  position: relative;
  flex: none;
  width: 70px;
  height: 120px;
  border: 3px solid var(--blue-deep);
  border-top-width: 1px;
  border-radius: 0 0 12px 12px;
  background: #fff;
  overflow: hidden;
}
.ll-tank-fill {
  position: absolute;
  inset: 0;
  background: linear-gradient(#5fb2ff, #1d6fd1);
  transform-origin: center bottom;
  transform: scaleY(var(--p, 0));
}
.ll-tank-mark { position: absolute; left: 0; right: 0; height: 0; border-top: 2px dashed #7a1f1f; }
.ll-mark-hi { bottom: 80%; }
.ll-mark-lo { bottom: 20%; }
.ll-tank-text { margin: 0; color: var(--muted); }
.ll-tank-text strong { color: var(--ink); font-size: 1.3rem; font-family: var(--ll-mono); }

/* ---------- Ladder ---------- */

.ll-ladder-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  margin-bottom: 0.5rem;
}
.ll-ladder-head h3 { margin: 0; }
.ll-hint { margin: 0 0 0.6rem; color: var(--muted); font-size: 0.95rem; }

.ll-ladder {
  background: var(--ll-panel);
  border-radius: 18px;
  padding: 0.75rem 0.5rem;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  max-width: 100%;
  box-shadow: var(--shadow);
}
.ll-ladder:focus-visible { outline: 3px solid var(--blue-bright); outline-offset: 3px; }

.ll-rungs {
  list-style: none;
  margin: 0;
  padding: 0;
  width: max-content;
  min-width: 100%;
}

.ll-rung {
  position: relative;
  border-left: 4px solid var(--ll-live);
  border-right: 4px solid var(--ll-dim);
  padding-bottom: 0.35rem;
}
.ll-rung:first-child { padding-top: 0.2rem; }

.ll-rung-label {
  display: flex;
  justify-content: space-between;
  margin: 0;
  padding: 0.15rem 0.45rem 0;
  color: var(--ll-sub);
  font-family: var(--ll-mono);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
}
.ll-rung-state { color: var(--ll-sub); }
.ll-rung.live .ll-rung-state { color: var(--ll-live); font-weight: 700; }

.ll-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(var(--cw), 1fr)) minmax(var(--ow), 1.5fr);
  grid-auto-rows: var(--rh);
}
.ll-c1 { grid-column: 1; } .ll-c2 { grid-column: 2; } .ll-c3 { grid-column: 3; } .ll-c4 { grid-column: 4; } .ll-c5 { grid-column: 5; }
.ll-r1 { grid-row: 1; } .ll-r2 { grid-row: 2; } .ll-r3 { grid-row: 3; } .ll-r4 { grid-row: 4; }

.ll-slot { position: relative; min-width: 0; }

.ll-cell {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ll-text);
  font: inherit;
  text-align: center;
}
button.ll-cell { cursor: pointer; border-radius: 10px; }
button.ll-cell:focus-visible { outline: 3px solid var(--ll-live); outline-offset: -3px; }
.editing .ll-cell:not(.ll-cell-empty) { box-shadow: inset 0 0 0 1px rgba(159, 182, 210, 0.28); border-radius: 10px; }
.ll-cell.selected { box-shadow: inset 0 0 0 3px #ffc233 !important; }

/* Wires: left half and right half of each cell, so power can stop at an open contact */
.ll-w {
  position: absolute;
  top: calc(50% - var(--wire) / 2);
  height: var(--wire);
  width: calc(50% + 0.5px);
  background: var(--ll-dim);
}
.ll-wl { left: 0; }
.ll-wr { right: 0; }
.ll-w.on, .ll-v.on { background: var(--ll-live); box-shadow: var(--ll-glow); }

/* Vertical branch links reach up to the main line */
.ll-v {
  position: absolute;
  bottom: 50%;
  width: var(--wire);
  background: var(--ll-dim);
  border-radius: 2px;
  height: var(--rh);
}
.ll-v[data-up="2"] { height: calc(var(--rh) * 2); }
.ll-v[data-up="3"] { height: calc(var(--rh) * 3); }
.ll-vl { left: 0; }
.ll-vr { right: 0; }
.ll-v.on { z-index: 1; }

.ll-el {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  grid-template-rows: 1fr auto 1fr;
  justify-items: center;
  align-items: center;
  pointer-events: none;
}
.ll-tag {
  align-self: end;
  padding-bottom: 3px;
  font-family: var(--ll-mono);
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--ll-text);
  white-space: nowrap;
}
.ll-name {
  align-self: start;
  padding-top: 3px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.7rem;
  color: var(--ll-sub);
}

.ll-sym { display: block; position: relative; background: var(--ll-panel); }
.ll-contact {
  width: 16px;
  height: 26px;
  border-left: 3px solid var(--ll-sub);
  border-right: 3px solid var(--ll-sub);
}
.ll-nc::after {
  content: "";
  position: absolute;
  left: 50%;
  top: -3px;
  width: 3px;
  height: 32px;
  margin-left: -1.5px;
  background: var(--ll-sub);
  transform: rotate(35deg);
}
.ll-el.closed .ll-contact { border-color: var(--ll-live); }
.ll-el.closed .ll-nc::after { background: var(--ll-live); }

.ll-coil {
  min-width: 26px;
  height: 26px;
  padding: 0 3px;
  border-left: 3px solid var(--ll-sub);
  border-right: 3px solid var(--ll-sub);
  border-radius: 50%;
  display: grid;
  place-items: center;
}
.ll-coil-wide { min-width: 40px; border-radius: 40%; }
.ll-coil-l { font-family: var(--ll-mono); font-size: 0.68rem; font-weight: 700; color: var(--ll-text); line-height: 1; }
.ll-el.active .ll-coil { border-color: var(--ll-live); background: #123a66; box-shadow: var(--ll-glow); }

.ll-box {
  padding: 2px 6px;
  border: 2px solid var(--ll-sub);
  border-radius: 6px;
  font-family: var(--ll-mono);
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--ll-text);
  line-height: 1.3;
}
.ll-el.active .ll-box { border-color: var(--ll-live); box-shadow: var(--ll-glow); background: #123a66; }
.ll-cell-out .ll-name { font-family: var(--ll-mono); color: var(--ll-text); }

.ll-plus {
  position: absolute;
  inset: 8px 6px;
  display: grid;
  place-items: center;
  border: 1.5px dashed var(--ll-dim);
  border-radius: 10px;
  color: var(--ll-sub);
  font-size: 1.3rem;
}

.ll-rung-tools { width: 0; min-width: 100%; box-sizing: border-box; }
.ll-rung-tools,
.ll-edit-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  padding: 0.45rem 0.45rem 0.6rem;
}
.ll-rung-tools .ll-btn { background: var(--ll-panel-2); color: var(--ll-text); border-color: var(--ll-dim); }
.ll-rung-tools .ll-btn-warn { color: #ffb4ae; border-color: #8f4a45; }
.ll-edit-tools { padding: 0.75rem 0 0; }
.ll-empty { color: var(--ll-sub); margin: 0.5rem; }
.ll-note { min-height: 1.4em; margin: 0.5rem 0 0; color: var(--blue-deep); font-weight: 600; font-size: 0.95rem; }

/* ---------- Editor ---------- */

.ll-editor {
  margin-top: 0.8rem;
  padding: 1rem;
  border: 2px solid var(--blue);
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.ll-editor h3 {
  font-family: var(--serif);
  font-size: 1.3rem;
  letter-spacing: -0.02em;
  text-transform: none;
  color: var(--ink);
}
.ll-editor h3:focus { outline: none; }
.ll-editor h3:focus-visible { outline: 3px solid var(--blue-bright); outline-offset: 3px; }
.ll-type-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr));
  gap: 0.45rem;
  margin-bottom: 0.8rem;
}
.ll-choice { border-radius: 14px; flex-direction: column; gap: 0; padding: 0.4rem 0.6rem; line-height: 1.2; }
.ll-choice-short { font-family: var(--ll-mono); font-weight: 700; }
.ll-choice-long { font-size: 0.78rem; font-weight: 500; opacity: 0.9; }
.ll-fields { display: grid; gap: 0.7rem; grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr)); margin-bottom: 0.9rem; }

/* ---------- Notes / static explanation ---------- */

.ll-notes { max-width: 46rem; }
.ll-steps { padding-left: 1.3rem; }
.ll-steps li { margin-bottom: 0.45rem; }
.ll-steps strong { color: var(--ink); }
.ll-legend { margin: 0; display: grid; gap: 0.8rem; }
.ll-legend dt { display: flex; align-items: center; gap: 0.6rem; font-weight: 700; color: var(--ink); }
.ll-legend dd { margin: 0.15rem 0 0 2.6rem; }
.ll-key { flex: none; display: inline-block; width: 2rem; height: 1.4rem; position: relative; }
.ll-key::before { content: ""; position: absolute; left: 0; right: 0; top: calc(50% - 1px); height: 2px; background: var(--blue-deep); }
.ll-key::after { content: ""; position: absolute; top: 2px; bottom: 2px; left: calc(50% - 6px); width: 6px; background: var(--card); border-left: 2px solid var(--blue-deep); border-right: 2px solid var(--blue-deep); }
.ll-key-nc::after { background: linear-gradient(55deg, transparent 44%, var(--blue-deep) 44%, var(--blue-deep) 56%, transparent 56%), var(--card); }
.ll-key-coil::after { left: calc(50% - 7px); width: 10px; border-radius: 50%; }
.ll-key-box::after { left: calc(50% - 9px); width: 14px; border: 2px solid var(--blue-deep); border-radius: 3px; }
.ll-key-branch::after { left: 4px; right: 4px; width: auto; top: calc(50% - 1px); bottom: 1px; background: transparent; border: 2px solid var(--blue-deep); border-top: 0; }

/* ---------- Bigger screens ---------- */

@media (min-width: 640px) {
  .ll-wrap { --cw: 86px; --ow: 128px; --rh: 84px; }
  .ll-tag { font-size: 0.8rem; }
  .ll-name { font-size: 0.78rem; }
  .ll-in-grid { grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr)); }
}
@media (min-width: 960px) {
  .ll-in-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Small phones: let the dark ladder panel use the full width */
@media (max-width: 420px) {
  .ll-wrap { --cw: 55px; --ow: 80px; }
  .ll-ladder { margin-inline: -0.5rem; border-radius: 14px; padding-inline: 0.25rem; }
}

@media (prefers-reduced-motion: reduce) {
  .ll-wrap { --ll-glow: none; }
  .ll-momentary.on { transform: none; }
  .ll-lamp.on .ll-bulb { box-shadow: none !important; }
}

@media (hover: hover) {
  .ll-btn:hover:not(:disabled) { border-color: var(--blue); }
  button.ll-cell:hover { background: rgba(143, 199, 255, 0.08); }
}
