/*
 * Profile Leveling → Contour Map · stylesheet
 *
 * Cascade layers, lowest to highest priority (Task 29):
 *   reset → tokens → base → components → views
 * - tokens: every colour, type size, space, radius and shadow. Change the look here.
 * - components: the shared parts (buttons, fields, switches, badges, help, menus,
 *   empty states) built by src/ui/actionGroup.js.
 * - views: the layout of each workspace (header, field book, tables, map, report)
 *   and print.
 * Design rules live in DESIGN.md ("Monochrome Precision Minimalist").
 */
@layer reset, tokens, base, components, views;

@property --fade-start {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

@property --fade-end {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

/* ------------------------------------------------------------------ tokens */
@layer tokens {
  :root {
    color-scheme: light;

    /* Colour: Monochrome Precision Minimalist. Every text pair is WCAG AA (≥ 4.5:1) and control borders ≥ 3:1. */
    --paper: #f8fafc;
    --surface: #ffffff;
    --surface-sunken: #f1f5f9;
    --paper-bright: var(--surface);
    --ink: #0f172a;
    --muted: #475569;
    --accent: #0f172a;
    --accent-hover: #1e293b;
    --accent-tint: #f1f5f9;
    --green: #166534;
    --green-dark: #14532d;
    --green-tint: #f0fdf4;
    --red: #b91c1c;
    --red-text: #991b1b;
    --red-tint: #fef2f2;
    --amber-text: #92400e;
    --amber-tint: #fffbeb;
    --border-strong: #64748b;
    --rule: #cbd5e1;
    --rule-light: #e2e8f0;
    --focus: #1d4ed8;

    /* Type: system UI font for words, monospace for numbers. */
    --font-ui: "Segoe UI Variable Text", "Segoe UI Variable", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
    --font-num: ui-monospace, "Cascadia Mono", "Segoe UI Mono", "SF Mono", Consolas, monospace;
    --mono: var(--font-num);
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-md: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.375rem;
    --text-2xl: 1.75rem;
    --type-body: var(--text-md);
    --type-dense: var(--text-sm);

    /* Space: 4 px scale. */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.25rem;
    --space-6: 1.5rem;
    --space-7: 2rem;
    --space-8: 2.5rem;
    --sheet-gutter: clamp(0.75rem, 2.5vw, 1.5rem);

    /* Shape and depth: crisp architectural corners, flat hairlines (elevation reserved for popovers/dialogs). */
    --radius-control: 4px;
    --radius-panel: 6px;
    --radius-pill: 999px;
    --elev-1: none;
    --elev-pop: 0 12px 28px rgb(15 23 42 / 0.12), 0 2px 6px rgb(15 23 42 / 0.06);
    --control-height: 2.375rem;

    /* Tables. */
    --table-head-bg: var(--surface-sunken);
    --table-head-ink: var(--ink);
    --table-head-border: var(--rule);
  }
}

/* ------------------------------------------------------------------- reset */
@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  [hidden] {
    display: none !important;
  }

  html {
    text-size-adjust: 100%;
  }

  button,
  input,
  select,
  textarea {
    font: inherit;
    color: inherit;
  }
}

/* -------------------------------------------------------------------- base */
@layer base {
  html {
    min-width: 320px;
    min-height: 100%;
    background: var(--paper);
  }

  body {
    min-height: 100vh;
    min-height: 100dvh;
    margin: 0;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    color: var(--ink);
    background: transparent;
    font-family: var(--font-ui);
    font-size: var(--text-md);
    line-height: 1.5;
    overflow-wrap: break-word;
    font-synthesis: none;
    -webkit-font-smoothing: antialiased;
  }

  :where(a, code) {
    overflow-wrap: anywhere;
  }

  h1,
  h2,
  h3 {
    font-weight: 650;
    line-height: 1.25;
    text-wrap: balance;
  }

  p {
    text-wrap: pretty;
  }

  /* Numbers line up: tables, outputs and decimal inputs use tabular monospace digits. */
  table,
  output,
  .num,
  input[inputmode="decimal"] {
    font-family: var(--font-num);
    font-variant-numeric: tabular-nums;
  }

  th,
  caption {
    font-family: var(--font-ui);
  }

  :focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
  }

  ::selection {
    color: var(--surface);
    background: var(--ink);
  }

  * {
    scrollbar-color: var(--rule) transparent;
    scrollbar-width: thin;
  }

  .icon {
    flex: 0 0 auto;
    width: 1.15em;
    height: 1.15em;
  }
}

/* -------------------------------------------------------------- components */
@layer components {
  /* Buttons ---------------------------------------------------------------- */
  button,
  .btn {
    display: inline-flex;
    gap: var(--space-2);
    align-items: center;
    justify-content: center;
    min-height: var(--control-height);
    padding: 0 var(--space-4);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-control);
    color: var(--ink);
    background: var(--surface);
    font-family: var(--font-ui);
    font-size: var(--text-md);
    font-weight: 600;
    line-height: 1.2;
    text-align: center;
    cursor: pointer;
  }

  .btn--primary {
    color: var(--surface);
    border-color: var(--accent);
    background: var(--accent);
  }

  .btn--ghost {
    border-color: transparent;
    background: transparent;
  }

  .btn--danger {
    color: var(--red-text);
    border-color: var(--red);
  }

  .btn--icon {
    width: var(--control-height);
    padding: 0;
  }

  button:active:not(:disabled) {
    translate: 0 1px;
  }

  button:disabled {
    cursor: not-allowed;
    color: var(--muted);
    border-color: var(--rule-light);
    background: transparent;
  }

  /* Fields ----------------------------------------------------------------- */
  input,
  select,
  textarea {
    width: 100%;
    min-height: var(--control-height);
    padding: 0 var(--space-3);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-control);
    color: var(--ink);
    background: var(--surface);
    font-size: var(--text-md);
  }

  textarea {
    padding-block: var(--space-2);
  }

  input[type="checkbox"],
  input[type="radio"],
  input[type="range"] {
    width: auto;
    min-height: 0;
    padding: 0;
  }

  input::placeholder {
    color: var(--muted);
    opacity: 1;
  }

  input[readonly] {
    border-color: transparent;
    background: color-mix(in srgb, var(--rule-light) 36%, transparent);
  }

  input[aria-invalid="true"],
  select[aria-invalid="true"] {
    border-color: var(--red);
  }

  .field-error {
    min-height: 0.55rem;
    margin: 0;
    color: var(--red-text);
    font-size: var(--text-sm);
    line-height: 1.35;
  }

  .field-error:empty {
    display: none;
  }

  /* Switches ("Show working", "Node elevations") ------------------------------ */
  .working-toggle,
  .map-toggle {
    display: inline-flex;
    gap: var(--space-2);
    align-items: center;
    min-height: 44px;
    font-size: var(--text-md);
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
  }

  .working-toggle input[type="checkbox"],
  .map-toggle input[type="checkbox"] {
    position: relative;
    flex: 0 0 auto;
    width: 2.25rem;
    height: 1.25rem;
    margin: 0;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-pill);
    background: var(--surface-sunken);
    appearance: none;
    cursor: pointer;
  }

  .working-toggle input[type="checkbox"]::before,
  .map-toggle input[type="checkbox"]::before {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: calc(1.25rem - 6px);
    height: calc(1.25rem - 6px);
    border-radius: 50%;
    background: var(--muted);
  }

  .working-toggle input[type="checkbox"]:checked,
  .map-toggle input[type="checkbox"]:checked {
    border-color: var(--accent);
    background: var(--accent);
  }

  .working-toggle input[type="checkbox"]:checked::before,
  .map-toggle input[type="checkbox"]:checked::before {
    left: calc(100% - (1.25rem - 6px) - 2px);
    background: var(--surface);
  }

  /* Status badges (PASS / OPEN / FAIL / INVALID): icon + word, never colour alone */
  .badge {
    display: inline-flex;
    gap: var(--space-1);
    align-items: center;
    align-self: flex-start;
    padding: 0.15rem 0.6rem 0.15rem 0.45rem;
    border: 1px solid currentColor;
    border-radius: var(--radius-pill);
    color: var(--muted);
    background: var(--surface);
    font-family: var(--font-ui);
    font-size: var(--text-sm);
    font-weight: 700;
    letter-spacing: 0.02em;
    line-height: 1.3;
    white-space: nowrap;
  }

  .badge .icon {
    width: 1em;
    height: 1em;
  }

  .badge[data-status="PASS"] {
    color: var(--green-dark);
    background: var(--green-tint);
  }

  .badge[data-status="OPEN"] {
    color: var(--amber-text);
    background: var(--amber-tint);
  }

  .badge[data-status="FAIL"],
  .badge[data-status="INVALID"] {
    color: var(--red-text);
    background: var(--red-tint);
  }

  .check-status {
    padding: 0.3rem 0.85rem 0.3rem 0.6rem;
    font-size: var(--text-md);
  }

  /* Action groups ------------------------------------------------------------ */
  .action-group {
    display: inline-flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    align-items: center;
  }

  .action-group-label {
    margin-right: var(--space-1);
    color: var(--muted);
    font-size: var(--text-sm);
    font-weight: 600;
  }

  /* Help "?" and popover ------------------------------------------------------- */
  .help {
    display: inline-flex;
    vertical-align: middle;
  }

  .help-button {
    width: 1.75rem;
    min-height: 1.75rem;
    height: 1.75rem;
    padding: 0;
    border-color: transparent;
    border-radius: 50%;
    color: var(--muted);
    background: transparent;
  }

  .help-button .icon {
    width: 1.25rem;
    height: 1.25rem;
  }

  .help-popover {
    max-width: min(22rem, calc(100vw - 2rem));
    margin: auto;
    padding: var(--space-4);
    border: 1px solid var(--rule);
    border-radius: var(--radius-panel);
    color: var(--ink);
    background: var(--surface);
    box-shadow: var(--elev-pop);
    font-family: var(--font-ui);
    font-size: var(--text-md);
    font-weight: 400;
    line-height: 1.5;
    text-align: left;
    text-transform: none;
    letter-spacing: normal;
    white-space: normal;
  }

  .help-popover--anchored {
    inset: auto;
    margin: var(--space-2) 0 0;
    position-area: bottom span-right;
    position-try-fallbacks: flip-block, flip-inline;
  }

  .help-title {
    display: block;
    margin-bottom: var(--space-2);
  }

  .help-popover p {
    margin: 0 0 var(--space-2);
  }

  .help-popover p:last-child {
    margin-bottom: 0;
  }

  .help-formula {
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-control);
    color: var(--ink);
    background: var(--surface-sunken);
    font-family: var(--font-num);
    font-size: var(--text-sm);
  }

  /* Menus (File ▾) ----------------------------------------------------------- */
  .menu {
    display: inline-flex;
  }

  .menu-trigger .icon--chevronDown {
    width: 1em;
    height: 1em;
    margin-left: calc(-1 * var(--space-1));
  }

  .menu-popover {
    min-width: 12rem;
    margin: auto;
    padding: var(--space-1);
    border: 1px solid var(--rule);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-pop);
  }

  .menu-popover:popover-open {
    display: grid;
    gap: 2px;
  }

  .menu-popover--anchored {
    inset: auto;
    margin: var(--space-1) 0 0;
    position-area: bottom span-left;
    position-try-fallbacks: flip-block, flip-inline;
  }

  .menu-item {
    justify-content: flex-start;
    width: 100%;
    font-weight: 500;
  }

  /* Empty state (steps 4–7 waiting for field books) ---------------------------- */
  .empty-state {
    display: grid;
    justify-items: start;
    gap: var(--space-3);
    max-width: 44rem;
    margin: var(--space-7) auto;
    padding: var(--space-6);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  .empty-state h3 {
    margin: 0;
    font-size: var(--text-lg);
  }

  .empty-state p {
    max-width: 60ch;
    margin: 0;
    color: var(--muted);
  }

  .empty-state .empty-state-list-title {
    color: var(--ink);
    font-weight: 600;
  }

  .error-list {
    margin: 0;
    padding-left: var(--space-5);
    color: var(--red-text);
    font-size: var(--text-sm);
  }

  .empty-state .error-list {
    color: var(--ink);
  }

  dialog:modal {
    overscroll-behavior: contain;
  }

  dialog::backdrop {
    overflow: hidden;
    overscroll-behavior: contain;
  }

  /* Hover, touch and motion -------------------------------------------------- */
  @media (hover: hover) and (pointer: fine) {
    button:hover:not(:disabled) {
      border-color: var(--ink);
      background: var(--accent-tint);
    }

    .btn--primary:hover:not(:disabled) {
      border-color: var(--accent-hover);
      background: var(--accent-hover);
    }

    .btn--ghost:hover:not(:disabled),
    .help-button:hover:not(:disabled) {
      border-color: transparent;
      color: var(--ink);
      background: var(--accent-tint);
    }
  }

  @media (pointer: coarse) {
    button,
    input:not([type="checkbox"], [type="radio"], [type="range"]),
    select {
      min-height: 44px;
    }

    .help-button {
      width: 44px;
      height: 44px;
    }

    .project-line .btn--icon,
    .field-book-table th .help-button {
      position: relative;
      width: 1.75rem;
      min-height: 1.75rem;
      height: 1.75rem;
    }

    .project-line .btn--icon::after,
    .field-book-table th .help-button::after {
      content: "";
      position: absolute;
      inset: -8px;
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    button,
    input,
    select,
    .working-toggle input[type="checkbox"]::before,
    .map-toggle input[type="checkbox"]::before {
      transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease, left 120ms ease;
    }

    .confirm-dialog,
    .help-popover,
    .menu-popover {
      opacity: 0;
      transform: translateY(-4px) scale(0.98);
      transition-property: opacity, transform, display, overlay;
      transition-duration: 160ms;
      transition-timing-function: ease-out;
      transition-behavior: allow-discrete;
    }

    :is(.confirm-dialog[open], .help-popover:popover-open, .menu-popover:popover-open):where(:not([data-closing])) {
      opacity: 1;
      transform: translateY(0) scale(1);

      @starting-style {
        opacity: 0;
        transform: translateY(-4px) scale(0.98);
      }
    }

    .confirm-dialog::backdrop {
      background-color: rgb(23 26 23 / 0);
      transition-property: background-color, display, overlay;
      transition-duration: 160ms;
      transition-timing-function: ease-out;
      transition-behavior: allow-discrete;
    }

    .confirm-dialog[open]:where(:not([data-closing]))::backdrop {
      background-color: rgb(23 26 23 / 0.35);

      @starting-style {
        background-color: rgb(23 26 23 / 0);
      }
    }
  }
}

/* -------------------------------------------------------------------- views */
@layer views {
  /* App shell (Task 30): header, stepper with progress, status bar, footer ------ */
  .app-header {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3) var(--space-5);
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--sheet-gutter);
    border-bottom: 1px solid var(--rule-light);
    background: var(--surface);
  }

  .brand {
    display: flex;
    flex: 1 1 20rem;
    gap: var(--space-3);
    align-items: center;
    min-width: 0;
  }

  .brand-mark {
    display: grid;
    flex: 0 0 auto;
    place-items: center;
    width: 2.5rem;
    height: 2.5rem;
    border-radius: var(--radius-control);
    color: var(--surface);
    background: var(--green);
  }

  .brand-mark .icon {
    width: 1.4rem;
    height: 1.4rem;
  }

  .brand-text {
    display: grid;
    min-width: 0;
  }

  .brand-title {
    margin: 0;
    font-size: var(--text-lg);
    font-weight: 700;
    line-height: 1.2;
  }

  .project-line {
    display: flex;
    gap: var(--space-1);
    align-items: center;
    min-width: 0;
    color: var(--muted);
    font-size: var(--text-sm);
  }

  .project-name {
    overflow: hidden;
    min-width: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .project-line .btn--icon {
    width: 1.75rem;
    min-height: 1.75rem;
    height: 1.75rem;
  }

  .project-line .btn--icon .icon {
    width: 1rem;
    height: 1rem;
  }

  .project-name-input {
    width: min(24rem, 100%);
    min-height: 1.75rem;
    height: 1.75rem;
    padding-inline: var(--space-2);
    font-size: var(--text-sm);
  }

  .header-meta {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-3);
    align-items: center;
    color: var(--muted);
    font-size: var(--text-sm);
  }

  .meta-chip {
    padding: 0.1rem 0.6rem;
    border-radius: var(--radius-pill);
    color: var(--ink);
    background: var(--surface-sunken);
    font-size: var(--text-sm);
    white-space: nowrap;
  }

  .save-state {
    display: inline-flex;
    gap: var(--space-2);
    align-items: center;
    white-space: nowrap;
  }

  .save-state::before {
    content: "";
    flex: 0 0 auto;
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--rule);
  }

  .save-state[data-kind="saved"]::before {
    background: var(--green);
  }

  .save-state[data-kind="failed"] {
    color: var(--red-text);
    font-weight: 600;
  }

  .save-state[data-kind="failed"]::before {
    background: var(--red);
  }

  .header-actions {
    display: flex;
    gap: var(--space-1);
    align-items: center;
  }

  .header-actions [data-action="open-report"] {
    margin-left: var(--space-2);
  }

  .step-nav {
    padding: var(--space-2) var(--sheet-gutter);
    border-bottom: 1px solid var(--rule-light);
  }

  .workflow-menu-toggle {
    justify-content: flex-start;
    width: 100%;
    min-height: 48px;
    text-align: left;
  }

  .workflow-menu-icon {
    flex: 0 0 auto;
    width: 1.25rem;
    height: 1.25rem;
  }

  .workflow-menu-toggle [data-workflow-menu-label] {
    overflow: hidden;
    min-width: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .step-list {
    display: none;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-1);
    margin: var(--space-2) 0 0;
    padding: 0;
    list-style: none;
  }

  .step-nav[data-open="true"] .step-list {
    display: grid;
  }

  .step-button {
    display: grid;
    grid-template-areas:
      "number label progress"
      "number detail detail";
    grid-template-columns: 2rem minmax(0, 1fr) auto;
    column-gap: var(--space-2);
    justify-content: stretch;
    align-items: center;
    width: 100%;
    height: 100%;
    min-height: 56px;
    padding: var(--space-2) var(--space-3);
    border-color: transparent;
    border-radius: var(--radius-panel);
    background: transparent;
    font-weight: 400;
    text-align: left;
  }

  .step-number {
    display: grid;
    grid-area: number;
    place-items: center;
    width: 2rem;
    height: 2rem;
    border: 1.5px solid var(--border-strong);
    border-radius: 50%;
    background: var(--surface);
    font-size: var(--text-sm);
    font-weight: 700;
  }

  .step-label {
    grid-area: label;
    min-width: 0;
    font-size: var(--text-md);
    font-weight: 650;
    line-height: 1.2;
  }

  .step-detail {
    grid-area: detail;
    color: var(--muted);
    font-size: var(--text-sm);
    line-height: 1.25;
  }

  .step-progress {
    display: inline-flex;
    gap: var(--space-1);
    align-items: center;
    grid-area: progress;
    justify-self: end;
    padding: 0 0.5rem 0 0.35rem;
    border-radius: var(--radius-pill);
    color: var(--muted);
    background: var(--surface-sunken);
    font-size: var(--text-sm);
    font-weight: 600;
    line-height: 1.5;
    white-space: nowrap;
  }

  .step-progress:empty {
    display: none;
  }

  .step-progress .icon {
    width: 0.95em;
    height: 0.95em;
  }

  .step-button[data-progress="done"] .step-progress,
  .step-button[data-progress="ready"] .step-progress {
    color: var(--green-dark);
    background: var(--green-tint);
  }

  .step-button[data-progress="todo"] .step-progress {
    color: var(--amber-text);
    background: var(--amber-tint);
  }

  .step-button[data-progress="error"] .step-progress {
    color: var(--red-text);
    background: var(--red-tint);
  }

  .step-button[data-progress="done"] .step-number {
    border-color: var(--green);
    color: var(--green-dark);
  }

  .step-button[aria-current="step"] {
    border-color: var(--green);
    background: var(--green-tint);
  }

  .step-button[aria-current="step"] .step-number {
    color: var(--surface);
    border-color: var(--green-dark);
    background: var(--green);
  }

  .status-bar {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-4);
    align-items: center;
    justify-content: space-between;
    min-height: 2.75rem;
    padding: var(--space-2) var(--sheet-gutter);
    border-top: 1px solid var(--rule-light);
    background: var(--surface);
  }

  .status-message {
    display: flex;
    flex: 1 1 24rem;
    gap: var(--space-2);
    align-items: flex-start;
    min-width: 0;
    margin: 0;
    font-size: var(--text-sm);
  }

  .status-message .icon {
    margin-top: 0.15rem;
    color: var(--muted);
  }

  .status-message[data-kind="success"] .icon {
    color: var(--green);
  }

  .status-message[data-kind="warning"] .icon {
    color: var(--amber-text);
  }

  .status-message[data-kind="error"] .icon {
    color: var(--red-text);
  }

  .status-precision {
    display: inline-flex;
    gap: var(--space-1);
    align-items: center;
    color: var(--muted);
    font-size: var(--text-sm);
    white-space: nowrap;
  }

  .app-footer {
    padding: var(--space-2) var(--sheet-gutter);
    border-top: 1px solid var(--rule-light);
    color: var(--muted);
    font-size: var(--text-sm);
    text-align: center;
  }

  @media (hover: hover) and (pointer: fine) {
    .step-button:hover:not([aria-current="step"]) {
      border-color: var(--rule-light);
      background: var(--surface);
    }

    .step-button[aria-current="step"]:hover {
      border-color: var(--green);
      background: var(--green-tint);
    }
  }

  @media (min-width: 1101px) {
    .workflow-menu-toggle {
      display: none;
    }

    .step-list {
      display: grid;
      grid-template-columns: repeat(7, minmax(0, 1fr));
      margin: 0;
    }

    /* Fine steps show only their icon here; the word stays for screen readers and in the tooltip. */
    .step-button:is([data-progress="done"], [data-progress="ready"], [data-progress="waiting"]) .step-progress {
      padding: 0.2rem;
    }

    .step-button:is([data-progress="done"], [data-progress="ready"], [data-progress="waiting"]) .step-progress-text {
      position: absolute;
      overflow: hidden;
      width: 1px;
      height: 1px;
      clip-path: inset(50%);
      white-space: nowrap;
    }
  }

  @media print {
    .header-actions,
    .project-line .btn,
    .step-nav,
    .status-bar,
    .app-footer {
      display: none;
    }

    .app-header {
      border: 0;
      background: none;
    }
  }

  .app-shell {
    width: 100%;
    max-width: 120rem;
    min-height: 100vh;
    min-height: 100dvh;
    margin: 0 auto;
  }

  .microcopy {
    font-size: var(--type-dense);
  }

  .workspace {
    display: block;
    overflow: visible;
    border-bottom: 1px solid var(--rule-light);
  }

  .setup-panel,
  .field-book-panel,
  .check-panel {
    min-width: 0;
    padding: 0.55rem 0.7rem 0.75rem;
    border-right: 0;
    border-bottom: 1px solid var(--rule-light);
  }

  .check-panel {
    border-bottom: 0;
  }

  .setup-panel {
    container: setup / inline-size;
  }

  .field-book-panel {
    container: fieldbook / inline-size;
  }

  .panel-title {
    margin: 0 0 0.8rem;
    padding-bottom: 0.45rem;
    border-bottom: 1px dashed var(--green-dark);
    font-size: 1rem;
    line-height: 1.2;
    letter-spacing: 0.025em;
    text-transform: uppercase;
  }

  .setup-form {
    display: grid;
    gap: 0.4rem;
  }

  .form-section {
    min-width: 0;
    margin: 0;
    padding: 0.35rem 0 0;
    border: 0;
    border-top: 1px solid var(--rule);
  }

  .form-section:first-child {
    padding-top: 0;
    border-top: 0;
  }

  .form-section legend {
    padding: 0 0 0.25rem;
    font-size: 0.875rem;
    font-weight: 800;
  }

  .field-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.35rem 0.55rem;
  }

  .field {
    display: grid;
    gap: 0.15rem;
    min-width: 0;
  }

  .field--wide {
    grid-column: auto;
  }

  .field label {
    font-size: 0.875rem;
    font-weight: 700;
  }

  .book-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.65rem;
    align-items: end;
    margin-bottom: 0.4rem;
  }

  .field-book-panel {
    position: relative;
  }

  .field-book-panel > .book-toolbar:first-child {
    min-height: 42px;
    padding-right: 0;
  }

  .book-identity {
    min-width: 0;
  }

  .book-identity h2 {
    overflow: hidden;
    margin: 0;
    font-size: 1rem;
    line-height: 1.25;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .table-wrap {
    overflow-x: auto;
    overflow-y: visible;
    max-height: none;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
    border-top: 1px solid var(--green-dark);
    border-bottom: 1px solid var(--green-dark);
  }

  .field-book-table {
    width: 100%;
    min-width: 48rem;
    border-spacing: 0;
    border-collapse: separate;
    table-layout: fixed;
    font-size: 0.875rem;
  }

  .field-book-table th,
  .field-book-table td {
    height: 36px;
    padding: 0.12rem 0.3rem;
    border-right: 1px solid var(--rule-light);
    border-bottom: 1px solid var(--rule-light);
    text-align: right;
    vertical-align: middle;
  }

  .field-book-table th {
    position: sticky;
    z-index: 2;
    top: 0;
    color: var(--table-head-ink);
    border-color: var(--table-head-border);
    background: var(--table-head-bg);
    text-align: center;
  }

  .field-book-table th:first-child,
  .field-book-table td:first-child,
  .field-book-table th:last-child,
  .field-book-table td:last-child {
    text-align: left;
  }

  .field-book-table input {
    min-height: 32px;
    padding: 0.1rem 0.3rem;
    border-color: transparent;
    background: transparent;
    text-align: right;
  }

  .field-book-table tr[data-selected="true"] td {
    background-color: color-mix(in srgb, var(--green) 9%, transparent);
  }

  .field-book-table tr[data-row-type="tp"] td {
    border-top: 2px dashed var(--green-dark);
  }

  .cell-empty {
    color: var(--muted);
    text-align: center;
  }

  .remarks-cell {
    white-space: nowrap;
  }

  /* Shared action rows: a labelled button group per job (Turning point, File…). */
  .book-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.4rem;
  }

  .book-view-options {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 0.25rem 1rem;
    align-items: center;
  }


  /* Readings that are not numbers yet, or that the leveling core rejects. */
  /* Ink text keeps full contrast; the dashed border is a cue that does not rely on colour alone (Task 28). */
  .field-book-table input[aria-invalid="true"] {
    color: var(--ink);
    border: 2px dashed var(--red);
    background: color-mix(in srgb, var(--red) 8%, transparent);
  }

  /* Show working: substituted formulas beside each computed row. */
  .field-book-table--working {
    min-width: 64rem;
  }

  .results-table--working {
    min-width: 64rem;
  }

  .results-table--working th:last-child {
    width: 46%;
  }

  .tables-workspace[data-step="5"] .results-table--working {
    min-width: 98rem;
  }

  .tables-workspace[data-step="5"] .results-table--working th:last-child {
    width: 42%;
  }

  .field-book-table .working-cell,
  .results-table .working-cell {
    height: auto;
    padding-block: 0.3rem;
    color: var(--green-dark);
    font-family: var(--mono);
    font-size: 0.78rem;
    line-height: 1.4;
    text-align: left;
    white-space: pre-line;
  }

  .results-table--working th:last-child {
    text-align: left;
  }

  .working-list {
    display: grid;
    gap: 0.35rem;
    margin: 0;
    padding: 0.6rem 0.6rem 0.6rem 1.75rem;
    color: var(--green-dark);
    border: 1px solid var(--rule);
    background: color-mix(in srgb, var(--paper-bright) 70%, transparent);
    font-family: var(--mono);
    font-size: 0.78rem;
    line-height: 1.4;
    overflow-wrap: anywhere;
  }

  .check-panel {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }

  .check-list {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.55rem 0.75rem;
    margin: 0;
    padding: 0;
    font-size: 0.875rem;
  }

  .check-list dt,
  .check-list dd {
    margin: 0;
  }

  .check-list dd {
    font-weight: 800;
    text-align: right;
  }

  .check-note {
    margin: 0;
    padding: 0.75rem;
    color: var(--red-text);
    border: 1px dashed var(--red);
    font-size: 0.875rem;
    line-height: 1.45;
  }

  .check-note span {
    display: block;
  }

  .check-guidance {
    margin: 0;
    color: var(--red-text);
    font-size: 0.875rem;
    line-height: 1.45;
  }

  .tables-workspace {
    min-height: 0;
    overflow: visible;
    padding: 0.75rem var(--sheet-gutter) 1rem;
    border-bottom: 2px solid var(--green-dark);
  }

  .results-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.75rem;
    align-items: end;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--green-dark);
  }

  .results-heading h2,
  .results-heading p {
    margin: 0;
  }

  .results-heading h2 {
    font-size: var(--type-body);
    line-height: 1.25;
    text-transform: uppercase;
  }

  .results-heading p {
    max-width: 70ch;
    margin-top: 0.35rem;
    color: var(--muted);
    font-size: 0.875rem;
  }

  .interval-field .field-error {
    min-height: 1.2rem;
  }

  .results-actions {
    display: flex;
    gap: 1rem;
    align-items: stretch;
    flex-direction: column;
    justify-content: space-between;
    min-height: 52px;
    padding: 0.55rem 0;
  }

  .results-actions button {
    width: 100%;
  }

  .results-count {
    font-size: 0.875rem;
    font-weight: 800;
  }

  .results-content {
    min-height: 0;
    overflow: visible;
  }

  .spatial-section {
    padding-bottom: 1rem;
    border-bottom: 2px solid var(--green-dark);
  }

  .spatial-summary,
  .spatial-section p,
  .results-subheading {
    margin: 0;
  }

  .spatial-summary-title,
  .results-subheading {
    font-size: var(--type-body);
    line-height: 1.25;
    text-transform: uppercase;
  }

  .spatial-summary {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    justify-content: space-between;
    min-height: 44px;
    padding: 0.35rem 0;
    cursor: pointer;
    font-weight: 800;
  }

  .spatial-summary-title {
    font-weight: 800;
  }

  .spatial-summary-hint {
    color: var(--muted);
    font-size: var(--type-dense);
    font-weight: 400;
    text-align: right;
  }

  .spatial-section[open] .spatial-summary-hint {
    visibility: hidden;
  }

  .spatial-section p {
    max-width: 75ch;
    margin-top: 0.25rem;
    color: var(--muted);
    font-size: var(--type-dense);
  }

  .spatial-grid-wrap {
    overflow: auto;
    max-height: 30rem;
    margin-top: 0.65rem;
    border-top: 1px solid var(--green-dark);
    border-bottom: 1px solid var(--green-dark);
    background: color-mix(in srgb, var(--paper-bright) 36%, transparent);
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .spatial-grid {
    width: max-content;
    min-width: 100%;
    border-spacing: 0;
    border-collapse: separate;
    font-size: var(--type-dense);
    font-variant-numeric: tabular-nums;
  }

  .spatial-grid th,
  .spatial-grid td {
    border-right: 1px solid var(--rule);
    border-bottom: 1px solid var(--rule);
  }

  .spatial-grid thead th {
    position: sticky;
    z-index: 4;
    top: 0;
    min-width: 7rem;
    height: 30px;
    padding: 0.2rem 0.45rem;
    color: var(--table-head-ink);
    background: var(--table-head-bg);
    text-align: center;
  }

  .spatial-grid tbody > tr > th {
    position: sticky;
    z-index: 3;
    left: 0;
    min-width: 3.25rem;
    padding: 0.3rem 0.45rem;
    color: var(--table-head-ink);
    background: var(--table-head-bg);
    text-align: center;
  }

  .spatial-grid .spatial-corner {
    z-index: 5;
    left: 0;
    min-width: 3.25rem;
  }

  .spatial-grid .spatial-header-gap {
    min-width: 9rem;
    color: transparent;
    background: color-mix(in srgb, var(--table-head-bg) 60%, var(--surface));
  }

  .spatial-cell {
    min-width: 7rem;
    padding: 0.35rem 0.45rem;
    vertical-align: top;
  }

  .spatial-cell--node {
    height: 4rem;
    background: color-mix(in srgb, var(--paper-bright) 74%, transparent);
    text-align: center;
  }

  .spatial-cell--horizontal {
    min-width: 9rem;
    background: color-mix(in srgb, var(--green) 7%, transparent);
  }

  .spatial-cell--vertical {
    height: 5.5rem;
    background: color-mix(in srgb, var(--green) 7%, transparent);
  }

  .spatial-cell--gap {
    min-width: 9rem;
    background: color-mix(in srgb, var(--rule-light) 30%, transparent);
  }

  .spatial-node-id,
  .spatial-node-elevation,
  .spatial-edge-name,
  .spatial-items strong,
  .spatial-items span {
    display: block;
  }

  .spatial-node-id {
    font-size: var(--type-dense);
  }

  .spatial-node-elevation {
    margin-top: 0.2rem;
    color: var(--muted);
  }

  .spatial-edge-name {
    padding-bottom: 0.2rem;
    border-bottom: 1px dashed var(--rule);
    color: var(--muted);
    font-size: var(--type-dense);
    text-align: center;
  }

  .spatial-items {
    display: grid;
    gap: 0.3rem;
    margin: 0.3rem 0 0;
    padding: 0;
    list-style: none;
    text-align: center;
  }

  .spatial-items li {
    padding-top: 0.25rem;
    border-top: 1px solid var(--rule-light);
  }

  .spatial-items li:first-child {
    padding-top: 0;
    border-top: 0;
  }

  .spatial-items span {
    margin-top: 0.1rem;
    color: var(--muted);
    font-size: var(--type-dense);
  }

  .results-subheading {
    margin-top: 1rem;
    padding-bottom: 0.45rem;
    border-bottom: 1px dashed var(--green-dark);
  }

  .results-table-wrap {
    overflow: auto;
    max-height: 28rem;
    border-top: 1px solid var(--green-dark);
    border-bottom: 1px solid var(--green-dark);
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .results-table {
    width: 100%;
    min-width: 42rem;
    border-spacing: 0;
    border-collapse: separate;
    font-size: 0.875rem;
    font-variant-numeric: tabular-nums;
  }

  .tables-workspace[data-step="5"] .results-table {
    min-width: 58rem;
  }

  .results-table th,
  .results-table td {
    height: 36px;
    padding: 0.25rem 0.55rem;
    border-right: 1px solid var(--rule-light);
    border-bottom: 1px solid var(--rule-light);
    text-align: right;
    white-space: nowrap;
  }

  .results-table th {
    position: sticky;
    z-index: 2;
    top: 0;
    color: var(--table-head-ink);
    border-color: var(--table-head-border);
    background: var(--table-head-bg);
    text-align: center;
  }

  .results-table tbody tr:nth-child(5n) td {
    border-bottom-color: var(--rule);
  }

  .results-table .results-key {
    font-weight: 800;
    text-align: left;
  }

  .map-workspace {
    min-height: 0;
    overflow: visible;
    padding: 0.75rem var(--sheet-gutter) 1rem;
    border-bottom: 2px solid var(--green-dark);
  }

  .map-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.75rem;
    align-items: end;
    padding-bottom: 0.75rem;
    border-bottom: 2px solid var(--green-dark);
  }

  .map-heading h2,
  .map-heading p,
  .map-summary {
    margin: 0;
  }

  .map-heading h2 {
    font-size: var(--type-body);
    line-height: 1.25;
    text-transform: uppercase;
  }

  .map-heading p {
    max-width: 70ch;
    margin-top: 0.35rem;
    color: var(--muted);
    font-size: 0.875rem;
  }

  .map-project-actions,
  .map-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: stretch;
    flex-direction: column;
    justify-content: flex-start;
  }

  .map-project-actions button,
  .map-controls button {
    width: 100%;
  }

  .map-view-tabs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-top: 0.75rem;
    border-bottom: 2px solid var(--green-dark);
  }

  .map-view-tab {
    min-height: 44px;
    border-color: var(--rule);
    border-bottom: 0;
    font-weight: 800;
  }

  .map-view-tab + .map-view-tab {
    border-left: 0;
  }

  .map-view-tab[aria-selected="true"] {
    color: var(--paper-bright);
    border-color: var(--green-dark);
    background: var(--green);
  }

  .map-view-panel {
    min-width: 0;
  }

  .map-fullscreen-button {
    display: inline-flex;
    gap: 0.45rem;
    align-items: center;
    justify-content: center;
  }

  .map-fullscreen-button svg {
    width: 1rem;
    height: 1rem;
    fill: none;
    stroke: currentColor;
    stroke-linecap: square;
    stroke-linejoin: miter;
    stroke-width: 1.75;
  }

  .map-fullscreen-active {
    overflow: hidden;
  }

  .map-view-panel:fullscreen,
  .map-view-panel--fullscreen-fallback {
    box-sizing: border-box;
    display: flex;
    overflow: auto;
    width: 100%;
    height: 100dvh;
    min-width: 0;
    padding:
      max(0.75rem, env(safe-area-inset-top))
      max(0.75rem, env(safe-area-inset-right))
      max(0.75rem, env(safe-area-inset-bottom))
      max(0.75rem, env(safe-area-inset-left));
    flex-direction: column;
    color: var(--ink);
    background: var(--paper);
    overscroll-behavior: contain;
  }

  .map-view-panel:fullscreen::backdrop {
    background: var(--green-dark);
  }

  .map-view-panel--fullscreen-fallback {
    position: fixed;
    z-index: 1000;
    inset: 0;
  }

  .map-view-panel:fullscreen .map-toolbar,
  .map-view-panel:fullscreen .terrain-toolbar,
  .map-view-panel--fullscreen-fallback .map-toolbar,
  .map-view-panel--fullscreen-fallback .terrain-toolbar {
    flex: 0 0 auto;
  }

  .map-view-panel:fullscreen .map-canvas,
  .map-view-panel--fullscreen-fallback .map-canvas {
    flex: 1 0 clamp(18rem, 55dvh, 42rem);
  }

  .map-view-panel:fullscreen .contour-map-svg,
  .map-view-panel:fullscreen .terrain-map-svg,
  .map-view-panel--fullscreen-fallback .contour-map-svg,
  .map-view-panel--fullscreen-fallback .terrain-map-svg {
    width: 100%;
    height: 100%;
    min-width: 42rem;
  }

  .map-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 1rem;
    align-items: center;
    min-height: 52px;
    padding: 0.55rem 0;
  }

  .map-summary {
    font-size: 0.875rem;
    font-weight: 800;
  }

  .map-canvas {
    display: grid;
    overflow-x: auto;
    overflow-y: hidden;
    flex: 1;
    place-items: stretch start;
    min-height: 0;
    border-top: 1px solid var(--green-dark);
    border-bottom: 1px solid var(--green-dark);
    background: color-mix(in srgb, var(--paper-bright) 58%, transparent);
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }

  .contour-map-svg {
    display: block;
    width: 100%;
    height: auto;
    min-width: 0;
    min-height: 0;
  }

  .terrain-toolbar {
    display: grid;
    gap: 0.75rem;
    padding: 0.65rem 0;
  }

  .terrain-settings {
    min-width: 0;
    margin: 0;
    padding: 0.65rem;
    border: 1px solid var(--rule);
  }

  .terrain-settings legend {
    padding: 0 0.4rem;
    font-size: var(--type-dense);
    font-weight: 800;
    text-transform: uppercase;
  }

  .terrain-camera-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.65rem;
  }

  .terrain-range {
    display: grid;
    gap: 0.3rem;
    min-width: 0;
  }

  .terrain-range-label {
    display: flex;
    gap: 0.75rem;
    align-items: baseline;
    justify-content: space-between;
    font-size: var(--type-dense);
    font-weight: 700;
  }

  .terrain-range output {
    color: var(--green-dark);
    font-variant-numeric: tabular-nums;
    font-weight: 800;
    white-space: nowrap;
  }

  .terrain-range input {
    width: 100%;
    min-height: 30px;
    margin: 0;
    padding: 0;
    accent-color: var(--green);
  }

  .terrain-actions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: 0.75rem;
  }

  .terrain-actions button {
    width: 100%;
  }

  .terrain-map-svg {
    display: block;
    width: 100%;
    height: auto;
    min-width: 0;
    min-height: 0;
  }

  .map-canvas[data-map-scale="detail"] .contour-map-svg,
  .map-canvas[data-map-scale="detail"] .terrain-map-svg {
    min-width: 42rem;
  }

  .terrain-legend-mark--surface {
    width: 0.9rem;
    height: 0.9rem;
    border: 1px solid var(--green-dark);
    background: color-mix(in srgb, var(--green) 42%, var(--paper-bright));
  }

  .map-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.65rem 1.25rem;
    align-items: center;
    margin: 0;
    padding: 0.55rem 0 0;
    list-style: none;
    color: var(--muted);
    font-size: var(--type-dense);
  }

  .map-legend li {
    display: inline-flex;
    gap: 0.4rem;
    align-items: center;
  }

  .map-legend-mark {
    display: inline-block;
    flex: 0 0 auto;
  }

  .map-legend-mark--node {
    width: 0.45rem;
    height: 0.45rem;
    background: var(--green);
  }

  .map-legend-mark--contour {
    width: 1.4rem;
    height: 0;
    border-top: 2px solid var(--green-dark);
  }

  .map-legend-mark--bm {
    width: 0;
    height: 0;
    border-right: 0.35rem solid transparent;
    border-bottom: 0.65rem solid var(--ink);
    border-left: 0.35rem solid transparent;
  }

  .map-empty {
    margin-top: 1rem;
  }

  /*
   * Scrolling panels are the positioning box for the absolutely positioned .sr-only text
   * inside them (Task 36). Otherwise hidden text below a panel's fold is placed against
   * the page and stretches it, so the desktop page scrolls into blank space.
   */
  .workspace,
  .tables-workspace,
  .map-workspace,
  .report-workspace,
  .welcome-workspace,
  .setup-panel,
  .field-book-panel,
  .check-panel,
  .table-wrap,
  .results-content,
  .results-table-wrap,
  .spatial-grid-wrap {
    position: relative;
  }

  .sr-only {
    position: absolute;
    overflow: hidden;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  @media (min-width: 761px) {
    .workspace {
      display: grid;
      grid-template-columns: minmax(15rem, 32%) minmax(0, 68%);
    }

    .setup-panel {
      border-right: 1px solid var(--rule-light);
    }

    .check-panel {
      grid-column: 1 / -1;
    }

    .field-grid,
    .form-section:nth-of-type(3) .field-grid {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .field--wide {
      grid-column: 1 / -1;
    }

    .form-section:nth-of-type(3) .field--wide {
      grid-column: auto;
    }

    .form-section:nth-of-type(3) .field:last-child {
      grid-column: 1 / -1;
    }

    .book-toolbar {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .book-identity {
      grid-column: 1 / -1;
    }

    .results-actions {
      align-items: center;
      flex-direction: row;
    }

    .results-actions button {
      width: auto;
    }

    .map-project-actions,
    .map-controls {
      align-items: center;
      flex-direction: row;
    }

    .map-project-actions button,
    .map-controls button {
      width: auto;
    }

    .terrain-camera-grid {
      grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .terrain-actions {
      align-items: center;
      flex-direction: row;
    }

    .terrain-actions button {
      width: auto;
    }

    .map-view-panel:fullscreen .map-canvas,
    .map-view-panel--fullscreen-fallback .map-canvas {
      flex-basis: clamp(22rem, 62dvh, 48rem);
    }

    .results-table-wrap {
      max-height: 44rem;
    }
  }

  @media (min-width: 1101px) {
    .app-shell {
      display: grid;
      grid-template-rows: auto auto minmax(0, 1fr) auto auto;
      height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
      min-height: 0;
    }

    .workspace {
      grid-template-columns: minmax(15rem, 21%) minmax(0, 1fr) minmax(14.5rem, 20%);
      min-height: 0;
      overflow: hidden;
    }

    .setup-panel,
    .field-book-panel {
      border-right: 1px solid var(--rule-light);
      border-bottom: 0;
    }

    .map-scale-button {
      display: none;
    }

    .map-canvas[data-map-scale="detail"] .contour-map-svg,
    .map-canvas[data-map-scale="detail"] .terrain-map-svg {
      min-width: 0;
    }

    .setup-panel,
    .check-panel {
      min-height: 0;
      overflow-y: auto;
    }

    .check-panel {
      grid-column: auto;
    }

    .field-book-panel {
      display: flex;
      overflow: hidden;
      min-height: 0;
      flex-direction: column;
    }

    .book-toolbar {
      grid-template-columns: 1fr minmax(8rem, auto) minmax(8rem, auto);
    }

    .book-identity {
      grid-column: auto;
    }

    .table-wrap {
      overflow: auto;
      flex: 1;
      min-height: 0;
    }

    .results-header {
      grid-template-columns: minmax(0, 1fr) minmax(15rem, 19rem);
      gap: 1.5rem;
    }

    .tables-workspace:not([hidden]),
    .map-workspace:not([hidden]) {
      display: flex;
      overflow: hidden;
      flex-direction: column;
    }

    .results-content {
      overflow: auto;
      flex: 1;
      min-height: 0;
      padding-right: 0.25rem;
    }

    .map-header,
    .map-toolbar {
      grid-template-columns: minmax(0, 1fr) auto;
    }

    .map-view-panel:not([hidden]) {
      display: flex;
      overflow: hidden;
      flex: 1;
      min-height: 0;
      flex-direction: column;
    }

    .terrain-toolbar {
      grid-template-columns: minmax(13rem, 0.6fr) minmax(34rem, 1.4fr);
      align-items: center;
    }

    .map-project-actions,
    .map-controls {
      justify-content: flex-end;
    }

    .map-canvas {
      height: auto;
      min-height: 0;
    }

    .contour-map-svg,
    .terrain-map-svg {
      width: 100%;
      height: 100%;
      min-width: 0;
    }

    .map-view-panel:fullscreen,
    .map-view-panel--fullscreen-fallback {
      overflow: hidden;
    }

    .map-view-panel:fullscreen .map-canvas,
    .map-view-panel--fullscreen-fallback .map-canvas {
      flex-basis: 0;
    }

    .map-view-panel:fullscreen .contour-map-svg,
    .map-view-panel:fullscreen .terrain-map-svg,
    .map-view-panel--fullscreen-fallback .contour-map-svg,
    .map-view-panel--fullscreen-fallback .terrain-map-svg {
      min-width: 0;
    }
  }

  .spatial-summary-title {
    font-size: var(--text-lg);
    letter-spacing: normal;
    text-transform: none;
  }

  @media (hover: hover) and (pointer: fine) {
    .map-view-tab[aria-selected="true"]:hover {
      background: var(--green-dark);
    }

    .field-book-table input:not([readonly]):hover {
      border-color: var(--rule);
      background: var(--paper-bright);
    }

    .results-table tbody tr:hover td {
      background: color-mix(in srgb, var(--green) 8%, transparent);
    }
  }

  @media (pointer: coarse) {
    .field-book-table th,
    .field-book-table td {
      height: 44px;
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    .field-book-table tbody tr {
      animation: print-advance 90ms steps(2, end);
    }

    @keyframes print-advance {
      from {
        translate: 0 -2px;
      }

      to {
        translate: 0 0;
      }
    }
  }

  @media print {
    .app-shell {
      border: 0;
    }

    .map-project-actions,
    .map-controls,
    .map-fullscreen-button,
    .map-scale-button,
    .map-view-tabs,
    .terrain-settings {
      display: none;
    }

    .workspace {
      grid-template-columns: 24% 76%;
    }

    .check-panel {
      grid-column: 1 / -1;
    }

    .map-workspace {
      overflow: visible;
      padding: 0;
      border: 0;
    }

    .map-header {
      grid-template-columns: 1fr;
    }

    .map-toolbar {
      display: block;
    }

    .map-canvas {
      overflow: visible;
      height: auto;
      border: 0;
    }

    .contour-map-svg,
    .terrain-map-svg {
      width: 100%;
      height: auto;
      min-width: 0;
    }
  }

  .results-empty [data-action="go-to-incomplete"] {
    margin: 0.25rem 0 0.75rem;
  }

  /* Printable solution report (Task 21). */
  .report-workspace {
    min-height: 0;
    overflow: auto;
    padding: 0.75rem var(--sheet-gutter) 1.5rem;
    border-bottom: 2px solid var(--green-dark);
  }

  .report-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem 1.25rem;
    align-items: center;
    justify-content: space-between;
    max-width: 62rem;
    margin: 0 auto 1rem;
  }

  .report-toolbar h2 {
    margin: 0;
    font-size: 1.1rem;
  }

  .report-toolbar-intro p {
    margin: 0.2rem 0 0;
  }

  .report {
    max-width: 62rem;
    margin: 0 auto;
    padding: clamp(1rem, 3vw, 2rem);
    border: 1px solid var(--rule);
    background: var(--paper-bright);
    color: var(--ink);
  }

  .report h1 {
    margin: 0;
    font-size: 1.35rem;
  }

  .report h2 {
    margin: 1.5rem 0 0.35rem;
    padding-bottom: 0.3rem;
    border-bottom: 2px solid var(--green-dark);
    font-size: 1.1rem;
  }

  .report h3 {
    margin: 0;
    font-size: 1rem;
  }

  .report-muted {
    margin: 0.2rem 0 0.6rem;
    color: var(--muted);
    font-size: var(--type-dense);
  }

  .report-settings {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 0.2rem 1rem;
    margin: 0.75rem 0 0;
    font-size: var(--type-dense);
  }

  .report-settings dt {
    font-weight: 800;
  }

  .report-settings dd {
    margin: 0;
  }

  .report-book {
    margin-top: 1rem;
  }

  .report-book-header {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.9rem;
    align-items: baseline;
    margin-bottom: 0.35rem;
  }

  .report-status {
    padding: 0 0.35rem;
    border: 1px solid currentColor;
    color: var(--green-dark);
    font-size: var(--type-dense);
    font-weight: 900;
  }

  .report-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
  }

  .report-table th,
  .report-table td {
    padding: 0.15rem 0.4rem;
    border: 1px solid var(--rule-light);
    text-align: right;
    vertical-align: top;
  }

  .report-table th {
    color: var(--table-head-ink);
    border-color: var(--table-head-border);
    background: var(--table-head-bg);
    text-align: center;
  }

  .report-table .report-key {
    font-weight: 800;
    text-align: left;
  }

  .report-table .working-cell {
    color: var(--green-dark);
    font-family: var(--mono);
    font-size: 0.72rem;
    text-align: left;
    white-space: pre-line;
  }

  .report-table tr[data-row-type="tp"] td {
    border-top: 2px dashed var(--green-dark);
  }

  .report-check {
    display: grid;
    gap: 0.35rem;
    margin-top: 0.4rem;
    font-size: var(--type-dense);
  }

  .report-check-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.2rem 1.25rem;
    margin: 0;
  }

  .report-check-list dt {
    font-weight: 800;
  }

  .report-check-list dd {
    margin: 0 0 0 0.3rem;
  }

  .report-check-list dt,
  .report-check-list dd {
    display: inline;
  }

  .report-warning,
  .report-warning-block {
    color: var(--red-text);
  }

  .report-map svg {
    width: 100%;
    height: auto;
  }

  .report-footer {
    margin-top: 1.5rem;
    padding-top: 0.5rem;
    border-top: 1px solid var(--rule);
    color: var(--muted);
    font-size: 0.75rem;
  }

  @media print {
    /* Ordinary page prints: never print buttons. */
    .action-group,
    .book-actions,
    .working-toggle,
    .results-actions,
    .interval-field {
      display: none !important;
    }

    /* Report prints: only the report article. */
    html.report-open .app-shell {
      display: block;
      height: auto;
      border: 0;
      background: none;
    }

    html.report-open .app-shell > :not(.report-workspace),
    html.report-open .report-toolbar {
      display: none !important;
    }

    html.report-open .report-workspace {
      overflow: visible;
      padding: 0;
      border: 0;
    }

    .report {
      max-width: none;
      padding: 0;
      border: 0;
      background: none;
    }

    .report-table {
      font-size: 8.5pt;
    }

    .report-table thead {
      display: table-header-group;
    }

    .report-table tr,
    .report-check,
    .report-book-header {
      break-inside: avoid;
    }

    .report-book-header,
    .report h2 {
      break-after: avoid;
    }

    .report-part--table,
    .report-map {
      break-before: page;
    }

    .report-map svg {
      max-height: 24cm;
    }
  }

  /*
   * Large grids (Task 25): skip layout of big blocks until they scroll into view.
   * Measured on a 40 × 40 grid: report layout 4.1 s → 28 ms, Step 5 2.1 s → 17 ms.
   * Screen only, so printing always renders everything.
   */
  @media screen {
    .report-book,
    .report-part--table,
    .report-map {
      content-visibility: auto;
      contain-intrinsic-size: auto 40rem;
    }

    .results-table-wrap,
    .spatial-grid-wrap {
      content-visibility: auto;
      contain-intrinsic-size: auto 28rem;
    }
  }

  /* Accessibility (Task 28). */
  .skip-link {
    position: absolute;
    z-index: 100;
    top: -4rem;
    left: 0.5rem;
    padding: 0.5rem 0.9rem;
    color: var(--paper-bright);
    background: var(--green-dark);
    font-weight: 800;
    text-decoration: none;
  }

  .skip-link:focus {
    top: 0.5rem;
  }

  main:focus {
    outline: none;
  }

  main:focus-visible,
  [role="region"]:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: -2px;
  }

  @media print {
    .skip-link {
      display: none;
    }
  }

  /* Field-book workspace (Task 31) --------------------------------------------- */
  .panel-title {
    display: flex;
    gap: var(--space-1);
    align-items: center;
    margin: 0 0 var(--space-3);
    padding: 0;
    border: 0;
    font-size: var(--text-lg);
    letter-spacing: normal;
    text-transform: none;
  }

  .setup-panel,
  .field-book-panel,
  .check-panel {
    padding: var(--space-4) var(--sheet-gutter);
  }

  .setup-card {
    display: grid;
    gap: var(--space-3);
    margin: 0 0 var(--space-3);
    padding: var(--space-4);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  .setup-card-title {
    margin: 0;
    font-size: var(--text-md);
  }

  .setup-card .form-section {
    padding: 0;
    border: 0;
  }

  .setup-card .form-section + .form-section {
    padding-top: var(--space-3);
    border-top: 1px solid var(--rule-light);
  }

  .setup-card .form-section legend {
    padding: 0 0 var(--space-2);
    color: var(--muted);
    font-size: var(--text-sm);
    font-weight: 600;
  }

  .setup-card-actions {
    display: grid;
    gap: var(--space-2);
  }

  .setup-card-actions > .btn,
  .setup-card .btn[data-action="edit-setup"] {
    width: 100%;
  }

  .setup-card-actions .action-group {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .setup-card-actions .action-group-label {
    grid-column: 1 / -1;
  }

  .label-row {
    display: flex;
    gap: var(--space-1);
    align-items: center;
  }

  .setup-summary {
    display: grid;
    grid-template-columns: max-content minmax(0, 1fr);
    gap: var(--space-1) var(--space-3);
    margin: 0;
    font-size: var(--text-sm);
  }

  .setup-summary dt {
    color: var(--muted);
  }

  .setup-summary dd {
    margin: 0;
    font-weight: 600;
  }

  .setup-apply {
    display: grid;
    gap: var(--space-2);
  }

  .setup-apply p {
    margin: 0;
    color: var(--muted);
  }

  .book-header {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    align-items: end;
    justify-content: space-between;
  }

  .book-title-row {
    display: flex;
    gap: var(--space-2);
    align-items: center;
  }

  .book-identity h2 {
    font-size: var(--text-xl);
  }

  .book-pickers {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
  }

  .book-pickers .field {
    min-width: 9rem;
  }

  .line-chips {
    display: flex;
    gap: var(--space-1);
    margin: var(--space-3) 0;
    padding-bottom: var(--space-1);
    overflow-x: auto;
    overscroll-behavior-inline: contain;
  }

  .line-chip {
    flex: 0 0 auto;
    gap: var(--space-1);
    min-height: 2.25rem;
    padding: 0 var(--space-3);
    border-color: var(--rule);
    border-radius: var(--radius-pill);
    font-family: var(--font-num);
    font-weight: 600;
  }

  .line-chip-mark {
    display: inline-flex;
    gap: 2px;
    align-items: center;
    font-size: var(--text-sm);
  }

  .line-chip-mark .icon {
    width: 0.95em;
    height: 0.95em;
  }

  .line-chip[data-status="PASS"] .line-chip-mark {
    color: var(--green);
  }

  .line-chip[data-status="OPEN"] .line-chip-mark {
    color: var(--amber-text);
  }

  .line-chip[data-status="FAIL"] .line-chip-mark,
  .line-chip[data-status="INVALID"] .line-chip-mark {
    color: var(--red-text);
  }

  .line-chip[aria-current="true"] {
    color: var(--surface);
    border-color: var(--green-dark);
    background: var(--green);
  }

  .line-chip[aria-current="true"] .line-chip-mark {
    color: var(--surface);
  }

  .book-actions {
    gap: var(--space-2) var(--space-4);
    margin: 0 0 var(--space-3);
  }

  .table-wrap {
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
  }

  .field-book-table {
    font-size: 0.9375rem;
  }

  .field-book-table th,
  .field-book-table td {
    height: 40px;
    padding: var(--space-1) var(--space-2);
    border-right: 0;
  }

  .field-book-table th {
    font-size: var(--text-sm);
    font-weight: 650;
    white-space: nowrap;
  }

  .field-book-table th .help-button {
    width: 1.5rem;
    min-height: 1.5rem;
    height: 1.5rem;
    vertical-align: middle;
  }

  .field-book-table td[data-computed="hi"],
  .field-book-table td[data-computed="elevation"] {
    background: color-mix(in srgb, var(--surface-sunken) 45%, transparent);
  }

  .field-book-table td[data-computed="elevation"] {
    font-weight: 600;
  }

  .field-book-table tr[data-row-type="tp"] td:first-child {
    color: var(--green-dark);
    font-weight: 700;
  }

  .field-book-table input {
    border-radius: var(--radius-control);
  }

  .field-book-table input[aria-invalid="true"] {
    padding-left: 1.6rem;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23a83725' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 3.5l9.5 16.5h-19z'/%3E%3Cpath d='M12 10v4.5M12 17.6v.1'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: 0.35rem center;
    background-size: 1rem;
  }

  .check-panel .check-list {
    padding: var(--space-3);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
  }

  .check-list dd {
    font-family: var(--font-num);
  }

  .check-working summary {
    color: var(--green-dark);
    font-weight: 600;
    cursor: pointer;
  }

  .check-working .working-list {
    margin-top: var(--space-2);
  }

  .check-note {
    border-radius: var(--radius-control);
    background: var(--red-tint);
  }

  .confirm-dialog {
    width: min(28rem, calc(100vw - 2rem));
    padding: var(--space-6);
    border: 1px solid var(--rule);
    border-radius: var(--radius-panel);
    color: var(--ink);
    background: var(--surface);
    box-shadow: var(--elev-pop);
  }

  .confirm-dialog::backdrop {
    background: rgb(23 26 23 / 0.35);
  }

  .confirm-dialog h2 {
    margin: 0 0 var(--space-2);
    font-size: var(--text-lg);
  }

  .confirm-dialog p {
    margin: 0 0 var(--space-5);
    color: var(--muted);
  }

  .confirm-dialog-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    justify-content: flex-end;
  }

  /* Chips replace the pickers from tablet width up; phones keep the compact selects. */
  @media (min-width: 761px) {
    .book-pickers {
      display: none;
    }
  }

  @media (max-width: 760px) {
    .line-chips {
      display: none;
    }
  }

  /* Results, map and report (Task 32) ------------------------------------------ */
  .tables-workspace,
  .map-workspace,
  .report-workspace {
    padding: var(--space-4) var(--sheet-gutter) var(--space-5);
    border-bottom: 0;
  }

  .results-header,
  .map-header {
    gap: var(--space-4);
    margin-bottom: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  .results-heading h2,
  .map-heading h2 {
    display: flex;
    gap: var(--space-1);
    align-items: center;
    margin: 0;
    font-size: var(--text-xl);
    text-transform: none;
  }

  .results-heading p,
  .map-heading p {
    margin: var(--space-1) 0 0;
    color: var(--muted);
    font-size: var(--text-md);
  }

  .results-actions {
    flex-direction: row;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-4);
    align-items: center;
    justify-content: space-between;
    min-height: 0;
    padding: 0 0 var(--space-3);
  }

  .results-actions button {
    width: auto;
  }

  .results-count {
    color: var(--muted);
    font-weight: 600;
  }

  .spatial-section,
  .results-table-wrap {
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
  }

  .spatial-section {
    padding: var(--space-3) var(--space-4);
  }

  .results-subheading {
    margin: var(--space-4) 0 var(--space-2);
    padding: 0;
    border: 0;
    font-size: var(--text-lg);
  }

  .results-table th,
  .results-table td {
    padding: var(--space-1) var(--space-3);
    border-right: 0;
  }

  .map-view-tabs {
    display: inline-grid;
    grid-template-columns: repeat(2, auto);
    gap: 2px;
    width: fit-content;
    margin: 0 0 var(--space-3);
    padding: 3px;
    border: 1px solid var(--rule);
    border-radius: calc(var(--radius-control) + 3px);
    background: var(--surface-sunken);
  }

  .map-view-tab,
  .map-view-tab + .map-view-tab {
    min-height: 2.25rem;
    padding: 0 var(--space-5);
    border: 0;
    border-radius: var(--radius-control);
    color: var(--muted);
    background: transparent;
    font-weight: 600;
  }

  .map-view-tab[aria-selected="true"] {
    color: var(--ink);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  .map-toolbar,
  .terrain-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-4);
    align-items: center;
    justify-content: space-between;
    padding: 0 0 var(--space-3);
  }

  .map-controls,
  .terrain-actions {
    flex-direction: row;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-3);
    align-items: center;
  }

  .map-controls button,
  .terrain-actions button {
    width: auto;
  }

  .map-summary {
    color: var(--muted);
    font-size: var(--text-sm);
    font-weight: 500;
  }

  .map-canvas {
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
  }

  .map-legend {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin: 0;
    padding: var(--space-3) 0 0;
    list-style: none;
  }

  .map-legend li {
    display: inline-flex;
    gap: var(--space-2);
    align-items: center;
    padding: 0.2rem 0.75rem;
    border-radius: var(--radius-pill);
    background: var(--surface-sunken);
    font-size: var(--text-sm);
  }

  .terrain-settings {
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
  }

  .report-toolbar {
    padding: var(--space-4) var(--space-5);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  .report {
    border-color: var(--rule-light);
    border-radius: var(--radius-panel);
    box-shadow: var(--elev-1);
  }

  .report-table th,
  .report-table td {
    border-color: var(--rule-light);
  }

  @media (hover: hover) and (pointer: fine) {
    .map-view-tab[aria-selected="true"]:hover {
      color: var(--ink);
      background: var(--surface);
    }

    .map-view-tab:not([aria-selected="true"]):hover {
      color: var(--ink);
      background: color-mix(in srgb, var(--surface) 60%, transparent);
    }
  }

  @media print {
    .report,
    .report-toolbar {
      border: 0;
      box-shadow: none;
    }
  }

  /* Welcome / start screen (Task 33) -------------------------------------------- */
  .welcome-workspace {
    min-height: 0;
    overflow: auto;
    padding: var(--space-6) var(--sheet-gutter) var(--space-7);
  }

  .welcome-intro,
  .welcome-choices,
  .welcome-steps,
  .welcome-footer {
    max-width: 68rem;
    margin-inline: auto;
  }

  .welcome-intro {
    margin-bottom: var(--space-6);
  }

  .welcome-intro h2 {
    max-width: 30ch;
    margin: 0 0 var(--space-2);
    font-size: clamp(1.5rem, 3vw, var(--text-2xl));
  }

  .welcome-intro h2:focus {
    outline: none;
  }

  .welcome-intro p {
    max-width: 60ch;
    margin: 0;
    color: var(--muted);
    font-size: var(--text-lg);
  }

  .welcome-choices {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
    gap: var(--space-4);
    margin-bottom: var(--space-7);
  }

  .welcome-choice {
    display: grid;
    grid-template-rows: auto auto 1fr auto;
    gap: var(--space-2);
    padding: var(--space-5);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  .welcome-choice--primary {
    border-color: var(--green);
    box-shadow: 0 0 0 1px var(--green), var(--elev-1);
  }

  .welcome-choice h3 {
    margin: var(--space-1) 0 0;
    font-size: var(--text-lg);
  }

  .welcome-choice p {
    margin: 0;
    color: var(--muted);
  }

  .welcome-choice-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: var(--space-2);
  }

  .welcome-steps h3 {
    margin: 0 0 var(--space-3);
    font-size: var(--text-lg);
  }

  .welcome-step-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
    gap: var(--space-4);
    margin: 0 0 var(--space-6);
    padding: 0;
    list-style: none;
    counter-reset: welcome-step;
  }

  .welcome-step-list li {
    display: grid;
    gap: var(--space-2);
    align-content: start;
    padding: var(--space-4);
    border-top: 3px solid var(--green);
    border-radius: 0 0 var(--radius-panel) var(--radius-panel);
    background: color-mix(in srgb, var(--surface) 70%, transparent);
    counter-increment: welcome-step;
  }

  .welcome-step-list strong::before {
    content: counter(welcome-step) ". ";
    color: var(--green-dark);
  }

  .welcome-step-list p {
    margin: 0;
    color: var(--muted);
    font-size: var(--text-sm);
  }

  .welcome-footer {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    align-items: center;
    justify-content: space-between;
    padding-top: var(--space-4);
    border-top: 1px solid var(--rule-light);
    color: var(--muted);
    font-size: var(--text-sm);
  }

  .welcome-footer p {
    margin: 0;
  }

  @media print {
    .welcome-workspace {
      display: none;
    }
  }
}

/* Student guide page, guide.html (Task 34) ------------------------------------- */
@layer views {
  .guide-shell {
    display: grid;
    grid-template-rows: auto 1fr auto;
    min-height: 100vh;
    min-height: 100dvh;
    max-width: 120rem;
    margin: 0 auto;
  }

  .guide-course {
    margin: 0;
    color: var(--muted);
    font-size: var(--text-sm);
  }

  .guide-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2) var(--space-3);
    align-items: center;
  }

  a.btn {
    text-decoration: none;
  }

  .guide-layout {
    display: grid;
    gap: var(--space-6);
    width: 100%;
    max-width: 76rem;
    margin: 0 auto;
    padding: var(--space-6) var(--sheet-gutter) var(--space-8);
  }

  .guide-toc-details {
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  /* list-item keeps the disclosure triangle; the padding keeps a 44 px target. */
  .guide-toc summary {
    display: list-item;
    padding: 0.65rem 0;
    font-weight: 650;
    cursor: pointer;
  }

  .guide-toc ol {
    display: grid;
    gap: var(--space-1);
    margin: 0;
    padding-left: 1.5rem;
  }

  .guide-toc a,
  .guide-content a {
    color: var(--green-dark);
    text-underline-offset: 0.18em;
  }

  .guide-toc a {
    display: inline-block;
    padding: var(--space-1) 0;
  }

  .guide-content {
    min-width: 0;
    max-width: 48rem;
  }

  .guide-content:focus {
    outline: none;
  }

  .guide-intro h1 {
    margin: 0 0 var(--space-2);
    font-size: clamp(1.75rem, 4vw, 2.25rem);
  }

  .guide-lead {
    margin: 0 0 var(--space-6);
    color: var(--muted);
    font-size: var(--text-lg);
  }

  .guide-name-line {
    display: none;
  }

  .guide-section {
    margin-bottom: var(--space-8);
    scroll-margin-top: var(--space-4);
  }

  .guide-section h2 {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    margin: 0 0 var(--space-4);
    padding-bottom: var(--space-2);
    border-bottom: 2px solid var(--green);
    font-size: var(--text-xl);
  }

  .guide-number {
    display: inline-grid;
    flex: 0 0 auto;
    place-items: center;
    width: 2rem;
    height: 2rem;
    border-radius: 50%;
    color: var(--surface);
    background: var(--green);
    font-size: var(--text-md);
  }

  .guide-section h3 {
    margin: var(--space-6) 0 var(--space-2);
    font-size: var(--text-lg);
  }

  .guide-section ul,
  .guide-section ol {
    display: grid;
    gap: var(--space-2);
    padding-left: 1.4rem;
  }

  .guide-section code {
    padding: 0 0.25em;
    border-radius: 4px;
    background: var(--surface-sunken);
    font-family: var(--font-num);
    font-size: 0.92em;
    overflow-wrap: anywhere;
  }

  .guide-callout {
    margin-bottom: var(--space-4);
    padding: var(--space-4);
    border-left: 4px solid var(--green);
    border-radius: 0 var(--radius-panel) var(--radius-panel) 0;
    background: var(--green-tint);
  }

  .guide-callout p {
    margin: 0;
  }

  .guide-callout-title {
    font-weight: 700;
  }

  .guide-formula {
    display: inline-block;
    padding: 0.05em 0.4em;
    border-radius: 4px;
    background: var(--surface);
    box-shadow: inset 0 0 0 1px var(--rule-light);
    font-family: var(--font-num);
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
  }

  .guide-formula--block {
    display: block;
    padding: var(--space-3) var(--space-4);
  }

  .guide-working li {
    line-height: 2;
  }

  .guide-working .guide-formula {
    margin-right: var(--space-2);
  }

  .guide-table-wrap {
    overflow-x: auto;
    margin: var(--space-3) 0;
  }

  .guide-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    font-size: var(--text-sm);
  }

  .guide-table caption {
    padding-bottom: var(--space-2);
    color: var(--muted);
    text-align: left;
  }

  .guide-table th,
  .guide-table td {
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--rule-light);
    text-align: right;
    white-space: nowrap;
  }

  .guide-table th {
    background: var(--table-head-bg);
  }

  .guide-table th:first-child,
  .guide-table td:first-child,
  .guide-table th:last-child,
  .guide-table td:last-child {
    text-align: left;
  }

  /* HI and Elevation are calculated, shaded as in the app. */
  .guide-table td:nth-child(3),
  .guide-table td:nth-child(6) {
    background: var(--green-tint);
  }

  .guide-figure {
    max-width: 30rem;
    margin: var(--space-4) 0;
    padding: var(--space-3);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
  }

  .guide-figure svg {
    display: block;
    width: 100%;
    height: auto;
    font-family: var(--font-num);
    font-size: 13px;
  }

  .guide-figure text {
    fill: var(--ink);
  }

  .guide-fig-ground {
    stroke: var(--border-strong);
    stroke-width: 1.5;
    stroke-dasharray: 4 4;
  }

  .guide-fig-slope {
    stroke: var(--ink);
    stroke-width: 2;
  }

  .guide-fig-contour {
    stroke: var(--green);
    stroke-width: 2;
  }

  .guide-fig-node {
    fill: var(--ink);
  }

  .guide-fig-cross {
    fill: var(--green);
  }

  .guide-exercise {
    margin-bottom: var(--space-4);
    padding: var(--space-4) var(--space-5);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--surface);
    box-shadow: var(--elev-1);
  }

  .guide-exercise h3 {
    margin-top: 0;
  }

  .guide-answer summary {
    display: list-item;
    padding: 0.65rem 0;
    color: var(--green-dark);
    font-weight: 650;
    cursor: pointer;
  }

  .guide-answer[open] {
    padding: 0 var(--space-3) var(--space-1);
    border-radius: var(--radius-control);
    background: var(--green-tint);
  }

  .guide-answer-space {
    display: none;
  }

  .guide-faq dt,
  .guide-glossary dt {
    margin-top: var(--space-4);
    font-weight: 700;
  }

  .guide-faq dd,
  .guide-glossary dd {
    margin: var(--space-1) 0 0;
  }

  .guide-glossary dd p {
    margin: 0 0 var(--space-1);
  }

  .guide-glossary dd .guide-formula {
    display: table;
  }

  @media (min-width: 901px) {
    .guide-layout {
      grid-template-columns: 15rem minmax(0, 1fr);
      align-items: start;
    }

    .guide-toc {
      position: sticky;
      top: var(--space-4);
    }

    /* On wide screens the contents always stay open. */
    .guide-toc summary {
      pointer-events: none;
    }

    .guide-toc summary::marker {
      content: "";
    }
  }

  /* Print: an A4 lab handout. Answers print only when the switch is on. */
  @media print {
    @page {
      size: A4;
      margin: 16mm 15mm;
    }

    html:has(.guide-page),
    .guide-page {
      background: #fff;
    }

    .guide-page {
      font-size: 11pt;
    }

    .guide-toc,
    .guide-actions,
    .guide-answer summary {
      display: none;
    }

    .guide-shell,
    .guide-layout {
      display: block;
      min-height: 0;
      padding: 0;
    }

    .guide-header {
      padding: 0 0 4mm;
      border-bottom: 1px solid #000;
    }

    .guide-page .guide-footer {
      display: block;
      margin-top: 6mm;
      border-top: 1px solid #000;
      color: #000;
    }

    .guide-content {
      max-width: none;
    }

    .guide-name-line {
      display: block;
      margin: 0 0 6mm;
    }

    .guide-blank {
      display: inline-block;
      width: 60mm;
      margin-right: 6mm;
      border-bottom: 1px solid #000;
    }

    .guide-blank--short {
      width: 30mm;
    }

    .guide-section {
      margin-bottom: 0;
      break-before: page;
    }

    #opening {
      break-before: auto;
    }

    .guide-section h2,
    .guide-section h3 {
      break-after: avoid;
    }

    .guide-exercise,
    .guide-figure,
    .guide-table-wrap,
    .guide-formula--block,
    .guide-callout,
    .guide-faq dd {
      break-inside: avoid;
      box-shadow: none;
    }

    .guide-number {
      color: #000;
      background: none;
      box-shadow: inset 0 0 0 1.5px #000;
    }

    /* The web address already shows its URL as its text. */
    .guide-section a[href^="http"]:not([data-site-link] a)::after {
      content: " (" attr(href) ")";
      font-size: 0.9em;
    }

    .guide-answer-space {
      display: block;
      height: 32mm;
      margin-top: 3mm;
      border: 1px dashed #777;
      border-radius: 4px;
    }

    .guide-answer {
      display: none;
    }

    .guide-print-answers .guide-answer {
      display: block;
      padding: 0;
      background: none;
    }

    .guide-print-answers .guide-answer-space {
      display: none;
    }
  }

  /* Responsive & UX polish (Task 37): sticky columns, scroll cues, container queries, mobile ergonomics */
  @media (min-width: 1101px) and (max-width: 1359px) {
    .step-button {
      grid-template-columns: 1.75rem minmax(0, 1fr) auto;
      column-gap: var(--space-1);
      padding: var(--space-2);
    }

    .step-number {
      width: 1.75rem;
      height: 1.75rem;
    }

    .step-label {
      font-size: var(--text-sm);
      overflow-wrap: normal;
    }
  }

  @media (min-width: 1360px) {
    .workspace {
      grid-template-columns: minmax(17rem, 20%) minmax(0, 1fr) minmax(17rem, 20%);
    }
  }

  @container setup (max-width: 17.5rem) {
    .setup-panel .field-grid,
    .setup-panel .form-section:nth-of-type(3) .field-grid {
      grid-template-columns: minmax(0, 1fr);
    }
  }

  /* Sticky first column (Station / Node / Edge) keeps row identity visible during horizontal scroll. */
  .field-book-table th:first-child,
  .field-book-table td:first-child,
  .results-table--sticky-first th:first-child,
  .results-table--sticky-first td:first-child {
    position: sticky;
    inset-inline-start: 0;
  }

  .field-book-table thead th:first-child,
  .results-table--sticky-first thead th:first-child {
    z-index: 3;
    border-right: 1px solid var(--table-head-border);
    background: var(--table-head-bg);
  }

  .field-book-table tbody td:first-child,
  .results-table--sticky-first tbody td:first-child {
    z-index: 1;
    border-right: 1px solid var(--rule-light);
    background: var(--surface);
  }

  .field-book-table tr[data-selected="true"] td:first-child {
    background: color-mix(in srgb, var(--green) 9%, var(--surface));
  }

  @media (hover: hover) and (pointer: fine) {
    .results-table--sticky-first tbody tr:hover td:first-child {
      background: color-mix(in srgb, var(--green) 8%, var(--surface));
    }
  }

  /* Horizontal scroll shadows for wide tables (works in all browsers, never fades cell digits). */
  .table-wrap,
  .results-table-wrap,
  .guide-table-wrap {
    background:
      linear-gradient(to right, var(--surface) 30%, transparent) left / 2.25rem 100% no-repeat local,
      linear-gradient(to left, var(--surface) 30%, transparent) right / 2.25rem 100% no-repeat local,
      radial-gradient(farthest-side at 0 50%, rgb(23 26 23 / 0.14), transparent) left / 0.75rem 100% no-repeat scroll,
      radial-gradient(farthest-side at 100% 50%, rgb(23 26 23 / 0.14), transparent) right / 0.75rem 100% no-repeat scroll,
      var(--surface);
  }

  .guide-table-wrap {
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    overscroll-behavior-inline: contain;
  }

  /* Horizontal edge fades for the line-chips strip (scroll-driven + IntersectionObserver fallback). */
  .line-chips {
    --fade-size: 2rem;
    --fade-dir: right;
    --fade-start-pos: 0;
    --fade-end-pos: 100%;
    scroll-padding-inline: var(--fade-size);
    mask:
      linear-gradient(black, black) no-clip subtract,
      linear-gradient(to var(--fade-dir), black, transparent) var(--fade-start-pos) 0 / var(--fade-start) 100% no-repeat no-clip,
      linear-gradient(to var(--fade-dir), transparent, black) var(--fade-end-pos) 0 / var(--fade-end) 100% no-repeat no-clip;
  }

  .line-chips.more-start {
    --fade-start: var(--fade-size);
  }

  .line-chips.more-end {
    --fade-end: var(--fade-size);
  }

  @media (prefers-reduced-motion: no-preference) {
    .line-chips {
      animation: reveal-fade-start linear both, hide-fade-end linear both;
      animation-timeline: scroll(self inline);
      animation-range: 0 var(--fade-size), calc(100% - var(--fade-size)) 100%;
    }

    @keyframes reveal-fade-start {
      to {
        --fade-start: var(--fade-size);
      }
    }

    @keyframes hide-fade-end {
      from {
        --fade-end: var(--fade-size);
      }
    }
  }

  /* Compact, calmer mobile header and toolbar ergonomics (320–760px). */
  @media (max-width: 760px) {
    .app-header {
      gap: var(--space-2) var(--space-3);
      padding: var(--space-3) var(--sheet-gutter);
    }

    .brand {
      flex: 1 1 100%;
    }

    .brand-mark {
      width: 2.15rem;
      height: 2.15rem;
    }

    .brand-title {
      font-size: var(--text-md);
    }

    .header-meta {
      flex: 1 1 auto;
      gap: var(--space-1) var(--space-2);
    }

    .header-actions {
      flex-wrap: wrap;
      justify-content: flex-end;
    }

    .header-actions [data-action="open-report"] {
      margin-left: var(--space-1);
    }

    .book-pickers {
      width: 100%;
    }

    .book-pickers .field {
      flex: 1 1 8rem;
      min-width: 0;
    }

    .results-header,
    .map-header {
      padding: var(--space-3) var(--space-4);
    }

    .map-view-tabs {
      width: 100%;
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .map-view-tab,
    .map-view-tab + .map-view-tab {
      padding: 0 var(--space-3);
    }

    .guide-header .guide-actions {
      width: 100%;
      justify-content: flex-start;
    }

    .guide-layout {
      gap: var(--space-4);
      padding: var(--space-4) var(--sheet-gutter) var(--space-6);
    }

    .guide-exercise {
      padding: var(--space-3) var(--space-4);
    }
  }

  @media (max-width: 520px) {
    .header-actions {
      width: 100%;
      justify-content: space-between;
    }

    .results-actions,
    .map-toolbar,
    .terrain-toolbar {
      align-items: stretch;
    }

    .results-actions .menu,
    .results-actions .menu-trigger,
    .map-controls .menu,
    .map-controls .menu-trigger,
    .map-controls .map-scale-button,
    .map-controls .map-fullscreen-button,
    .terrain-actions .btn,
    .terrain-actions .menu,
    .terrain-actions .menu-trigger,
    .terrain-actions .map-fullscreen-button {
      flex: 1 1 auto;
    }

    .welcome-choice-actions .btn {
      width: 100%;
    }

    .guide-actions .btn {
      flex: 1 1 10rem;
    }
  }

  /* Monochrome Precision Minimalist overhaul (Task 38): flat hairlines, slim chrome, anti-slop */
  .badge {
    border-radius: var(--radius-control);
    font-weight: 600;
    letter-spacing: 0.03em;
  }

  .app-header {
    padding: var(--space-2) var(--sheet-gutter);
    gap: var(--space-2) var(--space-4);
  }

  .brand-mark {
    width: 2rem;
    height: 2rem;
    border-radius: var(--radius-control);
    color: var(--surface);
    background: var(--accent);
  }

  .brand-mark .icon {
    width: 1.15rem;
    height: 1.15rem;
  }

  .brand-title {
    font-size: var(--text-md);
    font-weight: 650;
    letter-spacing: -0.015em;
  }

  .meta-chip {
    padding: 0;
    border-radius: 0;
    color: var(--muted);
    background: transparent;
  }

  .step-nav {
    padding: 0 var(--sheet-gutter);
    background: var(--paper);
  }

  .workflow-menu-toggle {
    margin: var(--space-2) 0;
    border-color: var(--rule);
    background: var(--surface);
  }

  .step-button {
    border-radius: var(--radius-control);
  }

  .step-number {
    width: 1.5rem;
    height: 1.5rem;
    border: 1px solid var(--rule);
    color: var(--muted);
    background: var(--surface);
    font-family: var(--font-num);
    font-size: var(--text-xs);
    font-weight: 600;
  }

  .step-button[data-progress="done"] .step-number {
    border-color: var(--ink);
    color: var(--ink);
  }

  .step-button[aria-current="step"] {
    border-color: var(--ink);
    background: var(--surface);
  }

  .step-button[aria-current="step"] .step-number {
    color: var(--surface);
    border-color: var(--ink);
    background: var(--ink);
  }

  @media (min-width: 1101px) {
    .step-list {
      gap: 0;
    }

    .step-button {
      grid-template-areas: "number label progress";
      grid-template-columns: auto minmax(0, 1fr) auto;
      min-height: 2.75rem;
      padding: var(--space-2) var(--space-2);
      border: 0;
      border-bottom: 2px solid transparent;
      border-radius: 0;
      background: transparent;
    }

    .step-detail {
      display: none;
    }

    .step-label {
      font-size: var(--text-sm);
      font-weight: 500;
      color: var(--muted);
    }

    .step-button[aria-current="step"] {
      border-color: transparent;
      border-bottom-color: var(--ink);
      background: transparent;
    }

    .step-button[aria-current="step"] .step-label {
      color: var(--ink);
      font-weight: 650;
    }
  }

  @media (hover: hover) and (pointer: fine) {
    .step-button:hover:not([aria-current="step"]) {
      border-color: transparent;
      border-bottom-color: var(--rule);
      background: var(--accent-tint);
    }

    .step-button:hover:not([aria-current="step"]) .step-label {
      color: var(--ink);
    }

    .step-button[aria-current="step"]:hover {
      border-color: transparent;
      border-bottom-color: var(--ink);
      background: transparent;
    }
  }

  /* Flat architectural workspace panes: setup & check on quiet alabaster, field book on pure white. */
  .setup-panel,
  .check-panel {
    background: var(--paper);
  }

  .field-book-panel {
    background: var(--surface);
  }

  .setup-card {
    margin: 0;
    padding: var(--space-3) 0;
    border: 0;
    border-top: 1px solid var(--rule-light);
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  .setup-card:first-child {
    padding-top: 0;
    border-top: 0;
  }

  .setup-card-title {
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .line-chip {
    min-height: 2rem;
    padding: 0 var(--space-2);
    border-color: var(--rule-light);
    border-radius: var(--radius-control);
    background: var(--paper);
    font-size: var(--text-sm);
  }

  .line-chip[aria-current="true"] {
    color: var(--surface);
    border-color: var(--ink);
    background: var(--ink);
  }

  .field-book-table tr[data-selected="true"] td {
    background-color: rgb(15 23 42 / 0.045);
  }

  .field-book-table tr[data-selected="true"] td:first-child {
    background: #f3f5f8;
  }

  .field-book-table tr[data-row-type="tp"] td {
    border-top: 1.5px dashed var(--border-strong);
  }

  .field-book-table tr[data-row-type="tp"] td:first-child {
    color: var(--ink);
  }

  .field-book-table td[data-computed="hi"],
  .field-book-table td[data-computed="elevation"] {
    background: var(--paper);
  }

  .field-book-table .working-cell,
  .results-table .working-cell,
  .working-list {
    color: var(--ink);
  }

  .check-panel .check-list {
    border-radius: var(--radius-control);
    border-color: var(--rule-light);
  }

  .check-working summary {
    color: var(--ink);
  }

  .check-note {
    border-style: solid;
    border-color: color-mix(in oklab, var(--red) 35%, var(--surface));
  }

  /* Flat results, map, report, and empty-state headers (zero box-in-a-box card soup). */
  .tables-workspace,
  .map-workspace,
  .report-workspace {
    background: var(--surface);
  }

  .results-header,
  .map-header,
  .report-toolbar {
    padding: 0 0 var(--space-3);
    border: 0;
    border-bottom: 1px solid var(--rule-light);
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  .results-heading h2,
  .map-heading h2 {
    letter-spacing: -0.015em;
  }

  .spatial-section,
  .results-table-wrap,
  .table-wrap,
  .map-canvas,
  .terrain-settings,
  .report {
    border-radius: var(--radius-panel);
    box-shadow: none;
  }

  .spatial-cell--horizontal,
  .spatial-cell--vertical {
    background: var(--surface-sunken);
  }

  .map-view-tabs {
    border-color: var(--rule-light);
    border-radius: var(--radius-control);
    background: var(--surface-sunken);
  }

  .map-view-tab[aria-selected="true"] {
    border: 1px solid var(--rule);
    box-shadow: none;
  }

  .map-legend li {
    padding: 0.1rem 0;
    border-radius: 0;
    background: transparent;
    color: var(--muted);
  }

  .terrain-range input {
    accent-color: var(--ink);
  }

  .terrain-range output {
    color: var(--ink);
  }

  .empty-state {
    border-color: var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--paper);
    box-shadow: none;
  }

  /* Minimalist editorial Welcome screen (zero icon circles, zero colored top bars). */
  .welcome-workspace {
    background: var(--surface);
  }

  .welcome-intro h2 {
    letter-spacing: -0.025em;
    font-weight: 650;
  }

  .welcome-choice {
    grid-template-rows: auto 1fr auto;
    padding: var(--space-5);
    border: 1px solid var(--rule-light);
    border-radius: var(--radius-panel);
    background: var(--paper);
    box-shadow: none;
  }

  .welcome-choice--primary {
    border-color: var(--ink);
    background: var(--surface);
    box-shadow: none;
  }

  .welcome-choice h3 {
    margin: 0;
    font-size: var(--text-md);
    font-weight: 650;
  }

  .welcome-steps h3 {
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
  }

  .welcome-step-list li {
    padding: var(--space-3) 0 0;
    border-top: 1px solid var(--rule);
    border-radius: 0;
    background: transparent;
  }

  .welcome-step-list strong::before {
    content: "0" counter(welcome-step) "  ";
    color: var(--muted);
    font-family: var(--font-num);
    font-weight: 500;
  }

  /* Student guide (guide.html) aligned to Monochrome Precision Minimalist. */
  .guide-page {
    background: var(--surface);
  }

  .guide-toc-details,
  .guide-exercise,
  .guide-figure {
    box-shadow: none;
    background: var(--paper);
  }

  .guide-toc a,
  .guide-content a {
    color: var(--ink);
  }

  .guide-section h2 {
    border-bottom: 1px solid var(--ink);
  }

  .guide-number {
    width: 1.75rem;
    height: 1.75rem;
    border-radius: var(--radius-control);
    color: var(--surface);
    background: var(--ink);
    font-family: var(--font-num);
    font-size: var(--text-sm);
  }

  .guide-callout {
    border-left: 3px solid var(--ink);
    border-radius: 0 var(--radius-control) var(--radius-control) 0;
    background: var(--surface-sunken);
  }

  .guide-table td:nth-child(3),
  .guide-table td:nth-child(6) {
    background: var(--paper);
  }

  .guide-answer summary {
    color: var(--ink);
  }

  .guide-answer[open] {
    border: 1px solid var(--rule-light);
    background: var(--surface);
  }

  /* Forced Colors Mode (Windows High Contrast) fallbacks. */
  @media (forced-colors: active) {
    .step-button[aria-current="step"],
    .line-chip[aria-current="true"],
    .map-view-tab[aria-selected="true"],
    .welcome-choice--primary {
      border: 2px solid Highlight;
    }

    .field-book-table tr[data-selected="true"] td {
      outline: 1px solid Highlight;
    }

    .working-toggle input[type="checkbox"]:checked::before,
    .map-toggle input[type="checkbox"]:checked::before {
      background: HighlightText;
    }
  }
}
