/**
 * app.css — School Data application shell
 *
 * CONTENTS
 * --------
 *  1. Design tokens (:root)
 *  2. Viewport-height and motion adaptations
 *  3. Reset and base typography
 *  4. Accessibility utilities (skip links, sr-only, focus ring)
 *  5. App layout grid
 *  6. Top bar
 *  7. Left tool rail
 *  8. Working area
 *  9. Bottom bar and file actions
 * 10. Popup menus
 * 11. Form controls
 * 12. Responsive breakpoints
 * 13. Dark mode, high contrast, forced colours, larger text
 *
 * ACCESSIBILITY DECISIONS MADE IN THIS FILE
 * -----------------------------------------
 * Focus appearance (WCAG 2.2 SC 2.4.11 / 2.4.13)
 *   Every focusable element receives a 3px solid outline in --focus plus a 2px
 *   offset, giving a minimum 2px-thick indicator with an area well over the
 *   required 2 CSS px perimeter, and a contrast ratio above 3:1 against both
 *   the light and dark surfaces it can appear on.  :focus-visible is used so a
 *   mouse click does not leave a ring behind, but :focus is kept as a fallback
 *   for engines without :focus-visible.
 *
 * Target size (SC 2.5.8, and the stricter 2.5.5 where practical)
 *   --min-touch-target is 44px and is applied as a min-width/min-height to
 *   every button, not just the obvious ones.  Small inline controls inside the
 *   data table keep 44px by padding rather than by shrinking.
 *
 * Colour is never the only signal (SC 1.4.1)
 *   Chart series carry a colour *and* a pattern *and* a printed number.  The
 *   unsaved-changes indicator is a dot *and* a text label read by screen
 *   readers.  Errors are red *and* prefixed with the word "Problem".
 *
 * Reflow (SC 1.4.10)
 *   The layout collapses to a single column at 480px so the app works at 320px
 *   width and at 400% zoom without two-dimensional scrolling.  The tool rail
 *   becomes a horizontal strip rather than disappearing.
 *
 * Text spacing (SC 1.4.12)
 *   No fixed heights on text containers; line-height 1.5 and paragraph spacing
 *   in ems, so a user stylesheet can increase spacing without clipping.
 *
 * Motion (SC 2.3.3)
 *   All transitions route through --transition-* tokens which collapse to
 *   0.01ms under prefers-reduced-motion.
 */

/* ══ 1. Design tokens ═══════════════════════════════════════════════════════ */
:root {
  /* Surfaces and ink.  Teal accent distinguishes School Data from School
     Draw's indigo while keeping the same tonal weight. */
  --bg: #f4f7f9;
  --panel: #ffffff;
  --panel-2: #eef2f6;
  --ink: #16202b;
  --ink-soft: #3d4a59;
  --muted: #55647a;
  --line: #ccd6e0;
  --line-strong: #93a3b5;

  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #ccfbf1;
  --accent-2: #4338ca;

  --danger: #b3261e;
  --danger-soft: #fdecea;
  --success: #0b6e4f;
  --success-soft: #e7f6ef;
  --warning: #8a4b00;
  --warning-soft: #fdf3e3;

  /* Amber focus ring: >3:1 against white, near-white, dark navy and the teal
     accent, so one token works on every surface in the app. */
  --focus: #b45309;
  --focus-ring-width: 3px;
  --focus-ring-offset: 2px;

  --topbar-h: 56px;
  --bottombar-h: 72px;
  --side-w: 84px;
  --side-w-expanded: 176px;

  --radius: 12px;
  --radius-sm: 8px;
  --min-touch-target: 44px;

  --vhfix: 100vh;

  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif;
  --font-size: 16px;
  --font-size-sm: 0.875rem;
  --font-size-lg: 1.125rem;

  --transition-fast: 0.14s ease;
  --transition-normal: 0.24s ease;

  --z-menu: 1000;
  --z-modal: 2000;
  --z-toast: 3000;
  --z-tooltip: 4000;

  /* Written at runtime by core.js._sizing */
  --dynamic-tool-size: 56px;
  --dynamic-tool-icon: 34px;
  --dynamic-tool-gap: 9px;

  --shadow-1: 0 1px 2px rgba(16, 32, 43, 0.08), 0 1px 3px rgba(16, 32, 43, 0.06);
  --shadow-2: 0 4px 12px rgba(16, 32, 43, 0.12);
  --shadow-3: 0 12px 32px rgba(16, 32, 43, 0.22);
}

/* ══ 2. Viewport height and motion ═════════════════════════════════════════ */
/* 100vh is wrong on mobile browsers whose address bar overlays the viewport;
   svh/dvh fix it where supported without breaking older engines. */
@supports (height: 100svh) {
  :root {
    --vhfix: 100svh;
  }
}
@supports (height: 100dvh) {
  :root {
    --vhfix: 100dvh;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --transition-fast: 0.01ms;
    --transition-normal: 0.01ms;
  }
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ══ 3. Reset and base ═════════════════════════════════════════════════════ */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: var(--font-size);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.5;
  overflow: hidden;
}

h1,
h2,
h3,
h4 {
  margin: 0 0 0.5em;
  line-height: 1.25;
  font-weight: 700;
}

p {
  margin: 0 0 0.75em;
}

p:last-child {
  margin-bottom: 0;
}

img,
svg {
  max-width: 100%;
}

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

/* ══ 4. Accessibility utilities ════════════════════════════════════════════ */

/* Visually hidden but available to assistive technology.  clip-path rather
   than display:none, and 1px rather than 0, because several screen readers
   skip zero-area and display:none content. */
.sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.skip-links {
  position: absolute;
  top: 0;
  left: 0;
  z-index: var(--z-toast);
}

.skip-link {
  position: absolute;
  top: -1000px;
  left: 8px;
  padding: 0.75rem 1.25rem;
  background: var(--panel);
  color: var(--ink);
  border: 3px solid var(--focus);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-3);
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

/* On focus the link becomes visible and is NOT obscured by any sticky element,
   which is what SC 2.4.11 Focus Not Obscured requires. */
.skip-link:focus {
  top: 8px;
}

.skip-links a:nth-of-type(2):focus {
  top: 8px;
  left: 260px;
}

/* One focus treatment for the whole app. */
:where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: var(--focus-ring-width) solid var(--focus);
  outline-offset: var(--focus-ring-offset);
  /* A second, inner light ring guarantees contrast when the focused control
     sits on a dark background of a similar hue to the amber ring. */
  box-shadow: 0 0 0 calc(var(--focus-ring-width) + var(--focus-ring-offset))
    rgba(255, 255, 255, 0.9);
  border-radius: var(--radius-sm);
}

/* Fallback for engines without :focus-visible support. */
@supports not selector(:focus-visible) {
  :where(a, button, input, select, textarea, [tabindex]):focus {
    outline: var(--focus-ring-width) solid var(--focus);
    outline-offset: var(--focus-ring-offset);
  }
}

.svg-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.noscript-notice {
  max-width: 40rem;
  margin: 3rem auto;
  padding: 1.5rem;
  background: var(--panel);
  border: 2px solid var(--danger);
  border-radius: var(--radius);
}

/* ══ 5. App layout ═════════════════════════════════════════════════════════ */
.app {
  display: grid;
  grid-template-columns: var(--side-w) minmax(0, 1fr);
  grid-template-rows: var(--topbar-h) minmax(0, 1fr) auto;
  grid-template-areas:
    "topbar topbar"
    "rail   work"
    "bbar   bbar";
  height: var(--vhfix);
  max-height: var(--vhfix);
  overflow: hidden;
}

/* ══ 6. Top bar ════════════════════════════════════════════════════════════ */
.topbar {
  grid-area: topbar;
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0 0.75rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-1);
  min-width: 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0;
  font-weight: 800;
  font-size: var(--font-size-lg);
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.logo-mark {
  display: inline-flex;
  width: 28px;
  height: 28px;
}

.filename {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0;
  padding: 0.15rem 0.6rem;
  max-width: 22ch;
  background: var(--panel-2);
  border-radius: 999px;
  color: var(--ink-soft);
  font-size: var(--font-size-sm);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* The unsaved marker is a dot for sighted users and a sentence for screen
   reader users — never colour alone. */
.dirty-dot::before {
  content: "";
  display: inline-block;
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: var(--warning);
  vertical-align: middle;
}

.sysmsg {
  margin: 0;
  min-width: 0;
  padding: 0 0.25rem;
  color: var(--accent);
  font-size: var(--font-size-sm);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transition: opacity var(--transition-normal);
}

.sysmsg.show {
  opacity: 1;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--min-touch-target);
  min-height: var(--min-touch-target);
  padding: 0.4rem;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--ink-soft);
  cursor: pointer;
  text-decoration: none;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast);
}

.icon-btn svg {
  width: 24px;
  height: 24px;
  pointer-events: none;
}

.icon-btn:hover {
  background: var(--panel-2);
  border-color: var(--line);
}

.icon-btn[aria-expanded="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
}

/* Signed-in state: a green ring on the account button, plus the accessible
   name is rewritten by loginTool.js to name the signed-in provider. */
.icon-btn.is-signed-in {
  border-color: var(--success);
  box-shadow: inset 0 0 0 2px var(--success-soft);
}

/* ══ 7. Left tool rail ═════════════════════════════════════════════════════ */
.left-panel {
  grid-area: rail;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.panel-header {
  display: flex;
  justify-content: center;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--line);
}

.panel-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--min-touch-target);
  min-height: 36px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--muted);
  cursor: pointer;
}

.panel-toggle svg {
  width: 20px;
  height: 20px;
}

.panel-toggle:hover {
  background: var(--panel-2);
  border-color: var(--line);
}

.tools {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--dynamic-tool-gap);
  padding: 0.5rem 0.35rem;
  min-height: 0;
  flex: 1;
}

.tools.needs-scroll {
  overflow-y: auto;
  justify-content: flex-start;
  scrollbar-width: thin;
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-width: var(--min-touch-target);
  min-height: var(--min-touch-target);
  height: var(--dynamic-tool-size);
  padding: 2px;
  background: var(--panel);
  border: 2px solid transparent;
  border-radius: var(--radius);
  color: var(--ink-soft);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast),
    transform var(--transition-fast);
}

.tool-btn svg {
  width: var(--dynamic-tool-icon);
  height: var(--dynamic-tool-icon);
  pointer-events: none;
}

.tool-btn:hover {
  background: var(--panel-2);
  border-color: var(--line-strong);
}

/* Active tool: a filled background, a heavy border AND a left marker bar.
   Three simultaneous signals, so the state survives greyscale printing,
   Windows High Contrast, and colour vision deficiency. */
.tool-btn[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
  position: relative;
}

.tool-btn[aria-pressed="true"]::before {
  content: "";
  position: absolute;
  left: -0.35rem;
  top: 15%;
  bottom: 15%;
  width: 4px;
  border-radius: 2px;
  background: var(--accent);
}

.tool-label {
  font-size: 0.625rem;
  line-height: 1.1;
  text-align: center;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Collapsed rail hides the labels but keeps the accessible names, which live
   on data-tip-title and the tooltip description. */
.left-panel[data-collapsed] .tool-label,
.left-panel[data-collapsed] .rail-resource-label,
.left-panel[data-collapsed] .panel-resources-title {
  display: none;
}

/* ── Resources ─────────────────────────────────────────────────────────────
   Examples is a resource, not a tool, and everything here exists to make that
   legible at a glance rather than only in the markup:

     - it sits below a rule, in its own captioned group, so it is plainly not
       one of the six buttons above;
     - it is a wide rounded pill rather than a square tile, so its silhouette
       differs from a tool even when the rail is collapsed and no label shows;
     - it is dashed and un-filled, borrowing the "this opens something" look
       the branching tool's answer nodes use;
     - it has no aria-pressed styling at all, because it is never "on".

   The previous version reused .tool-btn, which meant it inherited the pressed
   appearance it could never actually show, and — sitting directly beneath the
   Records tile — read as though it belonged to Records. */
.panel-resources {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  padding: 0.5rem 0.35rem calc(0.5rem + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel-2) 45%, transparent);
}

.panel-resources-title {
  margin: 0;
  font-size: 0.5625rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  text-align: center;
}

.rail-resource {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-width: var(--min-touch-target);
  min-height: var(--min-touch-target);
  padding: 0.3rem 0.2rem;
  background: var(--panel);
  border: 2px dashed var(--line-strong);
  /* A pill, not a tile: a different shape from every tool above it. */
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast),
    color var(--transition-fast);
}

.rail-resource svg {
  width: calc(var(--dynamic-tool-icon) * 0.78);
  height: calc(var(--dynamic-tool-icon) * 0.78);
  pointer-events: none;
}

.rail-resource:hover {
  background: var(--panel-2);
  border-color: var(--accent);
  color: var(--accent);
}

.rail-resource-label {
  font-size: 0.625rem;
  line-height: 1.1;
  text-align: center;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ══ 8. Working area ═══════════════════════════════════════════════════════ */
.workspace {
  grid-area: work;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}

.board-scroll {
  height: 100%;
  overflow: auto;
  /* A narrow gutter keeps the working surface visibly separate from both the
     tool rail and the feature panel without giving up useful chart space. */
  padding: 0.5rem;
  scrollbar-width: thin;
}

.board {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 1.25rem;
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  min-height: 100%;
}

.board.board-branching {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.board:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus);
  outline-offset: var(--focus-ring-offset);
}

/* ══ 9. Bottom bar ═════════════════════════════════════════════════════════ */
.bottombar {
  grid-area: bbar;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: stretch;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  background: var(--panel);
  border-top: 1px solid var(--line);
  box-shadow: 0 -1px 2px rgba(16, 32, 43, 0.06);
  min-height: var(--bottombar-h);
  max-height: 45vh;
}

.context {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.tool-context {
  min-width: 0;
  overflow-x: auto;
  overflow-y: auto;
  scrollbar-width: thin;
}

.bottom-actions {
  display: flex;
  align-items: stretch;
  min-width: 0;
}

.context-edge-actions {
  display: flex;
  align-items: center;
  padding-right: 0.2rem;
}

.context-edge-actions[hidden] {
  display: none;
}

.branch-test-button {
  min-width: 4.75rem;
  min-height: var(--min-touch-target);
}

.context-edge-actions:not([hidden]) + .file-actions {
  padding-left: 0.2rem;
}

.file-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding-left: 0.75rem;
  border-left: 1px solid var(--line);
}

.iconbtn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-width: max(var(--min-touch-target), 56px);
  min-height: var(--min-touch-target);
  padding: 0.3rem 0.35rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--ink-soft);
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast);
}

.iconbtn svg {
  width: 24px;
  height: 24px;
  pointer-events: none;
}

.iconbtn:hover:not(:disabled) {
  background: var(--panel-2);
  border-color: var(--line-strong);
}

.btn-label {
  font-size: 0.625rem;
  line-height: 1;
}

/* Disabled controls must still meet 3:1 non-text contrast for their border so
   they remain perceivable, while clearly reading as unavailable. */
.iconbtn:disabled,
button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.file-actions-menu {
  display: none;
}

.menu-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--min-touch-target);
  min-height: var(--min-touch-target);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--ink-soft);
  cursor: pointer;
}

.menu-toggle svg {
  width: 22px;
  height: 22px;
}

/* ══ 10. Popup menus ═══════════════════════════════════════════════════════ */
.menu-wrap {
  position: relative;
}

.popup-menu {
  position: absolute;
  right: 0;
  min-width: 14rem;
  padding: 0.35rem;
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-3);
  z-index: var(--z-menu);
}

.topbar .popup-menu {
  top: calc(100% + 0.35rem);
}

.bottombar .popup-menu {
  bottom: calc(100% + 0.35rem);
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  min-height: var(--min-touch-target);
  padding: 0.5rem 0.65rem;
  background: transparent;
  border: 0;
  border-radius: var(--radius-sm);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.menu-item:hover {
  background: var(--panel-2);
}

/* Checked menu items show a tick glyph as well as aria-checked, so the state
   is not conveyed by background colour alone. */
.menu-item[role="menuitemcheckbox"]::before {
  content: "";
  flex: 0 0 1.1rem;
  width: 1.1rem;
  height: 1.1rem;
  border: 2px solid var(--line-strong);
  border-radius: 4px;
}

.menu-item[role="menuitemcheckbox"][aria-checked="true"]::before {
  content: "\2713";
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-size: 0.8rem;
  font-weight: 700;
}

.menu-divider {
  height: 1px;
  margin: 0.3rem 0.25rem;
  background: var(--line);
}

.menu-backdrop {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-menu) - 1);
  background: transparent;
}

/* ══ 11. Form controls ═════════════════════════════════════════════════════ */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-width: 0;
}

.field-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.field-label {
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--ink-soft);
}

input[type="text"],
input[type="number"],
input[type="date"],
input[type="search"],
select,
textarea {
  min-height: var(--min-touch-target);
  padding: 0.35rem 0.55rem;
  background: var(--panel);
  border: 2px solid var(--line-strong);
  border-radius: var(--radius-sm);
  color: var(--ink);
  max-width: 100%;
}

input:disabled,
select:disabled {
  background: var(--panel-2);
}

/* Native number spinners are far below 24px, so School Data hides them and
   supplies its own 44px plus/minus buttons instead (SC 2.5.8). */
input[type="number"].no-spinner {
  -moz-appearance: textfield;
  appearance: textfield;
}

input[type="number"].no-spinner::-webkit-outer-spin-button,
input[type="number"].no-spinner::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: var(--min-touch-target);
  min-width: var(--min-touch-target);
  padding: 0.45rem 0.9rem;
  background: var(--panel);
  border: 2px solid var(--line-strong);
  border-radius: var(--radius-sm);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast);
}

.btn:hover:not(:disabled) {
  background: var(--panel-2);
}

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

.btn-primary:hover:not(:disabled) {
  background: #115e56;
  border-color: #115e56;
}

.btn-danger {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
}

.btn-icon {
  padding: 0.35rem;
  min-width: var(--min-touch-target);
}

.btn-icon svg {
  width: 20px;
  height: 20px;
  pointer-events: none;
}

/* Inline validation message.  Prefixed with a word, not just coloured. */
.field-error {
  color: var(--danger);
  font-size: var(--font-size-sm);
  font-weight: 600;
}

.hint {
  color: var(--muted);
  font-size: var(--font-size-sm);
}

/* ══ 12. Responsive ════════════════════════════════════════════════════════ */

/* Wide screens: show the tool names permanently. */
@media (min-width: 1280px) {
  :root {
    --side-w: 108px;
  }
}

/* On classroom laptop widths, compact the file buttons so the contextual
   controls retain enough horizontal space to stay on one row. */
@media (max-width: 1280px) {
  .file-actions .iconbtn .btn-label {
    display: none;
  }
  .file-actions .iconbtn {
    min-width: var(--min-touch-target);
  }
}

@media (max-width: 720px) {
  :root {
    --side-w: 64px;
    --topbar-h: 52px;
  }
  .filename {
    display: none;
  }
  .tool-label {
    display: none;
  }
  .file-actions #btnImport,
  .file-actions #btnPrintPreview {
    display: none;
  }
  .file-actions-menu {
    display: block;
  }
}

/* Narrow / high-zoom: single column.  The rail becomes a horizontal strip
   above the working area so nothing is lost and there is no horizontal
   scrolling of the page as a whole (SC 1.4.10 Reflow at 320px). */
@media (max-width: 520px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: var(--topbar-h) auto minmax(0, 1fr) auto;
    grid-template-areas:
      "topbar"
      "rail"
      "work"
      "bbar";
  }
  .left-panel {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .panel-header {
    display: none;
  }
  .tools {
    flex-direction: row;
    overflow-x: auto;
    justify-content: flex-start;
    padding: 0.4rem;
  }
  .tool-btn {
    width: auto;
    flex: 0 0 auto;
    min-width: var(--min-touch-target);
    aspect-ratio: 1;
  }
  .tool-btn[aria-pressed="true"]::before {
    left: 15%;
    right: 15%;
    top: auto;
    bottom: -0.3rem;
    width: auto;
    height: 4px;
  }
  /* The rail is a horizontal strip here, so the resources group sits at its
     end behind a vertical rule rather than stacked underneath it. */
  .left-panel {
    flex-direction: row;
    align-items: stretch;
  }
  .tools {
    flex: 1 1 auto;
  }
  .panel-resources {
    flex: 0 0 auto;
    flex-direction: row;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 0.5rem;
    border-top: 0;
    border-left: 1px solid var(--line);
  }
  .panel-resources-title {
    /* The visible caption costs too much width here; the group keeps its
       accessible name through aria-labelledby either way. */
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
  .rail-resource {
    width: auto;
    flex-direction: row;
    gap: 0.35rem;
    padding: 0.3rem 0.7rem;
  }
  .board-scroll {
    padding: 0.5rem;
  }
  .board {
    padding: 0.75rem;
  }
  .bottombar {
    grid-template-columns: minmax(0, 1fr);
    max-height: 55vh;
  }
  .bottom-actions {
    border-top: 1px solid var(--line);
    padding-top: 0.5rem;
  }
  .context-edge-actions {
    padding-right: 0.2rem;
  }
  .file-actions {
    padding-left: 0;
    border-left: 0;
    border-top: 0;
    padding-top: 0;
    flex: 1;
    justify-content: space-around;
  }
  .context-edge-actions:not([hidden]) + .file-actions {
    padding-left: 0;
  }
}

/* Short viewports (a laptop with the on-screen keyboard open). */
@media (max-height: 560px) {
  :root {
    --bottombar-h: 64px;
  }
  .bottombar {
    max-height: 60vh;
  }
}

/* ══ 13. Themes ════════════════════════════════════════════════════════════ */

/* Dark mode.  Contrast ratios re-checked: --ink on --bg is 14.4:1, --muted on
   --panel is 7.1:1, and the amber focus ring is 4.9:1 against --panel. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10161d;
    --panel: #1b232c;
    --panel-2: #26313c;
    --ink: #eef4f8;
    --ink-soft: #c6d3de;
    --muted: #9fb0bf;
    --line: #384654;
    --line-strong: #5b6d7d;

    --accent: #5eead4;
    --accent-ink: #05231f;
    --accent-soft: #10403a;
    --accent-2: #a5b4fc;

    --danger: #fca5a5;
    --danger-soft: #3a1c1c;
    --success: #6ee7b7;
    --success-soft: #123a2c;
    --warning: #fcd34d;
    --warning-soft: #3a2c10;

    --focus: #fbbf24;

    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.5);
    --shadow-2: 0 4px 12px rgba(0, 0, 0, 0.55);
    --shadow-3: 0 12px 32px rgba(0, 0, 0, 0.65);
  }
  .skip-link {
    background: var(--panel);
    color: var(--ink);
  }
  :where(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
    box-shadow: 0 0 0 calc(var(--focus-ring-width) + var(--focus-ring-offset))
      rgba(0, 0, 0, 0.85);
  }
}

/* Explicit high-contrast theme, toggled from the Display options menu and
   persisted.  Aims at WCAG AAA (7:1 for body text, 4.5:1 for large text) and
   removes every soft tint so nothing relies on subtle shading. */
:root[data-contrast] {
  --bg: #ffffff;
  --panel: #ffffff;
  --panel-2: #f0f0f0;
  --ink: #000000;
  --ink-soft: #000000;
  --muted: #262626;
  --line: #000000;
  --line-strong: #000000;
  --accent: #00514b;
  --accent-ink: #ffffff;
  --accent-soft: #d7f5f1;
  --danger: #8f0f0a;
  --danger-soft: #ffffff;
  --success: #044f37;
  --success-soft: #ffffff;
  --warning: #5a3000;
  --warning-soft: #ffffff;
  --focus: #7a2e00;
  --focus-ring-width: 4px;
  --shadow-1: none;
  --shadow-2: none;
  --shadow-3: 0 0 0 2px #000000;
}

:root[data-contrast] .board,
:root[data-contrast] .popup-menu,
:root[data-contrast] .iconbtn,
:root[data-contrast] .btn {
  border-width: 2px;
}

@media (prefers-color-scheme: dark) {
  :root[data-contrast] {
    --bg: #000000;
    --panel: #000000;
    --panel-2: #1a1a1a;
    --ink: #ffffff;
    --ink-soft: #ffffff;
    --muted: #e6e6e6;
    --line: #ffffff;
    --line-strong: #ffffff;
    --accent: #6ff0e0;
    --accent-ink: #000000;
    --accent-soft: #003c36;
    --focus: #ffd166;
  }
}

/* Larger text.  Scales the root font size rather than individual rules, so
   every em-based dimension grows together and nothing clips (SC 1.4.4). */
:root[data-large-text] {
  --font-size: 20px;
  --min-touch-target: 52px;
  --bottombar-h: 112px;
}

/* Windows High Contrast / forced-colours mode.  Hand every colour back to the
   OS and re-establish the boundaries with system keywords, because our custom
   properties are ignored here. */
@media (forced-colors: active) {
  .tool-btn,
  .iconbtn,
  .pictogram-image-button,
  .data-editor-launch-button,
  .btn,
  .popup-menu,
  .board,
  input,
  select,
  textarea {
    border: 1px solid CanvasText;
  }
  .tool-btn[aria-pressed="true"] {
    border: 3px solid Highlight;
    forced-color-adjust: none;
    background: Canvas;
    color: CanvasText;
  }
  .tool-btn[aria-pressed="true"]::before {
    background: Highlight;
  }
  .rail-resource {
    border: 2px dashed CanvasText;
  }
  :where(a, button, input, select, textarea, [tabindex]):focus-visible {
    outline: 3px solid Highlight;
    outline-offset: 2px;
    box-shadow: none;
  }
  .dirty-dot::before {
    background: CanvasText;
  }
  .menu-item[role="menuitemcheckbox"][aria-checked="true"]::before {
    background: Highlight;
    color: Canvas;
  }
}
