/* SDI Lab shared UI kit. Source of truth: projects/_shared/ui/ui.css.
   Apps keep a copy in public/ui.css; update all copies with scripts/sync-ui.sh. */

:root {
  --bg: #f6f6f3;
  --surface: #ffffff;
  --surface-2: #f0f0ec;
  --text: #1b1b19;
  --muted: #65655f;
  --border: #e2e2dc;
  --accent: #1453c4;
  --accent-text: #ffffff;
  --accent-weak: #e7eefb;
  --good: #1a7f37;
  --good-weak: #e6f4ea;
  --warn: #8a5a00;
  --warn-weak: #fdf3dc;
  --bad: #b42318;
  --bad-weak: #fdecea;
  --focus: #1453c4;
  --radius: 10px;
  --radius-small: 6px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 1px 1px rgba(0, 0, 0, 0.03);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121211;
    --surface: #1b1b1a;
    --surface-2: #232321;
    --text: #ececea;
    --muted: #a3a39c;
    --border: #2f2f2c;
    --accent: #7aa7ff;
    --accent-text: #0b1530;
    --accent-weak: #1a2640;
    --good: #5cc27a;
    --good-weak: #15301e;
    --warn: #e0b453;
    --warn-weak: #33290f;
    --bad: #ff7b6e;
    --bad-weak: #3a1714;
    --focus: #7aa7ff;
    --shadow: none;
    color-scheme: dark;
  }
}

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

/* The hidden attribute always wins (component display rules like .btn must not override it). */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
}

a { color: var(--accent); text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }

h1, h2, h3 { line-height: 1.25; margin: 0 0 0.5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 0.75rem; }

code, pre, .mono { font-family: var(--mono); font-size: 0.88em; }
pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  padding: 12px;
  overflow-x: auto;
  margin: 0 0 0.75rem;
}

/* ---- Top bar ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; color: var(--text); text-decoration: none; letter-spacing: 0.01em; }
.topbar .app-name { font-weight: 600; }
.topbar .sep { color: var(--muted); }
.topnav { display: flex; gap: 4px; flex-wrap: wrap; margin-left: auto; }
.topnav a {
  color: var(--text);
  text-decoration: none;
  padding: 6px 10px;
  border-radius: var(--radius-small);
}
.topnav a:hover { background: var(--surface-2); }
.topnav a[aria-current="page"] { background: var(--accent-weak); color: var(--accent); font-weight: 600; }

/* ---- Layout ---- */
.container { max-width: 1180px; margin: 0 auto; padding: 20px 16px 48px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { color: var(--muted); margin: 0; }
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(min(240px, 100%), 1fr)); }
.grid-2 { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(min(340px, 100%), 1fr)); }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 0.86rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---- Cards ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.card-link { display: block; color: inherit; text-decoration: none; }
.card-link:hover { border-color: var(--accent); }

/* ---- KPI tiles ---- */
.kpi .kpi-label { color: var(--muted); font-size: 0.86rem; }
.kpi .kpi-value { font-size: 2rem; font-weight: 700; line-height: 1.1; margin: 4px 0; font-variant-numeric: tabular-nums; }
.kpi .kpi-meta { color: var(--muted); font-size: 0.82rem; }

/* ---- Badges and pills ---- */
.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge.good { background: var(--good-weak); color: var(--good); border-color: transparent; }
.badge.warn { background: var(--warn-weak); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-weak); color: var(--bad); border-color: transparent; }
.badge.accent { background: var(--accent-weak); color: var(--accent); border-color: transparent; }
.pill-demo {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--warn-weak);
  color: var(--warn);
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-small);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  line-height: 1.2;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--bad); }
.btn-small { padding: 4px 10px; font-size: 0.86rem; }
.btn[disabled] { opacity: 0.55; cursor: not-allowed; }

/* ---- Forms ---- */
.form { display: grid; gap: 12px; }
.form .field { display: grid; gap: 4px; }
.form label { font-weight: 600; font-size: 0.9rem; }
.form .hint { color: var(--muted); font-size: 0.82rem; }
input[type="text"], input[type="email"], input[type="tel"], input[type="number"], input[type="date"], input[type="month"], input[type="search"], select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
textarea { min-height: 140px; resize: vertical; }

/* ---- Tables ---- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 8px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table th { position: sticky; top: 0; background: var(--surface-2); font-size: 0.82rem; color: var(--muted); font-weight: 600; white-space: nowrap; }
.table tr:last-child td { border-bottom: 0; }
.table td.num, .table th.num { text-align: right; }
.table tbody tr:hover { background: var(--surface-2); }

/* ---- Tabs ---- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tabs a { padding: 8px 12px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a[aria-current="page"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

/* ---- Notices ---- */
.banner { padding: 10px 14px; border-radius: var(--radius-small); background: var(--accent-weak); color: var(--text); border: 1px solid transparent; }
.banner.good { background: var(--good-weak); }
.banner.warn { background: var(--warn-weak); }
.banner.bad { background: var(--bad-weak); }
.empty { padding: 32px; text-align: center; color: var(--muted); }

/* ---- Breadcrumb ---- */
.breadcrumb { display: flex; gap: 6px; flex-wrap: wrap; color: var(--muted); font-size: 0.9rem; margin-bottom: 8px; }
.breadcrumb a { color: var(--muted); }

/* ---- Charts ---- */
.chart { width: 100%; height: 320px; }
.chart-small { width: 100%; height: 200px; }

/* ---- Toggle switch (e.g. pause) ---- */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.switch input { width: 40px; height: 22px; appearance: none; background: var(--border); border-radius: 999px; position: relative; cursor: pointer; margin: 0; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--surface); transition: left 150ms ease; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { left: 21px; }

/* ---- Footer ---- */
.footer { color: var(--muted); font-size: 0.82rem; border-top: 1px solid var(--border); margin-top: 40px; padding-top: 16px; display: flex; gap: 12px; flex-wrap: wrap; }

/* ---- Hint: a small "!" that explains on hover, focus or tap ----
   <span class="hint" tabindex="0" aria-label="Demo: names, companies and numbers are made up.">
     <span class="hint-tip" role="tooltip">Demo: names, companies and numbers are made up.</span>
   </span>
   Add .hint-left to open the tip towards the left (e.g. at the right edge of the top bar). */
.hint {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1.5px solid var(--muted);
  color: var(--muted);
  font: 700 11px/1 var(--font);
  cursor: help;
  vertical-align: middle;
  flex: none;
  transition: color 120ms ease, border-color 120ms ease, background 120ms ease;
}
.hint::before { content: "!"; }
.hint::after { content: ""; position: absolute; inset: -11px; }   /* 40px tap area around the 18px icon */
.hint:hover, .hint:focus-visible, .hint:focus-within { color: var(--accent); border-color: var(--accent); background: var(--accent-weak); }
/* Closed tips are display:none, so they never widen the page (a hidden-but-laid-out tip made phone pages
   scroll sideways). They fade in with @starting-style where supported, and simply appear elsewhere. */
.hint-tip {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  left: 50%;
  translate: -50% 0;
  z-index: 30;
  width: max-content;
  max-width: min(280px, calc(100vw - 32px));
  padding: 8px 10px;
  border-radius: var(--radius-small);
  background: var(--text);
  color: var(--bg);
  font: 400 0.82rem/1.45 var(--font);
  text-align: left;
  white-space: normal;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
  pointer-events: none;
  opacity: 1;
  transition: opacity 120ms ease, display 120ms allow-discrete;
}
.hint.hint-left .hint-tip { left: auto; right: -6px; translate: 0 0; }
.hint:hover .hint-tip, .hint:focus .hint-tip, .hint:focus-within .hint-tip { display: block; }
@starting-style {
  .hint:hover .hint-tip, .hint:focus .hint-tip, .hint:focus-within .hint-tip { opacity: 0; }
}
@media (max-width: 640px) {
  /* On phones, open tips as a strip near the top of the screen instead of next to the icon. */
  .hint-tip { position: fixed; top: 12px; left: 16px; right: 16px; translate: none; width: auto; max-width: none; }
  .hint.hint-left .hint-tip { right: 16px; }
}
.hint.hint-left:hover .hint-tip, .hint.hint-left:focus .hint-tip, .hint.hint-left:focus-within .hint-tip { translate: 0 0; }

/* ---- Toast: short result messages that fade away (see ui.js) ---- */
.toast-stack { position: fixed; z-index: 50; right: 16px; bottom: 16px; display: grid; gap: 8px; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  padding: 10px 14px;
  border-radius: var(--radius-small);
  background: var(--text);
  color: var(--bg);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
  font-size: 0.9rem;
  animation: toast-in 180ms ease-out;
}
.toast.good { border-left: 4px solid var(--good); }
.toast.warn { border-left: 4px solid var(--warn); }
.toast.bad { border-left: 4px solid var(--bad); }
.toast.leaving { opacity: 0; translate: 0 6px; transition: opacity 200ms ease, translate 200ms ease; }
@keyframes toast-in { from { opacity: 0; translate: 0 8px; } to { opacity: 1; translate: 0 0; } }

/* ---- Motion and feedback ---- */
@view-transition { navigation: auto; }          /* smooth same-site page changes (Chrome, Edge, Safari) */
:root { interpolate-size: allow-keywords; }     /* lets <details> animate to height:auto */
details::details-content { block-size: 0; overflow: clip; transition: block-size 220ms ease, content-visibility 220ms allow-discrete; }
details[open]::details-content { block-size: auto; }
summary { cursor: pointer; }
.card-link { transition: border-color 120ms ease, translate 120ms ease, box-shadow 120ms ease; }
.card-link:hover { translate: 0 -2px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08); }
.btn { transition: background 120ms ease, transform 80ms ease, filter 120ms ease; }
.btn:active { transform: scale(0.97); }
.btn[aria-busy="true"] { opacity: 0.7; cursor: progress; }
.btn[aria-busy="true"]::after {
  content: ""; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: spin 700ms linear infinite;
}
@keyframes spin { to { rotate: 360deg; } }
.table tbody tr { transition: background 100ms ease; }
.is-hidden { display: none !important; }
.flash-row { animation: flash-row 1.6s ease-out; }
@keyframes flash-row { from { background: var(--accent-weak); } to { background: transparent; } }

@media (max-width: 640px) {
  h1 { font-size: 1.35rem; }
  .kpi .kpi-value { font-size: 1.6rem; }
  .container { padding: 16px 12px 40px; }
  /* On phones the top bar scrolls away with the page, and the nav is one swipeable row. */
  .topbar { position: static; gap: 6px 10px; }
  .topnav { margin-left: 0; width: 100%; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .topnav::-webkit-scrollbar { display: none; }
  .topnav a { white-space: nowrap; }
  /* Tap targets of at least 40px */
  .btn, .btn-small, .topnav a, .tabs a { min-height: 40px; }
  .topnav a, .tabs a { display: inline-flex; align-items: center; }
  /* If the nav still overflows, fade its right edge as a "swipe for more" cue */
  .topnav { mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent); }
  /* Main action of a page head stays together and full-width-friendly */
  .page-head { align-items: stretch; }
  .footer a { display: inline-flex; align-items: center; min-height: 40px; }
  select, input[type="text"], input[type="email"], input[type="tel"], input[type="search"], input[type="number"], input[type="month"], input[type="date"] { min-height: 40px; }
  summary { padding-block: 10px; }
  .topbar .brand, .topbar .app-name, .topbar a.app-name { display: inline-flex; align-items: center; min-height: 40px; }
  .table td a, .table th a, .table th button { display: inline-flex; align-items: center; min-height: 40px; }
  label:has(> input[type="checkbox"]), label:has(> input[type="radio"]) { display: inline-flex; align-items: center; gap: 8px; min-height: 40px; }
}

/* Safety net: nothing may make a page scroll sideways (clip keeps position: sticky working). */
body { overflow-x: clip; }

@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; }
}
