/* ==========================================================================
   XUI CORE STYLESHEET
   Reset, derived tokens, base typography, layout primitives, utilities and
   the three global surfaces (toaster, portal, device frame).
   Component-specific rules live in xui/components/<name>/component.css.
   ========================================================================== */

/* ---------- derived tokens ---------- */
:root,
[data-theme] {
  --radius-xs: calc(var(--radius) - 6px);
  --radius-sm: calc(var(--radius) - 4px);
  --radius-md: calc(var(--radius) - 2px);
  --radius-lg: var(--radius);
  --radius-xl: calc(var(--radius) + 4px);
  --radius-2xl: calc(var(--radius) + 12px);
  --radius-full: 9999px;

  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.25rem;
  --text-2xl: 1.5rem;
  --text-3xl: 1.875rem;
  --text-4xl: 2.25rem;
  --text-5xl: 3rem;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;
  --space-16: 4rem;

  --control-h-sm: 2rem;
  --control-h: 2.25rem;
  --control-h-lg: 2.75rem;
  --tap: 44px;                /* minimum touch target */
  --header-h: 3.5rem;
  --sidebar-w: 16rem;
  --sidebar-w-icon: 3.25rem;
  --z-sticky: 30;
  --z-drawer: 50;
  --z-overlay: 60;
  --z-modal: 70;
  --z-popover: 80;
  --z-toast: 90;
}

/* ---------- reset ---------- */
*,
*::before,
*::after { box-sizing: border-box; }

* { margin: 0; }

html {
  -webkit-text-size-adjust: 100%;
  -moz-tab-size: 4;
  tab-size: 4;
  scroll-behavior: smooth;
}

body {
  min-height: 100dvh;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  line-height: 1.5;
  letter-spacing: var(--tracking-normal);
  color: var(--foreground);
  background: var(--background);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  transition: background-color var(--dur-base) var(--ease-out),
              color var(--dur-base) var(--ease-out);
}

img, svg, video, canvas, audio, iframe, embed, object { display: block; max-width: 100%; }
img, video { height: auto; }

input, button, textarea, select {
  font: inherit;
  color: inherit;
  letter-spacing: inherit;
}

button { background: none; border: none; padding: 0; cursor: pointer; }

p, h1, h2, h3, h4, h5, h6 { overflow-wrap: break-word; }

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-display);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
  line-height: 1.25;
}

a { color: inherit; text-decoration: none; }

hr { border: none; border-top: 1px solid var(--border); }

ul, ol { padding: 0; list-style: none; }

table { border-collapse: collapse; border-spacing: 0; }

code, pre, kbd, samp { font-family: var(--font-mono); font-size: 0.9em; }

[hidden] { display: none !important; }

::placeholder { color: var(--muted-foreground); opacity: 1; }

::selection { background: color-mix(in oklab, var(--primary) 25%, transparent); }

/* ---------- focus ---------- */
:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
}
:focus:not(:focus-visible) { outline: none; }

/* ---------- scrollbars ---------- */
* { scrollbar-width: thin; scrollbar-color: color-mix(in oklab, var(--foreground) 22%, transparent) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: color-mix(in oklab, var(--foreground) 18%, transparent);
  border: 3px solid transparent;
  background-clip: content-box;
  border-radius: 9999px;
}
::-webkit-scrollbar-thumb:hover { background: color-mix(in oklab, var(--foreground) 32%, transparent); background-clip: content-box; }

/* ---------- page shell ----------
   #xui-root is a *container*, not just a div: every responsive rule in xui is a
   container query against it. That is what makes the mobile/desktop preview
   toggle behave exactly like a real narrow viewport — the layout reacts to the
   frame width, not to the browser width.
   Floating layers (dialog, popover, menus) are portaled to #xui-portal, which
   sits outside the container so position:fixed still means "the viewport".   */
#xui-root {
  container-type: inline-size;
  container-name: xui-device;
}
.xui-page { min-height: 100dvh; display: flex; flex-direction: column; }

/* The portal generates no box of its own: its children are all fixed or
   absolute, and a real box would become a stray flex item of whatever it sits
   in (the mobile preview turns <body> into a flex row). */
.xui-portal { display: contents; }

/* ---------- layout primitives ---------- */
.xui-box { display: block; }

.xui-row, .xui-col, .xui-stack {
  display: flex;
  min-width: 0;
}
.xui-row { flex-direction: row; align-items: center; }
.xui-col, .xui-stack { flex-direction: column; }
.xui-row[data-wrap="true"] { flex-wrap: wrap; }

.xui-grid { display: grid; min-width: 0; }

.xui-container {
  width: 100%;
  margin-inline: auto;
  padding-inline: var(--space-4);
  max-width: var(--container-max, 80rem);
}
@media (min-width: 768px) { .xui-container { padding-inline: var(--space-6); } }

.xui-spacer { flex: 1 1 auto; min-width: 0; min-height: 0; }
.xui-center { display: flex; align-items: center; justify-content: center; }

/* ---------- utilities (small, deliberate set) ---------- */
.xui-muted { color: var(--muted-foreground); }
.xui-truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.xui-clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.xui-clamp-3 { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.xui-mono { font-family: var(--font-mono); }
.xui-nowrap { white-space: nowrap; }
.xui-full { width: 100%; }
.xui-sr {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.xui-scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.xui-scroll-y { overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* responsive visibility — container based, so the device toggle drives it */
.xui-only-mobile { display: none; }
@container xui-device (max-width: 767px) {
  .xui-only-desktop { display: none !important; }
  .xui-only-mobile { display: revert; }
}
@supports not (container-type: inline-size) {
  @media (max-width: 767px) {
    .xui-only-desktop { display: none !important; }
    .xui-only-mobile { display: revert; }
  }
}

/* ---------- icons ---------- */
.xui-icon {
  display: inline-block;
  flex: none;
  vertical-align: middle;
  pointer-events: none;
}

/* ---------- shared interactive surface helpers ---------- */
.xui-surface {
  background: var(--card);
  color: var(--card-foreground);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

/* The scrim behind a modal surface.
   It is ALWAYS a child of a wrapper that already carries the real z-index
   (.xui-dialog, .xui-sheet, .xui-drawer, .xui-command), so it must sit at the
   bottom of that wrapper's stacking context — z-index 0, with the panel above
   it at 1. Giving it --z-overlay here would paint the scrim, and its blur, on
   top of the very panel it is meant to sit behind. */
.xui-overlay {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: color-mix(in oklab, black 55%, transparent);
  backdrop-filter: blur(2px);
  animation: xui-fade-in var(--dur-base) var(--ease-out);
}
.xui-overlay[data-state="closed"] { animation: xui-fade-out var(--dur-fast) var(--ease-out); }

.xui-ring-offset { box-shadow: 0 0 0 2px var(--background), 0 0 0 4px var(--ring); }

/* ---------- shared form control surface ----------
   Input, Textarea, Select, NativeSelect and Combobox all sit on this base so
   a form line stays exactly the same height whatever it is made of. */
.xui-control {
  display: flex;
  align-items: center;
  width: 100%;
  min-width: 0;
  height: var(--control-h);
  padding-inline: var(--space-3);
  background: var(--background);
  color: var(--foreground);
  border: 1px solid var(--input);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  box-shadow: var(--shadow-xs);
  transition: border-color var(--dur-fast) var(--ease-out),
              box-shadow var(--dur-fast) var(--ease-out),
              background-color var(--dur-fast) var(--ease-out);
}
.xui-control:hover:not([disabled]):not([aria-disabled="true"]) {
  border-color: color-mix(in oklab, var(--ring) 45%, var(--input));
}
.xui-control:focus,
.xui-control:focus-within,
.xui-control[data-state="open"] {
  outline: none;
  border-color: var(--ring);
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--ring) 22%, transparent);
}
.xui-control[disabled],
.xui-control[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
  background: var(--muted);
}
.xui-control[data-invalid="true"] {
  border-color: var(--destructive);
}
.xui-control[data-invalid="true"]:focus,
.xui-control[data-invalid="true"]:focus-within {
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--destructive) 22%, transparent);
}
.xui-control--sm { height: var(--control-h-sm); font-size: var(--text-xs); padding-inline: var(--space-2); }
.xui-control--lg { height: var(--control-h-lg); font-size: var(--text-base); padding-inline: var(--space-4); }

/* on real phones a <16px font makes iOS zoom on focus */
@media (pointer: coarse) {
  .xui-control { min-height: var(--tap); font-size: 1rem; }
}

/* field-level helper text, shared by Field / Input / Textarea / Select */
.xui-hint { font-size: var(--text-xs); color: var(--muted-foreground); line-height: 1.45; }
.xui-error {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-size: var(--text-xs);
  color: var(--destructive);
  font-weight: var(--weight-medium);
  line-height: 1.45;
}

/* ---------- shared floating menu surface ----------
   Select, Combobox, DropdownMenu, ContextMenu, Menubar and Command all render
   their popup with these classes, so every menu in the app has one look. */
.xui-menu {
  --menu-pad: 0.25rem;
  position: fixed;
  z-index: var(--z-popover);
  min-width: 8rem;
  max-height: min(22rem, 60vh);
  display: flex;
  flex-direction: column;
  padding: var(--menu-pad);
  background: var(--popover);
  color: var(--popover-foreground);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: xui-zoom-in var(--dur-fast) var(--ease-out);
}
.xui-menu[data-state="closed"] { animation: xui-fade-out 100ms var(--ease-out) forwards; }
.xui-menu[data-side="top"] { transform-origin: bottom; }

.xui-menu__list { overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 1px; }
.xui-menu:focus, .xui-menu:focus-visible { outline: none; }
.xui-menu__item:focus, .xui-menu__item:focus-visible {
  outline: none;
  background: var(--accent);
  color: var(--accent-foreground);
}
.xui-menu__item--destructive:focus,
.xui-menu__item--destructive:focus-visible {
  background: color-mix(in oklab, var(--destructive) 12%, transparent);
  color: var(--destructive);
}

.xui-menu__item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: 0.4rem 0.5rem;
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  line-height: 1.35;
  text-align: left;
  color: inherit;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.xui-menu__item:hover,
.xui-menu__item[data-active="true"] { background: var(--accent); color: var(--accent-foreground); }
.xui-menu__item[aria-selected="true"] { font-weight: var(--weight-medium); }
.xui-menu__item[aria-disabled="true"] { opacity: 0.45; pointer-events: none; }
.xui-menu__item--destructive { color: var(--destructive); }
.xui-menu__item--destructive:hover,
.xui-menu__item--destructive[data-active="true"] {
  background: color-mix(in oklab, var(--destructive) 12%, transparent);
  color: var(--destructive);
}

.xui-menu__text { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; flex: 1; }
.xui-menu__desc { font-size: var(--text-xs); color: var(--muted-foreground); white-space: normal; }
.xui-menu__hint {
  margin-left: auto;
  font-size: var(--text-xs);
  color: var(--muted-foreground);
  font-family: var(--font-mono);
  white-space: nowrap;
}
.xui-menu__check { margin-left: auto; color: var(--primary); display: flex; }
.xui-menu__tick { display: flex; width: 0.9rem; flex: none; color: var(--primary); }

.xui-menu__label {
  padding: 0.4rem 0.5rem 0.25rem;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-caps);
  text-transform: var(--case-label);
  color: var(--muted-foreground);
}
.xui-menu__sep { height: 1px; margin: var(--menu-pad) calc(var(--menu-pad) * -1); background: var(--border); flex: none; }

.xui-menu__search {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0.35rem 0.5rem;
  margin: calc(var(--menu-pad) * -1) calc(var(--menu-pad) * -1) var(--menu-pad);
  border-bottom: 1px solid var(--border);
  color: var(--muted-foreground);
}
.xui-menu__search input {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  outline: none;
  font-size: var(--text-sm);
  color: var(--foreground);
}
.xui-menu__empty {
  padding: var(--space-4) var(--space-3);
  text-align: center;
  font-size: var(--text-sm);
  color: var(--muted-foreground);
}

/* ---------- toaster ---------- */
.xui-toaster {
  position: fixed;
  z-index: var(--z-toast);
  bottom: max(var(--space-4), env(safe-area-inset-bottom));
  right: var(--space-4);
  left: var(--space-4);
  display: flex;
  flex-direction: column-reverse;
  gap: var(--space-2);
  pointer-events: none;
}
@media (min-width: 640px) {
  .xui-toaster { left: auto; width: 22rem; }
}
.xui-toaster > * { pointer-events: auto; }

/* ---------- animations ---------- */
@keyframes xui-fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes xui-fade-out { from { opacity: 1; } to { opacity: 0; } }
@keyframes xui-zoom-in { from { opacity: 0; transform: scale(0.96) translateY(4px); } to { opacity: 1; transform: none; } }
@keyframes xui-slide-up { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes xui-slide-down { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
@keyframes xui-slide-in-right { from { transform: translateX(100%); } to { transform: none; } }
@keyframes xui-slide-in-left { from { transform: translateX(-100%); } to { transform: none; } }
@keyframes xui-slide-in-bottom { from { transform: translateY(100%); } to { transform: none; } }
@keyframes xui-spin { to { transform: rotate(360deg); } }
@keyframes xui-pulse { 50% { opacity: 0.45; } }
@keyframes xui-shimmer { 100% { transform: translateX(100%); } }

/* the animated element flash used by Component.js('flash') */
@keyframes xui-flash {
  0% { box-shadow: 0 0 0 0 color-mix(in oklab, var(--ring) 60%, transparent); }
  100% { box-shadow: 0 0 0 10px transparent; }
}
.xui-flash { animation: xui-flash 700ms var(--ease-out); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- device preview frame ----------
   The frame is a real scrollport, and because #xui-root is a CSS container it
   is also the containing block for position:fixed descendants. That is what
   lets a dialog, a sheet or a toast land *inside* the phone instead of over the
   whole screen — the runtime portals them into #xui-portal-device, which lives
   inside the root, whenever data-device is "mobile".                        */
html[data-device="mobile"] body {
  background: color-mix(in oklab, var(--foreground) 9%, var(--background));
  padding: var(--space-6) var(--space-4);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: hidden;                 /* the frame scrolls, the page behind does not */
}

html[data-device="mobile"] #xui-root {
  flex: none;
  width: 100%;
  max-width: 420px;
  height: min(860px, calc(100dvh - 3rem));
  min-height: 0;                    /* beats .xui-page's min-height: 100dvh */
  overflow: hidden auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  background: var(--background);
  border: 10px solid color-mix(in oklab, var(--foreground) 82%, var(--background));
  border-radius: 2.5rem;
  box-shadow: var(--shadow-xl);
}
html[data-device="mobile"] #xui-root::-webkit-scrollbar { width: 0; }

/* the body-level hosts are unused in preview mode; the device-scoped ones are
   unused outside it */
html[data-device="mobile"] > body > #xui-toasts { display: none; }
html:not([data-device="mobile"]) #xui-toasts-device { display: none; }

/* inside the frame the toaster spans the phone, not the browser window */
html[data-device="mobile"] #xui-toasts-device {
  position: fixed;
  inset: auto var(--space-3) var(--space-4) var(--space-3);
  width: auto;
}

/* a fixed overlay inside the frame must round with it, or it spills over the
   bezel's corners */
html[data-device="mobile"] #xui-portal-device > .xui-overlay,
html[data-device="mobile"] #xui-portal-device > .xui-dialog > .xui-overlay,
html[data-device="mobile"] #xui-portal-device > .xui-sheet > .xui-overlay,
html[data-device="mobile"] #xui-portal-device > .xui-drawer > .xui-overlay {
  border-radius: 1.9rem;
}

/* ---------- websocket status pill ---------- */
.xui-conn {
  position: fixed;
  left: 50%;
  top: var(--space-4);
  translate: -50% 0;
  z-index: 999;
  display: none;
  gap: var(--space-2);
  align-items: center;
  padding: 0.375rem 0.75rem;
  border-radius: var(--radius-full);
  background: var(--destructive);
  color: var(--destructive-foreground);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  box-shadow: var(--shadow-lg);
}
html[data-conn="down"] .xui-conn { display: flex; animation: xui-slide-down var(--dur-base) var(--ease-out); }

/* ------------------------------------------------------------------
   Responsive by default: on a small screen every layout reflows —
   rows wrap, fields go fluid, wide things scroll inside themselves —
   without any component asking for it. */
@media (max-width: 640px) {
  .xui-row { flex-wrap: wrap; }
  .xui-row > * { min-width: 0; }
  .xui-field { flex: 1 1 12rem; max-width: 100%; }
  .xui-card { max-width: 100%; }
  .xui-menu { max-width: calc(100vw - 0.75rem); }
  .xui-menu__item { padding-top: 0.55rem; padding-bottom: 0.55rem; }
  .xui-table { display: block; overflow-x: auto; }
  img, video, canvas { max-width: 100%; height: auto; }
}
