/* ==========================================================================
   OpsSync — calc.css: savings calculator (detented knobs + readout screen)
   Without JS: native range inputs (amber) and the static readouts.
   .calc.is-live   → calc.js has drawn a knob over every range input.
   .calc.has-scope → the CH1/CH2 trace canvas is running.
   ========================================================================== */

.calc {
  --knob: 120px;
  --dial: calc(var(--knob) * 1.5);
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-areas:
    "knobs   screen"
    "actions screen";
  grid-template-rows: 1fr auto;
  gap: clamp(16px, 1.8vw, 24px) clamp(20px, 2.6vw, 36px);
  padding: clamp(16px, 2.2vw, 30px);
}

/* ---------- Knob panel ---------- */
.calc__knobs {
  grid-area: knobs;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, var(--dial)));
  gap: clamp(22px, 2.4vw, 34px) clamp(14px, 1.6vw, 22px);
  align-content: start;
  justify-content: center;
}

.knob-field { display: grid; gap: 8px; align-content: start; min-width: 0; }
.knob-field > label {
  font-family: var(--f-caps); font-weight: 600; font-size: 0.8rem; letter-spacing: 0.1em; text-transform: uppercase;
  line-height: 1.3; color: var(--ink-2);
  text-wrap: balance;
}
/* Fallback slider (no JS): the native control, themed; rows align on the slider, not the label. */
.calc:not(.is-live) .knob-field { align-content: end; }
.knob-field input[type="range"] { width: 100%; min-height: 44px; margin: 0; accent-color: var(--amber); }
.knob-field__value {
  font-family: var(--f-mono); font-weight: 700; font-size: 1.4rem; line-height: 1;
  color: var(--amber); font-variant-numeric: tabular-nums;
}

/* ---------- Live knob ---------- */
.calc.is-live .knob-field {
  grid-template-areas: "dial" "ctrl" "label";
  justify-items: center;
  gap: 6px;
}
.calc.is-live .knob-field > label { grid-area: label; max-width: 24ch; margin-top: 6px; text-align: center; }

.knob-field__dial {
  grid-area: dial;
  position: relative;
  width: var(--dial); height: var(--dial);
  margin-bottom: calc(var(--dial) * -0.05);
  /* Touch: the knob itself takes every drag (touch-action: none on .knob__body); the scale ring around it
     lets a vertical swipe scroll the page, so thumbing past the calculator on a phone never cranks a knob
     (calc.js undoes the detents a cancelled touch moved). Horizontal drags on the ring still turn it. */
  touch-action: pan-y;
  cursor: grab;
  user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.knob-field.is-dragging .knob-field__dial { cursor: grabbing; }

/* The native input stays focusable and in the accessibility tree, laid over the knob. */
.knob-field__dial > input[type="range"] {
  position: absolute; left: 50%; top: 50%;
  width: var(--knob); height: var(--knob); min-height: 0;
  margin: calc(var(--knob) / -2) 0 0 calc(var(--knob) / -2);
  padding: 0; border: 0; border-radius: 50%;
  opacity: 0;
  pointer-events: none;
  -webkit-appearance: none; appearance: none;
}

.knob { position: absolute; inset: 0; pointer-events: none; }
/* Mounting well cut into the panel. Proportional to the knob so the scale ring (calc.js: radii 78–91
   of a 100-unit dial = 0.585–0.68 knob) always clears it with the same dark gap, at every size. */
.knob::before {
  content: "";
  position: absolute; left: 50%; top: 50%;
  width: calc(var(--knob) * 1.12); height: calc(var(--knob) * 1.12);
  margin: calc(var(--knob) * -0.56) 0 0 calc(var(--knob) * -0.56);
  border-radius: 50%;
  background: radial-gradient(circle, #1B1C18 0 62%, #26271F 100%);
  box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.75), 0 1px 2px rgba(255, 255, 255, 0.06);
}

/* Scale: detent ticks around a 270° sweep */
.knob__scale { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.knob__tick { stroke: rgba(237, 235, 227, 0.24); stroke-width: 1.3; stroke-linecap: round; transition: stroke 140ms var(--ease-out); }
.knob__tick--major { stroke: rgba(237, 235, 227, 0.5); stroke-width: 2.1; }
.knob__tick.is-lit { stroke: rgba(241, 158, 40, 0.62); }
.knob__tick--major.is-lit { stroke: var(--amber); }
.knob__tick.is-now { stroke: var(--amber-hot); stroke-width: 2.6; }

.knob__num {
  position: absolute; top: 90%;
  transform: translate(-50%, -50%);
  font-family: var(--f-mono); font-size: 0.72rem; line-height: 1;
  color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap;
}
.knob__num--min { left: 17%; }
.knob__num--max { left: 83%; }

/* Machined knob: knurled skirt (rotates), spun cap, amber index, fixed lighting on top */
.knob__body {
  position: absolute; left: 50%; top: 50%;
  width: var(--knob); height: var(--knob);
  margin: calc(var(--knob) / -2) 0 0 calc(var(--knob) / -2);
  border-radius: 50%;
  box-shadow:
    0 2px 3px rgba(0, 0, 0, 0.55),
    0 12px 22px -6px rgba(0, 0, 0, 0.75);
  pointer-events: auto;                     /* the grab target: its events bubble to the dial */
  touch-action: none;
}
.knob__rot {
  position: absolute; inset: 0;
  border-radius: 50%;
  background:
    radial-gradient(circle, transparent 0 58%, rgba(0, 0, 0, 0.3) 100%),
    repeating-conic-gradient(from 0deg, #22231D 0deg 1.8deg, #6B6C62 2.5deg 3.3deg, #2B2C26 4deg 6deg);
  transition: transform 80ms var(--ease-snap);
  will-change: transform;
}
.knob__cap {
  position: absolute; inset: 12%;
  border-radius: 50%;
  background:
    repeating-radial-gradient(circle, rgba(255, 255, 255, 0.028) 0 1px, transparent 1px 3px),
    radial-gradient(circle at 50% 50%, #4C4D45 0%, #3D3E37 52%, #2C2D27 100%);
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.6),
    inset 0 1px 0 rgba(255, 255, 255, 0.14),
    0 2px 4px rgba(0, 0, 0, 0.45);
}
.knob__index {
  position: absolute; left: 50%; top: 15%;
  width: 3px; height: 28%;
  margin-left: -1.5px;
  border-radius: 2px;
  background: var(--amber);
  box-shadow: 0 0 6px var(--amber-glow), 0 0 1px var(--amber-hot);
  transition: box-shadow var(--dur-1) var(--ease-out), background-color var(--dur-1) var(--ease-out);
}
.knob__light {
  position: absolute; inset: 0;
  border-radius: 50%;
  pointer-events: none;
  background:
    radial-gradient(120% 120% at 30% 16%, rgba(255, 255, 255, 0.17) 0%, rgba(255, 255, 255, 0.04) 30%, transparent 55%),
    radial-gradient(90% 90% at 72% 92%, rgba(0, 0, 0, 0.38), transparent 60%);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.2), inset 0 -2px 4px rgba(0, 0, 0, 0.5);
}
.knob__light::after {                       /* spun-metal sheen: stays put while the cap turns */
  content: "";
  position: absolute; inset: 12%;
  border-radius: 50%;
  background: conic-gradient(from 200deg, transparent 0deg, rgba(255, 255, 255, 0.1) 32deg, transparent 72deg, transparent 180deg, rgba(255, 255, 255, 0.07) 212deg, transparent 252deg);
}

.knob-field__dial:hover .knob__index,
.knob-field.is-dragging .knob__index { background: var(--amber-hot); box-shadow: 0 0 10px var(--amber), 0 0 2px var(--amber-hot); }
.knob-field__dial:hover .knob::before { box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.75), 0 0 0 1px rgba(241, 158, 40, 0.28); }

/* Keyboard focus lands on the (transparent) input: ring the knob inside its dark well, clear of the ticks. */
.knob-field__dial > input:focus-visible + .knob .knob__body { outline: 2px solid var(--amber); outline-offset: 2px; }

/* − value + */
.knob-field__ctrl { grid-area: ctrl; display: flex; align-items: center; gap: 6px; max-width: 100%; }
.knob-step {
  flex: none;
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; padding: 0;
  border: 1px solid var(--edge-soft); border-radius: var(--r-key);
  background: linear-gradient(180deg, #3C3D36, #30312B);
  color: var(--ink-2);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 1px 2px rgba(0, 0, 0, 0.4);
  touch-action: manipulation;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--dur-1) var(--ease-out), border-color var(--dur-1) var(--ease-out), transform var(--dur-1) var(--ease-out);
}
.knob-step .icon { width: 18px; height: 18px; stroke-width: 2; }
.knob-step:hover { color: var(--amber); border-color: #5E5F55; }
.knob-step:active { transform: translateY(1px); box-shadow: inset 0 2px 3px rgba(0, 0, 0, 0.4); }
.knob-step[aria-disabled="true"] { color: #75766D; cursor: default; }
.knob-step[aria-disabled="true"]:hover { color: #75766D; border-color: var(--edge-soft); }
.knob-step[aria-disabled="true"]:active { transform: none; }

.calc.is-live .knob-field__value {
  flex: 1 1 auto;
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  min-width: 0; height: 44px; padding: 0 10px;
  border-radius: 7px;
  background: var(--glass);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.85), inset 0 2px 8px rgba(0, 0, 0, 0.6), 0 1px 2px rgba(255, 255, 255, 0.05);
  font-size: 22px;
}
.calc.is-live .knob-field__value .seg { font-size: 22px; }
.knob-field__value .calc__u { font-size: 0.76rem; }

/* Units beside seven-segment digits */
.calc__u {
  font-family: var(--f-caps); font-weight: 700; font-size: 0.82rem; letter-spacing: 0.06em; line-height: 1;
  color: var(--muted); white-space: nowrap;
}

/* ---------- Screen ---------- */
.calc__screen {
  grid-area: screen;
  display: flex; flex-direction: column;
  min-width: 0;
  padding: clamp(14px, 1.8vw, 24px);
}
.calc.has-scope .calc__screen { --grat-on: 0; }

.calc__scopebar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px;
  padding-bottom: 12px;
  font-family: var(--f-caps); font-weight: 600; font-size: 0.78rem; letter-spacing: 0.1em; text-transform: uppercase;
  line-height: 1.2; color: var(--ink-2);
}
.calc__ch { display: inline-flex; align-items: center; gap: 8px; white-space: nowrap; }
.calc__ch b {
  font-weight: 700; font-size: 0.72rem; letter-spacing: 0.08em;
  padding: 3px 5px 2px; border-radius: 3px; color: #1D1E19;
}
.calc__ch--1 b { background: var(--phos-white); }
.calc__ch--2 b { background: var(--amber); }
.calc__chv { font-family: var(--f-mono); font-weight: 400; letter-spacing: 0; text-transform: none; color: var(--ink); font-variant-numeric: tabular-nums; }
.calc__ch--2 .calc__chv { color: var(--amber-hot); }
.calc__dm { margin: 0 -0.2em; }            /* mono decimal mark: tighten its full-width cell */
.calc__range { margin-left: auto; font-family: var(--f-mono); font-weight: 400; letter-spacing: 0; text-transform: none; color: var(--muted); font-variant-numeric: tabular-nums; }
.calc__trig { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); white-space: nowrap; }
.calc__scopebar.is-locked .calc__trig { color: var(--amber); }
.calc__scopebar.is-locked .calc__trig .led { background: var(--amber); box-shadow: 0 0 0 2px rgba(241, 158, 40, 0.18), 0 0 10px var(--amber-glow); }

.calc__trace { display: none; width: 100%; height: clamp(150px, 13vw, 200px); border-radius: 4px; }
.calc.has-scope .calc__trace { display: block; }
/* The trace window takes whatever height the knob column leaves free. */
.calc__scope { position: relative; flex: 1 1 0; min-height: clamp(150px, 13vw, 190px); max-height: 330px; }
.calc__scope .calc__trace { position: absolute; inset: 0; width: 100%; height: 100%; }

/* Readouts: the weekly number large on the left, a measurement column on the right */
.calc__out {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
  grid-template-rows: repeat(3, auto);
  column-gap: clamp(16px, 2vw, 28px);
  margin-top: clamp(12px, 1.4vw, 18px);
  border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.calc__cell {
  grid-column: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  min-width: 0;
  padding: 13px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.calc__cell--main + .calc__cell { border-top: 0; }
.calc__cell--main {
  grid-column: 1; grid-row: 1 / span 3;
  flex-direction: column-reverse; align-items: flex-start; justify-content: center; gap: 12px;
  padding: clamp(14px, 1.6vw, 22px) clamp(14px, 1.8vw, 24px) clamp(14px, 1.6vw, 22px) 0;
  border-top: 0;
  border-right: 1px solid rgba(255, 255, 255, 0.07);
}
.calc__cell dt {
  min-width: 0; max-width: 24ch;
  font-family: var(--f-caps); font-weight: 600; font-size: 0.8rem; letter-spacing: 0.1em; text-transform: uppercase;
  line-height: 1.3; color: var(--muted);
}
.calc__cell--main dt { max-width: none; font-size: 0.9rem; color: var(--ink-2); }
.calc__cell dd {
  flex: none;
  display: flex; align-items: flex-end; justify-content: flex-end; gap: 8px;
  font-size: clamp(1.35rem, 1rem + 0.8vw, 1.9rem);
  line-height: 1;
}
.calc__cell--main dd {
  gap: 4px 14px; flex-wrap: wrap;           /* a unit that no longer fits drops below, never over the digits */
  max-width: 100%;
  font-size: clamp(3.4rem, 1.6rem + 4.4vw, 6.4rem);
}
.calc__cell dd > .seg { flex: none; }
.calc__cell--main .calc__u { font-size: 1rem; padding-bottom: 0.3em; color: var(--ink-2); }
.calc__cell dd .calc__u { padding-bottom: 0.15em; }
.calc__cell dd .calc__u--pre { padding-bottom: 0.15em; }
/* Thousands: a real gap between digit groups (a digit cell already leaves ~0.1em), so 5529600 reads 5 529 600. */
.calc .seg svg + svg { margin-left: 0.26em; }
.calc__cell--main .seg svg + svg { margin-left: 0.16em; }
.calc__cell--main .seg .s-on { filter: drop-shadow(0 0 5px rgba(241, 158, 40, 0.6)); }

.calc__note { margin-top: auto; padding-top: 14px; font-size: 0.86rem; line-height: 1.45; color: var(--muted); max-width: 70ch; }

/* ---------- Action ---------- */
.calc__actions { grid-area: actions; display: flex; align-items: flex-end; }
.calc__actions .key { width: 100%; white-space: normal; text-align: center; }

/* ---------- Responsive ---------- */
/* Side by side needs ~1200px: below that the screen column is too narrow for the readouts at their
   largest (1 920 h/sem beside its unit, US$ 5 529 600 beside its label), so the knobs go four across
   above the screen and the screen gets the full width. */
@media (max-width: 1199px) {
  .calc {
    --knob: 100px;
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "knobs" "screen" "actions";
    grid-template-rows: auto;
  }
  .calc__knobs { grid-template-columns: repeat(4, minmax(0, 1fr)); justify-content: stretch; }
  .calc__actions .key { width: auto; min-width: min(100%, 360px); }
}
@media (min-width: 1024px) and (max-width: 1199px) {
  .calc { --knob: 120px; }
}
@media (max-width: 759px) {
  .calc { --knob: 96px; padding: 10px; gap: 14px; }
  .calc__knobs { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 26px 8px; padding: 10px 0 4px; }
  .calc.is-live .knob-field__value { padding: 0 6px; }
  .calc__knobs .knob-field__ctrl { gap: 4px; }
  .calc__screen { padding: 14px; }
  /* Legend: each channel gets its own full row (its reading grows to "2400 h/sem" at the top of the
     knobs), then h/div and the lock state share the last row. */
  .calc__scopebar { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 9px 12px; }
  .calc__ch--1 { grid-area: 1 / 1 / 2 / 3; }
  .calc__ch--2 { grid-area: 2 / 1 / 3 / 3; }
  .calc__range { grid-area: 3 / 1; justify-self: start; margin-left: 0; }
  .calc__trig { grid-area: 3 / 2; justify-self: end; }
  .calc__out { grid-template-columns: minmax(0, 1fr); grid-template-rows: none; }
  .calc__cell { grid-column: 1; }
  .calc__cell--main { grid-row: auto; border-right: 0; padding-right: 0; }
  .calc__cell--main + .calc__cell { border-top: 1px solid rgba(255, 255, 255, 0.07); }
  .calc__actions .key { width: 100%; }
}
@media (max-width: 359px) {
  .calc { --knob: 84px; }
  .knob-field__value .calc__u--pre { display: none; }
}

/* Forced colors (Windows high contrast) drop every gradient, which would leave the knob invisible over
   its transparent input: draw it in system colours instead. */
@media (forced-colors: active) {
  .knob__body { border: 2px solid CanvasText; }
  .knob__index { forced-color-adjust: none; background: Highlight; box-shadow: none; }
  .knob__tick { stroke: GrayText; }
  .knob__tick.is-lit, .knob__tick.is-now { stroke: Highlight; }
  .knob-field__dial > input:focus-visible + .knob .knob__body { outline-color: Highlight; }
  .calc__ch b { forced-color-adjust: none; }
}

@media (prefers-reduced-motion: reduce) {
  .knob__rot, .knob__tick, .knob__index, .knob-step { transition: none; }
}
