/* WS-062 — user display settings: theme, text scale, typeface.
 *
 * The owner has presbyopia. A single fixed size cannot serve him and
 * every other pilot at once: the §4 floor was 18px, he ran the interface
 * at that size, found labels breaking words and pushing panels past the
 * right edge, and lowered his own rule to 16px on 2026-08-05. That was a
 * trade forced by having one number. This layer removes the need for one
 * number - each user picks, and the choice follows him everywhere.
 *
 * The selected value is a real effective text size, not document zoom.
 * Zooming the whole root to reach 32px also halves the effective layout
 * viewport, which made controls disappear and created page-wide horizontal
 * overflow. The late sim-pit typography layer already routes text (including
 * SVG text) through canonical variables, so this file changes those values
 * while leaving the physical viewport and 44px targets intact.
 *
 * Additive by design: this file defines its own tokens and never edits an
 * existing stylesheet, so it cannot collide with the readability work in
 * flight on another branch.
 */

:root {
  --cpms-effective-text-size: 16px;
  --cpms-user-font: "system";

  --cpms-font-system: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --cpms-font-humanist: Optima, Candara, "Gill Sans", "Trebuchet MS", sans-serif;
  --cpms-font-serif: "Iowan Old Style", Georgia, "Times New Roman", serif;
  --cpms-font-mono: ui-monospace, "SF Mono", "IBM Plex Mono", Menlo, monospace;
}

/* CSP-safe mapping for every supported effective text size. JavaScript only
   changes data attributes; it never writes a style attribute. */
html[data-cpms-effective-text-px="13"] { --cpms-effective-text-size: 13px; }
html[data-cpms-effective-text-px="14"] { --cpms-effective-text-size: 14px; }
html[data-cpms-effective-text-px="15"] { --cpms-effective-text-size: 15px; }
html[data-cpms-effective-text-px="16"] { --cpms-effective-text-size: 16px; }
html[data-cpms-effective-text-px="17"] { --cpms-effective-text-size: 17px; }
html[data-cpms-effective-text-px="18"] { --cpms-effective-text-size: 18px; }
html[data-cpms-effective-text-px="19"] { --cpms-effective-text-size: 19px; }
html[data-cpms-effective-text-px="20"] { --cpms-effective-text-size: 20px; }
html[data-cpms-effective-text-px="21"] { --cpms-effective-text-size: 21px; }
html[data-cpms-effective-text-px="22"] { --cpms-effective-text-size: 22px; }
html[data-cpms-effective-text-px="23"] { --cpms-effective-text-size: 23px; }
html[data-cpms-effective-text-px="24"] { --cpms-effective-text-size: 24px; }
html[data-cpms-effective-text-px="25"] { --cpms-effective-text-size: 25px; }
html[data-cpms-effective-text-px="26"] { --cpms-effective-text-size: 26px; }
html[data-cpms-effective-text-px="27"] { --cpms-effective-text-size: 27px; }
html[data-cpms-effective-text-px="28"] { --cpms-effective-text-size: 28px; }
html[data-cpms-effective-text-px="29"] { --cpms-effective-text-size: 29px; }
html[data-cpms-effective-text-px="30"] { --cpms-effective-text-size: 30px; }
html[data-cpms-effective-text-px="31"] { --cpms-effective-text-size: 31px; }
html[data-cpms-effective-text-px="32"] { --cpms-effective-text-size: 32px; }

html[data-cpms-effective-text-px] {
  --cpms-text-body: var(--cpms-effective-text-size);
  --cpms-text-heading: calc(var(--cpms-effective-text-size) * 1.5);
  --cpms-text-display: calc(var(--cpms-effective-text-size) * 2);
  --cpms-font-base: var(--cpms-effective-text-size);
  --cpms-font-md: calc(var(--cpms-effective-text-size) * 1.375);
  --cpms-font-lg: calc(var(--cpms-effective-text-size) * 1.75);
  --cpms-font-xl: calc(var(--cpms-effective-text-size) * 2.25);
}

/* ---- themes ---------------------------------------------------------
   `auto` writes no attribute and follows the operating system. An
   explicit choice stamps data-cpms-theme and wins in both directions. */

:root,
:root[data-cpms-theme="dark"] {
  --cpms-d-bg: #070A10;
  --cpms-d-surface: rgba(10, 16, 22, 0.92);
  --cpms-d-raised: rgba(20, 32, 48, 0.94);
  --cpms-d-edge: rgba(120, 160, 200, 0.24);
  --cpms-d-ink: #F5F2EC;
  --cpms-d-ink-dim: #A9BACE;
  --cpms-d-accent: #4FC3F7;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-cpms-theme="dark"]) {
    --cpms-d-bg: #EEF2F6;
    --cpms-d-surface: rgba(255, 255, 255, 0.94);
    --cpms-d-raised: #FFFFFF;
    --cpms-d-edge: rgba(20, 44, 70, 0.22);
    --cpms-d-ink: #10202F;
    --cpms-d-ink-dim: #3D5670;
    --cpms-d-accent: #0B6FA4;
    color-scheme: light;
  }
}

:root[data-cpms-theme="day"] {
  --cpms-d-bg: #EEF2F6;
  --cpms-d-surface: rgba(255, 255, 255, 0.94);
  --cpms-d-raised: #FFFFFF;
  --cpms-d-edge: rgba(20, 44, 70, 0.22);
  --cpms-d-ink: #10202F;
  --cpms-d-ink-dim: #3D5670;
  --cpms-d-accent: #0B6FA4;
  color-scheme: light;
}

/* ---- text size and typeface ---------------------------------------- */


html[data-cpms-font="humanist"] body { font-family: var(--cpms-font-humanist) !important; }
html[data-cpms-font="serif"]    body { font-family: var(--cpms-font-serif) !important; }
html[data-cpms-font="mono"]     body { font-family: var(--cpms-font-mono) !important; }
html[data-cpms-font="system"]   body { font-family: var(--cpms-font-system) !important; }

/* Diagrams: SVG text does not inherit a family through `font` shorthand
   in every engine, so it is named explicitly. */
html[data-cpms-font] svg text,
html[data-cpms-font] svg tspan { font-family: inherit; }

/* ---- the settings surface ------------------------------------------ */

.cpms-display-launcher {
  position: fixed; top: 18px; right: 18px; bottom: auto; z-index: 900;
  min-height: 44px; min-width: 44px;
  display: inline-flex; align-items: center; gap: 10px;
  padding: 0 18px; border-radius: 2px; cursor: pointer;
  font: 800 16px/1 var(--cpms-font-system); letter-spacing: .035em;
  color: var(--cpms-d-ink);
  background:
    linear-gradient(180deg, rgba(24, 52, 69, .98), rgba(4, 18, 29, .99)),
    #06131f;
  border: 1px solid rgba(117, 216, 244, .54);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .08),
    0 8px 22px rgba(0, 0, 0, .46);
}
.cpms-display-launcher:hover { border-color: #ffe298; background: linear-gradient(180deg, #244b62, #071b29); }
.cpms-display-launcher:focus-visible { outline: 3px solid var(--cpms-d-accent); outline-offset: 3px; }

.cpms-display-launcher__icon {
  color: var(--cpms-d-accent);
  font-size: 1.18em;
}

.cpms-display-launcher--header {
  position: static;
  z-index: auto;
  flex: 0 0 auto;
}

@media (max-width: 1600px) {
  .cpms-display-launcher--header {
    position: static;
  }
}

.cpms-display-panel {
  position: fixed; inset: 0; z-index: 901;
  display: none; align-items: center; justify-content: center;
  padding: 24px; background: rgba(4, 8, 14, 0.72);
}
.cpms-display-panel[open] { display: flex; }

.cpms-display-card {
  box-sizing: border-box;
  width: min(560px, 100%); max-width: 100%;
  max-height: calc(100dvh - 48px); min-width: 0; overflow: hidden;
  background: var(--cpms-d-bg); color: var(--cpms-d-ink);
  border: 1px solid var(--cpms-d-edge); border-radius: 12px;
  font-family: var(--cpms-font-system);
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto;
}
.cpms-display-card > *,
.cpms-display-body,
.cpms-field,
.cpms-per-screen-settings,
.cpms-surface-list { min-width: 0; }
.cpms-display-header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 20px 28px 16px;
  border-bottom: 1px solid var(--cpms-d-edge);
  background: var(--cpms-d-bg);
}
.cpms-display-card h2 { margin: 0; font-size: 26px; line-height: 1.25; font-weight: 650; }
.cpms-display-card p  { margin: 0; font-size: 17px; color: var(--cpms-d-ink-dim); }
.cpms-display-header-close {
  display: grid; flex: 0 0 44px; width: 44px; min-height: 44px; place-items: center;
  border: 1px solid var(--cpms-d-edge); border-radius: 8px;
  color: var(--cpms-d-ink); background: var(--cpms-d-raised);
  font: 600 28px/1 var(--cpms-font-system); cursor: pointer;
}
.cpms-display-header-close:focus-visible {
  outline: 3px solid var(--cpms-d-accent); outline-offset: 2px;
}
.cpms-display-body {
  display: flex; min-height: 0; flex-direction: column; gap: 26px;
  overflow-y: auto; overscroll-behavior: contain;
  padding: 20px 28px 24px;
}

.cpms-field { display: flex; flex-direction: column; gap: 12px; }
.cpms-field > label {
  font-size: 17px; letter-spacing: .07em; text-transform: uppercase;
  color: var(--cpms-d-ink-dim); font-weight: 650;
}
.cpms-seg { display: flex; gap: 10px; flex-wrap: wrap; }
.cpms-seg button {
  flex: 1 1 auto; min-height: 44px; min-width: 88px; cursor: pointer;
  padding: 0 16px; border-radius: 8px; font: 600 17px/1 var(--cpms-font-system);
  color: var(--cpms-d-ink); background: var(--cpms-d-raised);
  border: 1px solid var(--cpms-d-edge);
}
.cpms-seg button[aria-pressed="true"] {
  border-color: var(--cpms-d-accent);
  box-shadow: inset 0 0 0 1px var(--cpms-d-accent);
}
.cpms-seg button:focus-visible { outline: 3px solid var(--cpms-d-accent); outline-offset: 2px; }

.cpms-field input[type="range"] {
  box-sizing: border-box; width: 100%; max-width: 100%; min-width: 0;
  height: 44px; margin: 0; accent-color: var(--cpms-d-accent);
}
.cpms-range-control {
  box-sizing: border-box;
  width: 100%;
  min-width: 0;
  overflow-x: clip;
  padding-inline: 2px;
}
.cpms-readout { font: 650 20px/1 var(--cpms-font-mono); color: var(--cpms-d-accent); }

.cpms-preview {
  border: 1px dashed var(--cpms-d-edge); border-radius: 8px; padding: 18px;
  display: flex; flex-direction: column; gap: 10px; background: var(--cpms-d-surface);
}
.cpms-preview strong { font-size: 17px; }
.cpms-preview span   { font-size: 17px; color: var(--cpms-d-ink-dim); }
.cpms-preview svg    { width: 100%; height: 54px; }

.cpms-display-actions {
  display: flex; gap: 12px; justify-content: flex-end;
  padding: 16px 28px 20px;
  border-top: 1px solid var(--cpms-d-edge);
  background: var(--cpms-d-bg);
}
.cpms-per-screen-settings { display: flex; flex-direction: column; gap: 12px; }
.cpms-display-action {
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--cpms-d-edge);
  border-radius: 8px;
  color: var(--cpms-d-ink);
  background: var(--cpms-d-raised);
  font: 600 17px/1 var(--cpms-font-system);
  cursor: pointer;
}
.cpms-display-action--primary { border-color: var(--cpms-d-accent); }
.cpms-display-action:focus-visible { outline: 3px solid var(--cpms-d-accent); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) { .cpms-display-panel { transition: none; } }

/* ---- per-screen override list --------------------------------------
   Each row shows what that screen is doing right now: "follows" means it
   tracks the global slider, "tuned" means the pilot set it apart. The
   distinction is carried by a word and a border, never colour alone. */

.cpms-surface-list { display: flex; flex-direction: column; gap: 6px; }

.cpms-surface-row {
  display: grid; grid-template-columns: 1fr 130px 58px 84px;
  align-items: center; gap: 12px;
  padding: 8px 10px; border-radius: 8px;
  border: 1px solid transparent;
}
.cpms-surface-row[data-mode="tuned"] {
  border-color: var(--cpms-d-accent);
  background: var(--cpms-d-raised);
}
.cpms-surface-row[data-current] { box-shadow: inset 3px 0 0 var(--cpms-d-accent); }

.cpms-surface-name { font-size: 17px; }
.cpms-surface-row[data-mode="tuned"] .cpms-surface-name::after {
  content: " ·"; color: var(--cpms-d-accent);
}
.cpms-surface-val {
  font: 650 17px/1 var(--cpms-font-mono); text-align: right;
  color: var(--cpms-d-ink-dim); font-variant-numeric: tabular-nums;
}
.cpms-surface-row[data-mode="tuned"] .cpms-surface-val { color: var(--cpms-d-accent); }
.cpms-surface-row input[type="range"] {
  box-sizing: border-box; width: 100%; max-width: 100%; min-width: 0;
  height: 44px; margin: 0; accent-color: var(--cpms-d-accent);
}

.cpms-surface-clear {
  min-height: 44px; min-width: 76px; cursor: pointer; border-radius: 8px;
  font: 600 16px/1 var(--cpms-font-system);
  color: var(--cpms-d-ink); background: transparent;
  border: 1px solid var(--cpms-d-edge);
}
.cpms-surface-clear:disabled { opacity: .35; cursor: default; }
.cpms-surface-clear:focus-visible { outline: 3px solid var(--cpms-d-accent); outline-offset: 2px; }

@media (max-width: 560px) {
  .cpms-surface-row { grid-template-columns: 1fr 58px 84px; }
  .cpms-surface-row input[type="range"] { grid-column: 1 / -1; }
}

/* The Owner acceptance viewport is 1280×592 CSS px. Use the available width
   there so all global controls appear immediately, while the long per-screen
   list remains the only scrollable continuation. The close control stays in
   the sticky dialog heading at every scroll position. */
@media (min-width: 700px) and (max-height: 680px) {
  .cpms-display-panel {
    align-items: center;
    padding: 12px;
  }

  .cpms-display-card {
    width: min(860px, 100%);
    max-height: calc(100dvh - 24px);
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px 18px;
    padding: 18px;
  }

  .cpms-display-header,
  .cpms-field:has(.cpms-per-screen-settings),
  .cpms-display-actions {
    grid-column: 1 / -1;
  }

  .cpms-display-header {
    padding: 0 0 10px;
  }

  .cpms-field {
    gap: 7px;
  }

  .cpms-preview {
    padding: 10px;
    gap: 5px;
  }

  .cpms-preview svg {
    height: 38px;
  }
}

/* HELP is a separate fixed launcher with a deliberately high z-index. While
   Display Settings is modal it must leave both the visual and keyboard layer,
   otherwise it sits above the card and overlaps its controls. */
body:has(.cpms-display-panel[open]) .cpms-help-launcher {
  visibility: hidden;
  pointer-events: none;
}

/* Large-text mode is a layout mode, not a zoom level. Fixed cockpit shells
   become ordinary scrollable documents and dense multi-column regions stack.
   The selectors intentionally cover both the authenticated command surface
   and every current standalone feature family. */
html[data-cpms-large-text] body,
html[data-cpms-large-text] #app-root {
  width: 100% !important;
  height: auto !important;
  max-height: none !important;
  min-height: 100vh !important;
  overflow-x: clip !important;
  overflow-y: auto !important;
}

html[data-cpms-large-text] :where(main, section, article, aside, nav, header, footer, div, form) {
  min-width: 0;
}

html[data-cpms-large-text] :where(p, span, strong, small, label, a, button, td, th, dt, dd) {
  overflow-wrap: anywhere;
  white-space: normal;
}

html[data-cpms-large-text] :where(button, a, input, select, textarea) {
  min-height: 44px;
}

html[data-cpms-large-text] .auth-command-layout,
html[data-cpms-large-text] .command-dashboard,
html[data-cpms-large-text] .training-header,
html[data-cpms-large-text] .training-hero,
html[data-cpms-large-text] .training-gate,
html[data-cpms-large-text] .cpms-briefing__layout,
html[data-cpms-large-text] .cpms-analytics__layout,
html[data-cpms-large-text] .builder-layout,
html[data-cpms-large-text] .mission-builder-layout {
  grid-template-columns: minmax(0, 1fr) !important;
}

html[data-cpms-large-text] :where(.auth-command-layout, .command-dashboard, .cpms-briefing__layout, .cpms-analytics__layout, .training-shell) {
  width: min(100%, 100vw) !important;
  height: auto !important;
  max-height: none !important;
  overflow: visible !important;
}

html[data-cpms-large-text] :where(.app-header, .mode-switcher, .header-controls, .cpms-briefing__topbar, .cpms-briefing__actions, .cpms-analytics__topbar, .cpms-analytics__top-actions, .training-footer) {
  flex-wrap: wrap !important;
  height: auto !important;
}

html[data-cpms-large-text] :where(.cpms-briefing__rail, .cpms-briefing__ack-panel, .cpms-analytics__rail, .cpms-analytics__aside) {
  position: static !important;
  grid-column: auto !important;
  width: 100% !important;
  height: auto !important;
  max-height: none !important;
}

html[data-cpms-large-text] :where(.cpms-briefing__actions, .cpms-analytics__top-actions, .training-nav, .workflow-step-nav) {
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)) !important;
}

html[data-cpms-large-text] :where(.cpms-briefing__summary-grid, .cpms-briefing__intel-grid, .cpms-briefing__kneeboard-grid, .training-ladder-list, .training-mission-grid, .workflow-grid, .workflow-columns) {
  grid-template-columns: minmax(0, 1fr) !important;
}

html[data-cpms-large-text] .cpms-display-panel {
  align-items: stretch;
  padding: 8px;
}

html[data-cpms-large-text] .cpms-display-card {
  width: min(760px, 100%);
  max-height: calc(100vh - 16px);
}

html[data-cpms-large-text] .cpms-display-header,
html[data-cpms-large-text] .cpms-display-body,
html[data-cpms-large-text] .cpms-display-actions {
  padding-inline: 18px;
}

html[data-cpms-large-text] .cpms-surface-row {
  grid-template-columns: minmax(0, 1fr) auto !important;
}

html[data-cpms-large-text] .cpms-surface-row input[type="range"] {
  grid-column: 1 / -1;
  grid-row: auto;
}
