/* Hand-written base styles of the design-language CSS package; values come from tokens.css.
   Browsers: Safari 18.4, Chrome 131, Firefox 143 */

@layer design-language.tokens, design-language.base, design-language.layout;

@layer design-language.base {

  /* Typography (typography.md). The root font size stays at the browser default: Type Levels are in rem. */
  html {
    font-family: var(--font-stack);
    font-size-adjust: ex-height 0.508;
    font-synthesis: none;
    font-optical-sizing: auto;
    font-variant-numeric: tabular-nums;
  }

  /* Each Type Level sets size, line height and weight, and the stroke of the Icons inside it (ICN-3). */
  .type-title, h1 {
    font-size: var(--type-title-size);
    line-height: var(--type-title-line);
    font-weight: var(--type-title-weight);
    --icon-stroke: var(--type-title-stroke);
  }

  .type-heading, h2, h3, h4 {
    font-size: var(--type-heading-size);
    line-height: var(--type-heading-line);
    font-weight: var(--type-heading-weight);
    --icon-stroke: var(--type-heading-stroke);
  }

  .type-text, body {
    font-size: var(--type-text-size);
    line-height: var(--type-text-line);
    font-weight: var(--type-text-weight);
    --icon-stroke: var(--type-text-stroke);
  }

  .type-compact {
    font-size: var(--type-compact-size);
    line-height: var(--type-compact-line);
    font-weight: var(--type-compact-weight);
    --icon-stroke: var(--type-compact-stroke);
  }

  .type-caption, th, small {
    font-size: var(--type-caption-size);
    line-height: var(--type-caption-line);
    font-weight: var(--type-caption-weight);
    --icon-stroke: var(--type-caption-stroke);
  }

  /* Raw data (coordinates, code) in the Monospace stack, at the size of the text around it. */
  code, kbd, samp, pre {
    font-family: var(--font-stack-mono);
    font-size: inherit;
  }

  /* Maps (data-colour.md): a map is content and stays in its light style in every mode, so the Data Colours
     inside it resolve to their light values. Nothing else is set here. */
  .map-content {
    color-scheme: light;
  }

  /* Map Label (LAY-9): the name of an object at its place on the map. It belongs to the map's drawing, so it stays
     light like the map wherever it is placed; one line at Caption, 20 px high with the tooltip radius (RAD-1). */
  .map-label {
    color-scheme: light;
    display: inline-flex;
    align-items: center;
    block-size: 20px; /* literal: the Height of the tooltip row of radius.md */
    padding-inline: var(--space-element);
    white-space: nowrap;
    font-size: var(--type-caption-size);
    line-height: var(--type-caption-line);
    font-weight: var(--type-caption-weight);
    color: var(--text);
    background: var(--background);
    border: var(--hairline) solid var(--separator);
    border-radius: var(--radius-tooltip);
  }

  /* Badge (LAY-9): one line beside an object's name that states a property of it. It is no own surface: no line,
     no shadow, and it follows the mode of the interface, unlike a Map Label; 20 px high with the tooltip radius (RAD-1). */
  .badge {
    display: inline-flex;
    align-items: center;
    block-size: 20px; /* literal: the Height of the tooltip row of radius.md */
    padding-inline: var(--space-element);
    white-space: nowrap;
    font-size: var(--type-caption-size);
    line-height: var(--type-caption-line);
    font-weight: var(--type-caption-weight);
    color: var(--text);
    background: var(--fill);
    border-radius: var(--radius-tooltip);
  }

  /* Card (LAY-11): a box that stands for one object as a whole, on background only. Inner padding Group (SPC-1),
     concentric with the card radius (RAD-2), opaque, no line, no shadow. An Own Surface (LAY-2) has no class. */
  .card {
    background: var(--surface);
    padding: var(--space-group);
    border-radius: var(--radius-card);
  }
  /* A selected Card (LAY-9): signal over surface, so it stays raised above background. The ground alone marks it;
     the Product sets aria-selected in a listbox or grid, aria-current on the object shown. */
  .card[aria-current="true"], .card[aria-selected="true"] {
    background: var(--signal-tint-surface);
  }

  /* Icons (icons.md): <svg class="icon" aria-hidden="true"><use href="icons.svg#close"/></svg>.
     Size is the line height of the surrounding Type Level, the stroke its --icon-stroke in em, not scaled
     (vector-effect sits on the shapes of icons.svg), the colour the text colour. */
  .icon {
    inline-size: 1lh;
    block-size: 1lh;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: var(--icon-stroke);
  }
}

/* Native elements and the states of Controls (colour.md, ticket #66, decisions #39 and #51).
   Hooks: elements for native controls; data-main, data-destructive, data-icon-only for variants;
   data-line="sequence|flush" for the only separating lines (SPC-6).
   States, all changes without transition (a Control answers at once):
   - no hover style, only cursor: pointer;
   - a button lies on fill with a transparent 1 px border (the height stays); fields, selects and the segmented control keep the control-border;
   - pressed (:active:not(:disabled) or aria-pressed="true"): fill mixed 15 % toward text; pressed data-main: signal mixed 15 % toward text;
     data-icon-only has no surface until it is pressed, then fill;
   - disabled: text and Icon in text-disabled, border in separator (fields), background unchanged, cursor: default, no opacity;
     disabled data-main: fill with text-disabled;
   - selected segment: fill;
   - aria-invalid="true": border in error;
   - accent-color: signal for the controls the browser draws itself (checkbox, radio, range, progress). */
@layer design-language.base {
  *, ::before, ::after { box-sizing: border-box; }

  button, input, select, textarea {
    font: inherit;
    font-size-adjust: inherit;
    color: var(--text);
    background: var(--background);
    border: var(--hairline) solid var(--control-border);
    border-radius: var(--radius-control);
    min-block-size: var(--hit);
    padding-inline: var(--space-element);
    margin: 0;
    accent-color: var(--signal);
  }

  input[type="checkbox"], input[type="radio"] {
    min-block-size: 0;
    padding: 0;
  }

  textarea { padding-block: var(--space-element); }
  input::placeholder, textarea::placeholder { color: var(--text-secondary); }

  button {
    background: var(--fill);
    border-color: transparent;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-pair);
    cursor: pointer;
  }

  button[data-main] {
    background: var(--signal);
    color: var(--on-signal);
    border-color: var(--signal);
  }

  button[data-destructive] { color: var(--error); }

  button[data-icon-only] {
    inline-size: var(--hit);
    padding: 0;
    background: transparent;
    color: var(--text-secondary);
  }

  /* Focus ring (COL-10) */
  :focus-visible,
  fieldset[data-segmented] label:has(:focus-visible) {   /* the radio input itself is invisible */
    outline: 2px solid var(--signal);
    outline-offset: 2px;
  }

  /* Pressed */
  button:active:not(:disabled),
  button[aria-pressed="true"] {
    background: color-mix(in oklab, var(--fill), var(--text) 15%);
    color: var(--text);
  }

  button[data-icon-only]:active:not(:disabled),
  button[data-icon-only][aria-pressed="true"] {
    background: var(--fill);
  }

  button[data-main]:active:not(:disabled),
  button[data-main][aria-pressed="true"] {
    background: color-mix(in oklab, var(--signal), var(--text) 15%);
    color: var(--on-signal);
  }

  /* Invalid */
  input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] {
    border-color: var(--error);
  }

  /* Disabled: after the variants, so it wins at equal specificity */
  :is(button, input, select, textarea):disabled {
    color: var(--text-disabled);
    border-color: var(--separator);
    cursor: default;
  }

  button[data-main]:disabled {
    background: var(--fill);
    color: var(--text-disabled);
    border-color: transparent;
  }

  /* Plain buttons have no border; the :not() lifts the specificity above the generic disabled rule, so rule order is irrelevant. */
  button:not([data-main]):disabled { border-color: transparent; }
  button[data-icon-only]:disabled { background: transparent; }

  /* Segmented control: radio inputs in a fieldset, the look needs no script. */
  fieldset[data-segmented] {
    position: relative;   /* contains the hidden radio inputs, so they never extend a scroll area */
    display: inline-flex;
    margin: 0;
    padding: 2px;
    gap: 2px;
    min-inline-size: 0;
    border: var(--hairline) solid var(--control-border);
    border-radius: var(--radius-control);
  }

  fieldset[data-segmented] label {
    display: inline-flex;
    align-items: center;
    /* Fits inside the fieldset: --hit minus its 2 x 2px padding and its two hairline borders */
    min-block-size: calc(var(--hit) - 4px - 2 * var(--hairline));
    padding-inline: var(--space-element);
    border-radius: var(--radius-segment);
    cursor: pointer;
  }

  fieldset[data-segmented] input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
  }

  fieldset[data-segmented] label:has(:checked) { background: var(--fill); }
  fieldset[data-segmented] label:has(:disabled) { color: var(--text-disabled); cursor: default; }
  fieldset[data-segmented]:has(:disabled) { border-color: var(--separator); }
}

/* Web Presentations float in the Functional Layer: opaque, concentric, no shadow (RAD-2). dialog, popover and menu appear at once (MOT-1). */
@layer design-language.base {
  dialog {
    color: var(--text);
    background: var(--surface);
    border: 0;
    border-radius: var(--radius-dialog);
    padding: var(--space-element);
  }

  dialog::backdrop { background: color-mix(in srgb, var(--text) 30%, transparent); }

  [popover] {
    color: var(--text);
    background: var(--surface);
    border: 0;
    border-radius: var(--radius-menu);
    padding: var(--space-pair);
    margin: 0;
  }
}

/* Collapsible area: the one thing that moves on a click (MOT-1). interpolate-size is the silent exception:
   without it (Safari, Firefox) the area snaps open instead of animating. */
@layer design-language.base {
  html { interpolate-size: allow-keywords; }

  details::details-content {
    block-size: 0;
    overflow: clip;
    transition: block-size var(--motion), content-visibility var(--motion) allow-discrete;
  }

  details[open]::details-content { block-size: auto; }

  /* Disclosure: Expand stands first in the summary (the Product writes it first; the order is not set here),
     replaces the native marker and turns from the end of the row (0) to down (90deg) with the area it opens.
     The only rotation of an Icon in the package. */
  summary { list-style: none; }
  summary::-webkit-details-marker { display: none; }
  summary::marker { content: none; }

  summary .icon:has(use[href$="#expand"]) {
    rotate: 0deg;
    transition: rotate var(--motion);
  }

  details[open] > summary .icon:has(use[href$="#expand"]) { rotate: 90deg; }
}

/* Tables: gaps are Spacing Roles (SPC-2); a line exists only with its case (SPC-6). */
@layer design-language.base {
  table { border-collapse: separate; border-spacing: 0; }
  th { text-align: start; color: var(--text-secondary); padding: 0 var(--space-group) var(--space-pair) 0; }
  td { padding: var(--space-element) var(--space-group) var(--space-element) 0; }

  [data-line="sequence"] > tbody > tr > * { border-block-end: var(--hairline) solid var(--separator); }
  [data-line="sequence"] > tbody > tr:last-child > * { border-block-end: 0; }
}
