/* ==========================================================================
   Components — controls, panels, tables, overlays
   ========================================================================== */

/* -- buttons -------------------------------------------------------------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 12px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--rule-strong); border-radius: var(--r-md);
  font-family: var(--font-ui); font-size: var(--t-body); font-weight: 500;
  cursor: pointer; white-space: nowrap;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}
.btn:hover { background: var(--surface-hover); border-color: var(--ink-muted); }
.btn:active { background: var(--surface-active); }
.btn[disabled] { opacity: .45; cursor: not-allowed; }

.btn--primary {
  background: var(--ink); color: var(--ink-inverse); border-color: var(--ink);
  font-weight: 600;
}
.btn--primary:hover { background: var(--ink-secondary); border-color: var(--ink-secondary); color: var(--ink-inverse); }

.btn--accent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn--accent:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--accent-ink); }

.btn--danger { color: var(--critical-text); border-color: var(--rule-strong); }
.btn--danger:hover { background: var(--critical-soft); border-color: var(--critical); }

.btn--ghost { background: transparent; border-color: transparent; color: var(--ink-secondary); }
.btn--ghost:hover { background: var(--surface-hover); color: var(--ink); border-color: transparent; }

.btn--icon { width: 32px; padding: 0; color: var(--ink-secondary); }
.btn--icon svg { width: 15px; height: 15px; }

.btn--sm { height: 26px; padding: 0 9px; font-size: var(--t-small); }

/* A toggle button that is currently on. */
.btn[aria-pressed="true"] {
  background: var(--accent-soft); border-color: var(--accent); color: var(--accent);
  font-weight: 600;
}

/* -- segmented control ---------------------------------------------------- */

.segmented {
  display: inline-flex; padding: 2px;
  background: var(--plane-sunken); border-radius: var(--r-md);
  border: 1px solid var(--rule);
}
.segmented button {
  height: 26px; padding: 0 10px;
  background: transparent; border: 0; border-radius: var(--r-sm);
  font-size: var(--t-small); font-weight: 500; color: var(--ink-secondary);
  cursor: pointer;
}
.segmented button:hover { color: var(--ink); }
.segmented button[aria-pressed="true"] {
  background: var(--surface); color: var(--ink);
  box-shadow: 0 1px 2px rgba(0,0,0,.10);
}

/* -- form fields ---------------------------------------------------------- */

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > .label { color: var(--ink-secondary); }

.input, .select, .textarea {
  width: 100%; height: 32px; padding: 0 9px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--rule-strong); border-radius: var(--r-md);
  font-size: var(--t-body);
}
.textarea { height: auto; min-height: 74px; padding: 8px 9px; resize: vertical; line-height: 1.5; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); outline-offset: 0; }
.input::placeholder, .textarea::placeholder { color: var(--ink-muted); }
.input[aria-invalid="true"] { border-color: var(--critical); }

.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 15px) 14px, calc(100% - 10px) 14px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 26px;
}

.field__error { font-size: var(--t-small); color: var(--critical-text); }
.field__hint  { font-size: var(--t-small); color: var(--ink-muted); }

/* -- panel ---------------------------------------------------------------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
}
.panel__head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  padding: 13px 16px 11px;
  border-bottom: 1px solid var(--rule);
}
.panel__title { font-family: var(--font-display); font-weight: 600; font-size: var(--t-base); }
.panel__sub { font-size: var(--t-small); color: var(--ink-muted); }
.panel__body { padding: 14px 16px 16px; }
.panel__body--flush { padding: 0; }

/* -- stage pill (color + text, never color alone) -------------------------- */

.stage {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: var(--t-small); font-weight: 500; color: var(--ink-secondary);
  white-space: nowrap;
}
.stage__dot { width: 8px; height: 8px; border-radius: 1px; flex: none; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  height: 20px; padding: 0 7px;
  border-radius: var(--r-sm);
  font-family: var(--font-mono); font-size: var(--t-micro); font-weight: 500;
  letter-spacing: 0.04em; text-transform: uppercase;
  background: var(--plane-sunken); color: var(--ink-secondary);
  white-space: nowrap;
}
.badge--good     { background: var(--good-soft);     color: var(--good-text); }
.badge--warning  { background: var(--warning-soft);  color: var(--warning-text); }
.badge--critical { background: var(--critical-soft); color: var(--critical-text); }
.badge--accent   { background: var(--accent-soft);   color: var(--accent); }

/* -- table ---------------------------------------------------------------- */

.tablewrap { overflow-x: auto; }

.table { width: 100%; border-collapse: collapse; font-size: var(--t-body); }
.table th, .table td { text-align: left; padding: 0 12px; }
.table thead th {
  height: 34px;
  background: var(--surface);
  border-bottom: 1px solid var(--rule-strong);
  font-family: var(--font-mono); font-size: var(--t-micro); font-weight: 500;
  letter-spacing: 0.07em; text-transform: uppercase; color: var(--ink-muted);
  white-space: nowrap;
}
.table tbody td { height: 46px; border-bottom: 1px solid var(--rule); vertical-align: middle; }
.table tbody tr:hover td { background: var(--surface-hover); }
.table tbody tr.is-selected td { background: var(--accent-soft); }
.table .align-r { text-align: right; }
.table .num, .table .mono { font-variant-numeric: tabular-nums; }

.th-sort {
  display: inline-flex; align-items: center; gap: 4px;
  background: none; border: 0; padding: 0; cursor: pointer;
  font: inherit; letter-spacing: inherit; text-transform: inherit; color: inherit;
}
.th-sort:hover { color: var(--ink); }
.th-sort[aria-sort="ascending"], .th-sort[aria-sort="descending"] { color: var(--ink); }
.th-sort__caret { width: 8px; height: 8px; opacity: 0; }
.th-sort[aria-sort="ascending"] .th-sort__caret,
.th-sort[aria-sort="descending"] .th-sort__caret { opacity: 1; }
.th-sort[aria-sort="descending"] .th-sort__caret { transform: rotate(180deg); }

/* -- menu ----------------------------------------------------------------- */

.menu { position: relative; }
.menu__panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 60;
  min-width: 210px; padding: 4px;
  background: var(--surface-raised);
  border: 1px solid var(--rule-strong); border-radius: var(--r-md);
  box-shadow: var(--shadow-pop);
}
.menu__panel--left { right: auto; left: 0; }
.menu__item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 7px 9px;
  background: none; border: 0; border-radius: var(--r-sm);
  font-size: var(--t-body); text-align: left; color: var(--ink); cursor: pointer;
}
.menu__item:hover { background: var(--surface-hover); }
.menu__item--danger { color: var(--critical-text); }
.menu__item--danger:hover { background: var(--critical-soft); }
.menu__sep { height: 1px; margin: 4px 0; background: var(--rule); }
.menu__heading { padding: 7px 9px 4px; }

/* -- modal ---------------------------------------------------------------- */

.overlay {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(8, 9, 11, .48);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 6vh 16px 16px;
  overflow-y: auto;
  animation: fade .12s ease;
}
@keyframes fade { from { opacity: 0; } }

.modal {
  width: 100%; max-width: 660px;
  background: var(--surface-raised);
  border: 1px solid var(--rule-strong); border-radius: var(--r-lg);
  box-shadow: var(--shadow-pop);
}
.modal--sm { max-width: 420px; }
.modal__head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 15px 18px; border-bottom: 1px solid var(--rule);
}
.modal__body { padding: 18px; }
.modal__foot {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px;
  padding: 13px 18px; border-top: 1px solid var(--rule);
}
.modal__foot .modal__foot-left { margin-right: auto; }

.formgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.formgrid .span-2 { grid-column: span 2; }
@media (max-width: 620px) {
  .formgrid { grid-template-columns: 1fr; }
  .formgrid .span-2 { grid-column: span 1; }
}

/* -- drawer --------------------------------------------------------------- */

.drawer-scrim {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(8, 9, 11, .40);
  animation: fade .12s ease;
}
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 71;
  width: min(480px, 100vw);
  background: var(--surface);
  border-left: 1px solid var(--rule-strong);
  box-shadow: var(--shadow-pop);
  display: flex; flex-direction: column;
  animation: slidein .16s ease;
}
@keyframes slidein { from { transform: translateX(16px); opacity: .6; } }
.drawer__head { padding: 16px 18px 14px; border-bottom: 1px solid var(--rule); }
.drawer__body { flex: 1; overflow-y: auto; padding: 16px 18px 28px; }
.drawer__foot {
  padding: 12px 18px; border-top: 1px solid var(--rule);
  display: flex; gap: 8px; align-items: center;
}

/* -- toast ---------------------------------------------------------------- */

.toast-root {
  position: fixed; left: 50%; bottom: 22px; z-index: 90;
  transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none;
}
.toast {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 14px;
  background: var(--ink); color: var(--ink-inverse);
  border-radius: var(--r-md); box-shadow: var(--shadow-pop);
  font-size: var(--t-body);
  pointer-events: auto;
  animation: toastin .16s ease;
}
@keyframes toastin { from { transform: translateY(8px); opacity: 0; } }
.toast__undo {
  background: none; border: 0; padding: 0;
  color: var(--ink-inverse); font-weight: 600; font-size: var(--t-body);
  text-decoration: underline; cursor: pointer;
}

/* -- empty state ---------------------------------------------------------- */

.empty {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 52px 20px; text-align: center;
}
.empty__title { font-family: var(--font-display); font-weight: 600; font-size: var(--t-lead); }
.empty p { color: var(--ink-secondary); font-size: var(--t-body); max-width: 42ch; }

/* -- chart chrome --------------------------------------------------------- */

.chart { position: relative; }
.chart svg { width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-line { stroke: var(--rule-strong); stroke-width: 1; }
.chart .tick {
  fill: var(--ink-muted); font-family: var(--font-ui);
  font-size: 11px; font-variant-numeric: tabular-nums;
}
.chart .mark-label {
  fill: var(--ink-secondary); font-family: var(--font-ui);
  font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums;
}
.chart .hit { fill: transparent; cursor: pointer; }
.chart .hit:hover + .hover-band, .chart .hit:focus-visible + .hover-band { opacity: 1; }
.chart .hover-band { fill: var(--ink); opacity: 0; pointer-events: none; }
.chart .mark { transition: opacity .1s ease; }
.chart.is-hovering .mark:not(.is-hot) { opacity: .45; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-bottom: 10px; }
.legend__item { display: inline-flex; align-items: center; gap: 6px; font-size: var(--t-small); color: var(--ink-secondary); }
.legend__swatch { width: 10px; height: 10px; border-radius: 1px; flex: none; }

.charttip {
  position: absolute; z-index: 20;
  min-width: 128px; padding: 8px 10px;
  background: var(--surface-raised);
  border: 1px solid var(--rule-strong); border-radius: var(--r-md);
  box-shadow: var(--shadow-pop);
  font-size: var(--t-small); pointer-events: none;
  transform: translate(-50%, calc(-100% - 10px));
}
.charttip__title {
  font-family: var(--font-mono); font-size: var(--t-micro);
  letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-muted);
  margin-bottom: 5px;
}
.charttip__row { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.charttip__row + .charttip__row { margin-top: 3px; }
.charttip__key { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-secondary); }
.charttip__val { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--ink); }
