/* ==========================================================================
   Components. Each has a Jinja macro in templates/layout/macros/.
   ========================================================================== */

/* Icon -------------------------------------------------------------------- */
.icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.85;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon--sm { width: 14px; height: 14px; }
.icon--lg { width: 20px; height: 20px; stroke-width: 1.75; }
.icon--xl { width: 24px; height: 24px; stroke-width: 1.6; }

/* Button ------------------------------------------------------------------ */
.btn {
  --btn-bg: var(--surface-2);
  --btn-bg-hover: var(--surface-3);
  --btn-fg: var(--text);
  --btn-border: var(--border-strong);

  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: var(--control-height);
  padding: 0 var(--space-3);
  border: 1px solid var(--btn-border);
  border-radius: var(--radius-md);
  background: var(--btn-bg);
  box-shadow: var(--shadow-sm), var(--highlight-top);
  color: var(--btn-fg);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  line-height: 1;
  white-space: nowrap;
  text-decoration: none;
  user-select: none;
  transition: background var(--duration-fast) var(--ease),
    border-color var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease),
    transform var(--duration-fast) var(--ease);
}

.btn:hover {
  background: var(--btn-bg-hover);
  text-decoration: none;
}

.btn:active {
  transform: translateY(1px);
}

.btn:focus-visible {
  box-shadow: var(--focus-ring);
}

.btn--primary {
  --btn-bg: var(--accent-strong);
  --btn-bg-hover: var(--accent-strong-hover);
  --btn-fg: var(--on-accent);
  --btn-border: transparent;
}

.btn--danger {
  --btn-bg: var(--danger-strong);
  --btn-bg-hover: color-mix(in srgb, var(--danger-strong) 88%, white);
  --btn-fg: #ffffff;
  --btn-border: transparent;
}

.btn--ghost {
  --btn-bg: transparent;
  --btn-bg-hover: var(--surface-2);
  --btn-fg: var(--text-secondary);
  --btn-border: transparent;

  box-shadow: none;
}

.btn--ghost:hover {
  color: var(--text);
}

.btn--soft {
  --btn-bg: var(--accent-soft);
  --btn-bg-hover: var(--accent-soft-strong);
  --btn-fg: var(--accent-text);
  --btn-border: transparent;

  box-shadow: none;
}

.btn--sm {
  height: var(--control-height-sm);
  padding: 0 var(--space-2);
  gap: var(--space-1-5);
  border-radius: var(--radius-sm);
  font-size: var(--text-xs);
}

.btn--lg {
  height: var(--control-height-lg);
  padding: 0 var(--space-5);
  font-size: var(--text-md);
}

.btn--icon { width: var(--control-height); padding: 0; }
.btn--icon.btn--sm { width: var(--control-height-sm); }
.btn--icon.btn--lg { width: var(--control-height-lg); }
.btn--block { width: 100%; }

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.5;
  pointer-events: none;
  box-shadow: none;
}

.btn.is-loading {
  color: transparent;
  pointer-events: none;
}

.btn.is-loading > * {
  visibility: hidden;
}

.btn.is-loading::after {
  content: "";
  position: absolute;
  inset: 0;
  width: 14px;
  height: 14px;
  margin: auto;
  border: 2px solid var(--btn-fg);
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.btn-group {
  display: inline-flex;
}

.btn-group > .btn {
  border-radius: 0;
}

.btn-group > .btn + .btn {
  margin-left: -1px;
}

.btn-group > .btn:first-child { border-radius: var(--radius-md) 0 0 var(--radius-md); }
.btn-group > .btn:last-child { border-radius: 0 var(--radius-md) var(--radius-md) 0; }

/* Spinner and kbd --------------------------------------------------------- */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 var(--space-1);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: var(--radius-xs);
  background: var(--surface-1);
  color: var(--text-tertiary);
  font-family: var(--font-mono);
  font-size: var(--text-2xs);
}

/* Form fields ------------------------------------------------------------- */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1-5);
  min-width: 0;
}

.field__label {
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
}

.field__required {
  margin-left: 2px;
  color: var(--danger);
}

.field__hint {
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}

.field__error {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  color: var(--danger);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
}

.input,
.select select,
.textarea {
  width: 100%;
  height: var(--control-height);
  padding: 0 var(--space-3);
  border: 1px solid var(--border-control);
  border-radius: var(--radius-md);
  background: var(--surface-inset);
  color: var(--text);
  font-size: var(--text-sm);
  transition: border-color var(--duration-fast) var(--ease),
    box-shadow var(--duration-fast) var(--ease);
}

.input::placeholder,
.textarea::placeholder {
  color: var(--text-tertiary);
}

.input:hover,
.select select:hover,
.textarea:hover {
  border-color: var(--text-tertiary);
}

.input:focus,
.select select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft-strong);
}

.input[aria-invalid="true"],
.select select[aria-invalid="true"],
.textarea[aria-invalid="true"] {
  border-color: var(--danger);
}

.input[aria-invalid="true"]:focus,
.textarea[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px var(--danger-soft);
}

.input:disabled,
.select select:disabled,
.textarea:disabled {
  border-color: var(--border);
  background: var(--surface-2);
  color: var(--text-disabled);
  cursor: not-allowed;
}

.input--sm { height: var(--control-height-sm); font-size: var(--text-xs); }
.input--num { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

.textarea {
  height: auto;
  min-height: 88px;
  padding: var(--space-2) var(--space-3);
  line-height: var(--leading);
  resize: vertical;
}

.input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.input-wrap > .icon {
  position: absolute;
  left: var(--space-3);
  color: var(--text-tertiary);
  pointer-events: none;
}

.input-wrap > .icon ~ .input {
  padding-left: 34px;
}

.input-wrap > .input:has(+ .input-wrap__suffix) {
  padding-right: 72px;
}

.input-wrap__suffix {
  position: absolute;
  right: var(--space-3);
  color: var(--text-tertiary);
  font-size: var(--text-xs);
  pointer-events: none;
}

.select {
  position: relative;
  display: block;
}

.select select {
  padding-right: var(--space-8);
  appearance: none;
  cursor: pointer;
}

.select::after {
  content: "";
  position: absolute;
  top: 50%;
  right: var(--space-3);
  width: 7px;
  height: 7px;
  margin-top: -5px;
  border-right: 1.5px solid var(--text-tertiary);
  border-bottom: 1.5px solid var(--text-tertiary);
  transform: rotate(45deg);
  pointer-events: none;
}

/* Checkbox and radio */
.check {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  cursor: pointer;
}

.check input {
  position: relative;
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  border: 1px solid var(--border-control);
  border-radius: var(--radius-xs);
  background: var(--surface-inset);
  appearance: none;
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease),
    border-color var(--duration-fast) var(--ease);
}

.check input[type="radio"] {
  border-radius: 50%;
}

.check input:checked {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.check input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 5px;
  width: 4px;
  height: 8px;
  border-right: 2px solid var(--on-accent);
  border-bottom: 2px solid var(--on-accent);
  transform: rotate(45deg);
}

.check input[type="radio"]:checked::after {
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 50%;
  background: var(--on-accent);
}

.check input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Switch */
.switch {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  font-size: var(--text-sm);
  cursor: pointer;
}

.switch input {
  position: relative;
  flex-shrink: 0;
  width: 34px;
  height: 20px;
  border: 1px solid var(--border-control);
  border-radius: var(--radius-full);
  background: var(--surface-3);
  appearance: none;
  cursor: pointer;
  transition: background var(--duration) var(--ease), border-color var(--duration) var(--ease);
}

.switch input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-secondary);
  box-shadow: var(--shadow-sm);
  transition: transform var(--duration) var(--ease-out), background var(--duration) var(--ease);
}

.switch input:checked {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.switch input:checked::after {
  background: var(--on-accent);
  transform: translateX(14px);
}

.switch input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Segmented control */
.segmented {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-inset);
}

.segmented__item {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1-5);
  height: calc(var(--control-height) - 6px);
  padding: 0 var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  transition: background var(--duration-fast) var(--ease), color var(--duration-fast) var(--ease);
}

.segmented__item:hover {
  color: var(--text);
  text-decoration: none;
}

.segmented__item.is-active,
.segmented__item[aria-current="page"],
.segmented__item[aria-pressed="true"] {
  background: var(--surface-3);
  box-shadow: var(--shadow-sm), var(--highlight-top);
  color: var(--text);
}

.segmented--sm .segmented__item {
  height: calc(var(--control-height-sm) - 6px);
  padding: 0 var(--space-2);
  font-size: var(--text-xs);
}

/* Callout ----------------------------------------------------------------- */
.callout {
  --callout-color: var(--info);
  --callout-bg: var(--info-soft);

  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid color-mix(in srgb, var(--callout-color) 30%, transparent);
  border-radius: var(--radius-lg);
  background: var(--callout-bg);
  font-size: var(--text-sm);
}

.callout > .icon {
  margin-top: 2px;
  color: var(--callout-color);
}

.callout__title {
  font-weight: var(--weight-semibold);
}

.callout__body {
  color: var(--text-secondary);
}

.callout--success { --callout-color: var(--success); --callout-bg: var(--success-soft); }
.callout--warning { --callout-color: var(--warning); --callout-bg: var(--warning-soft); }
.callout--danger { --callout-color: var(--danger); --callout-bg: var(--danger-soft); }

/* Card -------------------------------------------------------------------- */
.card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-1);
  box-shadow: var(--shadow-sm), var(--highlight-top);
}

.card__header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
}

.card__heading {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.card__title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.card__subtitle {
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}

.card__actions {
  display: flex;
  gap: var(--space-1);
  margin-left: auto;
}

.card__body {
  flex: 1;
  padding: var(--space-5);
}

.card__body--flush {
  padding: 0;
}

.card__footer {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--border-subtle);
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}

a.card,
.card--interactive {
  color: inherit;
  transition: border-color var(--duration) var(--ease), transform var(--duration) var(--ease),
    box-shadow var(--duration) var(--ease);
}

a.card:hover,
.card--interactive:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-md);
  text-decoration: none;
}

/* Stat tile --------------------------------------------------------------- */
.stat {
  display: flex;
  flex-direction: column;
  gap: var(--space-1-5);
  min-width: 0;
  padding: var(--space-4) var(--space-5);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-1);
  box-shadow: var(--shadow-sm), var(--highlight-top);
}

.stat__label {
  display: flex;
  align-items: center;
  gap: var(--space-1-5);
  color: var(--text-secondary);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
}

.stat__value {
  font-family: var(--font-mono);
  font-size: var(--text-2xl);
  font-weight: var(--weight-medium);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: var(--leading-tight);
}

.stat__foot {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}

/* Badge ------------------------------------------------------------------- */
.badge {
  --badge-color: var(--text-secondary);
  --badge-bg: var(--surface-3);

  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  height: 20px;
  padding: 0 var(--space-2);
  border-radius: var(--radius-full);
  background: var(--badge-bg);
  color: var(--badge-color);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.badge--dot::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.badge--accent { --badge-color: var(--accent-text); --badge-bg: var(--accent-soft); }
.badge--up { --badge-color: var(--up); --badge-bg: var(--up-soft); }
.badge--down { --badge-color: var(--down); --badge-bg: var(--down-soft); }
.badge--success { --badge-color: var(--success); --badge-bg: var(--success-soft); }
.badge--warning { --badge-color: var(--warning); --badge-bg: var(--warning-soft); }
.badge--danger { --badge-color: var(--danger); --badge-bg: var(--danger-soft); }
.badge--info { --badge-color: var(--info); --badge-bg: var(--info-soft); }
.badge--outline { --badge-bg: transparent; border: 1px solid var(--border-strong); }

/* Change indicator: colour plus an arrow and a sign, never colour alone ---- */
.change {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.change::before {
  font-size: 0.7em;
  line-height: 1;
}

.change--up { color: var(--up); }
.change--up::before { content: "▲"; content: "▲" / ""; }
.change--down { color: var(--down); }
.change--down::before { content: "▼"; content: "▼" / ""; }
.change--flat { color: var(--flat); }
.change--flat::before { content: "■"; content: "■" / ""; font-size: 0.55em; }

.change--pill {
  padding: 1px var(--space-1-5);
  border-radius: var(--radius-sm);
}

.change--pill.change--up { background: var(--up-soft); }
.change--pill.change--down { background: var(--down-soft); }
.change--pill.change--flat { background: var(--surface-3); }

/* Avatar ------------------------------------------------------------------ */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent) 0%, #7c5cf0 100%);
  color: #ffffff;
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.02em;
}

.avatar--muted {
  background: var(--surface-3);
  color: var(--text-secondary);
}

/* Table ------------------------------------------------------------------- */
.table-wrap {
  position: relative;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-1);
}

.card .table-wrap {
  border: 0;
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

.table-wrap--scroll {
  max-height: 420px;
}

.table {
  --row-height: 38px;
  --cell-x: var(--space-4);

  width: 100%;
  font-size: var(--text-sm);
}

.table--dense { --row-height: 30px; --cell-x: var(--space-3); font-size: var(--text-xs); }
.table--comfortable { --row-height: 46px; --cell-x: var(--space-5); }

.table th,
.table td {
  height: var(--row-height);
  padding: var(--space-1) var(--cell-x);
  border-bottom: 1px solid var(--border-subtle);
  text-align: left;
  white-space: nowrap;
}

.table thead th {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  height: calc(var(--row-height) - 4px);
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-tertiary);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
}

.table tbody tr {
  transition: background var(--duration-fast) var(--ease);
}

.table tbody tr:hover {
  background: var(--surface-2);
}

.table tbody tr:last-child td {
  border-bottom: 0;
}

.table tbody tr[aria-selected="true"] {
  background: var(--accent-soft);
}

.table .num,
.table .is-num {
  text-align: right;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.table__primary {
  font-weight: var(--weight-semibold);
}

.table__secondary {
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}

.sort-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
}

.is-num .sort-btn {
  flex-direction: row-reverse;
}

.sort-btn:hover {
  color: var(--text);
}

.sort-btn .icon {
  width: 12px;
  height: 12px;
  opacity: 0.45;
}

th[aria-sort="ascending"] .sort-btn,
th[aria-sort="descending"] .sort-btn {
  color: var(--text);
}

th[aria-sort="ascending"] .sort-btn .icon,
th[aria-sort="descending"] .sort-btn .icon {
  opacity: 1;
  color: var(--accent-text);
}

th[aria-sort="descending"] .sort-btn .icon {
  transform: scaleY(-1);
}

/* Tabs -------------------------------------------------------------------- */
.tabs {
  display: flex;
  gap: var(--space-5);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.tab {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: 40px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  transition: color var(--duration-fast) var(--ease);
}

.tab:hover {
  color: var(--text);
  text-decoration: none;
}

.tab::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: -1px;
  left: 0;
  height: 2px;
  border-radius: 2px 2px 0 0;
  background: transparent;
  transition: background var(--duration) var(--ease);
}

.tab[aria-selected="true"],
.tab[aria-current="page"] {
  color: var(--text);
}

.tab[aria-selected="true"]::after,
.tab[aria-current="page"]::after {
  background: var(--accent);
}

.tab__count {
  padding: 0 var(--space-1-5);
  border-radius: var(--radius-full);
  background: var(--surface-3);
  color: var(--text-tertiary);
  font-size: var(--text-2xs);
  font-variant-numeric: tabular-nums;
}

.tab-panel {
  padding-top: var(--space-4);
}

/* Pagination -------------------------------------------------------------- */
.pagination {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-1);
}

.pagination__link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--control-height-sm);
  height: var(--control-height-sm);
  padding: 0 var(--space-2);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

.pagination__link:hover {
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
}

.pagination__link[aria-current="page"] {
  border-color: var(--border-strong);
  background: var(--surface-3);
  color: var(--text);
}

.pagination__link[aria-disabled="true"] {
  opacity: 0.4;
  pointer-events: none;
}

.pagination__gap {
  color: var(--text-tertiary);
  padding: 0 var(--space-1);
}

/* Dropdown menu ----------------------------------------------------------- */
.dropdown {
  position: relative;
  display: inline-flex;
}

.menu {
  position: absolute;
  top: calc(100% + var(--space-1-5));
  right: 0;
  z-index: var(--z-dropdown);
  display: none;
  min-width: 220px;
  padding: var(--space-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-overlay);
  box-shadow: var(--shadow-lg);
}

.menu--left {
  right: auto;
  left: 0;
}

.dropdown.is-open .menu {
  display: block;
  animation: pop-in var(--duration) var(--ease-out);
}

.menu__header {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: var(--space-1);
}

.menu__item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-1-5) var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text-secondary);
  font-size: var(--text-sm);
  text-align: left;
}

.menu__item:hover,
.menu__item:focus-visible {
  background: var(--surface-3);
  color: var(--text);
  text-decoration: none;
  box-shadow: none;
}

.menu__item[aria-disabled="true"] {
  opacity: 0.5;
  pointer-events: none;
}

.menu__item--danger:hover {
  color: var(--danger);
}

.menu__separator {
  height: 1px;
  margin: var(--space-1) 0;
  background: var(--border-subtle);
}

@keyframes pop-in {
  from { opacity: 0; transform: translateY(-4px) scale(0.98); }
}

/* Modal and drawer (native <dialog>) -------------------------------------- */
.modal,
.drawer {
  max-width: none;
  max-height: none;
  padding: 0;
  border: 1px solid var(--border);
  background: var(--surface-overlay);
  box-shadow: var(--shadow-lg);
  color: var(--text);
}

.modal::backdrop,
.drawer::backdrop {
  background: var(--backdrop);
  backdrop-filter: blur(3px);
}

.modal {
  width: min(520px, calc(100vw - var(--space-8)));
  margin: auto;
  border-radius: var(--radius-xl);
}

.modal--sm { width: min(400px, calc(100vw - var(--space-8))); }
.modal--lg { width: min(760px, calc(100vw - var(--space-8))); }

.modal[open] {
  animation: modal-in var(--duration) var(--ease-out);
}

.drawer {
  width: min(440px, 100vw);
  height: 100vh;
  margin: 0 0 0 auto;
  border-width: 0 0 0 1px;
  border-radius: 0;
}

.drawer[open] {
  display: flex;
  flex-direction: column;
  animation: drawer-in var(--duration) var(--ease-out);
}

.dialog__header {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-5) var(--space-3);
}

.dialog__title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}

.dialog__description {
  margin-top: var(--space-1);
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.dialog__close {
  margin: -4px -6px 0 auto;
}

.dialog__body {
  padding: var(--space-2) var(--space-5) var(--space-5);
}

.drawer .dialog__body {
  flex: 1;
  overflow-y: auto;
}

.dialog__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--border-subtle);
  background: var(--surface-2);
  border-radius: 0 0 var(--radius-xl) var(--radius-xl);
}

.drawer .dialog__footer {
  border-radius: 0;
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
}

@keyframes drawer-in {
  from { transform: translateX(24px); opacity: 0; }
}

/* Toast ------------------------------------------------------------------- */
.toast-region {
  position: fixed;
  right: var(--space-5);
  bottom: var(--space-5);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: min(380px, calc(100vw - var(--space-8)));
  pointer-events: none;
}

.toast {
  --toast-color: var(--info);

  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-left: 3px solid var(--toast-color);
  border-radius: var(--radius-lg);
  background: var(--surface-overlay);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  pointer-events: auto;
  animation: toast-in var(--duration) var(--ease-out);
}

.toast > .icon {
  margin-top: 2px;
  color: var(--toast-color);
}

.toast__body {
  flex: 1;
  min-width: 0;
}

.toast__title {
  font-weight: var(--weight-semibold);
}

.toast__message {
  color: var(--text-secondary);
}

.toast--success { --toast-color: var(--success); }
.toast--warning { --toast-color: var(--warning); }
.toast--danger { --toast-color: var(--danger); }

.toast.is-leaving {
  opacity: 0;
  transform: translateX(12px);
  transition: opacity var(--duration) var(--ease), transform var(--duration) var(--ease);
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
}

/* Tooltip (CSS only: data-tooltip="…") ------------------------------------ */
[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  z-index: var(--z-tooltip);
  width: max-content;
  max-width: 260px;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  background: var(--text);
  color: var(--bg);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  line-height: var(--leading-snug);
  letter-spacing: normal;
  text-align: center;
  text-transform: none;
  white-space: normal;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 4px);
  transition: opacity var(--duration) var(--ease), transform var(--duration) var(--ease);
}

[data-tooltip-pos="bottom"]::after {
  top: calc(100% + 8px);
  bottom: auto;
  transform: translate(-50%, -4px);
}

[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after {
  opacity: 1;
  transform: translate(-50%, 0);
  transition-delay: 300ms;
}

/* For controls at the right edge: the tooltip ends where the control does. */
[data-tooltip-align="end"]::after { left: auto; right: 0; transform: translate(0, 4px); }
[data-tooltip-align="end"]:hover::after,
[data-tooltip-align="end"]:focus-visible::after { transform: translate(0, 0); }

/* Empty state ------------------------------------------------------------- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-10) var(--space-6);
  text-align: center;
}

.empty__icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-bottom: var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  color: var(--text-secondary);
  box-shadow: var(--highlight-top);
}

.empty__title {
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
}

.empty__message {
  max-width: 380px;
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.empty__actions {
  display: flex;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

/* Skeleton loader --------------------------------------------------------- */
.skeleton {
  display: block;
  height: 12px;
  border-radius: var(--radius-sm);
  background: linear-gradient(
    90deg,
    var(--skeleton-base) 0%,
    var(--skeleton-shine) 50%,
    var(--skeleton-base) 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

.skeleton--title { height: 20px; width: 40%; }
.skeleton--value { height: 28px; width: 60%; }
.skeleton--block { height: 180px; border-radius: var(--radius-lg); }
.skeleton--circle { width: 28px; height: 28px; border-radius: 50%; }

@keyframes shimmer {
  from { background-position: 100% 0; }
  to { background-position: -100% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .skeleton {
    animation: none;
  }

  /* A spinner that stops says nothing; slow it down instead. */
  .spinner,
  .btn.is-loading::after {
    animation-duration: 1.6s;
  }
}

.stat__value--text {
  font-family: var(--font-sans);
  font-size: var(--text-xl);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-tight);
}

/* ==========================================================================
   Forms and account management (Phase 2)
   ========================================================================== */

/* Form layout ------------------------------------------------------------- */
.form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
}

.form-card {
  max-width: 780px;
}

.form-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.form-section__title {
  margin-bottom: var(--space-3);
  padding: 0;
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
}

/* Choice cards: radios presented as selectable cards ---------------------- */
.choice-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: var(--space-3);
}

.choice {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border-control);
  border-radius: var(--radius-lg);
  background: var(--surface-inset);
  cursor: pointer;
  transition: border-color var(--duration-fast) var(--ease),
    background var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease);
}

.choice:hover {
  border-color: var(--text-tertiary);
}

.choice input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.choice:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.choice:has(input:focus-visible) {
  box-shadow: inset 0 0 0 1px var(--accent), 0 0 0 3px var(--accent-soft-strong);
}

.choice__icon,
.upcoming__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  background: var(--surface-3);
  color: var(--text-secondary);
}

.choice:has(input:checked) .choice__icon {
  background: var(--accent-strong);
  color: var(--on-accent);
}

.choice__text,
.upcoming__text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.choice__title {
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
}

.choice__description,
.upcoming__summary {
  color: var(--text-secondary);
  font-size: var(--text-xs);
  line-height: var(--leading-snug);
}

/* The temporary password field appears only when that option is chosen. */
.onboarding__password {
  display: none;
}

.onboarding:has(input[value="temporary"]:checked) .onboarding__password {
  display: block;
}

/* Password field with a show/hide button ---------------------------------- */
.input-wrap--action > .input {
  padding-right: 42px;
}

.input-wrap__action {
  position: absolute;
  right: var(--space-1);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text-tertiary);
}

.input-wrap__action:hover {
  background: var(--surface-3);
  color: var(--text);
}

.input-wrap__action .icon-hide,
.input-wrap__action[aria-pressed="true"] .icon-show {
  display: none;
}

.input-wrap__action[aria-pressed="true"] .icon-hide {
  display: block;
}

/* Read-only value with a copy button -------------------------------------- */
.copy-field {
  display: flex;
  gap: var(--space-2);
}

.copy-field .input {
  flex: 1;
  min-width: 0;
  font-size: var(--text-xs);
}

/* Filter bar above a list ------------------------------------------------- */
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--space-2);
}

.filter-bar .field {
  gap: 0;
}

.filter-bar__search {
  flex: 1 1 260px;
  min-width: 200px;
  max-width: 420px;
}

.filter-bar .select select {
  min-width: 150px;
}

.list-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

/* People ------------------------------------------------------------------ */
.avatar--lg {
  width: 44px;
  height: 44px;
  font-size: var(--text-sm);
}

.user-cell {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.user-cell__text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: var(--leading-snug);
}

/* Shown only below / above the tablet breakpoint. */
.narrow-only { display: none !important; }

@media (max-width: 1024px) {
  .narrow-only { display: revert !important; }
  .wide-only { display: none !important; }
}

.identity {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding-bottom: var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
}

.identity__name {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
}

.identity__meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.user-button {
  gap: var(--space-1);
  padding: 0 var(--space-1);
}

.user-button__chevron {
  color: var(--text-tertiary);
}

.device {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

.device__icon {
  display: inline-flex;
  color: var(--text-tertiary);
}

/* Key-value list ---------------------------------------------------------- */
.kv {
  display: flex;
  flex-direction: column;
  margin: 0;
}

.kv > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--border-subtle);
  font-size: var(--text-sm);
}

.kv > div:first-child { padding-top: 0; }
.kv > div:last-child { padding-bottom: 0; border-bottom: 0; }
.kv dt { color: var(--text-secondary); }
.kv dd { margin: 0; color: var(--text); text-align: right; }

/* A list of actions, each with an explanation ----------------------------- */
.action-list {
  display: flex;
  flex-direction: column;
}

.action-list__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--border-subtle);
}

.action-list__item:first-child { padding-top: 0; }
.action-list__item:last-child { padding-bottom: 0; border-bottom: 0; }

.action-list__title {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
}

.action-list__text {
  color: var(--text-secondary);
  font-size: var(--text-xs);
}

/* Card variants and page grids -------------------------------------------- */
.card--highlight {
  border-color: color-mix(in srgb, var(--accent) 50%, var(--border));
  box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow-sm);
}

.card--danger {
  border-color: color-mix(in srgb, var(--danger) 35%, var(--border));
}

.card--danger .card__title {
  color: var(--danger);
}

.card__footer.form-actions {
  justify-content: flex-end;
}

.split-grid {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
  gap: var(--space-6);
  align-items: start;
}

.account-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-6);
  align-items: start;
}

@media (max-width: 1100px) {
  .split-grid,
  .account-grid { grid-template-columns: minmax(0, 1fr); }
}

.table .is-actions {
  width: 1%;
  text-align: right;
}

/* Audit entries ----------------------------------------------------------- */
.action-code {
  padding: 2px 6px;
  border-radius: var(--radius-xs);
  background: var(--surface-3);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  white-space: nowrap;
}

.action-code--auth { background: var(--info-soft); color: var(--info); }
.action-code--account { background: var(--info-soft); color: var(--info); }
.action-code--user { background: var(--accent-soft); color: var(--accent-text); }
.action-code--setting { background: var(--warning-soft); color: var(--warning); }
.action-code--cron { background: var(--success-soft); color: var(--success); }
.action-code--task { background: var(--success-soft); color: var(--success); }

.table td.audit-detail {
  min-width: 200px;
  white-space: normal;
}

.audit-detail__text {
  display: -webkit-box;
  margin-top: 2px;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  color: var(--text-secondary);
  font-size: var(--text-xs);
  line-height: var(--leading-snug);
}

td:not(:has(.action-code)) > .audit-detail__text {
  margin-top: 0;
  font-size: inherit;
}

/* Dashboard: sections still to come --------------------------------------- */
.upcoming {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  margin: 0;
  padding: 0;
  list-style: none;
}

.upcoming__item {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
}

.upcoming__title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
}

/* Settings ---------------------------------------------------------------- */
.settings-list {
  display: flex;
  flex-direction: column;
}

.setting {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(220px, 300px);
  align-items: start;
  gap: var(--space-6);
  padding: var(--space-4) var(--space-5);
}

.setting + .setting {
  border-top: 1px solid var(--border-subtle);
}

.setting__info {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
}

.setting__label {
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
}

.setting__help {
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.setting__meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}

.setting__control {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-2);
}

.setting__control > .input,
.setting__number {
  width: 100%;
}

.setting__number .input {
  padding-right: 72px;
  padding-left: var(--space-3);
}

@media (max-width: 720px) {
  .setting { grid-template-columns: minmax(0, 1fr); gap: var(--space-3); }
  .setting__control { align-items: stretch; }
  .setting__reset { align-self: flex-start; }
}

a.sort-btn:hover {
  text-decoration: none;
}

/* Small selects ---------------------------------------------------------------- */
.select--sm select {
  height: var(--control-height-sm);
  padding-left: var(--space-2);
  padding-right: var(--space-6);
  font-size: var(--text-xs);
}

.select--sm::after { right: var(--space-2-5, 10px); width: 6px; height: 6px; }

/* Type-ahead search results (js/search.js) ----------------------------------- */
.search-results {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: var(--z-dropdown);
  width: max(100%, 360px);
  max-height: min(70vh, 480px);
  overflow-y: auto;
  padding: var(--space-1-5);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-overlay);
  box-shadow: var(--shadow-lg);
}

.search-results__group {
  margin: var(--space-2) var(--space-2) var(--space-1);
  color: var(--text-tertiary);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
}

.search-results__group:first-child { margin-top: var(--space-1); }

.search-results__empty {
  padding: var(--space-3);
  color: var(--text-tertiary);
  font-size: var(--text-sm);
}

.search-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-1-5) var(--space-2);
  border-radius: var(--radius-md);
  cursor: pointer;
}

.search-option[aria-selected="true"] { background: var(--accent-soft); }

.search-option__text {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
  min-width: 0;
}

.search-option__symbol {
  flex-shrink: 0;
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
}

.search-option__name {
  overflow: hidden;
  color: var(--text-secondary);
  font-size: var(--text-sm);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search-option__detail {
  flex-shrink: 0;
  color: var(--text-tertiary);
  font-size: var(--text-xs);
}

/* Menus with ticks and inline forms (watchlist menus) ------------------------- */
.menu__tick {
  display: inline-block;
  width: 1em;
  color: var(--accent-text);
  font-weight: var(--weight-bold);
}

.menu__form { padding: var(--space-1-5) var(--space-2); }

/* Sparklines (layout/macros/charts.html) --------------------------------------------------------------------- */
.sparkline { display: block; overflow: visible; }
.sparkline polyline { stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.sparkline--up polyline { stroke: var(--up); }
.sparkline--down polyline { stroke: var(--down); }
.sparkline--flat polyline { stroke: var(--flat); }

.sparkline--empty {
  display: block;
  height: 1px;
  margin: 12px 0;
  background: repeating-linear-gradient(90deg, var(--border) 0 4px, transparent 4px 8px);
}

/* The Notes panel on instrument, strategy and backtest pages (app/notes) -------------- */
.notes-panel { display: flex; flex-direction: column; gap: var(--space-2); }
.notes-panel__list { display: flex; flex-direction: column; gap: 1px; margin: 0; padding: 0; list-style: none; }
.notes-panel__item a { display: flex; flex-direction: column; gap: 2px; padding: var(--space-2) var(--space-2-5, 10px); border-radius: var(--radius-md); color: var(--text); }
.notes-panel__item a:hover { background: var(--surface-2); text-decoration: none; }
.notes-panel__title { display: flex; align-items: center; gap: var(--space-1-5); font-size: var(--text-sm); font-weight: var(--weight-semibold); }
.notes-panel__title .icon { color: var(--accent-text); }
.notes-panel__snippet { overflow: hidden; color: var(--text-secondary); font-size: var(--text-xs); text-overflow: ellipsis; white-space: nowrap; }
.notes-panel__empty { margin: 0; color: var(--text-tertiary); font-size: var(--text-sm); }
