/* ===================================================================
 * tw-components.css — TriviaWorks unified component pattern library
 * (2026-04-29, editor unification arc step 2)
 *
 * Depends on tw-tokens.css for all visual tokens. Components are
 * single-source-of-truth pattern primitives; per-surface styles
 * extend, never override base shape.
 *
 * Components in this file:
 *   .tw-btn (+ variants, sizes, icon, loading)
 *   .tw-input / .tw-field / .tw-search / select / textarea
 *   .tw-card (interactive + selectable)
 *   .tw-chip (filter / status / pool / removable)
 *   .tw-toast / .tw-toast-stack
 *   .tw-tabs / .tw-tabs-pill
 *   .tw-toolbar (sticky bulk-action bar)
 *   .tw-divider, .tw-spinner, .tw-badge
 *
 * Modal / drawer  → tw-modal.js + tw-drawer.js (step 3)
 * Table           → tw-table.js (step 4)
 * State patterns  → tw-state-patterns.js (step 5)
 * Command palette → tw-command-palette.js (step 6)
 * =================================================================== */

/* ===================================================================
 * Buttons
 * =================================================================== */

.tw-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  height: var(--row-h);
  padding: 0 var(--space-3);
  background: transparent;
  color: var(--text-primary);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font: var(--type-body-strong);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  min-width: var(--target-min);
  transition: background var(--motion-quick) var(--ease-standard),
              border-color var(--motion-quick) var(--ease-standard),
              color var(--motion-quick) var(--ease-standard),
              box-shadow var(--motion-quick) var(--ease-standard);
}
.tw-btn:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}
.tw-btn[disabled],
.tw-btn[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.tw-btn-primary {
  background: var(--accent-primary);
  color: var(--text-inverse);
}
.tw-btn-primary:hover { background: var(--accent-primary-pressed); }
.tw-btn-primary:active { background: var(--accent-primary-pressed); box-shadow: var(--elev-1); }

.tw-btn-secondary {
  background: var(--surface-raised);
  color: var(--text-primary);
  border-color: var(--border-strong);
}
.tw-btn-secondary:hover { background: var(--surface-overlay); }
.tw-btn-secondary:active { background: var(--surface-sunk); }

.tw-btn-tertiary {
  background: transparent;
  color: var(--text-primary);
}
.tw-btn-tertiary:hover { background: var(--surface-overlay); }
.tw-btn-tertiary:active { background: var(--surface-sunk); }

.tw-btn-destructive {
  background: var(--state-error);
  color: var(--text-inverse);
}
.tw-btn-destructive:hover { filter: brightness(0.92); }
.tw-btn-destructive:active { filter: brightness(0.85); }

/* Sizes */
.tw-btn-sm {
  height: 24px;
  padding: 0 var(--space-2);
  font: var(--type-caption);
  font-weight: 500;
  min-width: 24px;
}
.tw-btn-lg {
  height: 40px;
  padding: 0 var(--space-4);
  font: var(--type-body-strong);
  min-width: 40px;
}

/* Icon-only */
.tw-btn-icon {
  width: var(--row-h);
  padding: 0;
  flex: none;
}
.tw-btn-icon.tw-btn-sm { width: 24px; }
.tw-btn-icon.tw-btn-lg { width: 40px; }

/* Loading */
.tw-btn[data-loading="true"] {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}
.tw-btn[data-loading="true"]::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  border: 2px solid;
  border-color: currentColor currentColor currentColor transparent;
  border-radius: 50%;
  animation: tw-spin 800ms linear infinite;
}
.tw-btn-primary[data-loading="true"]::after,
.tw-btn-destructive[data-loading="true"]::after { color: var(--text-inverse); }
.tw-btn-secondary[data-loading="true"]::after,
.tw-btn-tertiary[data-loading="true"]::after { color: var(--text-primary); }

@keyframes tw-spin {
  to { transform: rotate(360deg); }
}

/* ===================================================================
 * Inputs / fields
 * =================================================================== */

.tw-input {
  display: block;
  width: 100%;
  height: var(--input-h);
  padding: 0 var(--space-3);
  background: var(--surface-raised);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  font: var(--type-body);
  transition: border-color var(--motion-quick) var(--ease-standard),
              box-shadow var(--motion-quick) var(--ease-standard);
}
.tw-input::placeholder { color: var(--text-tertiary); }
.tw-input:hover { border-color: var(--text-tertiary); }
.tw-input:focus,
.tw-input:focus-visible {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px var(--border-focus);
}
.tw-input[disabled] {
  background: var(--surface-sunk);
  color: var(--text-tertiary);
  cursor: not-allowed;
  border-color: var(--border-subtle);
}
.tw-input[aria-invalid="true"],
.tw-input.tw-invalid {
  border-color: var(--state-error);
}
.tw-input[aria-invalid="true"]:focus,
.tw-input.tw-invalid:focus {
  box-shadow: 0 0 0 2px var(--state-error);
}

textarea.tw-input {
  height: auto;
  padding: var(--space-2) var(--space-3);
  min-height: 80px;
  line-height: 1.5;
  resize: vertical;
}

select.tw-input {
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--text-tertiary) 50%),
    linear-gradient(135deg, var(--text-tertiary) 50%, transparent 50%);
  background-position: calc(100% - 14px) center, calc(100% - 9px) center;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  padding-right: 28px;
}

/* Field group: label + input + helper/error */
.tw-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.tw-field-label {
  font: var(--type-caption);
  color: var(--text-secondary);
  font-weight: 500;
}
.tw-field-required::after {
  content: " *";
  color: var(--state-error);
}
.tw-field-helper {
  font: var(--type-caption);
  color: var(--text-tertiary);
}
.tw-field-error {
  font: var(--type-caption);
  color: var(--state-error);
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

/* Inline horizontal field */
.tw-field-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}
.tw-field-row > .tw-field-label { min-width: 120px; flex: none; }

/* Search input — label + icon + clear */
.tw-search {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 100%;
  height: var(--input-h);
}
.tw-search input {
  flex: 1;
  width: 100%;
  height: 100%;
  padding: 0 32px 0 32px;
  background: var(--surface-raised);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  font: var(--type-body);
}
.tw-search input::placeholder { color: var(--text-tertiary); }
.tw-search input:focus,
.tw-search input:focus-visible {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px var(--border-focus);
}
.tw-search-icon {
  position: absolute;
  left: 10px;
  width: 16px;
  height: 16px;
  color: var(--text-tertiary);
  pointer-events: none;
}
.tw-search-clear {
  position: absolute;
  right: 6px;
  width: 20px;
  height: 20px;
  border: 0;
  background: transparent;
  color: var(--text-tertiary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tw-search-clear:hover {
  background: var(--surface-overlay);
  color: var(--text-primary);
}
.tw-search-clear:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 1px;
}
.tw-search-clear[hidden],
.tw-search:not([data-has-value]) .tw-search-clear { display: none; }

/* ===================================================================
 * Cards
 * =================================================================== */

.tw-card {
  background: var(--surface-raised);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  box-shadow: var(--elev-1);
  transition: box-shadow var(--motion-quick) var(--ease-standard),
              border-color var(--motion-quick) var(--ease-standard);
}
.tw-card-interactive {
  cursor: pointer;
  user-select: none;
}
.tw-card-interactive:hover {
  box-shadow: var(--elev-2);
  border-color: var(--border-strong);
}
.tw-card-interactive:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}
.tw-card-interactive:active { box-shadow: var(--elev-1); }

.tw-card[aria-selected="true"],
.tw-card-selected {
  border-color: var(--accent-primary);
  box-shadow: var(--elev-2), inset 0 0 0 1px var(--accent-primary);
}

.tw-card-header {
  font: var(--type-h2);
  color: var(--text-primary);
  margin: 0 0 var(--space-2);
}
.tw-card-body {
  font: var(--type-body);
  color: var(--text-secondary);
}
.tw-card-footer {
  margin-top: var(--space-3);
  display: flex;
  gap: var(--space-2);
  justify-content: flex-end;
  align-items: center;
}

.tw-card-pool-internal { border-left: 3px solid var(--pool-internal); }
.tw-card-pool-external { border-left: 3px solid var(--pool-external); }
.tw-card-pool-archive  { border-left: 3px solid var(--pool-archive); }

/* ===================================================================
 * Chips (filter / status / pool / removable)
 * =================================================================== */

.tw-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  height: 22px;
  padding: 0 var(--space-2);
  background: var(--surface-overlay);
  color: var(--text-secondary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  font: var(--type-caption);
  font-weight: 500;
  white-space: nowrap;
  user-select: none;
}
.tw-chip-interactive { cursor: pointer; }
.tw-chip-interactive:hover {
  background: var(--surface-sunk);
  border-color: var(--border-strong);
}
.tw-chip-interactive:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 1px;
}
.tw-chip[aria-pressed="true"],
.tw-chip-active {
  background: var(--accent-primary-bg);
  border-color: var(--accent-primary);
  color: var(--accent-primary-pressed);
}

.tw-chip-success { background: var(--state-success-bg); color: var(--state-success); border-color: transparent; }
.tw-chip-warning { background: var(--state-warning-bg); color: var(--state-warning); border-color: transparent; }
.tw-chip-error   { background: var(--state-error-bg);   color: var(--state-error);   border-color: transparent; }
.tw-chip-info    { background: var(--state-info-bg);    color: var(--state-info);    border-color: transparent; }

.tw-chip-pool-internal { background: var(--pool-internal-bg); color: var(--pool-internal); border-color: transparent; }
.tw-chip-pool-external { background: var(--pool-external-bg); color: var(--pool-external); border-color: transparent; }
.tw-chip-pool-archive  { background: var(--pool-archive-bg);  color: var(--pool-archive);  border-color: transparent; }

.tw-chip-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}

.tw-chip-remove {
  display: inline-flex;
  width: 14px;
  height: 14px;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  border-radius: var(--radius-pill);
  margin-left: 2px;
  margin-right: -3px;
  opacity: 0.7;
}
.tw-chip-remove:hover { opacity: 1; background: rgba(28, 20, 16, 0.1); }
.tw-chip-remove:focus-visible { outline: 1px solid var(--border-focus); }

/* ===================================================================
 * Toasts
 * =================================================================== */

.tw-toast-stack {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: var(--z-toast);
  pointer-events: none;
  max-width: 380px;
  width: calc(100vw - var(--space-7));
}

.tw-toast {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-3);
  background: var(--surface-raised);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: var(--elev-3);
  font: var(--type-body);
  pointer-events: auto;
  animation: tw-toast-in var(--motion-standard) var(--ease-enter);
}
.tw-toast[data-leaving="true"] {
  animation: tw-toast-out var(--motion-standard) var(--ease-exit) forwards;
}
@keyframes tw-toast-in {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes tw-toast-out {
  to { opacity: 0; transform: translateX(8px); }
}
.tw-toast-icon {
  flex: none;
  width: 20px;
  height: 20px;
  margin-top: 1px;
}
.tw-toast-content { flex: 1; min-width: 0; }
.tw-toast-title {
  font: var(--type-body-strong);
  color: var(--text-primary);
}
.tw-toast-message {
  font: var(--type-caption);
  color: var(--text-secondary);
  margin-top: 2px;
}
.tw-toast-close {
  flex: none;
  width: 20px;
  height: 20px;
  border: 0;
  background: transparent;
  color: var(--text-tertiary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  align-self: flex-start;
}
.tw-toast-close:hover { color: var(--text-primary); background: var(--surface-overlay); }
.tw-toast-close:focus-visible { outline: 1px solid var(--border-focus); }
@media (max-width: 1023px) {
  .tw-toast-close {
    width: 32px;
    height: 32px;
  }
}

.tw-toast-success { border-left: 3px solid var(--state-success); }
.tw-toast-success .tw-toast-icon { color: var(--state-success); }
.tw-toast-warning { border-left: 3px solid var(--state-warning); }
.tw-toast-warning .tw-toast-icon { color: var(--state-warning); }
.tw-toast-error   { border-left: 3px solid var(--state-error); }
.tw-toast-error   .tw-toast-icon { color: var(--state-error); }
.tw-toast-info    { border-left: 3px solid var(--state-info); }
.tw-toast-info    .tw-toast-icon { color: var(--state-info); }

/* ===================================================================
 * Tabs (segmented + pill)
 * =================================================================== */

.tw-tabs {
  display: flex;
  gap: var(--space-1);
  border-bottom: 1px solid var(--border-subtle);
}
.tw-tab {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: 36px;
  padding: 0 var(--space-3);
  background: transparent;
  color: var(--text-secondary);
  border: 0;
  border-bottom: 2px solid transparent;
  font: var(--type-body-strong);
  cursor: pointer;
  margin-bottom: -1px;
  transition: color var(--motion-quick) var(--ease-standard);
}
.tw-tab:hover {
  color: var(--text-primary);
  background: var(--surface-overlay);
}
.tw-tab[aria-selected="true"],
.tw-tab-active {
  color: var(--text-primary);
  border-bottom-color: var(--accent-primary);
}
.tw-tab:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: -2px;
  border-radius: var(--radius-sm);
}

.tw-tabs-pill {
  display: inline-flex;
  gap: 0;
  background: var(--surface-overlay);
  padding: 4px;
  border-radius: var(--radius-md);
  border-bottom: 0;
}
.tw-tabs-pill .tw-tab {
  height: 28px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-sm);
  border-bottom: 0;
  margin-bottom: 0;
  font: var(--type-caption);
  font-weight: 500;
}
.tw-tabs-pill .tw-tab:hover { background: var(--surface-raised); }
.tw-tabs-pill .tw-tab[aria-selected="true"],
.tw-tabs-pill .tw-tab.tw-tab-active {
  background: var(--surface-raised);
  border-bottom-color: transparent;
  box-shadow: var(--elev-1);
}

.tw-tab-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 18px;
  padding: 0 6px;
  background: var(--surface-overlay);
  color: var(--text-tertiary);
  border-radius: var(--radius-pill);
  font: var(--type-caption);
  font-weight: 500;
}
.tw-tab[aria-selected="true"] .tw-tab-count,
.tw-tab-active .tw-tab-count {
  background: var(--accent-primary-bg);
  color: var(--accent-primary-pressed);
}

/* ===================================================================
 * Toolbar (sticky bulk-action bar)
 * =================================================================== */

.tw-toolbar {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--surface-raised);
  border-top: 1px solid var(--border-subtle);
  box-shadow: 0 -4px 16px rgba(28, 20, 16, 0.06);
  z-index: var(--z-sticky);
  animation: tw-toolbar-in var(--motion-standard) var(--ease-enter);
}
@keyframes tw-toolbar-in {
  from { transform: translateY(100%); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}
.tw-toolbar-count {
  font: var(--type-body-strong);
  color: var(--text-primary);
}
.tw-toolbar-spacer { flex: 1; }
.tw-toolbar-actions {
  display: flex;
  gap: var(--space-2);
  align-items: center;
}
.tw-toolbar[hidden] { display: none; }

/* ===================================================================
 * Misc primitives
 * =================================================================== */

.tw-divider {
  height: 1px;
  background: var(--border-subtle);
  border: 0;
  margin: var(--space-3) 0;
}
.tw-divider-vertical {
  width: 1px;
  height: 20px;
  background: var(--border-subtle);
  margin: 0 var(--space-2);
  flex: none;
}

.tw-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-subtle);
  border-top-color: var(--accent-primary);
  border-radius: 50%;
  animation: tw-spin 800ms linear infinite;
}
.tw-spinner-lg { width: 24px; height: 24px; border-width: 3px; }
.tw-spinner-sm { width: 12px; height: 12px; border-width: 2px; }

/* ─── Skip link (WCAG 2.4.1) ────────────────────────────────────────────── */
.tw-skiplink {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 10000;
  padding: 8px 12px;
  background: var(--ink-strong-v4, #111);
  color: #fff;
  text-decoration: none;
  border-radius: 0 0 6px 0;
}
.tw-skiplink:focus {
  left: 0;
}

.tw-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  background: var(--accent-primary);
  color: var(--text-inverse);
  border-radius: var(--radius-pill);
  font: var(--type-caption);
  font-weight: 600;
  font-size: 11px;
  line-height: 1;
}
.tw-badge-subtle {
  background: var(--surface-overlay);
  color: var(--text-secondary);
}
.tw-badge-error    { background: var(--state-error); color: var(--text-inverse); }
.tw-badge-success  { background: var(--state-success); color: var(--text-inverse); }
.tw-badge-warning  { background: var(--state-warning); color: var(--text-inverse); }

/* Keyboard hint (kbd) — distinct from regular input */
.tw-kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  background: var(--surface-overlay);
  color: var(--text-secondary);
  border: 1px solid var(--border-subtle);
  border-bottom-width: 2px;
  border-radius: var(--radius-xs);
  font: var(--type-mono);
  font-size: 11px;
  line-height: 1;
}

/* ===================================================================
 * Touch ergonomics — viewport < 1024 px
 * All desktop appearance is unchanged above this breakpoint.
 * =================================================================== */
@media (max-width: 1023px) {
  /* T1 — .tw-btn-sm: bump from 24 px to 32 px */
  .tw-btn-sm {
    height: 32px;
    min-width: 32px;
  }
  .tw-btn-icon.tw-btn-sm {
    width: 32px;
  }

  /* T2 — .tw-chip-remove: visual stays 14 px; tap area expanded to 32×32 px */
  .tw-chip-remove {
    position: relative;
    width: 32px;
    height: 32px;
    margin-left: 0;
    margin-right: -7px;
  }

  /* T3a — .tw-search-clear: bump from 20 px to 32 px */
  .tw-search-clear {
    width: 32px;
    height: 32px;
    right: 2px;
  }

  /* T3b — .tw-toast-close: bump from 20 px to 32 px */
  .tw-toast-close {
    width: 32px;
    height: 32px;
  }

  /* T7 — .tw-tabs-pill .tw-tab: bump from 28 px to 32 px */
  .tw-tabs-pill .tw-tab {
    height: 32px;
  }
}
