/*
 * lateshiftcloud.com UI kit (docs/ui-kit.md). One stylesheet for mini apps
 * and Studio pages: the Studio's design tokens (the same palette as V2,
 * studio-shell/public/tokens.css; tests/ui-kit.test.mjs keeps them equal) and
 * a small set of components. Link it with
 *   <link rel="stylesheet" href="/ui-kit/lsc.css">
 * Light and dark follow the system, or the Studio's own theme when lsc.js
 * (or the page) sets html.dark / html.light.
 */
:root {
  --lsc-bg: #fafafa;
  --lsc-surface: #ffffff;
  --lsc-surface-muted: #f4f4f5;
  --lsc-text: #27272a;
  --lsc-text-muted: #71717a;
  --lsc-border: #e4e4e7;
  --lsc-border-strong: #d4d4d8;
  --lsc-accent: oklch(0.488 0.217 264);
  --lsc-accent-strong: oklch(0.43 0.217 264);
  --lsc-on-accent: #ffffff;
  --lsc-info: #0ea5e9;
  --lsc-success: #10b981;
  --lsc-warning: #f59e0b;
  --lsc-danger: #ef4444;
  --lsc-shadow: 0 10px 28px rgb(17 24 39 / 14%);
  --lsc-hover: rgb(0 0 0 / 4%);
  --lsc-press: rgb(0 0 0 / 7%);
  --lsc-scrim: rgb(0 0 0 / 32%);

  /* Square and flat (UI rules, docs/ui-kit.md): corners 2–4 px; 6 px only
     for sheets and dialogs. */
  --lsc-radius-sm: 2px;
  --lsc-radius: 4px;
  --lsc-radius-lg: 4px;
  --lsc-radius-xl: 6px;
  --lsc-radius-full: 999px;

  --lsc-space-1: 4px;
  --lsc-space-2: 8px;
  --lsc-space-3: 12px;
  --lsc-space-4: 16px;
  --lsc-space-5: 20px;
  --lsc-space-6: 24px;
  --lsc-space-8: 32px;
  --lsc-space-10: 40px;

  --lsc-font: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --lsc-font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --lsc-text-xs: 12px;
  --lsc-text-sm: 13px;
  --lsc-text-base: 14px;
  --lsc-text-md: 15px;
  --lsc-text-lg: 17px;
  --lsc-text-xl: 20px;
  --lsc-text-2xl: 24px;

  --lsc-control-h: 36px;
  --lsc-bar-h: 36px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not(.light) {
    --lsc-bg: #0a0a0a;
    --lsc-surface: #101010;
    --lsc-surface-muted: #19191b;
    --lsc-text: #f1f3f7;
    --lsc-text-muted: #a3a3a3;
    --lsc-border: rgb(255 255 255 / 8%);
    --lsc-border-strong: rgb(255 255 255 / 14%);
    --lsc-accent: oklch(0.588 0.217 264);
    --lsc-accent-strong: oklch(0.67 0.19 264);
    --lsc-on-accent: #ffffff;
    --lsc-info: #38bdf8;
    --lsc-success: #34d399;
    --lsc-warning: #fbbf24;
    --lsc-danger: #f87171;
    --lsc-shadow: 0 10px 28px rgb(0 0 0 / 50%);
    --lsc-hover: rgb(255 255 255 / 5%);
    --lsc-press: rgb(255 255 255 / 9%);
    --lsc-scrim: rgb(0 0 0 / 55%);
    color-scheme: dark;
  }
}

:root.dark {
  --lsc-bg: #0a0a0a;
  --lsc-surface: #101010;
  --lsc-surface-muted: #19191b;
  --lsc-text: #f1f3f7;
  --lsc-text-muted: #a3a3a3;
  --lsc-border: rgb(255 255 255 / 8%);
  --lsc-border-strong: rgb(255 255 255 / 14%);
  --lsc-accent: oklch(0.588 0.217 264);
  --lsc-accent-strong: oklch(0.67 0.19 264);
  --lsc-on-accent: #ffffff;
  --lsc-info: #38bdf8;
  --lsc-success: #34d399;
  --lsc-warning: #fbbf24;
  --lsc-danger: #f87171;
  --lsc-shadow: 0 10px 28px rgb(0 0 0 / 50%);
  --lsc-hover: rgb(255 255 255 / 5%);
  --lsc-press: rgb(255 255 255 / 9%);
  --lsc-scrim: rgb(0 0 0 / 55%);
  color-scheme: dark;
}

/* ---- Base ---------------------------------------------------------------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}
body {
  margin: 0;
  background: var(--lsc-bg);
  color: var(--lsc-text);
  font: var(--lsc-text-base) / 1.45 var(--lsc-font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
:focus-visible {
  outline: 2px solid var(--lsc-accent);
  outline-offset: 2px;
}
.lsc-muted { color: var(--lsc-text-muted); }
.lsc-mono { font-family: var(--lsc-font-mono); }
.lsc-title { font-size: var(--lsc-text-lg); font-weight: 600; margin: 0; }
.lsc-heading { font-size: var(--lsc-text-sm); font-weight: 600; color: var(--lsc-text-muted); margin: var(--lsc-space-5) var(--lsc-space-4) var(--lsc-space-2); }
.lsc-page { max-width: 720px; margin: 0 auto; padding: var(--lsc-space-4) var(--lsc-space-4) max(var(--lsc-space-8), env(safe-area-inset-bottom)); }
.lsc-stack { display: flex; flex-direction: column; gap: var(--lsc-space-3); }
.lsc-row { display: flex; align-items: center; gap: var(--lsc-space-2); }
.lsc-spacer { flex: 1; }

/* ---- Toolbar: the one quiet bar at the top ------------------------------ */
.lsc-toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: var(--lsc-space-2);
  height: var(--lsc-bar-h);
  padding: 0 var(--lsc-space-2);
  background: color-mix(in srgb, var(--lsc-bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--lsc-border);
}
.lsc-toolbar .lsc-title { flex: 1; min-width: 0; padding: 0 var(--lsc-space-1); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--lsc-text-base); }

/* ---- Buttons ------------------------------------------------------------ */
.lsc-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--lsc-space-2);
  height: var(--lsc-control-h);
  padding: 0 var(--lsc-space-4);
  border: 1px solid var(--lsc-border-strong);
  border-radius: var(--lsc-radius);
  background: var(--lsc-surface);
  color: var(--lsc-text);
  font: 500 var(--lsc-text-base) / 1 var(--lsc-font);
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.15s, border-color 0.15s, transform 0.1s;
}
.lsc-button:hover { background: color-mix(in srgb, var(--lsc-surface) 100%, var(--lsc-text) 4%); }
.lsc-button:active { transform: scale(0.98); }
.lsc-button:disabled, .lsc-button[aria-disabled="true"] { opacity: 0.5; pointer-events: none; }
.lsc-button svg { width: 16px; height: 16px; flex: none; }
.lsc-button.primary { background: var(--lsc-accent); border-color: transparent; color: var(--lsc-on-accent); }
.lsc-button.primary:hover { background: var(--lsc-accent-strong); }
.lsc-button.danger { background: var(--lsc-danger); border-color: transparent; color: #fff; }
.lsc-button.danger:hover { background: color-mix(in srgb, var(--lsc-danger) 88%, black); }
.lsc-button.ghost { background: transparent; border-color: transparent; }
.lsc-button.ghost:hover { background: var(--lsc-hover); }
.lsc-button.sm { height: 28px; padding: 0 var(--lsc-space-3); font-size: var(--lsc-text-sm); border-radius: var(--lsc-radius-sm); }
.lsc-button.lg { height: 44px; padding: 0 var(--lsc-space-5); font-size: var(--lsc-text-md); border-radius: var(--lsc-radius-lg); }
.lsc-button.block { width: 100%; }
.lsc-button.icon { width: var(--lsc-control-h); padding: 0; }
.lsc-button.icon.sm { width: 28px; }

/* ---- Form controls ------------------------------------------------------ */
.lsc-field { display: flex; flex-direction: column; gap: 6px; }
.lsc-label { font-size: var(--lsc-text-sm); font-weight: 500; }
.lsc-hint { font-size: var(--lsc-text-xs); color: var(--lsc-text-muted); }
.lsc-error { font-size: var(--lsc-text-sm); color: var(--lsc-danger); }
.lsc-input,
.lsc-select,
.lsc-textarea {
  width: 100%;
  min-height: var(--lsc-control-h);
  padding: 0 var(--lsc-space-3);
  border: 1px solid var(--lsc-border-strong);
  border-radius: var(--lsc-radius);
  background: var(--lsc-surface);
  color: var(--lsc-text);
  font: var(--lsc-text-md) / 1.4 var(--lsc-font);
  transition: border-color 0.15s, box-shadow 0.15s;
}
@media (min-width: 640px) {
  .lsc-input, .lsc-select, .lsc-textarea { font-size: var(--lsc-text-base); }
}
.lsc-textarea { padding: var(--lsc-space-2) var(--lsc-space-3); min-height: 88px; resize: vertical; }
.lsc-input::placeholder, .lsc-textarea::placeholder { color: var(--lsc-text-muted); }
.lsc-input:focus, .lsc-select:focus, .lsc-textarea:focus {
  outline: none;
  border-color: var(--lsc-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--lsc-accent) 22%, transparent);
}
.lsc-input[aria-invalid="true"] { border-color: var(--lsc-danger); }
.lsc-select {
  appearance: none;
  padding-right: 32px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2371717a' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px;
}

/* Switch: <input type="checkbox" class="lsc-switch" role="switch"> */
.lsc-switch {
  appearance: none;
  position: relative;
  flex: none;
  width: 44px;
  height: 26px;
  margin: 0;
  border-radius: var(--lsc-radius-full);
  background: var(--lsc-border-strong);
  cursor: pointer;
  transition: background-color 0.2s;
}
.lsc-switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 25%);
  transition: transform 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.lsc-switch:checked { background: var(--lsc-success); }
.lsc-switch:checked::after { transform: translateX(18px); }
.lsc-switch:disabled { opacity: 0.5; cursor: default; }

/* Segmented control: <div class="lsc-segmented" role="tablist"><button aria-selected="true"> */
.lsc-segmented {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  border-radius: var(--lsc-radius);
  background: var(--lsc-surface-muted);
  border: 1px solid var(--lsc-border);
}
.lsc-segmented > button {
  flex: 1;
  height: 30px;
  padding: 0 var(--lsc-space-3);
  border: 0;
  border-radius: calc(var(--lsc-radius) - 3px);
  background: transparent;
  color: var(--lsc-text-muted);
  font: 500 var(--lsc-text-sm) / 1 var(--lsc-font);
  cursor: pointer;
  white-space: nowrap;
}
.lsc-segmented > button[aria-selected="true"],
.lsc-segmented > button[aria-pressed="true"] {
  background: var(--lsc-surface);
  color: var(--lsc-text);
  box-shadow: 0 0 0 1px var(--lsc-border);
}

/* ---- Cards and grouped lists -------------------------------------------- */
.lsc-card {
  background: var(--lsc-surface);
  border: 1px solid var(--lsc-border);
  border-radius: var(--lsc-radius-lg);
  padding: var(--lsc-space-4);
}
.lsc-list {
  margin: 0;
  padding: 0;
  list-style: none;
  background: var(--lsc-surface);
  border: 1px solid var(--lsc-border);
  border-radius: var(--lsc-radius-lg);
  overflow: hidden;
}
.lsc-list > li + li, .lsc-list > a + a, .lsc-list > .lsc-list-row + .lsc-list-row { border-top: 1px solid var(--lsc-border); }
.lsc-list-row {
  display: flex;
  align-items: center;
  gap: var(--lsc-space-3);
  min-height: 48px;
  padding: var(--lsc-space-2) var(--lsc-space-4);
  color: inherit;
  text-decoration: none;
}
a.lsc-list-row, button.lsc-list-row {
  width: 100%;
  border: 0;
  background: none;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
a.lsc-list-row:hover, button.lsc-list-row:hover { background: var(--lsc-hover); }
a.lsc-list-row:active, button.lsc-list-row:active { background: var(--lsc-press); }
.lsc-list-row .lsc-list-text { flex: 1; min-width: 0; }
.lsc-list-row .lsc-list-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.lsc-list-row .lsc-list-sub { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--lsc-text-sm); color: var(--lsc-text-muted); }
.lsc-list-row .lsc-list-value { flex: none; color: var(--lsc-text-muted); font-variant-numeric: tabular-nums; }
.lsc-list-row .lsc-chevron { flex: none; width: 16px; height: 16px; color: var(--lsc-text-muted); }

/* ---- Badge and avatar --------------------------------------------------- */
.lsc-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 20px;
  padding: 0 8px;
  border-radius: var(--lsc-radius-full);
  background: var(--lsc-surface-muted);
  color: var(--lsc-text-muted);
  font: 600 11px / 1 var(--lsc-font);
  white-space: nowrap;
}
.lsc-badge.accent { background: color-mix(in srgb, var(--lsc-accent) 14%, transparent); color: var(--lsc-accent); }
.lsc-badge.success { background: color-mix(in srgb, var(--lsc-success) 16%, transparent); color: color-mix(in srgb, var(--lsc-success) 80%, var(--lsc-text)); }
.lsc-badge.warning { background: color-mix(in srgb, var(--lsc-warning) 18%, transparent); color: color-mix(in srgb, var(--lsc-warning) 70%, var(--lsc-text)); }
.lsc-badge.danger { background: color-mix(in srgb, var(--lsc-danger) 14%, transparent); color: var(--lsc-danger); }
.lsc-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  background: #64748b;
  color: #fff;
  font: 600 13px / 1 var(--lsc-font);
}
.lsc-avatar img { width: 100%; height: 100%; object-fit: cover; }
.lsc-avatar.sm { width: 24px; height: 24px; font-size: 11px; }
.lsc-avatar.lg { width: 48px; height: 48px; font-size: 18px; }

/* ---- Empty state -------------------------------------------------------- */
.lsc-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--lsc-space-2);
  padding: var(--lsc-space-10) var(--lsc-space-6);
  text-align: center;
  color: var(--lsc-text-muted);
}
.lsc-empty svg { width: 32px; height: 32px; opacity: 0.7; }
.lsc-empty strong { color: var(--lsc-text); font-size: var(--lsc-text-md); font-weight: 600; }

/* ---- Dialog and sheet: <dialog class="lsc-dialog"> ---------------------- */
.lsc-dialog {
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 64px);
  padding: 0;
  border: 1px solid var(--lsc-border);
  border-radius: var(--lsc-radius-xl);
  background: var(--lsc-surface);
  color: var(--lsc-text);
  box-shadow: var(--lsc-shadow);
}
.lsc-dialog::backdrop { background: var(--lsc-scrim); }
.lsc-dialog[open] { animation: lsc-pop 0.18s cubic-bezier(0.2, 0.8, 0.2, 1); }
.lsc-dialog-body { padding: var(--lsc-space-5); display: flex; flex-direction: column; gap: var(--lsc-space-3); }
.lsc-dialog-body h2 { margin: 0; font-size: var(--lsc-text-lg); font-weight: 600; }
.lsc-dialog-body p { margin: 0; color: var(--lsc-text-muted); }
.lsc-dialog-actions { display: flex; justify-content: flex-end; gap: var(--lsc-space-2); padding: 0 var(--lsc-space-5) var(--lsc-space-5); }
/* On phones a sheet rises from the bottom edge. */
.lsc-dialog.sheet { margin: auto auto 0; width: 100%; max-width: 560px; border-radius: var(--lsc-radius-xl) var(--lsc-radius-xl) 0 0; padding-bottom: env(safe-area-inset-bottom); }
.lsc-dialog.sheet[open] { animation: lsc-rise 0.24s cubic-bezier(0.2, 0.8, 0.2, 1); }
@keyframes lsc-pop { from { opacity: 0; transform: scale(0.96); } }
@keyframes lsc-rise { from { transform: translateY(100%); } }

/* ---- Toast (lsc.toast) -------------------------------------------------- */
.lsc-toasts {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--lsc-space-2);
  transform: translateX(-50%);
  pointer-events: none;
}
.lsc-toast {
  display: flex;
  align-items: center;
  gap: var(--lsc-space-2);
  max-width: min(440px, calc(100vw - 32px));
  padding: 10px 14px;
  border: 1px solid var(--lsc-border);
  border-radius: var(--lsc-radius-lg);
  background: var(--lsc-surface);
  color: var(--lsc-text);
  box-shadow: var(--lsc-shadow);
  font-size: var(--lsc-text-sm);
  pointer-events: auto;
  animation: lsc-rise-in 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.lsc-toast svg { width: 16px; height: 16px; flex: none; }
.lsc-toast.success svg { color: var(--lsc-success); }
.lsc-toast.error svg { color: var(--lsc-danger); }
.lsc-toast.leaving { opacity: 0; transform: translateY(6px); transition: opacity 0.2s, transform 0.2s; }
@keyframes lsc-rise-in { from { opacity: 0; transform: translateY(8px); } }

/* ---- Icons (lsc.icon) --------------------------------------------------- */
.lsc-icon { display: inline-block; width: 18px; height: 18px; flex: none; vertical-align: -0.2em; }

@media (prefers-reduced-motion: reduce) {
  .lsc-dialog[open], .lsc-dialog.sheet[open], .lsc-toast { animation: none; }
  .lsc-switch, .lsc-switch::after, .lsc-button { transition: none; }
}
