/*
 * Base elements and shared components. Depends on tokens.css.
 * Class names here are suggestions for the Leptos components; the numbers
 * are the contract.
 */

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

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg-chrome);
  color: var(--fg);
  font-family: var(--font-ui);
  font-feature-settings: var(--font-feature-ui);
  font-size: var(--text-md);
  line-height: var(--lh-md);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4, p { margin: 0; }
a { color: var(--accent-fg); text-decoration: none; }
a:hover { text-decoration: underline; }
code, kbd, .mono { font-family: var(--font-mono); font-size: 0.92em; }

/* One focus style everywhere: 2px ring, 2px offset, never removed. */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}
.focus-demo { outline: 2px solid var(--focus-ring); outline-offset: 2px; }

::selection { background: var(--code-selection); }

/* Thin, theme-aware scrollbars inside panes */
.scroll { overflow: auto; scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }

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

.icon { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor;
  stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.icon-sm { width: 14px; height: 14px; }
.icon-xs { width: 12px; height: 12px; }
.icon-lg { width: 20px; height: 20px; }

/* ---------- Text helpers ---------- */
.t-xs { font-size: var(--text-xs); line-height: var(--lh-xs); }
.t-sm { font-size: var(--text-sm); line-height: var(--lh-sm); }
.t-lg { font-size: var(--text-lg); line-height: var(--lh-lg); }
.t-xl { font-size: var(--text-xl); line-height: var(--lh-xl); }
.t-2xl { font-size: var(--text-2xl); line-height: var(--lh-2xl); }
.t-muted { color: var(--fg-muted); }
.t-subtle { color: var(--fg-subtle); }
.t-strong { font-weight: var(--weight-semibold); }
.t-medium { font-weight: var(--weight-medium); }
.t-danger { color: var(--danger-fg); }
.t-warning { color: var(--warning-fg); }
.t-success { color: var(--success-fg); }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.overline { font-size: var(--text-xs); line-height: var(--lh-xs); font-weight: var(--weight-semibold);
  color: var(--fg-subtle); text-transform: uppercase; letter-spacing: 0.04em; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-1-5);
  height: var(--control-h); padding: 0 var(--space-2-5, 10px);
  border: 1px solid var(--border-input); border-radius: var(--radius-md);
  background: var(--bg-chrome); color: var(--fg);
  font-size: var(--text-md); font-weight: var(--weight-medium); line-height: 1;
  white-space: nowrap; cursor: pointer; user-select: none;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
.btn:hover { background: var(--bg-hover); }
.btn:active { background: var(--bg-active); }
.btn[disabled], .btn.is-disabled { color: var(--fg-disabled); cursor: not-allowed; background: transparent; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--fg-on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:active { background: var(--accent-active); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: var(--fg-on-danger); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--fg-muted); }
.btn-ghost:hover { color: var(--fg); background: var(--bg-hover); }
.btn-ghost.is-active, .btn-ghost[aria-pressed="true"] { color: var(--fg); background: var(--bg-active); }
.btn-sm { height: var(--control-h-sm); padding: 0 var(--space-2); font-size: var(--text-sm); border-radius: var(--radius-sm); }
.btn-lg { height: var(--control-h-lg); padding: 0 var(--space-4); font-size: var(--text-lg); }
.btn-icon { width: var(--control-h); padding: 0; }
.btn-icon.btn-sm { width: var(--control-h-sm); }
.btn-block { width: 100%; }
.btn .count { font-size: var(--text-xs); color: var(--fg-subtle); font-weight: var(--weight-medium); }

/* ---------- Inputs ---------- */
.input {
  height: var(--control-h); padding: 0 var(--space-2);
  border: 1px solid var(--border-input); border-radius: var(--radius-md);
  background: var(--bg-chrome); color: var(--fg); min-width: 0;
}
.input::placeholder { color: var(--fg-subtle); }
.input:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: -1px; border-color: transparent; }
.input-lg { height: var(--control-h-lg); padding: 0 var(--space-3); font-size: var(--text-lg); }
.input-group { display: flex; align-items: center; gap: var(--space-1-5);
  height: var(--control-h); padding: 0 var(--space-2); border: 1px solid var(--border-input);
  border-radius: var(--radius-md); background: var(--bg-chrome); color: var(--fg-subtle); }
.input-group input { border: 0; background: transparent; outline: none; flex: 1; min-width: 0; color: var(--fg); height: 100%; }
.input-group:focus-within { outline: 2px solid var(--focus-ring); outline-offset: -1px; border-color: transparent; }
textarea.input { height: auto; padding: var(--space-1-5) var(--space-2); resize: vertical; line-height: var(--lh-md); }
.field { display: grid; gap: var(--space-1-5); }
.field-label { font-size: var(--text-sm); font-weight: var(--weight-medium); color: var(--fg-muted); }
.field-hint { font-size: var(--text-xs); color: var(--fg-subtle); }
select.input { padding-right: var(--space-6); appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--fg-subtle) 50%), linear-gradient(135deg, var(--fg-subtle) 50%, transparent 50%);
  background-position: calc(100% - 13px) 12px, calc(100% - 9px) 12px; background-size: 4px 4px; background-repeat: no-repeat; }

/* checkbox / switch */
.switch { position: relative; width: 28px; height: 16px; border-radius: 8px; background: var(--border-input);
  border: 0; padding: 0; flex: none; cursor: pointer; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 12px; height: 12px; border-radius: 50%;
  background: #fff; transition: transform var(--dur-base) var(--ease); }
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(12px); }
.check { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--border-input); background: var(--bg-chrome);
  display: inline-grid; place-items: center; flex: none; }
.check.is-on { background: var(--accent); border-color: var(--accent); }
.check.is-on::after { content: ""; width: 7px; height: 4px; border: 1.5px solid #fff; border-top: 0; border-right: 0; transform: translateY(-1px) rotate(-45deg); }
.radio { width: 14px; height: 14px; border-radius: 50%; border: 1px solid var(--border-input); background: var(--bg-chrome); flex: none; display: inline-grid; place-items: center; }
.radio.is-on { border: 4px solid var(--accent); }

/* ---------- Segmented control ---------- */
.seg { display: inline-flex; padding: 2px; gap: 2px; border-radius: var(--radius-md); background: var(--bg-inset);
  border: 1px solid var(--border); height: var(--control-h); }
.seg button { border: 0; background: transparent; color: var(--fg-muted); padding: 0 var(--space-2-5, 10px);
  border-radius: var(--radius-sm); font-size: var(--text-sm); font-weight: var(--weight-medium); cursor: pointer;
  display: inline-flex; align-items: center; gap: var(--space-1); }
.seg button:hover { color: var(--fg); }
.seg button[aria-pressed="true"], .seg button[aria-selected="true"] { background: var(--bg-chrome); color: var(--fg); box-shadow: var(--shadow-1); }
.seg-sm { height: var(--control-h-sm); }
.seg-sm button { padding: 0 var(--space-2); font-size: var(--text-xs); }

/* ---------- Chips, badges, status ---------- */
.chip { display: inline-flex; align-items: center; gap: var(--space-1-5); height: 22px; padding: 0 var(--space-2);
  border-radius: var(--radius-full); border: 1px solid var(--border); background: var(--bg-subtle);
  font-size: var(--text-xs); font-weight: var(--weight-medium); color: var(--fg-muted); white-space: nowrap; }
.chip-accent { background: var(--accent-subtle); border-color: var(--accent-border); color: var(--accent-fg); }
.chip-danger { background: var(--danger-subtle); border-color: var(--danger-border); color: var(--danger-fg); }
.chip-warning { background: var(--warning-subtle); border-color: var(--warning-border); color: var(--warning-fg); }
.chip-success { background: var(--success-subtle); border-color: var(--success-border); color: var(--success-fg); }
.badge { display: inline-grid; place-items: center; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px;
  font-size: var(--text-2xs); font-weight: var(--weight-semibold); line-height: 1; background: var(--bg-active); color: var(--fg); }
/* --fg, not --fg-muted: on a pressed button in the dark theme --fg-muted on --bg-active is 4.2:1 */
.badge-danger { background: var(--danger); color: var(--fg-on-danger); }
.badge-warning { background: var(--warning-subtle); color: var(--warning-fg); box-shadow: inset 0 0 0 1px var(--warning); }
.badge-accent { background: var(--accent); color: var(--fg-on-accent); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--fg-subtle); }
.dot-success { background: var(--success); }
.dot-warning { background: var(--warning); }
.dot-danger { background: var(--danger); }
.dot-accent { background: var(--accent); }
.kbd { display: inline-flex; align-items: center; height: 18px; padding: 0 5px; border-radius: var(--radius-xs);
  border: 1px solid var(--border); border-bottom-width: 2px; background: var(--bg-subtle);
  font-family: var(--font-mono); font-size: var(--text-2xs); color: var(--fg-muted); }

/* ---------- Avatars ---------- */
.avatar { width: 24px; height: 24px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
  background: var(--presence-1); color: var(--fg-on-presence); font-size: var(--text-2xs); font-weight: var(--weight-semibold); letter-spacing: 0.02em; }
.avatar-sm { width: 20px; height: 20px; font-size: 9px; }
.avatar-lg { width: 32px; height: 32px; font-size: var(--text-xs); }
.avatar.p2 { background: var(--presence-2); } .avatar.p3 { background: var(--presence-3); }
.avatar.p4 { background: var(--presence-4); } .avatar.p5 { background: var(--presence-5); }
.avatar-stack { display: inline-flex; }
.avatar-stack .avatar { box-shadow: 0 0 0 2px var(--bg-chrome); }
.avatar-stack .avatar + .avatar { margin-left: -6px; }
.avatar-more { background: var(--bg-active) !important; color: var(--fg-muted) !important; }

/* ---------- Menus and popovers ---------- */
.menu { min-width: 200px; padding: var(--space-1); background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-2); z-index: var(--z-menu); }
.menu-item { display: flex; align-items: center; gap: var(--space-2); height: 28px; padding: 0 var(--space-2);
  border-radius: var(--radius-sm); color: var(--fg); cursor: pointer; white-space: nowrap; }
.menu-item.is-tall { height: auto; min-height: 28px; padding-top: var(--space-1-5); padding-bottom: var(--space-1-5); align-items: center; }
.menu-item:hover, .menu-item.is-hover { background: var(--bg-hover); }
.menu-item .icon { color: var(--fg-muted); }
.menu-item .hint { margin-left: auto; color: var(--fg-subtle); font-size: var(--text-xs); padding-left: var(--space-4); }
.menu-item.is-danger { color: var(--danger-fg); }
.menu-item.is-danger .icon { color: var(--danger-fg); }
.menu-item.is-disabled { color: var(--fg-disabled); cursor: default; }
.menu-sep { height: 1px; background: var(--border); margin: var(--space-1) calc(-1 * var(--space-1)); }
.menu-label { padding: var(--space-1-5) var(--space-2) var(--space-1); }
.popover { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2); z-index: var(--z-popover); }
.tooltip { display: inline-flex; align-items: center; gap: var(--space-2); padding: 4px 8px; border-radius: var(--radius-sm);
  background: #1f232a; color: #f1f3f6; font-size: var(--text-xs); line-height: var(--lh-xs); box-shadow: var(--shadow-1);
  z-index: var(--z-tooltip); white-space: nowrap; }
.tooltip .kbd { background: transparent; border-color: #4a515c; color: #c6cbd3; height: 16px; }

/* ---------- Dialogs ---------- */
.backdrop { position: fixed; inset: 0; background: var(--bg-backdrop); z-index: var(--z-modal); display: grid; place-items: center; }
.dialog { width: 480px; max-width: calc(100vw - 48px); background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius-xl); box-shadow: var(--shadow-3); z-index: var(--z-modal); }
.dialog-header { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-4) var(--space-4) var(--space-3); }
.dialog-title { font-size: var(--text-xl); line-height: var(--lh-xl); font-weight: var(--weight-semibold); }
.dialog-body { padding: 0 var(--space-4) var(--space-4); display: grid; gap: var(--space-4); }
.dialog-footer { display: flex; justify-content: flex-end; gap: var(--space-2); padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border); }

/* ---------- Banners, toasts ---------- */
.banner { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-1-5) var(--space-3);
  border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-elevated);
  font-size: var(--text-sm); line-height: var(--lh-sm); }
.banner-danger { background: var(--danger-subtle); border-color: var(--danger-border); color: var(--fg); }
.banner-danger > .icon { color: var(--danger-fg); }
.banner-warning { background: var(--warning-subtle); border-color: var(--warning-border); }
.banner-warning > .icon { color: var(--warning-fg); }
.banner-info { background: var(--accent-subtle); border-color: var(--accent-border); }
.banner-info > .icon { color: var(--accent-fg); }
.toast { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-2) var(--space-2) var(--space-2) var(--space-3);
  min-width: 320px; max-width: 440px; border-radius: var(--radius-lg); background: var(--bg-elevated);
  border: 1px solid var(--border); box-shadow: var(--shadow-2); z-index: var(--z-toast); }

/* ---------- Panels (details panel, settings sections) ---------- */
.section { padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--border); display: grid; gap: var(--space-2); }
.section:last-child { border-bottom: 0; }
.section-title { display: flex; align-items: center; gap: var(--space-2); font-size: var(--text-xs); line-height: var(--lh-xs);
  font-weight: var(--weight-semibold); color: var(--fg-subtle); text-transform: uppercase; letter-spacing: 0.04em; }
.kv { display: grid; grid-template-columns: 96px 1fr; gap: var(--space-1-5) var(--space-3); font-size: var(--text-sm); line-height: var(--lh-sm); }
.kv dt { color: var(--fg-subtle); }
.kv dd { margin: 0; color: var(--fg); min-width: 0; }
.tabs { display: flex; gap: var(--space-4); border-bottom: 1px solid var(--border); padding: 0 var(--space-4); }
.tabs button { border: 0; background: none; padding: 0; height: 36px; color: var(--fg-muted); font-weight: var(--weight-medium);
  border-bottom: 2px solid transparent; margin-bottom: -1px; cursor: pointer; display: inline-flex; align-items: center; gap: var(--space-1-5); }
.tabs button[aria-selected="true"] { color: var(--fg); border-bottom-color: var(--accent); }

/* ---------- Lists and tables (app screens) ---------- */
.list { border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--bg-chrome); overflow: hidden; }
.list-row { display: flex; align-items: center; gap: var(--space-3); min-height: 44px; padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border); }
.list-row:last-child { border-bottom: 0; }
.list-row:hover { background: var(--bg-hover); }
.dtable { width: 100%; border-collapse: collapse; font-size: var(--text-md); }
.dtable th { text-align: left; font-size: var(--text-xs); font-weight: var(--weight-semibold); color: var(--fg-subtle);
  text-transform: uppercase; letter-spacing: 0.04em; padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); background: var(--bg-subtle); }
.dtable td { padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); height: 44px; }
.dtable tr:last-child td { border-bottom: 0; }
.dtable tbody tr:hover td { background: var(--bg-hover); }

/* ---------- Misc ---------- */
.divider-v { width: 1px; align-self: stretch; background: var(--border); margin: var(--space-1-5) var(--space-1); flex: none; }
.spacer { flex: 1; }
.row { display: flex; align-items: center; gap: var(--space-2); }
.stack { display: grid; gap: var(--space-2); }
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); flex: none; vertical-align: -1px; }
.logo { display: inline-flex; align-items: center; gap: var(--space-2); font-weight: var(--weight-semibold); color: var(--fg); letter-spacing: -0.01em; }
.logo-mark { width: 22px; height: 22px; flex: none; }
.spinner { width: 12px; height: 12px; border-radius: 50%; border: 1.5px solid var(--border-strong); border-top-color: var(--accent); }
.skeleton { background: var(--bg-inset); border-radius: var(--radius-sm); }
.md { font-size: var(--text-md); line-height: var(--lh-md); display: grid; gap: var(--space-2); }
.md p { margin: 0; }
.md code { background: var(--bg-inset); padding: 1px 4px; border-radius: var(--radius-xs); }
.md pre { margin: 0; padding: var(--space-2) var(--space-3); background: var(--code-bg); border: 1px solid var(--border);
  border-radius: var(--radius-md); font-family: var(--font-mono); font-size: var(--text-sm); line-height: 18px; overflow-x: auto; white-space: pre; }
