/* TT UI redesign v2 — reusable component primitives.
   Everything here is page-agnostic. Page composition lives in styles.css.
   Sections are added per phase; nothing is added speculatively.

   1. Base & reset
   2. Typography helpers
   3. Buttons
   4. Cards & surfaces
   5. Badges, pills, dots
   6. Form controls
   7. Menus
   8. Banners
   9. Toasts
  10. States (loading / empty / error)
  11. Utilities
*/

/* ============ 1. Base & reset ============ */

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

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4, h5, h6, p, figure {
  margin: 0;
}

ul, ol {
  margin: 0;
  padding: 0;
  list-style: none;
}

img, svg, video {
  display: block;
  max-width: 100%;
}

/* Must win over the display values components set on themselves. */
[hidden] {
  display: none !important;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  color: var(--accent-hover);
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: var(--space-5) 0;
}

/* One focus treatment for the whole app. Keyboard only — never on mouse click. */
:focus {
  outline: none;
}

:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
}

::selection {
  background: var(--accent-soft);
  color: var(--text);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ============ 2. Typography helpers ============ */

.u-eyebrow {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-muted);
}

.u-muted { color: var(--text-muted); }
.u-secondary { color: var(--text-secondary); }
.u-mono { font-family: var(--font-mono); font-size: 0.95em; }

/* ============ 3. Buttons ============ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0 var(--space-4);
  height: 36px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-raised);
  color: var(--text);
  font-size: var(--text-base);
  font-weight: 550;
  white-space: nowrap;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease),
    color var(--dur) var(--ease);
}

.btn:hover:not(:disabled) {
  background: var(--surface-overlay);
  border-color: var(--border-strong);
  color: var(--text);
}

.btn:active:not(:disabled) {
  transform: translateY(0.5px);
}

.btn:disabled,
.btn[aria-disabled='true'] {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.btn--primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: var(--on-accent);
}

.btn--primary:active:not(:disabled) {
  background: var(--accent-active);
}

/* Destructive but not primary: cancelling is a real action a creator may want, so it
   stays legible rather than hidden, and reads as danger only on the label and border. */
.btn--danger {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

.btn--danger:hover:not(:disabled) {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
}

.btn--ghost {
  background: transparent;
  border-color: var(--border);
}

.btn--ghost:hover:not(:disabled) {
  background: var(--surface-raised);
}

.btn--quiet {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}

.btn--quiet:hover:not(:disabled) {
  background: var(--surface-raised);
  color: var(--text);
}

.btn--lg {
  height: 44px;
  padding: 0 var(--space-6);
  font-size: var(--text-md);
}

.btn--sm {
  height: 30px;
  padding: 0 var(--space-3);
  font-size: var(--text-sm);
}

.btn--icon {
  width: 36px;
  padding: 0;
  color: var(--text-secondary);
  background: transparent;
  border-color: transparent;
}

.btn--icon:hover:not(:disabled) {
  background: var(--surface-raised);
  color: var(--text);
}

.btn--icon.btn--sm { width: 30px; }

.btn--block {
  width: 100%;
}

/* ============ 4. Cards & surfaces ============ */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
}

.card--raised {
  background: var(--surface-raised);
  box-shadow: var(--shadow-sm);
}

.card--flush {
  padding: 0;
  overflow: hidden;
}

.card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.card__title {
  font-size: var(--text-md);
  font-weight: 600;
  letter-spacing: var(--tracking-tight);
}

.card__note {
  font-size: var(--text-sm);
  color: var(--text-muted);
}

/* ============ 5. Badges, pills, dots ============ */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: 3px var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface-raised);
  color: var(--text-secondary);
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
}

.badge--live { background: var(--live-soft); border-color: transparent; color: var(--live); }
.badge--ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.badge--warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.badge--danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.badge--info { background: var(--info-soft); border-color: transparent; color: var(--info); }
.badge--accent { background: var(--accent-soft); border-color: transparent; color: var(--accent); }

.dot {
  width: 6px;
  height: 6px;
  flex: none;
  border-radius: var(--radius-full);
  background: currentColor;
}

.dot--pulse {
  animation: tt-pulse 1.8s var(--ease) infinite;
}

@keyframes tt-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.82); }
}

/* ============ 6. Form controls ============ */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

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

.input,
.select {
  height: 36px;
  padding: 0 var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-sunken);
  color: var(--text);
  font-size: var(--text-base);
  transition: border-color var(--dur) var(--ease);
}

.input:hover:not(:disabled),
.select:hover:not(:disabled) {
  border-color: var(--text-muted);
}

.input::placeholder {
  color: var(--text-muted);
}

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

.select {
  padding-right: var(--space-7);
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}

/* ============ 7. Menus ============ */

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

.menu[hidden] {
  display: none;
}

.menu__label {
  padding: var(--space-2) var(--space-3);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-muted);
}

.menu__item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--text-base);
  text-align: left;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.menu__item:hover {
  background: var(--surface-raised);
  color: var(--text);
}

.menu__item.is-active {
  background: var(--accent-soft);
  color: var(--accent);
}

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

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

/* ============ 8. Banners ============ */

.banner {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.banner__icon {
  flex: none;
  color: var(--text-muted);
}

.banner__body { flex: 1; min-width: 0; }
.banner__body strong { color: var(--text); font-weight: 600; }

.banner--accent { background: var(--accent-soft); border-bottom-color: var(--accent-border); }
.banner--accent .banner__icon { color: var(--accent); }
.banner--warn { background: var(--warn-soft); border-bottom-color: transparent; }
.banner--warn .banner__icon { color: var(--warn); }
.banner--danger { background: var(--danger-soft); border-bottom-color: transparent; }
.banner--danger .banner__icon { color: var(--danger); }

/* ============ 9. Toasts ============ */

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

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

.toast--ok { border-left-color: var(--ok); }
.toast--error { border-left-color: var(--danger); }
.toast--warn { border-left-color: var(--warn); }
.toast--info { border-left-color: var(--info); }

.toast.is-leaving {
  animation: tt-toast-out var(--dur) var(--ease) forwards;
}

.toast__body { flex: 1; min-width: 0; }
.toast__title { font-weight: 600; color: var(--text); }
.toast__text { color: var(--text-secondary); }

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

@keyframes tt-toast-out {
  to { opacity: 0; transform: translateX(12px); }
}

/* ============ 10. States ============ */
/* The single source of truth for loading / empty / error / disabled feedback.
   Every view uses these rather than inventing its own. */

.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-8) var(--space-5);
  text-align: center;
  color: var(--text-muted);
}

.state__icon {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface-raised);
  color: var(--text-muted);
  font-size: var(--text-md);
}

.state__title {
  color: var(--text);
  font-size: var(--text-md);
  font-weight: 600;
}

.state__text {
  max-width: 44ch;
  font-size: var(--text-sm);
}

.state--error .state__icon { border-color: transparent; background: var(--danger-soft); color: var(--danger); }
.state--inline { padding: var(--space-5) var(--space-4); }

.spinner {
  width: 16px;
  height: 16px;
  flex: none;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: var(--radius-full);
  animation: tt-spin 0.7s linear infinite;
}

.spinner--lg { width: 28px; height: 28px; border-width: 3px; }

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

.skeleton {
  border-radius: var(--radius-sm);
  background: linear-gradient(
    90deg,
    var(--surface-raised) 25%,
    var(--surface-overlay) 50%,
    var(--surface-raised) 75%
  );
  background-size: 200% 100%;
  animation: tt-shimmer 1.4s linear infinite;
}

@keyframes tt-shimmer {
  to { background-position: -200% 0; }
}

/* ============ 11. Switches, sliders, pills (Phase 2) ============ */

.switch {
  position: relative;
  width: 38px;
  height: 22px;
  flex: none;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-full);
  background: var(--surface-sunken);
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}

.switch__thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: var(--radius-full);
  background: var(--text-muted);
  transition: transform var(--dur) var(--ease), background var(--dur) var(--ease);
}

.switch[aria-checked='true'] {
  background: var(--accent);
  border-color: var(--accent);
}

.switch[aria-checked='true'] .switch__thumb {
  background: var(--on-accent);
  transform: translateX(16px);
}

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

.switch--sm { width: 32px; height: 19px; }
.switch--sm .switch__thumb { width: 13px; height: 13px; }
.switch--sm[aria-checked='true'] .switch__thumb { transform: translateX(13px); }

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-2) 0;
}

.toggle-row__text { display: flex; flex-direction: column; min-width: 0; }
.toggle-row__text b { font-size: var(--text-sm); font-weight: 600; }
.toggle-row__text small { font-size: var(--text-2xs); color: var(--text-muted); }

.slider { display: flex; flex-direction: column; gap: var(--space-1); width: 100%; }

.slider__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-2);
}

.slider__label { font-size: var(--text-2xs); font-weight: 600; letter-spacing: var(--tracking-caps); text-transform: uppercase; color: var(--text-muted); }
.slider__value { font-size: var(--text-sm); font-weight: 600; font-variant-numeric: tabular-nums; }

.slider__input {
  width: 100%;
  height: 18px;
  margin: 0;
  appearance: none;
  background: transparent;
  cursor: pointer;
}

.slider__input::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: var(--radius-full);
  background: var(--border-strong);
}

.slider__input::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -5px;
  border: 0;
  border-radius: var(--radius-full);
  background: var(--accent);
}

.slider__input::-moz-range-track {
  height: 4px;
  border-radius: var(--radius-full);
  background: var(--border-strong);
}

.slider__input::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 0;
  border-radius: var(--radius-full);
  background: var(--accent);
}

.pills { display: flex; gap: 2px; flex-wrap: wrap; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: 30px;
  padding: 0 var(--space-3);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: 550;
}

.pill:hover { background: var(--surface-raised); color: var(--text); }

.pill.is-active {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--accent);
}

.pill__count {
  font-size: var(--text-2xs);
  font-variant-numeric: tabular-nums;
  opacity: 0.75;
}

.input--compact { width: 100%; max-width: 140px; }

/* ============ 12. Drawer ============ */

.drawer { position: fixed; inset: 0; z-index: var(--z-drawer); }

.drawer__scrim {
  position: absolute;
  inset: 0;
  background: var(--scrim);
  animation: tt-fade-in var(--dur) var(--ease);
}

.drawer__panel {
  position: absolute;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  width: min(420px, 100vw);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
}

.drawer__panel--right {
  right: 0;
  border-left: 1px solid var(--border);
  animation: tt-slide-in-right var(--dur-slow) var(--ease-out);
}

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

.drawer__title { font-size: var(--text-md); font-weight: 650; }

.drawer__body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

.drawer__section { display: flex; flex-direction: column; gap: var(--space-2); }

.drawer__section-title {
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-muted);
}

.drawer__section-note { font-size: var(--text-xs); color: var(--text-muted); }

body.is-drawer-open { overflow: hidden; }

@keyframes tt-fade-in { from { opacity: 0; } }
@keyframes tt-slide-in-right { from { transform: translateX(100%); } }

@media (max-width: 640px) {
  .drawer__panel--right {
    top: auto;
    width: 100vw;
    max-height: 86vh;
    border-left: 0;
    border-top: 1px solid var(--border);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    animation: tt-slide-in-bottom var(--dur-slow) var(--ease-out);
  }
  @keyframes tt-slide-in-bottom { from { transform: translateY(100%); } }
}

/* ============ 13. Utilities ============ */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

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

.cluster--tight { gap: var(--space-2); }

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.u-hidden {
  display: none !important;
}

/* ============ 14. Tabs, modal, typeahead (Phase 3/4) ============ */

.tabs {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-sunken);
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: 30px;
  padding: 0 var(--space-4);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--text-base);
  font-weight: 550;
  white-space: nowrap;
}

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

.tab.is-selected {
  background: var(--surface-raised);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.tab__count {
  font-size: var(--text-2xs);
  font-variant-numeric: tabular-nums;
  opacity: 0.7;
}

.tab-panel:focus { outline: none; }

.modal { position: fixed; inset: 0; z-index: var(--z-modal); }

.modal__scrim {
  position: absolute;
  inset: 0;
  background: var(--scrim);
  animation: tt-fade-in var(--dur) var(--ease);
}

.modal__panel {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(560px, calc(100vw - var(--space-6)));
  max-width: 100%;
  max-height: min(84vh, 760px);
  margin: max(var(--space-6), 6vh) auto 0;
  overflow-x: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  animation: tt-modal-in var(--dur-slow) var(--ease-out);
}

.modal__panel--lg { width: min(760px, calc(100vw - var(--space-6))); }

@keyframes tt-modal-in {
  from { opacity: 0; transform: translateY(12px) scale(0.99); }
}

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

.modal__title { font-size: var(--text-md); font-weight: 650; }

.modal__body {
  flex: 1;
  min-width: 0;
  max-width: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  padding: var(--space-6);
}

.modal__foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--border);
}

body.is-modal-open { overflow: hidden; }

.typeahead-wrap { position: relative; }

.typeahead {
  position: absolute;
  top: calc(100% + 2px);
  left: 0;
  right: 0;
  z-index: 5;
  padding: var(--space-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-overlay);
  box-shadow: var(--shadow-lg);
}

.typeahead__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--text-sm);
  text-align: left;
}

.typeahead__item:hover { background: var(--surface-raised); color: var(--text); }

/* ============ 15. Thumbnails (Task 3) ============ */

.thumb {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--thumb-size, 32px);
  height: var(--thumb-size, 32px);
  flex: none;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface-sunken);
  color: var(--text-muted);
  font-size: var(--text-2xs);
  font-weight: 700;
}

.thumb--md { border-radius: var(--radius-sm); }
.thumb--full { border-radius: var(--radius-full); }

.thumb__fallback { display: grid; place-items: center; line-height: 1; }

.thumb__img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Once a real image lands, drop the placeholder chrome behind it. */
.thumb.has-image {
  border-color: transparent;
  background: transparent;
}

.thumb.has-image .thumb__fallback { display: none; }
