:root {
  --bg: #1a1a1a;
  --panel: #232323;
  --ink: #f1f1f1;
  --muted: #9a9a9a;
  --accent: #f3b431;
  --rule: #444;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

/* Layout container — also the rotation target in portrait viewports.
   When the viewport is portrait, JS sizes #rotwrap to viewport-height ×
   viewport-width and rotates it 90° so the layout always presents in
   landscape (works through iOS rotation lock). */
#rotwrap {
  display: grid;
  grid-template-rows: auto auto auto 1fr;   /* scale, usage, header, body */
  width: 100%;
  height: 100%;
}

#usage {
  margin: 0;
  padding: 8px 14px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--muted);
  background: var(--bg);
}
#usage strong { color: var(--ink); }

header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  background: var(--panel);
  border-top: 1px solid #000;
  border-bottom: 1px solid var(--rule);
}
header h1 {
  font-size: 14px;
  margin: 0;
  flex: 1;
  letter-spacing: 0.02em;
}
header button {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--rule);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
}

/* ── Scale ───────────────────────────────────────────── */
/* Anchored to the right but inset by 70 px so the right end of the
   scale clears the rounded top-right corner on iPhone 16 Pro and
   similar phones. */
#scale-section {
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  padding: 10px 110px 6px 6px;
  overflow-x: auto;
  background: var(--bg);
}
#scale-host {
  /* SVG dimensions set inline by JS so the scale renders at exact
     physical size from the calibrated px/mm. */
  display: block;
}
#scale-host svg { display: block; }

/* ── Below-scale: settings | hfd ─────────────────────── */
#below-scale {
  display: grid;
  grid-template-columns: 1fr auto;
  overflow: hidden;
  min-height: 0;
}

form#rfform {
  padding: 10px 14px;
  display: grid;
  gap: 8px;
  overflow-y: auto;
  align-content: start;
}

/* Horizontal layout: label text on the left, input on the right.
   Toggles, helper rows, and small subtitles span the full row below. */
form label {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  column-gap: 10px;
  row-gap: 4px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}
form label > input[type="number"] {
  width: 78px;
  justify-self: end;
}
form label > input#coc { width: 180px; }   /* room for the 'auto = …' placeholder */
form label > input[type="text"] { justify-self: stretch; }
form label > .seg-toggle,
form label > .row,
form label > small {
  grid-column: 1 / -1;
  justify-self: stretch;
}
form label > small { white-space: normal; }
form label.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
}
form label.nowrap { white-space: nowrap; }

form input[type="text"],
form input[type="number"],
form select {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid #333;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 16px;          /* prevents iOS auto-zoom on focus */
  width: 100%;
}

/* Horizontal segmented toggle (replaces dropdowns) */
.seg-toggle {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.seg-opt {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid #333;
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 13px;
  flex: 0 0 auto;
  cursor: pointer;
}
.seg-opt.active {
  background: var(--accent);
  color: #111;
  border-color: var(--accent);
  font-weight: 600;
}
.seg-opt:active { transform: translateY(1px); }
form a { color: var(--accent); }
form small {
  font-size: 11px;
  color: var(--muted);
}
form .row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
form button {
  background: var(--accent);
  color: #111;
  border: 0;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
}

form fieldset {
  border: 1px solid #333;
  border-radius: 8px;
  padding: 6px 10px 10px;
  display: grid;
  gap: 8px;
}
form fieldset legend {
  font-size: 11px;
  color: var(--muted);
  padding: 0 4px;
}

.footer-note {
  font-size: 10px;
  color: var(--muted);
  text-align: center;
  margin: 8px 0 4px;
}

button.big-action {
  background: var(--accent);
  color: #111;
  border: 0;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  margin: 6px 0;
}

/* Print-only layout: hide the app, show the printable card SVG.
   Override the dark theme to plain white/black so we don't waste ink
   on the page background. */
#print-area { display: none; }
@media print {
  html, body {
    background: white !important;
    color: black !important;
  }
  body > #rotwrap { display: none !important; }
  body > #print-area {
    display: block !important;
    margin: 0;
    padding: 0;
    background: white;
  }
  #print-area svg {
    display: block;
    width: 89mm;
    height: 51mm;
  }
  @page { margin: 10mm; }
}

/* ── HFD / flash panel (right side) ──────────────────── */
#hfd-panel {
  border-left: 1px solid var(--rule);
  padding: 10px 12px;
  overflow-y: auto;
}
#hfd-panel:empty { display: none; }

/* Side-by-side columns when both sections are visible */
.side-panel-grid {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}
.side-panel-grid > div {
  min-width: 105px;
}

.hfd-title {
  margin-bottom: 6px;
  line-height: 1.3;
}
.hfd-title .ht-name {
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.hfd-title .ht-params {
  font-size: 11px;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.hfd-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  padding: 1px 0;
}
.hfd-row .stop { color: var(--muted); }
.hfd-row .dist { color: var(--ink); }

/* ── Calibration overlay ─────────────────────────────── */
.overlay {
  position: absolute;     /* relative to #rotwrap, so inherits rotation */
  inset: 0;
  background: rgba(0,0,0,0.92);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}
.overlay[hidden] { display: none; }
.cal-inner {
  width: auto;
  min-width: min(360px, 100%);
  max-width: calc(100% - 24px);
  background: var(--panel);
  border-radius: 10px;
  padding: 12px 16px;
  text-align: center;
  max-height: 100%;
  overflow-y: auto;
}
.cal-inner h2 { margin: 0 0 6px; font-size: 18px; }
.cal-inner p { margin: 6px 0; font-size: 13px; }
#cal-card-wrap {
  display: flex;
  justify-content: center;
  margin: 10px 0;
}
#cal-card {
  background: var(--accent);
  border-radius: 4px;
  height: 8px;
}
#cal-slider { width: 100%; }
.cal-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  font-size: 13px;
}
#cal-save {
  background: var(--accent);
  color: #111;
  border: 0;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 600;
}
.cal-note {
  font-size: 11px;
  color: var(--muted);
  margin-top: 8px;
}
