/*
The theme contract for @elements/ui. The package's index.css imports the
base stylesheet, then this file, then the default theme; an app adds one
more theme import after @elements/ui to change the look.

Every token a theme controls is declared here with its default, and every
value matches the base stylesheet's default look, so this file alone changes
nothing on screen. A theme overrides these (all of them: see
notes/theme-contract.md). Templates read ONLY tokens: never a literal color,
radius, font, size, shadow or duration.

The tiers are the vocabulary. Each kind of thing on screen reads its own
tier, so a theme can make controls square and dialogs round, or surfaces
flat and overlays lifted:

  control   buttons, inputs, selects, toggles, tab triggers
  item      rows inside a list or menu: menu items, options, calendar days
  surface   cards, panels, tables, accordions: things in the page flow
  overlay   popovers, menus, tooltips, toasts, hover cards
  dialog    modal dialogs, alert dialogs, sheets, drawers
  tag       badges, chips, kbd
*/

@layer tokens {
  :root {
    /* Type: families */

    --font-body:      var(--font-sans);    /* running text */
    --font-heading:   var(--font-sans);    /* h1-h4, titles, display */
    --font-ui:        var(--font-sans);    /* controls, menus, labels, tables */
    /* --font-mono is @elements/style's own and is part of the contract */

    /* Type: weights and tracking */

    --weight-body:      var(--weight-regular);
    --weight-heading:   var(--weight-semibold);
    --weight-ui:        var(--weight-medium);     /* control labels */
    --weight-strong:    var(--weight-semibold);   /* titles inside surfaces */
    --tracking-heading: var(--tracking-tight);
    --tracking-ui:      var(--tracking-normal);
    --leading-heading:  var(--leading-tight);
    --case-ui:          none;                     /* text-transform for control labels */

    /* Type: role sizes */

    --text-display:   var(--text-5xl);
    --text-h1:        var(--text-4xl);
    --text-h2:        var(--text-3xl);
    --text-h3:        var(--text-2xl);
    --text-h4:        var(--text-xl);
    --text-title:     var(--text-lg);     /* dialog, card, sheet title */
    --text-body:      var(--text-base);
    --text-ui:        var(--text-sm);     /* control and menu text */
    --text-ui-sm:     var(--text-xs);
    --text-ui-lg:     var(--text-base);
    --text-caption:   var(--text-xs);     /* descriptions under labels, meta */
    --text-label:     var(--text-sm);     /* form labels */

    /* CONTROL tier */

    --control-height:        2.5rem;
    --control-height-sm:     2rem;
    --control-height-lg:     3rem;
    --control-padding-x:     1rem;
    --control-padding-x-sm:  0.75rem;
    --control-padding-x-lg:  1.25rem;
    --control-gap:           var(--space-2);      /* icon to label */
    --control-border-width:  1px;
    --control-border:        var(--rule);
    --control-bg:            var(--bg);
    --control-shadow:        none;
    /* radius: --radius-control (@elements/style's slot) */

    /* ITEM tier */

    --item-height:       2rem;
    --item-padding-x:    var(--space-2);
    --item-gap:          var(--space-2);
    --radius-item:       var(--radius-sm);
    --item-highlight:    var(--bg-muted);       /* hovered / keyboard-highlighted row */
    --item-highlight-ink: var(--ink);
    --item-selected:     var(--accent-soft);
    --item-selected-ink: var(--ink);

    /* SURFACE tier */

    --surface-bg:            var(--bg);
    --surface-border-width:  1px;
    --surface-border:        var(--rule);
    --surface-shadow:        var(--shadow-xs);
    --surface-padding:       var(--space-6);
    --surface-padding-sm:    var(--space-4);
    --surface-gap:           var(--space-4);
    /* radius: --radius-surface */

    /* OVERLAY tier */

    --overlay-bg:            var(--bg-elevated);
    --overlay-ink:           var(--ink);
    --overlay-border-width:  1px;
    --overlay-border:        var(--rule);
    --overlay-shadow:        var(--shadow-lg);
    --overlay-padding:       var(--space-1);       /* menus, listboxes */
    --overlay-padding-lg:    var(--space-4);       /* popover, hover card */
    --tooltip-bg:            var(--bg-inverse);
    --tooltip-ink:           var(--ink-inverse);
    /* radius: --radius-overlay */

    /* DIALOG tier */

    --dialog-bg:             var(--bg-elevated);
    --dialog-border-width:   1px;
    --dialog-border:         var(--rule);
    --dialog-shadow:         var(--shadow-2xl);
    --dialog-padding:        var(--space-6);
    --dialog-gap:            var(--space-4);
    --dialog-width:          32rem;
    --backdrop:              rgb(8 12 20 / 0.5);
    --backdrop-blur:         0px;
    /* radius: --radius-dialog */

    /* TAG tier */

    --tag-height:     1.375rem;
    --tag-padding-x:  var(--space-2);
    --tag-font-size:  var(--text-xs);
    --tag-weight:     var(--weight-medium);
    /* radius: --radius-tag */

    /* EXTRA RADIUS roles (the rest are @elements/style's) */

    --radius-check:   var(--radius-xs);    /* checkbox box */

    /* Icons */

    --icon-size:      1rem;
    --icon-size-sm:   0.875rem;
    --icon-size-lg:   1.25rem;
    --icon-stroke:    2;

    /* COLOR roles the semantic set does not name */

    --secondary:      var(--bg-muted);       /* secondary button fill */
    --secondary-ink:  var(--ink);
    --secondary-hover: var(--rule);
    --link:           var(--ink);            /* link-variant text */

    /* Focus */

    --focus-ring-width:   3px;
    --focus-ring-offset:  0px;
    --focus-ring-inner:   var(--bg);      /* the gap layer between control and ring, when offset > 0 */
    --focus-ring-color:   color-mix(in oklab, var(--focus) 45%, transparent);
    /* Motion */

    --duration-enter:   var(--duration-normal);
    --duration-exit:    var(--duration-fast);
    --ease-enter:       var(--ease-out);
    --ease-exit:        var(--ease-in);
    --enter-scale:      0.96;     /* overlays and dialogs grow from this */
    --enter-distance:   0.5rem;   /* popovers slide this far from their side */
    --duration-loop:    var(--duration-slower);   /* looping animations multiply it; never 0 */
    --caret-blink:      1120ms;   /* one blink of a drawn text caret */

    /* Parts: sizes and colors particular templates read */

    --check-size:            1rem;                 /* checkbox and radio box; the switch derives from it */
    --control-outline:       color-mix(in oklab, var(--ink-faint), var(--ink) 30%);   /* unchecked check, radio, switch edge: 3:1 on --bg */
    --switch-thumb:          var(--white);         /* switch knob, off */
    --switch-thumb-checked:  var(--accent-ink);    /* switch knob, on */
    --indicator-width:       2px;                  /* active tab line */
    --link-underline-offset: 0.2em;
    --avatar-size:           var(--control-height);
    --avatar-size-sm:        var(--control-height-sm);
    --avatar-size-lg:        var(--control-height-lg);
    --progress-height:       var(--space-2);
    --progress-height-sm:    var(--space-1);
    --progress-height-lg:    var(--space-3);
    --slider-thumb:          1rem;
    --slider-thickness:      0.375rem;
    --table-row-padding-y:   var(--space-2);       /* table body cells */
    --measure-narrow:        24rem;                /* centered short copy (Empty) */
    --toast-width:           var(--container-sm);
    --scrollbar-size:        var(--space-2-5);     /* ScrollArea bar */
    --carousel-gap:          var(--space-4);
    --drawer-canvas:         var(--bg-canvas);     /* around a page a Drawer scales back */
    --drawer-handle-width:   var(--space-24);
    --drawer-handle-height:  var(--space-1-5);
    --drawer-wrapper-inset:  var(--space-3);
    --sidebar-bg:            var(--bg-soft);
    --sidebar-ink:           var(--ink);
    --sidebar-accent:        var(--item-highlight);
    --sidebar-accent-ink:    var(--item-highlight-ink);
    --sidebar-border:        var(--rule);

    /* Code: syntax colors for CodeEditor and MessageResponse */

    --syntax-keyword:     var(--danger-ink);
    --syntax-string:      var(--info-ink);
    --syntax-number:      var(--warning-ink);
    --syntax-function:    var(--success-ink);
    --syntax-type:        var(--success-ink);
    --syntax-property:    var(--link);
    --syntax-comment:     var(--ink-dim);
    --syntax-variable:    var(--ink);
    --syntax-punctuation: var(--ink-soft);
    --code-selection:     color-mix(in oklab, var(--accent) 22%, transparent);
    --code-font-size:     calc(var(--text-ui) * 0.9375);

    /* Charts: the categorical series palette and mark radius */

    /* The dataviz reference palette (blue, orange, aqua, yellow, magenta,
       green, violet, red), validated for adjacent-pair color vision
       separation in both modes, written relative to --info-500 so a theme
       that moves its blue moves the palette. Re-run the validator on change. */
    --chart-1: light-dark(
      oklch(from var(--info-500) calc(l - 0.045) calc(c - 0.037) calc(h + 0.5)),
      oklch(from var(--info-500) calc(l + 0.002) calc(c - 0.039) calc(h + 0.1)));
    --chart-2: light-dark(
      oklch(from var(--info-500) calc(l + 0.051) calc(c - 0.025) calc(h + 145.6)),
      oklch(from var(--info-500) calc(l + 0.002) calc(c - 0.027) calc(h + 145.1)));
    --chart-3: light-dark(
      oklch(from var(--info-500) calc(l + 0.049) calc(c - 0.059) calc(h + 267.1)),
      oklch(from var(--info-500) calc(l + 0.001) calc(c - 0.072) calc(h + 268.1)));
    --chart-4: light-dark(
      oklch(from var(--info-500) calc(l + 0.144) calc(c - 0.039) calc(h + 180.1)),
      oklch(from var(--info-500) calc(l + 0.05) calc(c - 0.057) calc(h + 178.2)));
    --chart-5: light-dark(
      oklch(from var(--info-500) calc(l + 0.096) calc(c - 0.059) calc(h + 102.4)),
      oklch(from var(--info-500) calc(l + 0.002) calc(c - 0.029) calc(h + 105.8)));
    --chart-6: light-dark(
      oklch(from var(--info-500) calc(l - 0.091) calc(c - 0.02) calc(h + 247.5)),
      oklch(from var(--info-500) calc(l - 0.091) calc(c - 0.02) calc(h + 247.5)));
    --chart-7: light-dark(
      oklch(from var(--info-500) calc(l - 0.093) calc(c - 0.003) calc(h + 28.4)),
      oklch(from var(--info-500) calc(l + 0.05) calc(c - 0.055) calc(h + 31.8)));
    --chart-8: light-dark(
      oklch(from var(--info-500) calc(l + 0.003) calc(c - 0.009) calc(h + 129.9)),
      oklch(from var(--info-500) calc(l + 0.049) calc(c - 0.041) calc(h + 127.3)));
    --chart-radius: var(--radius-item);

    /* BRIDGES: @elements/style's own component slots follow the contract,
       so bare <button>, <input>, .card and the rest re-theme with it. */

    --button-height:       var(--control-height);
    --button-height-sm:    var(--control-height-sm);
    --button-height-lg:    var(--control-height-lg);
    --button-padding-x:    var(--control-padding-x);
    --button-padding-x-sm: var(--control-padding-x-sm);
    --button-padding-x-lg: var(--control-padding-x-lg);
    --button-font-size:    var(--text-ui);
    --button-font-size-lg: var(--text-ui-lg);
    --button-font-weight:  var(--weight-ui);
    --input-height:        var(--control-height);
    --input-height-sm:     var(--control-height-sm);
    --input-height-lg:     var(--control-height-lg);
    --input-font-size:     var(--text-ui);
    --input-bg:            var(--control-bg);
    --input-border:        var(--control-border);

    /* DERIVED: --focus-ring is rebuilt from the width, offset, inner layer
       and ring color, so a theme sets those four and never --focus-ring: a
       token one theme sets outside the contract leaks into every other look
       when the catalog loads them all. */
    --focus-ring: 0 0 0 var(--focus-ring-offset) var(--focus-ring-inner),
                  0 0 0 calc(var(--focus-ring-offset) + var(--focus-ring-width)) var(--focus-ring-color);
    --focus-ring-danger: 0 0 0 var(--focus-ring-offset) var(--focus-ring-inner),
                  0 0 0 calc(var(--focus-ring-offset) + var(--focus-ring-width)) var(--danger);
  }

  /* REDUCED MOTION: enter, exit and slow transitions go to zero, so every
     overlay and disclosure appears at once. --duration-loop is left alone:
     a spinner must keep turning (Spinner slows itself). Each theme repeats
     this block under its own look selector so it beats the theme's values. */
  @media (prefers-reduced-motion: reduce) {
    :root {
      --duration-enter: 0s;
      --duration-exit:  0s;
      --duration-slow:  0s;
    }
  }
}

/* BRIDGES: bare elements read the contract's type roles. In @elements/style
   these are --font-sans and fixed sizes; porting folds this into base.css. */
@layer base {
  html,
  body {
    font-family: var(--font-body);
    font-weight: var(--weight-body);
  }

  h1, h2, h3, h4, h5, h6 {
    font-family:    var(--font-heading);
    font-weight:    var(--weight-heading);
    letter-spacing: var(--tracking-heading);
    line-height:    var(--leading-heading);
  }

  h1 { font-size: var(--text-h1); }
  h2 { font-size: var(--text-h2); }
  h3 { font-size: var(--text-h3); }
  h4 { font-size: var(--text-h4); }
}

@layer components {
  :where(button, .button, input, select, textarea, .tab,
         input[type="submit"], input[type="reset"], input[type="button"]) {
    font-family:    var(--font-ui);
    letter-spacing: var(--tracking-ui);
  }

  .card {
    background:    var(--surface-bg);
    border:        var(--surface-border-width) solid var(--surface-border);
    box-shadow:    var(--surface-shadow);
  }
}
