/* The look both pages share (web/shared/): the design tokens, then the
   base rules (controls, icons, labels) that only combine them. Loaded
   before each page's own stylesheet. */

/* ---------------------------------------------------------------- tokens */

:root {
  color-scheme: light dark;

  /* Colour: a warm neutral desk, calm panels, one teal accent. */
  --desk: #dad7ce;
  --panel: #f1efe9;
  --paper: #fbfaf6;
  --ink: #23221e;
  --muted: #6c6962;
  /* A select's chevron, in --ink (a data URL takes no custom property). */
  --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M1.5 3.75 6 8.25l4.5-4.5' fill='none' stroke='%2323221e' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --rule: #cbc7bb;
  --rule-soft: #e0ddd4;
  --control: #ffffff;
  /* A control's border: a shade apart from its surface, the lift from
     its shadow (--shadow-ctl); a hovered one takes --control-border-hover
     and the --control-hover tint. */
  --control-border: #cdc9bd;
  --control-border-hover: #a8a496;
  --control-hover: #f8f7f3;
  /* A segmented control's track, and a selected row or tab's tint; the
     chosen segment's raised thumb; a switch's track while off. */
  --track: #e4e1d8;
  --track-strong: #c4c0b4;
  --thumb: #ffffff;
  /* A card on the panel (components.css `.card`: every section of the
     side panel, the memory view's lists, the home page's lists and
     settings), its hairline and its rows' hairlines (--rule-soft). */
  --card: #fdfcf9;
  --card-border: #e2dfd6;
  /* A control that cannot act: no fill of its own, so its text and icon
     are 3.6:1 on a segmented control's track, 4.1:1 on the panel and
     4.5:1 on paper (enabled text is over 15:1; a blanket opacity fell
     under 3:1); a very light border, just apart from the surface and
     fainter than an enabled one, so it reads as off at a glance. */
  --ink-disabled: #767369;
  --border-disabled: #d6d2c7;
  --border-disabled-style: solid;
  --hover: #e8e5dc;
  --accent: #2a7f82;
  /* The accent as small text (links, a 13 px line): 4.5:1 or more on the
     panel, paper, a control, a hover and the soft accent (the accent
     itself is 4.1:1 on the panel; web/test/theme.test.mjs). */
  --accent-text: #226f72;
  --accent-ink: #ffffff;
  --accent-soft: #d5e8e6;
  --error: #b2462a;
  --error-soft: #f6e4dd;
  /* Text on a solid error-coloured button (confirm.js): 5.5:1. */
  --error-ink: #ffffff;
  /* Something that is being put right by itself (AI access reconnecting): a dot, not text. */
  --warn: #b7791f;
  /* A calculator object shown as text: tinted and sunk, like a display. */
  --display: #dde1d3;
  --display-ink: #23271f;
  /* A folder's icon is filled, a file's hollow (components.css): a warm
     manila under the --muted outline. Light: 2.2:1 on paper, 1.8:1 on a
     selected row, the outline 2.4:1 on it; dark: 3.3:1 on paper, 2.3:1
     on a selected row, the outline 1.9:1 on it. */
  --folder-fill: #d9a441;
  --backdrop: rgba(20, 20, 19, 0.42);
  /* RPL in the palette's editor: one hue per kind of token, quiet enough
     to read a program by. */
  --syn-delim: #2a7f82;
  --syn-num: #8a5300;
  --syn-str: #4c7425;
  --syn-kw: #7b3f9e;
  --syn-cmd: #1d5c8c;
  --syn-var: #a2401d;
  --syn-com: #6c6962;
  --syn-match: #f2dc8c;

  /* Type: one humanist face for the chrome, a monospace for what the
     calculator says. Sizes step by one point. */
  --font: "Helvetica Neue", Helvetica, Arial, "Liberation Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --fs-xs: 11px;
  --fs-sm: 12px;
  --fs-md: 13px;
  --fs-base: 14px;
  --fs-lg: 16px;
  --fs-xl: 18px;
  --fs-2xl: 22px;
  --lh: 1.45;
  --lh-tight: 1.3;
  --track-caps: 0.06em;

  /* Spacing rhythm, in 4 px steps. */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 24px;
  --s6: 32px;
  --gutter: 16px;
  /* Between the sections of a settings column (a title and its card). */
  --sec-gap: 28px;
  /* A card's inner padding, and its rows'. */
  --card-pad: 12px;

  /* Radii: controls, cards and sheets, dialogs. */
  --r-sm: 4px;
  --r-md: 7px;
  --r-lg: 12px;
  --r-xl: 16px;
  --r-pill: 999px;

  /* Elevation: flat controls (a border only), raised sheets and popovers,
     floating dialogs. */
  --shadow-1: 0 1px 2px rgba(30, 26, 16, 0.08);
  --shadow-2: 0 8px 24px rgba(30, 26, 16, 0.18), 0 1px 3px rgba(30, 26, 16, 0.08);
  --shadow-3: 0 24px 64px rgba(30, 26, 16, 0.32), 0 2px 8px rgba(30, 26, 16, 0.12);
  /* A control's lift, and a card's (a list, a settings group). */
  --shadow-ctl: 0 1px 1.5px rgba(30, 26, 16, 0.07);
  --shadow-card: 0 1px 2px rgba(30, 26, 16, 0.04), 0 2px 8px rgba(30, 26, 16, 0.04);
  --shadow-thumb: 0 1px 2px rgba(30, 26, 16, 0.16), 0 0 0 0.5px rgba(30, 26, 16, 0.05);
  --inset: inset 0 1px 3px rgba(0, 0, 0, 0.22);

  /* Motion: short and purposeful; zero under prefers-reduced-motion. */
  --t-fast: 80ms;
  --t: 160ms;
  --ease: cubic-bezier(0.2, 0, 0, 1);

  /* Targets: controls are 32 px tall with a mouse, 44 px with a finger;
     list rows 26 px and 40 px. */
  --tap: 32px;
  --row: 28px;

  /* The device's safe areas (a notch, the home indicator). */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);

  /* Layout: the side panel's and the memory view's widths (the page sets
     them from what was dragged, app.js, within the minimums there), the
     top bar's height on narrow screens. */
  --panel-w: 320px;
  --layer-w: clamp(400px, 42vw, 640px);
  --bar-h: 48px;
  --bar-top: calc(var(--bar-h) + var(--safe-t));
  --palette-w: min(960px, 100vw - 2 * var(--gutter));
  --palette-h: min(620px, 100vh - 2 * 48px);
}

/* The dark tokens: when the page is set to Dark (`data-theme`,
   web/shared/theme.js), and with the device's dark scheme unless it is set to
   Light. The two sets are the same, kept so by web/test/theme.test.mjs:
   CSS cannot apply one rule under either condition. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --desk: #1c1c1b;
  --panel: #272724;
  --paper: #1e1e1c;
  --ink: #e8e5dd;
  --muted: #a19e95;
  --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M1.5 3.75 6 8.25l4.5-4.5' fill='none' stroke='%23e8e5dd' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  --rule: #3f3e39;
  --rule-soft: #33322e;
  --control: #373733;
  --control-border: #4b4a44;
  --control-border-hover: #78766c;
  --control-hover: #3d3d38;
  --track: #1f1f1d;
  --track-strong: #5a5952;
  --thumb: #4a4944;
  --card: #2e2e2b;
  --card-border: #3c3b36;
  --ink-disabled: #8f8c84;
  --border-disabled: #43423d;
  --hover: #383834;
  --accent: #5fbfba;
  --accent-text: #5fbfba;
  --accent-ink: #10201f;
  --accent-soft: #233f3d;
  --error: #e58b6a;
  --error-soft: #3d2a23;
  --error-ink: #1c120d;
  --warn: #e3a94b;
  --display: #2b3029;
  --display-ink: #dfe5d6;
  --folder-fill: #8a6a2e;
  --backdrop: rgba(0, 0, 0, 0.6);
  --syn-delim: #5fbfba;
  --syn-num: #e2b26a;
  --syn-str: #a3cf7c;
  --syn-kw: #cf9ce8;
  --syn-cmd: #79b8e8;
  --syn-var: #ee9a76;
  --syn-com: #a19e95;
  --syn-match: #5a4d1c;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-3: 0 24px 64px rgba(0, 0, 0, 0.6), 0 2px 8px rgba(0, 0, 0, 0.35);
  --shadow-ctl: 0 1px 1.5px rgba(0, 0, 0, 0.3);
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.2), 0 2px 8px rgba(0, 0, 0, 0.16);
  --shadow-thumb: 0 1px 2px rgba(0, 0, 0, 0.4), 0 0 0 0.5px rgba(0, 0, 0, 0.2);
  --inset: inset 0 1px 3px rgba(0, 0, 0, 0.45);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --desk: #1c1c1b;
    --panel: #272724;
    --paper: #1e1e1c;
    --ink: #e8e5dd;
    --muted: #a19e95;
    --select-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M1.5 3.75 6 8.25l4.5-4.5' fill='none' stroke='%23e8e5dd' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    --rule: #3f3e39;
    --rule-soft: #33322e;
    --control: #373733;
    --control-border: #4b4a44;
    --control-border-hover: #78766c;
    --control-hover: #3d3d38;
    --track: #1f1f1d;
    --track-strong: #5a5952;
    --thumb: #4a4944;
    --card: #2e2e2b;
    --card-border: #3c3b36;
    --ink-disabled: #8f8c84;
    --border-disabled: #43423d;
    --hover: #383834;
    --accent: #5fbfba;
    --accent-text: #5fbfba;
    --accent-ink: #10201f;
    --accent-soft: #233f3d;
    --error: #e58b6a;
    --error-soft: #3d2a23;
    --error-ink: #1c120d;
    --warn: #e3a94b;
    --display: #2b3029;
    --display-ink: #dfe5d6;
    --folder-fill: #8a6a2e;
    --backdrop: rgba(0, 0, 0, 0.6);
    --syn-delim: #5fbfba;
    --syn-num: #e2b26a;
    --syn-str: #a3cf7c;
    --syn-kw: #cf9ce8;
    --syn-cmd: #79b8e8;
    --syn-var: #ee9a76;
    --syn-com: #a19e95;
    --syn-match: #5a4d1c;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-2: 0 8px 24px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-3: 0 24px 64px rgba(0, 0, 0, 0.6), 0 2px 8px rgba(0, 0, 0, 0.35);
    --shadow-ctl: 0 1px 1.5px rgba(0, 0, 0, 0.3);
    --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.2), 0 2px 8px rgba(0, 0, 0, 0.16);
    --shadow-thumb: 0 1px 2px rgba(0, 0, 0, 0.4), 0 0 0 0.5px rgba(0, 0, 0, 0.2);
    --inset: inset 0 1px 3px rgba(0, 0, 0, 0.45);
  }
}

/* Set to Light on a dark device: the light tokens above, light controls. */
:root[data-theme="light"] {
  color-scheme: light;
}

@media (pointer: coarse) {
  :root {
    --tap: 44px;
    --row: 40px;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --t-fast: 0ms;
    --t: 0ms;
  }
}

/* ------------------------------------------------------------------ base */

* { box-sizing: border-box; }
[hidden] { display: none !important; }
/* Out of sight, still read by screen readers (a live region that stays). */
.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--desk);
  color: var(--ink);
  font: var(--fs-base)/var(--lh) var(--font);
  -webkit-font-smoothing: antialiased;
}

/* The icon set: one SVG sprite, shared/icons.svg (put into the page by
   shared/components/icons.js), drawn with strokes in the current colour. */
.ic {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  vertical-align: -5px;
}
.ic-sm { width: 16px; height: 16px; vertical-align: -3px; }

/* Controls: one height, a light border and a hair of lift; the border
   darkens under the pointer, a press sinks it (no lift, the hover tint). */
button, select, input:not([type=checkbox]):not([type=file]), summary {
  font: inherit;
  color: var(--ink);
}
button {
  background: var(--control);
  border: 1px solid var(--control-border);
  border-radius: var(--r-md);
  min-height: var(--tap);
  padding: 0 var(--s3);
  line-height: var(--lh-tight);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  box-shadow: var(--shadow-ctl);
  transition: background-color var(--t-fast), border-color var(--t-fast), color var(--t-fast), box-shadow var(--t-fast);
}
button:hover:not(:disabled, [aria-disabled="true"]) { border-color: var(--control-border-hover); background: var(--control-hover); }
button:active:not(:disabled, [aria-disabled="true"]) { background: var(--hover); box-shadow: none; }
/* The icon in a button's label: a step quieter than its text. */
button:not(.icon, .primary, .segmented > *) > .ic { color: var(--muted); transition: color var(--t-fast); }
button:not(.icon, .primary):hover > .ic { color: inherit; }
/* A compact button beside a line of text (Change ROM… after the ROM's name). */
button.small { min-height: max(28px, calc(var(--tap) - 8px)); padding: 0 var(--s2); font-size: var(--fs-md); border-radius: var(--r-sm); }
/* Disabled: the button sinks into its surface. No fill, no shadow, a
   very light border (the tokens above), text and icon in
   --ink-disabled; no hover or press. One look for every kind: plain,
   primary (gives up its accent), icon, segment, toolbar tool and menu
   item, whether :disabled or aria-disabled (an Edit that stays
   focusable to say why keeps its focus ring). `:root` lifts it over the
   kinds' own rules (`.stage-tools button.tool`, `.menu [role=menuitem]`).
   A flag's lamp and bit cells are left out: off while a write runs,
   they still show whether the flag is set. A link stays borderless. */
:root :is(button, [role=menuitem]):is(:disabled, [aria-disabled="true"]):where(:not(.cell, .lamp-toggle)) {
  background: transparent;
  border: 1px var(--border-disabled-style) var(--border-disabled);
  box-shadow: none;
  color: var(--ink-disabled);
  cursor: default;
  /* Its look never fades in: nothing of it changes under the pointer. */
  transition: none;
}
:root :is(button, [role=menuitem]):is(:disabled, [aria-disabled="true"]):where(:not(.cell, .lamp-toggle)) .ic { color: inherit; }
/* An icon button has no frame when on: off it gets none either, or the
   light frame reads as a toggle held down (the Edit pencil with no ROM).
   The ⋯ buttons, framed when on, keep theirs. */
:root button.icon:not(.more):is(:disabled, [aria-disabled="true"]) { border-color: transparent; }
:root button.link:is(:disabled, [aria-disabled="true"]) { border: 0; }
/* Segments sit on their track, which keeps its border: an off segment
   has none of its own. */
:root .segmented > button:is(:disabled, [aria-disabled="true"]) { border-width: 0; }
:is(button, select, input, summary, a, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.icon {
  padding: 0;
  flex: none;
  width: var(--tap);
  height: var(--tap);
  min-height: 0;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  color: var(--ink);
}
button.icon:hover:not(:disabled, [aria-disabled="true"]) { background: var(--hover); border-color: transparent; }
button.icon:active:not(:disabled, [aria-disabled="true"]) { background: var(--track); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover:not(:disabled, [aria-disabled="true"]) { background: color-mix(in srgb, var(--accent) 88%, var(--ink)); border-color: color-mix(in srgb, var(--accent) 88%, var(--ink)); color: var(--accent-ink); }
button.primary:active:not(:disabled, [aria-disabled="true"]) { background: color-mix(in srgb, var(--accent) 78%, var(--ink)); }
button.link {
  border: 0;
  background: none;
  min-height: 0;
  padding: 0;
  color: var(--accent-text);
  font-size: var(--fs-md);
  text-decoration: underline;
  text-underline-offset: 2px;
  justify-content: flex-start;
}
button.link { box-shadow: none; }
button.link:hover:not(:disabled, [aria-disabled="true"]) { background: none; color: var(--ink); }
/* A link that looks like a button (the home page's ways in, "Your calculators"). */
a.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: var(--tap);
  padding: 0 var(--s3);
  box-sizing: border-box;
  border: 1px solid var(--control-border);
  border-radius: var(--r-md);
  background: var(--control);
  color: var(--ink);
  line-height: var(--lh-tight);
  text-decoration: none;
  box-shadow: var(--shadow-ctl);
  transition: background-color var(--t-fast), border-color var(--t-fast), box-shadow var(--t-fast);
}
a.button:hover { border-color: var(--control-border-hover); background: var(--control-hover); }
a.button:active { background: var(--hover); box-shadow: none; }
a.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
a.button.primary:hover { background: color-mix(in srgb, var(--accent) 88%, var(--ink)); border-color: color-mix(in srgb, var(--accent) 88%, var(--ink)); }
a.button.primary:active { background: color-mix(in srgb, var(--accent) 78%, var(--ink)); }

/* The browser's own arrow sits against the right border: the page draws
   its chevron instead, centred and as far from the right border as the
   text is from the left (--s2), the text kept clear of it. */
select {
  -webkit-appearance: none;
  appearance: none;
  padding: 0 calc(var(--s2) * 2 + 12px) 0 var(--s2);
  min-height: var(--tap);
  background: var(--control) var(--select-chevron) no-repeat right var(--s2) center / 12px 12px;
  border: 1px solid var(--control-border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-ctl);
  transition: border-color var(--t-fast);
}
select:hover { border-color: var(--control-border-hover); }
/* Forced colours (Windows high contrast) drop background images: the
   browser's own arrow, in the system's colours, comes back. */
@media (forced-colors: active) {
  select {
    -webkit-appearance: auto;
    appearance: auto;
    background-image: none;
    padding-right: var(--s2);
  }
}
input[type=search], input[type=text] { -webkit-appearance: none; appearance: none; }
input[type=search]::-webkit-search-cancel-button { -webkit-appearance: none; }
.check { display: inline-flex; align-items: center; gap: var(--s2); min-height: var(--tap); }
.check input { margin: 0; width: 16px; height: 16px; accent-color: var(--accent); }

/* A switch (components.css, "Switch"): a checkbox drawn as a pill with
   a knob, its text before it; the whole label toggles it. The markup is
   a `.check`'s: `<label class="switch"><input type="checkbox"> <span>…`. */
.switch { display: flex; align-items: center; gap: var(--s3); min-height: var(--tap); cursor: pointer; }
.switch > :not(input) { order: 1; flex: 1 1 auto; min-width: 0; }
.switch input {
  order: 2;
  flex: none;
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
  width: 34px;
  height: 20px;
  border-radius: var(--r-pill);
  background: var(--track-strong);
  position: relative;
  cursor: pointer;
  transition: background-color var(--t) var(--ease);
}
.switch input::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.28);
  transition: transform var(--t) var(--ease);
}
.switch input:checked { background: var(--accent); }
.switch input:checked::before { transform: translateX(14px); }
.switch:hover input:not(:checked, :disabled) { background: color-mix(in srgb, var(--track-strong) 80%, var(--ink)); }
.switch input:disabled { opacity: 0.5; cursor: default; }
@media (forced-colors: active) {
  .switch input { -webkit-appearance: auto; appearance: auto; width: 16px; height: 16px; }
  .switch input::before { content: none; }
}

/* A segmented control (components.css, "Segmented"): a sunk track, the
   chosen segment a raised thumb in the surface's colour, the others plain
   text; `.fill` stretches it across its column with equal segments. */
.segmented {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  background: var(--track);
  border: 1px solid color-mix(in srgb, var(--ink) 6%, transparent);
  border-radius: var(--r-md);
  align-self: start;
}
.segmented.fill { display: flex; align-self: stretch; }
.segmented.fill > button { flex: 1 1 0; min-width: 0; }
.segmented button {
  border: 0;
  border-radius: calc(var(--r-md) - 3px);
  min-width: 48px;
  min-height: calc(var(--tap) - 8px);
  padding: 0 var(--s2);
  background: transparent;
  box-shadow: none;
  color: var(--ink);
  font-weight: 500;
  transition: background-color var(--t-fast), box-shadow var(--t-fast), color var(--t-fast);
}
.segmented button:hover:not(:disabled, [aria-disabled="true"], [aria-checked=true]) { background: color-mix(in srgb, var(--ink) 6%, transparent); }
.segmented button[aria-checked=true] { background: var(--thumb); color: var(--ink); font-weight: 600; box-shadow: var(--shadow-thumb); }
.segmented button:focus-visible { outline-offset: 1px; }

kbd {
  font: var(--fs-sm)/1.4 var(--mono);
  padding: 0 var(--s1);
  border: 1px solid var(--rule);
  border-bottom-width: 2px;
  border-radius: var(--r-sm);
  background: var(--control);
  color: var(--ink);
  white-space: nowrap;
}
/* A keyboard hint means nothing to a finger. */
@media (hover: none) and (pointer: coarse) {
  .kbd-hint { display: none !important; }
}

/* Column labels: small caps, tracked, muted (a table's head, a run of
   topics in a long list). The sections of a settings column take
   `.sec-title` (components.css) instead. */
.caps, .list th, .entry h4, .shortcuts-body h3, .about-body h3 {
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: var(--track-caps);
  text-transform: uppercase;
  color: var(--muted);
}
.muted { color: var(--muted); }
.num, .tab-count, .list .num, .lamp-grid .cell { font-variant-numeric: tabular-nums; }
