/*
 * The platform's styles. Three levels decide how a view looks, lowest first:
 *   1. @layer platform: this file, the design tokens and the defaults;
 *   2. @layer plugins: each plugin's own <style>, reaching only its views;
 *   3. the page's own <style>, in no layer, so the app has the last word.
 */
@layer platform, plugins;

@layer platform {
:root {
  --bg: light-dark(#f5f4f1, #101012);
  --bg-2: light-dark(#ebe9e4, #1a1a1e);
  --fg: light-dark(#1b1a17, #f0efec);
  --fg-2: light-dark(#66625a, #a5a39d);
  --border: light-dark(#e3e0d9, #2b2b31);
  --accent: light-dark(#3d4fd6, #99a5ff);
  --accent-fg: light-dark(#fff, #0c0e24);
  /* The main button of a form or a dialog. Ink, so the accent is left to mean "a link" and "focus". */
  --action: var(--fg);
  --action-fg: var(--bg);
  --danger: light-dark(#c0262d, #ff7b7b);
  --ok: light-dark(#18794e, #4cd38a);
  --warn: light-dark(#a35200, #f0b44c);
  --surface: light-dark(#ffffff, #18181c);
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  /* The height of every one-line control: buttons, fields and selects line up beside each other. */
  --control: 38px;
  /* A compact control inside a row of a table or a list. */
  --control-sm: 30px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --text-sm: 13px;
  --text-md: 15px;
  --text-lg: 17px;
  --text-xl: 21px;
  --text-2xl: 30px;
  --shadow-1: 0 1px 2px light-dark(rgb(30 25 10 / 0.05), rgb(0 0 0 / 0.4));
  --shadow-2: 0 12px 32px light-dark(rgb(30 25 10 / 0.12), rgb(0 0 0 / 0.5)), 0 2px 6px light-dark(rgb(30 25 10 / 0.06), rgb(0 0 0 / 0.3));
  /* The focus ring, the same on every control. */
  --ring: color-mix(in srgb, var(--accent) 70%, transparent);
  --duration: 140ms;
  --ease: cubic-bezier(0.2, 0, 0, 1);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: light dark;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--fg); font: var(--text-md)/1.55 var(--font); min-height: 100vh; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
a { color: var(--accent); text-underline-offset: 3px; text-decoration-thickness: 1px; }
::selection { background: color-mix(in srgb, var(--accent) 25%, transparent); }
/* A <slot name> in the page is where plugins draw; what the page put inside it shows until one does. */
slot, .fallback { display: contents; }
.fallback[hidden] { display: none; }

/* ---- App pages (app.html): header, main, an optional aside, footer. One column on phones. ---- */
body:not(.workbench) { display: grid; grid-template: "header header" auto "main aside" 1fr "footer footer" auto / minmax(0, 1fr) minmax(0, 320px); }
body:not(.workbench):not(:has(> aside)) { grid-template: "header" auto "main" 1fr "footer" auto / minmax(0, 1fr); }
body:not(.workbench) > header { grid-area: header; display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; min-height: 56px; padding: 10px 24px; border-bottom: 1px solid var(--border); background: var(--surface); }
body > header :is(h1, strong) a { color: inherit; text-decoration: none; }
body:not(.workbench) > header h1 { margin: 0; font-size: var(--text-lg); font-weight: 650; letter-spacing: -0.01em; }
body:not(.workbench) > main, body:not(.workbench) > aside { min-width: 0; padding: 24px; display: flex; flex-direction: column; align-content: start; gap: var(--space-4); }
body:not(.workbench) > main { width: 100%; max-width: 1080px; justify-self: center; }
body:not(.workbench) > main { grid-area: main; }
body:not(.workbench) > aside { grid-area: aside; border-left: 1px solid var(--border); }
body:not(.workbench) > footer { grid-area: footer; padding: 8px 24px; border-top: 1px solid var(--border); color: var(--fg-2); font-size: var(--text-sm); }
@media (max-width: 800px) {
  body:not(.workbench) { grid-template: "header" auto "main" auto "aside" auto "footer" auto / minmax(0, 1fr); }
  body:not(.workbench) > aside { border-left: 0; border-top: 1px solid var(--border); }
  body:not(.workbench) > header { padding: 10px 16px; }
  body:not(.workbench) > main, body:not(.workbench) > aside { padding: 16px; }
}

/* ---- The workbench (site/workbench/index.html): notes, every plugin's views, the plugins panel and the trace ---- */
.workbench { display: grid; grid-template-rows: auto 1fr auto auto; grid-template-columns: minmax(0, 1fr); }
.workbench header { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--bg-2); }
.workbench header .spacer { flex: 1; }
.workbench header .demos { display: flex; flex-wrap: wrap; min-width: 0; gap: var(--space-3); font-size: var(--text-sm); }
.workbench header .demos a { color: var(--accent); }
.workbench main { display: grid; grid-template-columns: 220px 1fr 260px; min-height: 360px; }
.workbench main > * { min-width: 0; padding: 12px 16px; overflow: auto; display: flex; flex-direction: column; gap: var(--space-4); }
.workbench main > nav { border-right: 1px solid var(--border); }
.workbench main > aside { border-left: 1px solid var(--border); }
.workbench footer { display: flex; gap: 16px; flex-wrap: wrap; padding: 6px 16px; border-top: 1px solid var(--border); background: var(--bg-2); color: var(--fg-2); font-size: 12px; font-variant-numeric: tabular-nums; min-height: 30px; }
/* Phones: one column, the main view first, the notes list and side panels after it. */
@media (max-width: 800px) {
  .workbench main { grid-template-columns: minmax(0, 1fr); }
  .workbench main > section { order: -1; }
  .workbench main > nav, .workbench main > aside { border: 0; border-top: 1px solid var(--border); }
  #mode { display: none; }
}
/* Review cards: a dialog that stays in view at the bottom of the screen until each card is answered. */
dialog#review { position: fixed; inset: auto 16px 16px; z-index: 20; width: auto; max-width: 760px; max-height: min(70vh, 640px); margin: 0 auto; padding: 16px; overflow: auto; border: 2px solid var(--accent); border-radius: var(--radius); background: var(--surface); color: var(--fg); box-shadow: 0 8px 32px light-dark(rgb(0 0 0 / 0.18), rgb(0 0 0 / 0.6)); }
dialog#review > [data-key] { border: 0; border-radius: 0; padding: 0; margin: 0; }
dialog#review > [data-key] + [data-key] { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.muted { color: var(--fg-2); }
.error { color: var(--danger); }
.ok { color: var(--ok); }
.stack { display: flex; flex-direction: column; gap: 8px; }
.stack.row { flex-direction: row; align-items: center; }
h2 { font-size: 15px; margin: 0; }
/* Buttons, and links made to look like one (<a class="button">). */
button, a.button { font: inherit; font-weight: 500; display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2); height: var(--control); padding: 0 16px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--fg); cursor: pointer; text-decoration: none; white-space: nowrap; transition: background var(--duration) var(--ease), border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease); }
/* States name the element as the base rule does, so they outrank it. */
button:hover, a.button:hover { background: var(--bg-2); border-color: color-mix(in srgb, var(--fg) 22%, var(--border)); }
button:active, a.button:active { transform: translateY(0.5px); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary, a.button.primary, form > button[type="submit"]:only-of-type { background: var(--action); color: var(--action-fg); border-color: var(--action); }
button.primary:hover, a.button.primary:hover, form > button[type="submit"]:only-of-type:hover { background: color-mix(in srgb, var(--action) 85%, var(--action-fg)); border-color: color-mix(in srgb, var(--action) 85%, var(--action-fg)); }
:where(button, a.button, input, select, textarea, summary):focus-visible { outline: 3px solid var(--ring); outline-offset: 1px; }
/* Fields on a plain page (an HTML view and label.field style their own below). */
:where(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"]), select) { font: inherit; min-height: var(--control); padding: 0 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--fg); transition: border-color var(--duration) var(--ease); }
:where(input, select, textarea):hover { border-color: color-mix(in srgb, var(--fg) 28%, var(--border)); }
:where(input, textarea)::placeholder { color: color-mix(in srgb, var(--fg-2) 75%, transparent); }
:where(input[type="checkbox"], input[type="radio"], input[type="range"], progress, meter) { accent-color: var(--accent); }
ul.list { list-style: none; margin: 0; padding: 0; }
ul.list li button { width: 100%; justify-content: flex-start; text-align: left; border: 0; background: none; box-shadow: none; }
ul.list li button[aria-current="true"] { background: color-mix(in srgb, var(--accent) 15%, transparent); }
textarea { width: 100%; min-height: 240px; font: 15px/1.7 var(--font); padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--fg); resize: vertical; }
label.field { display: flex; flex-direction: column; gap: 2px; font-size: var(--text-sm); }
label.field.checkbox { flex-direction: row; align-items: center; gap: 6px; }
label.field input:not([type="checkbox"]), label.field select { font: inherit; height: var(--control); padding: 0 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--fg); }
pre.diff { margin: 0; padding: 8px 0; max-height: 320px; overflow: auto; font: 13px/1.5 var(--mono); border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-2); }
pre.diff span { display: block; padding: 0 10px; white-space: pre-wrap; }
pre.diff .add { background: color-mix(in srgb, var(--ok) 16%, transparent); }
pre.diff .del { background: color-mix(in srgb, var(--danger) 16%, transparent); text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--danger) 60%, transparent); }
pre.diff .same { color: var(--fg-2); }
[data-key^="review:"] { border: 1px solid var(--accent); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; }
canvas { width: 100%; border: 1px solid var(--border); border-radius: var(--radius); }
#devtools { border-top: 1px solid var(--border); padding: 8px 16px; font-size: 12px; }
#devtools summary { cursor: pointer; color: var(--fg-2); }
#devtools .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
#trace { font-family: var(--mono); max-height: 220px; overflow: auto; margin: 0; padding-left: 0; list-style: none; }
#trace li { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#reference-text { font: 12px/1.5 var(--mono); max-height: 360px; overflow: auto; margin: 0 0 8px; padding: 8px 10px; white-space: pre-wrap; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-2); }
#trace li.denied, #trace li.error { color: var(--danger); }
#notice { position: fixed; right: 16px; bottom: 16px; max-width: 360px; }
#notice p { margin: 4px 0 0; padding: 10px 14px; border-radius: var(--radius); background: var(--fg); color: var(--bg); box-shadow: var(--shadow-2); }
/* Places: a plugin's views in a page that left no slot for them (places.ts). */
.place { display: contents; }
/* The default app layout puts body's children in named areas; a place right in <body> would land after the footer, so it spans the page instead. Target inside <main> or <header> to place it exactly. */
body:not(.workbench) > .place { display: block; grid-column: 1 / -1; }
[hidden] { display: none !important; }
.place-pending { outline: 2px dashed var(--accent); outline-offset: 4px; }
/* The Customize button (std/customize.js, in a <ui-panel>), on every app page: the host marks its slot pinned, and
   pins it to the bottom-left corner, above any floating chrome there (--chrome-bottom-left, see "Floating slots" below). */
slot[pinned] > [data-plugin] { position: fixed; left: 0; bottom: var(--chrome-bottom-left, 0px); z-index: 10; padding: 16px; }
/* The 16px around the button is inside its box, not outside it: a view's box clips what it paints, and the button's shadow has to fit. */
/* The page ends with room for that button, so the last thing on it can be scrolled clear of it. */
body:has(slot[pinned] > [data-plugin]) { padding-bottom: calc(72px + var(--chrome-bottom-left, 0px)); }

/* Inspect mode (inspect.ts): every plugin's view is outlined, the part under the pointer more so, and a panel says who drew it. */
body.inspecting [data-plugin][data-key] { outline: 1px dashed color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 2px; }
body.inspecting .inspect-hover { outline: 2px solid var(--accent) !important; outline-offset: 2px; cursor: crosshair; }
body.inspecting .inspect-picked { outline: 3px solid var(--accent) !important; outline-offset: 2px; }
.inspect { position: fixed; right: 16px; bottom: 16px; z-index: 20; width: min(380px, calc(100vw - 32px)); padding: var(--space-3) var(--space-4); border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); color: var(--fg); box-shadow: var(--shadow-1); font: var(--text-sm)/1.5 var(--font); }
.inspect[hidden] { display: none; }
.inspect p { margin: var(--space-1) 0; overflow-wrap: anywhere; }
.inspect code { font: var(--text-sm) var(--mono); }
.inspect-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); margin: var(--space-2) 0; }
.inspect-actions input { flex: 1; min-width: 8rem; font: inherit; height: 28px; padding: 0 6px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); color: var(--fg); }

/* ---- HTML views: plain HTML, styled from the tokens ---- */
/* hidden wins over every layout rule below, for elements and components alike. */
.html-view [hidden] { display: none !important; }
.html-view { display: flex; flex-direction: column; gap: var(--space-3); min-width: 0; }
.html-view :where(h1, h2, h3, h4) { margin: 0; line-height: 1.25; letter-spacing: -0.01em; }
.html-view h1 { font-size: var(--text-2xl); }
.html-view h2 { font-size: var(--text-xl); letter-spacing: -0.015em; }
.html-view h3 { font-size: var(--text-lg); }
.html-view h4 { font-size: var(--text-md); }
.html-view :where(p, ul, ol, dl, blockquote, figure, pre, table, form, fieldset) { margin: 0; }
.html-view :where(ul, ol) { padding-left: 1.25em; }
.html-view small { color: var(--fg-2); font-size: var(--text-sm); }
.html-view a { color: var(--accent); text-underline-offset: 2px; }
.html-view code, .html-view kbd { font: 0.92em var(--mono); background: var(--bg-2); border-radius: var(--radius-sm); padding: 0.1em 0.3em; }
.html-view pre { font: var(--text-sm)/1.5 var(--mono); background: var(--bg-2); border-radius: var(--radius); padding: var(--space-3); overflow: auto; }
.html-view pre code { background: none; padding: 0; }
.html-view blockquote { border-left: 3px solid var(--border); padding-left: var(--space-3); color: var(--fg-2); }
.html-view hr { border: 0; border-top: 1px solid var(--border); width: 100%; }
.html-view img { max-width: 100%; height: auto; border-radius: var(--radius); }
.html-view :where(ui-card, figure):has(> table) { overflow-x: auto; }
.html-view table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.html-view caption { text-align: left; color: var(--fg-2); font-size: var(--text-sm); padding-bottom: var(--space-1); }
.html-view :where(th, td) { text-align: left; padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); }
.html-view th { font-weight: 600; color: var(--fg-2); font-size: var(--text-sm); }
.html-view tbody tr:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.html-view details { border: 1px solid var(--border); border-radius: var(--radius); padding: var(--space-2) var(--space-3); }
.html-view summary { cursor: pointer; font-weight: 600; }
.html-view form, .html-view fieldset { display: flex; flex-direction: column; gap: var(--space-3); }
.html-view fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: var(--space-3); }
.html-view label { display: flex; flex-direction: column; gap: 6px; font-size: var(--text-sm); font-weight: 500; color: var(--fg-2); }
.html-view label:has(> input[type="checkbox"], > input[type="radio"]) { flex-direction: row; align-items: center; gap: var(--space-2); color: var(--fg); font-size: var(--text-md); }
.html-view :where(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"]), select, textarea) { font: inherit; color: var(--fg); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); min-height: var(--control); padding: 0 12px; transition: border-color var(--duration) var(--ease); }
/* A one-line field never grows taller than a line, even when a plugin gives it a flex size inside this column (a
   `flex: 1 1 12rem` meant for a row once made each account field 12rem tall). */
.html-view :where(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"]), select:not([multiple], [size])) { max-block-size: 48px; }
.html-view a.button { color: var(--fg); text-decoration: none; }
.html-view a.button.primary { color: var(--action-fg); }
.html-view textarea { padding: var(--space-2) 10px; min-height: 96px; resize: vertical; }
.html-view :where(input, select, textarea):hover { border-color: color-mix(in srgb, var(--fg) 30%, var(--border)); }
.html-view :where(input, select, textarea, button, a, summary):focus-visible { outline: 3px solid var(--ring); outline-offset: 1px; }
.html-view :where(input, select, textarea):focus { border-color: var(--accent); }
.html-view input:user-invalid { border-color: var(--danger); }
.html-view button { height: var(--control); padding: 0 16px; border-radius: var(--radius); font-weight: 500; }
.html-view button:hover { background: var(--bg-2); }
.html-view button[type="submit"], .html-view button[data-primary] { background: var(--action); color: var(--action-fg); border-color: var(--action); }
.html-view button[type="submit"]:hover, .html-view button[data-primary]:hover { background: color-mix(in srgb, var(--action) 85%, var(--action-fg)); }
.html-view button:disabled { opacity: 0.5; cursor: not-allowed; }
.html-view progress, .html-view meter { width: 100%; accent-color: var(--accent); }

/* ---- Components ---- */
/* Each ui-* element brings its own style from /elements/. Until its file has loaded, a layout element shows as a plain block. */
:where(ui-stack, ui-grid, ui-card, ui-toolbar, ui-stat, ui-empty, ui-table, ui-form):not(:defined) { display: block; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* Each view entry is drawn by one plugin (data-plugin, set by the host). Containment keeps what it draws inside it:
   !important here, in the first layer, beats any other declaration, so no plugin and no page can take it off. */
[data-plugin] { contain: layout paint !important; overflow-clip-margin: 4px; }

/* Floating slots: a slot the app marks float (<slot name="debug" float>) keeps what is drawn there in a corner of
   the screen. The view's element says which with corner="bottom-left|bottom-right|top-left|top-right" (bottom-left
   if it doesn't say); corner="inline" leaves it in the page's flow. The app decides which slots may float, so a
   plugin can't lift itself over the page. Nothing here names a plugin, slot or element. */
slot[float] > [data-plugin] { position: fixed; z-index: 1000; inset: auto auto var(--space-4) var(--space-4); }
slot[float] > [data-plugin]:has([corner="bottom-right"]) { inset: auto var(--space-4) var(--space-4) auto; }
slot[float] > [data-plugin]:has([corner="top-left"]) { inset: var(--space-4) auto auto var(--space-4); }
slot[float] > [data-plugin]:has([corner="top-right"]) { inset: var(--space-4) var(--space-4) auto auto; }
slot[float] > [data-plugin]:has([corner="inline"]) { position: static; }
/* Floating chrome in the bottom-left corner takes room there, and says so for other chrome (the Customize button). */
body:has(slot[float] > [data-plugin] > [corner="bottom-left"], slot[float] > [data-plugin] > :not([corner])) { --chrome-bottom-left: 52px; }
}

/* The site's own links in an app's header (Home, All demos, Docs, Workbench), pushed to the end. */
body > header nav.site { display: flex; flex-wrap: wrap; gap: 2px; margin-left: auto; }
body > header nav.site a { padding: 6px 10px; border-radius: var(--radius); color: var(--fg-2); text-decoration: none; font-weight: 500; transition: background var(--duration) var(--ease), color var(--duration) var(--ease); }
body > header nav.site a:hover { background: var(--bg-2); color: var(--fg); }
body > header nav.site a[aria-current="page"] { color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); font-weight: 600; }
