/* Components: base elements and the reusable pieces every screen is built from.
   Each block is self-contained and uses tokens only. See the Library screen (#/library). */

/* --- base ---------------------------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: var(--fw-regular) var(--fs-md) / var(--lh) var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: "cv11", "ss01";
  overscroll-behavior-y: none;
}
h1, h2, h3, h4, p { margin: 0; }
h1 { font-size: var(--fs-2xl); font-weight: var(--fw-bold); letter-spacing: var(--tracking-tight); line-height: var(--lh-tight); }
h2 { font-size: var(--fs-lg); font-weight: var(--fw-semibold); letter-spacing: -0.01em; line-height: var(--lh-tight); }
h3 { font-size: var(--fs-md); font-weight: var(--fw-semibold); }
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 2px; }
button, input, textarea, select { font: inherit; color: inherit; }
svg { display: block; }
:focus-visible { outline: none; box-shadow: var(--ring); border-radius: var(--r-xs); }
::selection { background: var(--accent-soft); }
.num, .tabular { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
.mono { font-family: var(--font-mono); font-size: 0.95em; }
.muted { color: var(--text-2); }
.subtle { color: var(--text-3); }
.small { font-size: var(--fs-sm); }
.xsmall { font-size: var(--fs-xs); }
.caps { font-size: var(--fs-2xs); font-weight: var(--fw-semibold); letter-spacing: var(--tracking-caps); text-transform: uppercase; color: var(--text-3); }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--text-2); }
.row-flex { display: flex; align-items: center; gap: var(--s-2); }
.spread { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); }
.stack { display: flex; flex-direction: column; gap: var(--s-3); min-width: 0; }
.stack-lg { display: flex; flex-direction: column; gap: var(--s-6); min-width: 0; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1 1 auto; min-width: 0; }
hr, .divider { border: 0; border-top: 1px solid var(--border); margin: var(--s-4) 0; }

/* --- icon ---------------------------------------------------------------------------------- */
.icon { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.icon--sm { width: 16px; height: 16px; }
.icon--lg { width: 22px; height: 22px; }

/* --- button -------------------------------------------------------------------------------- */
.btn {
  --btn-bg: var(--surface); --btn-fg: var(--text); --btn-bd: var(--border-strong);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  min-height: 36px; padding: 0 var(--s-4);
  border: 1px solid var(--btn-bd); border-radius: var(--r-sm);
  background: var(--btn-bg); color: var(--btn-fg);
  font-size: var(--fs-md); font-weight: var(--fw-medium); line-height: 1; white-space: nowrap;
  cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent;
  transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease), transform var(--t-fast) var(--ease), opacity var(--t-fast);
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:active { transform: scale(0.98); }
.btn[disabled], .btn[aria-busy="true"] { opacity: 0.55; pointer-events: none; }
.btn--primary { --btn-bg: var(--accent); --btn-fg: var(--text-on-accent); --btn-bd: transparent; }
.btn--primary:hover { background: var(--accent-hover); }
.btn--ghost { --btn-bg: transparent; --btn-bd: transparent; --btn-fg: var(--text-2); }
.btn--ghost:hover { background: var(--surface-2); color: var(--text); }
.btn--danger { --btn-fg: var(--down); }
.btn--sm { min-height: 30px; padding: 0 var(--s-3); font-size: var(--fs-sm); border-radius: var(--r-xs); }
.btn--lg { min-height: var(--tap); padding: 0 var(--s-5); font-size: var(--fs-lg); border-radius: var(--r-md); }
.btn--block { width: 100%; white-space: normal; text-align: center; line-height: 1.25; }
.btn--icon { width: 36px; padding: 0; }
.btn--icon.btn--sm { width: 30px; }
.btn .kbd { margin-left: var(--s-1); }

/* --- form ---------------------------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: var(--s-2); }
.label { font-size: var(--fs-sm); font-weight: var(--fw-medium); color: var(--text-2); }
.input {
  width: 100%; min-height: var(--tap); padding: 0 var(--s-3);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  font-size: var(--fs-lg); /* 16px stops iOS zooming on focus */
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
.input::placeholder { color: var(--text-3); }
.input:focus { outline: none; border-color: var(--accent); box-shadow: var(--ring); }
.otp { font-family: var(--font-mono); font-size: 28px; letter-spacing: 0.5em; text-align: center; padding-left: 0.5em; min-height: 60px; }
.help { font-size: var(--fs-sm); color: var(--text-3); }
.error-text { font-size: var(--fs-sm); color: var(--down); }

/* --- card ---------------------------------------------------------------------------------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-1); min-width: 0;
}
.card__head { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); padding: var(--s-4) var(--s-5) 0; }
.card__title { font-size: var(--fs-md); font-weight: var(--fw-semibold); display: flex; align-items: center; gap: var(--s-2); }
.card__body { padding: var(--s-4) var(--s-5) var(--s-5); }
.card__foot { padding: var(--s-3) var(--s-5); border-top: 1px solid var(--border); }
.card--flush .card__body { padding: var(--s-2) 0 var(--s-2); }
.card--interactive { cursor: pointer; transition: border-color var(--t-fast), box-shadow var(--t-fast), transform var(--t-fast); }
.card--interactive:hover { border-color: var(--border-strong); box-shadow: var(--shadow-2); }

/* --- badge & chip -------------------------------------------------------------------------- */
.badge {
  --b-bg: var(--neutral-soft); --b-fg: var(--text-2);
  display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 var(--s-2);
  border-radius: var(--r-full); background: var(--b-bg); color: var(--b-fg);
  font-size: var(--fs-xs); font-weight: var(--fw-semibold); white-space: nowrap; line-height: 1;
}
.badge--up { --b-bg: var(--up-soft); --b-fg: var(--up-strong); }
.badge--down { --b-bg: var(--down-soft); --b-fg: var(--down-strong); }
.badge--warn { --b-bg: var(--warn-soft); --b-fg: var(--warn); }
.badge--info { --b-bg: var(--info-soft); --b-fg: var(--info); }
.badge--accent { --b-bg: var(--accent-soft); --b-fg: var(--accent-text); }
.badge--solid { --b-bg: var(--text); --b-fg: var(--bg); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; flex: none; }
.dot--live { position: relative; }
.dot--live::after { content: ""; position: absolute; inset: -4px; border-radius: 50%; background: currentColor; opacity: 0.35; animation: pulse 2s var(--ease) infinite; }
@keyframes pulse { 0% { transform: scale(0.6); opacity: 0.5; } 100% { transform: scale(1.6); opacity: 0; } }

.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 var(--s-3);
  border: 1px solid var(--border-strong); border-radius: var(--r-full); background: var(--surface);
  font-size: var(--fs-sm); font-weight: var(--fw-medium); color: var(--text-2); cursor: pointer; white-space: nowrap;
  transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast);
}
.chip:hover { color: var(--text); background: var(--surface-2); }
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }
.chips { display: flex; gap: var(--s-2); overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }

/* --- segmented control --------------------------------------------------------------------- */
.seg { display: inline-flex; padding: 3px; gap: 2px; background: var(--surface-2); border-radius: var(--r-sm); border: 1px solid var(--border); }
.seg button {
  min-height: 28px; padding: 0 var(--s-3); border: 0; border-radius: 6px; background: transparent;
  color: var(--text-2); font-size: var(--fs-sm); font-weight: var(--fw-medium); cursor: pointer;
  transition: background var(--t-fast), color var(--t-fast);
}
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1); }

/* --- stat ---------------------------------------------------------------------------------- */
.stat { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.stat__label { font-size: var(--fs-xs); color: var(--text-3); font-weight: var(--fw-medium); }
.stat__value { font-size: var(--fs-xl); font-weight: var(--fw-semibold); letter-spacing: var(--tracking-tight); font-variant-numeric: tabular-nums; }
.stat__value--xl { font-size: var(--fs-4xl); font-weight: var(--fw-bold); letter-spacing: -0.025em; line-height: 1.05; }
.stat__meta { font-size: var(--fs-sm); color: var(--text-2); font-variant-numeric: tabular-nums; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: var(--s-4); }

.delta { display: inline-flex; align-items: center; gap: 4px; font-weight: var(--fw-semibold); font-variant-numeric: tabular-nums; }
.delta--up { color: var(--up); }
.delta--down { color: var(--down); }
.delta--flat { color: var(--text-2); }
.delta .icon { width: 14px; height: 14px; stroke-width: 2.25; }

/* --- list ---------------------------------------------------------------------------------- */
.list { display: flex; flex-direction: column; }
.list__item {
  display: flex; align-items: center; gap: var(--s-3); padding: var(--s-3) var(--s-5); min-height: 56px;
  border-top: 1px solid var(--border); color: inherit; text-decoration: none; min-width: 0;
  transition: background var(--t-fast);
}
.list__item:first-child { border-top: 0; }
a.list__item:hover, button.list__item:hover, .list__item--action:hover { background: var(--surface-2); text-decoration: none; }
button.list__item { width: 100%; text-align: left; background: none; border-left: 0; border-right: 0; border-bottom: 0; cursor: pointer; }
.list__main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.list__title { font-weight: var(--fw-medium); }
.list__meta { font-size: var(--fs-sm); color: var(--text-3); }
.list__end { flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 2px; text-align: right; }

/* --- symbol tile --------------------------------------------------------------------------- */
.sym {
  --hue: 240; flex: none; width: 36px; height: 36px; border-radius: 10px;
  display: grid; place-items: center; font-size: 11px; font-weight: var(--fw-bold); letter-spacing: 0.02em;
  background: hsl(var(--hue) 70% 95%); color: hsl(var(--hue) 55% 32%);
}
:root[data-theme="dark"] .sym { background: hsl(var(--hue) 40% 18%); color: hsl(var(--hue) 80% 78%); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .sym { background: hsl(var(--hue) 40% 18%); color: hsl(var(--hue) 80% 78%); } }
.sym--lg { width: 48px; height: 48px; border-radius: 14px; font-size: 13px; }

/* --- table --------------------------------------------------------------------------------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: var(--s-3) var(--s-4); text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th { font-size: var(--fs-xs); font-weight: var(--fw-semibold); color: var(--text-3); position: sticky; top: 0; background: var(--surface); }
.table th button { all: unset; cursor: pointer; display: inline-flex; gap: 4px; align-items: center; }
.table th button:focus-visible { box-shadow: var(--ring); border-radius: 4px; }
.table td.r, .table th.r { text-align: right; }
.table tbody tr { cursor: pointer; transition: background var(--t-fast); }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: 0; }

/* --- meter --------------------------------------------------------------------------------- */
.meter { height: 8px; border-radius: var(--r-full); background: var(--surface-3); overflow: hidden; display: flex; }
.meter__fill { height: 100%; background: var(--accent); border-radius: inherit; transition: width var(--t-slow) var(--ease-out); }
.meter--split .meter__fill { border-radius: 0; }
.meter__fill--up { background: var(--up); }
.meter__fill--down { background: var(--down); }
.meter__fill--warn { background: var(--warn); }

/* --- callout ------------------------------------------------------------------------------- */
.callout {
  --c: var(--info); --c-bg: var(--info-soft);
  display: flex; gap: var(--s-3); align-items: flex-start; padding: var(--s-3) var(--s-4);
  border-radius: var(--r-md); background: var(--c-bg); color: var(--text); text-decoration: none;
  border: 1px solid color-mix(in srgb, var(--c) 18%, transparent);
  transition: transform var(--t-fast), box-shadow var(--t-fast);
}
a.callout:hover { text-decoration: none; box-shadow: var(--shadow-2); }
.callout__icon { color: var(--c); margin-top: 1px; }
.callout__title { font-weight: var(--fw-semibold); }
.callout__body { font-size: var(--fs-sm); color: var(--text-2); }
.callout--bad { --c: var(--down); --c-bg: var(--down-soft); }
.callout--warn { --c: var(--warn); --c-bg: var(--warn-soft); }
.callout--accent { --c: var(--accent); --c-bg: var(--accent-soft); }
.callout--ok { --c: var(--up); --c-bg: var(--up-soft); }

/* --- skeleton, empty, error ---------------------------------------------------------------- */
.skel { display: block; border-radius: var(--r-xs); background: linear-gradient(90deg, var(--surface-2) 0%, var(--surface-3) 50%, var(--surface-2) 100%); background-size: 200% 100%; animation: shimmer 1.4s linear infinite; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skel { animation: none; } .dot--live::after { animation: none; } }
.empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--s-2); padding: var(--s-8) var(--s-5); color: var(--text-2); }
.empty__icon { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; background: var(--surface-2); color: var(--text-3); margin-bottom: var(--s-1); }
.empty__title { color: var(--text); font-weight: var(--fw-semibold); }
.empty p { max-width: 36ch; font-size: var(--fs-sm); }

/* --- kbd ----------------------------------------------------------------------------------- */
.kbd { display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 5px; border: 1px solid var(--border-strong); border-bottom-width: 2px; background: var(--surface); font: var(--fw-medium) 11px var(--font); color: var(--text-3); }

/* --- toast --------------------------------------------------------------------------------- */
.toasts { position: fixed; z-index: 90; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + var(--safe-bottom) + var(--s-3)); display: flex; flex-direction: column; gap: var(--s-2); pointer-events: none; width: min(92vw, 420px); }
.toast { pointer-events: auto; display: flex; gap: var(--s-3); align-items: center; padding: var(--s-3) var(--s-4); background: var(--text); color: var(--bg); border-radius: var(--r-md); box-shadow: var(--shadow-3); font-size: var(--fs-sm); animation: toast-in var(--t-slow) var(--ease-out); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@media (min-width: 1100px) { .toasts { bottom: var(--s-6); } }

/* --- sheet & scrim ------------------------------------------------------------------------- */
.scrim { position: fixed; inset: 0; z-index: 60; background: var(--overlay); opacity: 0; pointer-events: none; transition: opacity var(--t) var(--ease); }
.scrim[data-open="true"] { opacity: 1; pointer-events: auto; }

/* --- chart --------------------------------------------------------------------------------- */
.chart { position: relative; width: 100%; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.chart svg { width: 100%; height: 100%; overflow: visible; }
.chart__tip {
  position: absolute; top: 0; pointer-events: none; transform: translateX(-50%);
  padding: 4px 8px; border-radius: var(--r-xs); background: var(--text); color: var(--bg);
  font-size: var(--fs-xs); font-weight: var(--fw-semibold); white-space: nowrap; font-variant-numeric: tabular-nums;
  opacity: 0; transition: opacity var(--t-fast);
}
.chart[data-active="true"] .chart__tip { opacity: 1; }
.spark { width: 72px; height: 28px; flex: none; }
.accent-text { color: var(--accent-text); }
@media (max-width: 400px) { .hide-xs { display: none; } }

/* --- switch -------------------------------------------------------------------------------- */
.switch { position: relative; flex: none; width: 46px; height: 28px; border-radius: var(--r-full); border: 0; padding: 0; cursor: pointer;
  background: var(--surface-3); transition: background var(--t) var(--ease); box-shadow: inset 0 0 0 1px var(--border-strong); }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); transition: transform var(--t) var(--ease-out); }
.switch[aria-checked="true"] { background: var(--accent); box-shadow: none; }
.switch[aria-checked="true"]::after { transform: translateX(18px); }
.switch[aria-busy="true"] { opacity: 0.6; }

.select { appearance: none; -webkit-appearance: none; min-height: 36px; width: auto; padding-right: 32px; font-size: var(--fs-md);
  background-image: linear-gradient(45deg, transparent 50%, var(--text-3) 50%), linear-gradient(135deg, var(--text-3) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
.textarea { min-height: 88px; padding: var(--s-3); resize: vertical; line-height: 1.45; }
@media (min-width: 720px) { .textarea { font-size: var(--fs-md); } }
