/*
 * 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(#fbfaf8, #1c1b1a);
  --bg-2: light-dark(#f2f0ec, #252321);
  --fg: light-dark(#22201d, #ece9e4);
  --fg-2: light-dark(#5f5a53, #aaa49b);
  --border: light-dark(#dedad3, #3a3734);
  --accent: light-dark(#2563eb, #6b9bff);
  --accent-fg: light-dark(#fff, #0b1020);
  --danger: light-dark(#b42318, #ff7b72);
  --ok: light-dark(#1a7f37, #56d364);
  --warn: light-dark(#9a6700, #e3b341);
  --surface: light-dark(#ffffff, #232120);
  --radius-sm: 4px;
  --radius: 6px;
  --radius-lg: 10px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --text-sm: 12px;
  --text-md: 14px;
  --text-lg: 16px;
  --text-xl: 20px;
  --text-2xl: 26px;
  --shadow-1: 0 1px 2px light-dark(rgb(0 0 0 / 0.06), rgb(0 0 0 / 0.4));
  --duration: 140ms;
  --ease: cubic-bezier(0.2, 0, 0, 1);
  --font: system-ui, 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: 14px/1.45 var(--font); min-height: 100vh; }
/* 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: 4px 12px; align-items: baseline; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--bg-2); }
body:not(.workbench) > header h1 { margin: 0; font-size: var(--text-lg); }
body:not(.workbench) > main, body:not(.workbench) > aside { min-width: 0; padding: 16px; display: flex; flex-direction: column; gap: var(--space-4); }
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: 6px 16px; 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); }
}

/* ---- The 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; 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; }
button { font: inherit; height: 28px; padding: 0 10px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--bg); color: var(--fg); cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button:focus-visible, textarea:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
ul.list { list-style: none; margin: 0; padding: 0; }
ul.list li button { width: 100%; text-align: left; border: 0; background: 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; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); color: var(--fg); resize: vertical; }
label.field { display: flex; flex-direction: column; gap: 2px; font-size: 13px; }
label.field.checkbox { flex-direction: row; align-items: center; gap: 6px; }
label.field input:not([type="checkbox"]), label.field select { font: inherit; height: 28px; padding: 0 6px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); color: var(--fg); }
label.field :focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
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: 8px 12px; border-radius: var(--radius); background: var(--fg); color: var(--bg); }
/* 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 and panel, on every app page. */
.customize-button { position: fixed; left: 16px; bottom: 16px; z-index: 10; box-shadow: var(--shadow-1); background: var(--surface); }
dialog.customize { width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: var(--space-5); border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); color: var(--fg); font: var(--text-md)/1.5 var(--font); }
dialog.customize::backdrop { background: rgb(0 0 0 / 0.35); }
dialog.customize h2 { font-size: var(--text-lg); }
dialog.customize h3 { font-size: var(--text-md); margin: var(--space-4) 0 var(--space-2); }
dialog.customize p { margin: var(--space-1) 0; }
.customize-list, .customize-places { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-3); }
.customize-list > li { padding: var(--space-3); border: 1px solid var(--border); border-radius: var(--radius); }
.customize-places { gap: var(--space-1); margin-top: var(--space-2); font-size: var(--text-sm); }
.customize-places code { font: var(--text-sm) var(--mono); }
.customize-places button { margin-left: var(--space-2); height: 24px; }
.customize-actions, .customize-url { display: flex; gap: var(--space-2); margin-top: var(--space-2); flex-wrap: wrap; }
.customize-add { display: grid; gap: var(--space-2); justify-items: start; }
.customize-add textarea, .customize-add .customize-url { justify-self: stretch; }
.customize-add textarea { min-height: 96px; font: var(--text-sm)/1.5 var(--mono); }
.customize-url input { flex: 1; min-width: 0; font: inherit; height: 28px; padding: 0 6px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); color: var(--fg); }
.customize-close { margin-top: var(--space-4); }

/* 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); }
.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: var(--space-1); font-size: var(--text-sm); 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: 32px; padding: 0 var(--space-2); transition: border-color var(--duration) var(--ease); }
.html-view textarea { padding: var(--space-2); 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 color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 1px; }
.html-view input:user-invalid { border-color: var(--danger); }
.html-view button { height: 32px; padding: 0 var(--space-3); border-radius: var(--radius); font-weight: 500; transition: background var(--duration) var(--ease); }
.html-view button:hover { background: var(--bg-2); }
.html-view button[type="submit"], .html-view button[data-primary] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.html-view button[type="submit"]:hover, .html-view button[data-primary]:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
.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. */
[data-plugin] { contain: layout paint; overflow-clip-margin: 4px; }

/* The "debug" slot floats: what is drawn there (std/debug-console.js's bug) stays in a corner of the screen, where <ui-debug corner> says. */
slot[name="debug"] > [data-plugin] { position: fixed; z-index: 1000; inset: auto auto var(--space-4) var(--space-4); }
slot[name="debug"] > [data-plugin]:has(ui-debug[corner="bottom-right"]) { inset: auto var(--space-4) var(--space-4) auto; }
slot[name="debug"] > [data-plugin]:has(ui-debug[corner="top-left"]) { inset: var(--space-4) auto auto var(--space-4); }
slot[name="debug"] > [data-plugin]:has(ui-debug[corner="top-right"]) { inset: var(--space-4) var(--space-4) auto auto; }
slot[name="debug"] > [data-plugin]:has(ui-debug[corner="inline"]) { position: static; }
}

/* 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: 4px 12px; margin-left: auto; }
body > header nav.site a[aria-current="page"] { color: inherit; text-decoration: none; font-weight: 600; }
