/* TrnkeyAI component library — shared across all four templates.
   Source of truth: DESIGN.MD §4, §7. Consumes assets/css/tokens.css.
   Author once, copy byte-identical to insight-hub, sales-portal, command-center, onboarding. */

/* ==========================================================================
   1. Glass surfaces — .glass, .card, dropdown/hover-card panels
   (DESIGN §4.1 recipe, verbatim, plus dark override)
   ========================================================================== */

.glass,
.card,
.dropdown-menu,
.hovercard-panel {
  background: var(--card);
  backdrop-filter: blur(16px) saturate(140%);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: var(--radius);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.5),
    0 8px 24px rgba(15, 23, 42, 0.08),
    0 2px 6px rgba(15, 23, 42, 0.04);
}

[data-theme="dark"] .glass,
[data-theme="dark"] .card,
[data-theme="dark"] .dropdown-menu,
[data-theme="dark"] .hovercard-panel {
  border-color: rgba(255, 255, 255, 0.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 8px 24px rgba(0, 0, 0, 0.35),
    0 2px 6px rgba(0, 0, 0, 0.2);
}

.card {
  padding: 1.25rem 1.5rem;
}

/* Dense data surfaces where translucency hurts legibility (DESIGN §7.1) */
.card-solid {
  background: var(--card-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow:
    0 8px 24px rgba(15, 23, 42, 0.08),
    0 2px 6px rgba(15, 23, 42, 0.04);
  padding: 1.25rem 1.5rem;
}

[data-theme="dark"] .card-solid {
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.35),
    0 2px 6px rgba(0, 0, 0, 0.2);
}

/* ==========================================================================
   2. KPI tiles (DESIGN §7.2)
   ========================================================================== */

.kpi {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  padding: 1.25rem 1.5rem;
}

.kpi-label {
  font-size: var(--text-xs);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
}

.kpi-value {
  font-size: var(--text-2xl);
  font-weight: 600;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.kpi-delta {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  width: fit-content;
  font-size: var(--text-xs);
  font-weight: 500;
}

.kpi-delta-up { color: var(--success); }
.kpi-delta-down { color: var(--destructive); }
.kpi-delta-up::before { content: "\25B2"; font-size: 0.65em; }
.kpi-delta-down::before { content: "\25BC"; font-size: 0.65em; }

/* ==========================================================================
   3. Badges / status pills (DESIGN §7.3 — verbatim)
   ========================================================================== */

.badge-success,
.badge-warning,
.badge-danger,
.badge-info,
.badge-muted {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.125rem 0.625rem;
  font-size: var(--text-xs);
  font-weight: 500;
  white-space: nowrap;
}

.badge-success { background: color-mix(in srgb, var(--success) 15%, transparent); color: color-mix(in srgb, var(--success) 30%, var(--foreground) 70%); border: 1px solid color-mix(in srgb, var(--success) 35%, transparent); }
.badge-warning { background: color-mix(in srgb, var(--warning) 15%, transparent); color: color-mix(in srgb, var(--warning) 25%, var(--foreground) 75%); border: 1px solid color-mix(in srgb, var(--warning) 35%, transparent); }
.badge-danger  { background: color-mix(in srgb, var(--destructive) 15%, transparent); color: color-mix(in srgb, var(--destructive) 50%, var(--foreground) 50%); border: 1px solid color-mix(in srgb, var(--destructive) 35%, transparent); }
.badge-info    { background: color-mix(in srgb, var(--info) 15%, transparent); color: color-mix(in srgb, var(--info) 65%, var(--foreground) 35%); border: 1px solid color-mix(in srgb, var(--info) 35%, transparent); }
.badge-muted   { background: color-mix(in srgb, var(--muted) 15%, transparent); color: var(--muted); border: 1px solid color-mix(in srgb, var(--muted) 35%, transparent); }

/* Dark theme: brand success/warning/danger already clear 4.5:1 as pure tokens on their
   dark-composited tints (near-black surfaces mute the 15% tint far more than on light
   backgrounds), so keep them fully saturated instead of over-mixing toward foreground.
   --info (= --primary) is the one brand color that still falls short (3.86:1) on its own
   dark tint, so only it gets a (smaller) dark-specific mix. */
[data-theme="dark"] .badge-success { color: var(--success); }
[data-theme="dark"] .badge-warning { color: var(--warning); }
[data-theme="dark"] .badge-danger  { color: var(--destructive); }
[data-theme="dark"] .badge-info    { color: color-mix(in srgb, var(--info) 75%, var(--foreground) 25%); }

/* ==========================================================================
   4. Tables — sortable header pattern (DESIGN §7.4)
   ========================================================================== */

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius);
}

table {
  width: 100%;
  background: var(--card-solid);
  font-size: var(--text-sm);
  border-collapse: separate;
  border-spacing: 0;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--secondary);
  font-weight: 500;
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-align: left;
  padding: 0.625rem 0.875rem;
  white-space: nowrap;
}

td {
  padding: 0.625rem 0.875rem;
  border-top: 1px solid var(--border);
}

tbody tr:hover {
  background: color-mix(in srgb, var(--secondary) 50%, transparent);
}

th.num, td.num {
  text-align: right;
}

th button.sort-btn {
  all: unset;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  cursor: pointer;
  font: inherit;
  color: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  border-radius: 4px;
}

th button.sort-btn:hover {
  background: color-mix(in srgb, var(--secondary) 50%, transparent);
}

th button.sort-btn::after {
  content: "";
  font-size: 0.7em;
  color: var(--muted);
}

th button.sort-btn[data-sort="asc"]::after {
  content: "\25B2";
  color: var(--primary);
}

th button.sort-btn[data-sort="desc"]::after {
  content: "\25BC";
  color: var(--primary);
}

/* Mobile card-stack fallback (Insight Hub pattern) */
@media (max-width: 640px) {
  table.stack thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
    padding: 0;
    margin: -1px;
  }

  /* Table-cell/table-row internal display values, and this component's
     own `th { position: sticky }` rule, both ignore the clipped
     ancestor's 1px box (a browser table-layout/sticky-positioning
     quirk) and can escape it, widening the page. Force them out of
     table layout, out of sticky positioning, and re-zero their own
     box so the clip actually contains them; the thead stays in the
     accessibility tree for screen readers, just with no visual
     footprint. */
  table.stack thead tr,
  table.stack thead th {
    display: block;
    position: static;
    width: 1px;
    height: 1px;
    overflow: hidden;
    padding: 0;
    margin: 0;
    border: 0;
  }

  table.stack,
  table.stack tbody,
  table.stack tr,
  table.stack td {
    display: block;
    width: 100%;
  }

  table.stack tr {
    border-top: 1px solid var(--border);
    padding: 0.75rem 0;
  }

  table.stack tr:first-child {
    border-top: none;
  }

  table.stack td {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 1rem;
    padding: 0.25rem 0.875rem;
    border-top: none;
  }

  table.stack td::before {
    content: attr(data-label);
    font-weight: 500;
    color: var(--muted);
    text-align: left;
  }

  table.stack td.num {
    text-align: right;
  }
}

/* ==========================================================================
   5. Native <dialog> modals (DESIGN §7.5)
   ========================================================================== */

dialog {
  background: var(--card);
  backdrop-filter: blur(16px) saturate(140%);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: var(--radius);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.5),
    0 8px 24px rgba(15, 23, 42, 0.08),
    0 2px 6px rgba(15, 23, 42, 0.04);
  padding: 0;
  max-width: 32rem;
  width: calc(100% - 2rem);
  margin: auto;
}

[data-theme="dark"] dialog {
  border-color: rgba(255, 255, 255, 0.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 8px 24px rgba(0, 0, 0, 0.35),
    0 2px 6px rgba(0, 0, 0, 0.2);
}

dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(2px);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.dialog-title {
  font-size: var(--text-lg);
  font-weight: 600;
}

.dialog-body {
  padding: 1.5rem;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  padding: 1rem 1.5rem 1.5rem;
}

/* ==========================================================================
   6. Tabs (DESIGN §7.6)
   ========================================================================== */

.tabs-pill {
  display: inline-flex;
  gap: 0.25rem;
  background: var(--secondary);
  border-radius: 999px;
  padding: 0.25rem;
  overflow-x: auto;
}

.tabs-pill button {
  all: unset;
  cursor: pointer;
  padding: 0.375rem 0.875rem;
  border-radius: 999px;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--foreground);
  white-space: nowrap;
}

.tabs-pill button[aria-selected="true"] {
  background: var(--primary);
  color: var(--primary-foreground);
}

.tabs-underline {
  display: flex;
  gap: 1.5rem;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.tabs-underline button {
  all: unset;
  cursor: pointer;
  padding: 0.75rem 0.125rem;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}

.tabs-underline button[aria-selected="true"] {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* ==========================================================================
   7. Toasts (DESIGN §7.7)
   ========================================================================== */

.toast-stack {
  position: fixed;
  right: 1.25rem;
  bottom: 1.25rem;
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  width: min(22rem, calc(100vw - 2.5rem));
}

.toast {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 0.625rem;
  padding: 0.875rem 1rem;
  overflow: hidden;
  border-left: 3px solid var(--info);
}

.toast-success { border-left-color: var(--success); }
.toast-warning { border-left-color: var(--warning); }
.toast-danger { border-left-color: var(--destructive); }
.toast-info { border-left-color: var(--info); }

.toast-icon {
  flex-shrink: 0;
}

.toast-message {
  flex: 1;
  font-size: var(--text-sm);
}

.toast-close {
  all: unset;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
  padding: 0.125rem;
}

.toast-underline {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 3px;
  background: var(--info);
  transform-origin: left;
  animation: toast-shrink 4s linear forwards;
}

.toast-success .toast-underline { background: var(--success); }
.toast-warning .toast-underline { background: var(--warning); }
.toast-danger .toast-underline { background: var(--destructive); }
.toast-info .toast-underline { background: var(--info); }

@keyframes toast-shrink {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}

/* ==========================================================================
   8. Hover-cards — CSS-only (DESIGN §7.8)
   ========================================================================== */

.hovercard {
  position: relative;
  display: inline-block;
}

.hovercard-panel {
  position: absolute;
  top: calc(100% + 0.75rem);
  right: 0;
  width: max-content;
  max-width: 20rem;
  padding: 1rem;
  visibility: hidden;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
  z-index: 40;
}

.hovercard-panel::before {
  content: "";
  position: absolute;
  top: -5px;
  right: 1.25rem;
  width: 10px;
  height: 10px;
  background: var(--card);
  border-left: 1px solid rgba(255, 255, 255, 0.4);
  border-top: 1px solid rgba(255, 255, 255, 0.4);
  transform: rotate(45deg);
}

[data-theme="dark"] .hovercard-panel::before {
  border-color: rgba(255, 255, 255, 0.08);
}

.hovercard:hover .hovercard-panel,
.hovercard:focus-within .hovercard-panel {
  visibility: visible;
  opacity: 1;
  transform: translateY(0);
}

/* ==========================================================================
   9. Chart containers (DESIGN §7.9)
   ========================================================================== */

.chart-wrap {
  position: relative;
  height: 280px;
}

.chart-wrap--mini {
  height: 200px;
}

/* ==========================================================================
   10. Form controls
   ========================================================================== */

.field {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.field label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--foreground);
}

.field-hint {
  font-size: var(--text-xs);
  color: var(--muted);
}

.field-error {
  font-size: var(--text-xs);
  color: var(--destructive);
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="search"],
input[type="date"],
input[type="tel"],
input[type="url"],
textarea,
select {
  width: 100%;
  background: var(--card-solid);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: var(--text-sm);
  color: var(--foreground);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

textarea {
  resize: vertical;
  min-height: 6rem;
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: none;
  border-color: var(--ring);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 20%, transparent);
}

input::placeholder,
textarea::placeholder {
  color: var(--muted);
}

input:disabled,
textarea:disabled,
select:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

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

.checkbox-row,
.radio-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

input[type="checkbox"],
input[type="radio"] {
  width: 1rem;
  height: 1rem;
  accent-color: var(--primary);
}

/* ==========================================================================
   11. Buttons
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.5rem 1rem;
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

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

.btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 25%, transparent);
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-foreground);
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark);
}

/* D20: dark theme's button text is dark (--primary-foreground: #0B1220), so darkening
   the fill on hover moves the wrong way and drops the pair to 3.98:1 (measured) - below
   4.5:1. Rest already clears 4.5:1 (5.43:1) with --primary at full saturation, so only
   the hover shade needs help: lightening toward white keeps it recognizably the brand
   blue while restoring contrast (measured 6.64:1). Light theme's darker --primary-dark
   hover already passes (8.97:1) and is untouched. */
[data-theme="dark"] .btn-primary:hover:not(:disabled) {
  background: color-mix(in srgb, var(--primary) 85%, white 15%);
}

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

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

/* D20 precedent (same as .cc-nav-badge / .badge-danger): light theme's white-on-destructive
   pair measured 3.76:1 at rest and 3.38:1 on hover (the old `opacity: 0.9` hover treatment
   blends with whatever is behind the button - page, card or glass surface - so its contrast
   was backdrop-dependent and unreliable). Both are below 4.5:1. Deepened toward --foreground
   here, in the brand hue, rather than darkening --destructive itself, which drives components
   across all 66 pages. Rest measured 5.07:1, hover 5.85:1, both solid so backdrop no longer
   matters. Dark theme already clears 4.5:1 at full saturation (6.77:1 rest, 5.68:1 hover via
   the original opacity treatment), so it's restored below rather than muted unnecessarily. */
.btn-danger {
  background: color-mix(in srgb, var(--destructive) 70%, var(--foreground) 30%);
  color: var(--primary-foreground);
}

.btn-danger:hover:not(:disabled) {
  background: color-mix(in srgb, var(--destructive) 55%, var(--foreground) 45%);
}

[data-theme="dark"] .btn-danger {
  background: var(--destructive);
}

[data-theme="dark"] .btn-danger:hover:not(:disabled) {
  background: var(--destructive);
  opacity: 0.9;
}

.btn-sm {
  padding: 0.3rem 0.75rem;
  font-size: var(--text-xs);
}

/* ==========================================================================
   12. Drawers — right slide-in
   ========================================================================== */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(15, 23, 42, 0.45);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s;
}

.drawer-backdrop.open,
.drawer-backdrop.is-open,
.drawer-backdrop[data-open="true"] {
  opacity: 1;
  visibility: visible;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 61;
  width: min(28rem, 100vw);
  display: flex;
  flex-direction: column;
  background: var(--card-solid);
  border-left: 1px solid var(--border);
  box-shadow:
    0 8px 24px rgba(15, 23, 42, 0.08),
    0 2px 6px rgba(15, 23, 42, 0.04);
  transform: translateX(100%);
  transition: transform 0.25s ease;
}

[data-theme="dark"] .drawer {
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.35),
    0 2px 6px rgba(0, 0, 0, 0.2);
}

.drawer.open,
.drawer.is-open,
.drawer[data-open="true"] {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
}

.drawer-footer {
  padding: 1.25rem 1.5rem;
  border-top: 1px solid var(--border);
}

/* ==========================================================================
   13. Auth "sky" login treatment (DESIGN §4.3)
   ========================================================================== */

.auth-shell {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: linear-gradient(180deg, #EAF3FC 0%, #F8FAFC 55%, #FFFFFF 100%);
}

[data-theme="dark"] .auth-shell {
  background: linear-gradient(180deg, var(--secondary) 0%, var(--background) 55%, var(--background) 100%);
}

.auth-cloud-band {
  position: absolute;
  top: 28%;
  left: 50%;
  width: 60rem;
  max-width: 150vw;
  height: 18rem;
  transform: translate(-50%, -50%);
  background: color-mix(in srgb, var(--primary) 20%, transparent);
  filter: blur(60px);
  pointer-events: none;
}

.auth-rings {
  position: absolute;
  top: 10%;
  right: -6rem;
  width: 24rem;
  height: 24rem;
  pointer-events: none;
}

.auth-rings span {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 1px solid color-mix(in srgb, var(--primary) 15%, transparent);
}

.auth-rings span:nth-child(2) {
  inset: 2.5rem;
}

.auth-rings span:nth-child(3) {
  inset: 5rem;
}

.auth-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  margin: 1.5rem;
  padding: 2rem;
  background: rgba(255, 255, 255, 0.55);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 24px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.5),
    0 8px 24px rgba(15, 23, 42, 0.08),
    0 2px 6px rgba(15, 23, 42, 0.04);
}

[data-theme="dark"] .auth-card {
  background: var(--card);
  border-color: rgba(255, 255, 255, 0.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 8px 24px rgba(0, 0, 0, 0.35),
    0 2px 6px rgba(0, 0, 0, 0.2);
}

.auth-logo {
  display: block;
  height: 96px;
  width: auto;
  margin: 0 auto 1.5rem;
}

.auth-title {
  font-size: var(--text-xl);
  font-weight: 600;
  text-align: center;
  margin-bottom: 0.375rem;
}

.auth-subtitle {
  font-size: var(--text-sm);
  color: var(--muted);
  text-align: center;
  margin-bottom: 1.5rem;
}
