/* Toggle base design system & foundational styles */
@font-face {
  font-family: 'Space Grotesk';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('/fonts/space-grotesk-latin.woff2') format('woff2');
}

@font-face {
  font-family: 'Syne';
  font-style: normal;
  font-weight: 600 800;
  font-display: swap;
  src: url('/fonts/syne-latin.woff2') format('woff2');
}

@font-face {
  font-family: 'JetBrains Mono';
  font-style: normal;
  font-weight: 400 600;
  font-display: swap;
  src: url('/fonts/jetbrains-mono-latin.woff2') format('woff2');
}

@view-transition {
  navigation: auto;
}

::view-transition-old(root) {
  animation: fade-out var(--dur-base) var(--ease-out);
}
::view-transition-new(root) {
  animation: fade-in-up var(--dur-base) var(--ease-out);
}

@keyframes fade-out {
  from { opacity: 1; }
  to { opacity: 0; }
}
@keyframes fade-in-up {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

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

html {
  background-color: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.55;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  background-color: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* Typography rules */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
  letter-spacing: -0.03em;
  text-wrap: balance;
}

h1 {
  font-size: 26px;
}

h2 {
  font-size: 19px;
}

h3 {
  font-size: 16px;
}

p {
  text-wrap: pretty;
}

small {
  font-size: 13px;
  color: var(--muted);
  line-height: 1.4;
}

code, kbd, samp, pre, .mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.num, .tabular {
  font-variant-numeric: tabular-nums;
}

a {
  color: var(--accent-ink);
  text-decoration: none;
  transition: opacity var(--dur-fast) var(--ease-out);
}

a:hover {
  opacity: 0.85;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Accessible skip link */
.skip-link {
  position: absolute;
  top: -100px;
  left: var(--space-4);
  background: var(--surface);
  color: var(--text);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong);
  z-index: 1000;
  font-weight: 600;
  font-size: 13px;
  transition: top var(--dur-fast);
}

.skip-link:focus {
  top: var(--space-4);
}

/* Uniform Lucide SVG Icons */
.icon {
  width: 18px;
  height: 18px;
  stroke-width: 1.8;
  stroke: currentColor;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
  vertical-align: middle;
}

.icon-14 {
  width: 14px;
  height: 14px;
}

.icon-16 {
  width: 16px;
  height: 16px;
}

.icon-20 {
  width: 20px;
  height: 20px;
}

.icon-24 {
  width: 24px;
  height: 24px;
}

.icon-brand {
  fill: currentColor;
  stroke: none;
}

/* Brand mark component */
.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text, currentColor);
  font-family: var(--font-display, var(--font-body));
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  text-decoration: none;
}

.brand-mark svg,
.brand-glyph,
.brand-toggle-glyph {
  width: 24px;
  height: 24px;
  display: block;
  flex: none;
  border-radius: 6px;
}

/* 1. BUTTONS (Minimum 44px hit targets) */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: 44px;
  min-height: 44px;
  padding: 0 var(--space-5);
  border-radius: var(--radius-pill);
  font-family: var(--font-body);
  font-size: 14px;
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: transform var(--dur-fast) var(--ease-out), background-color var(--dur-fast) var(--ease-out), opacity var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out);
  white-space: nowrap;
  user-select: none;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

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

.btn-primary:hover:not(:disabled) {
  opacity: 0.92;
  transform: translateY(-1px);
}

.btn-secondary {
  background-color: var(--surface-2);
  color: var(--text);
  border-color: var(--line);
}

.btn-secondary:hover:not(:disabled) {
  background-color: color-mix(in srgb, var(--text) 8%, var(--surface));
  border-color: var(--line-strong);
  transform: translateY(-1px);
}

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

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

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

.btn-danger:hover:not(:disabled) {
  background-color: color-mix(in srgb, var(--danger) 12%, transparent);
}

.btn-danger-solid {
  background-color: color-mix(in srgb, var(--danger) 15%, transparent);
  color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 32%, transparent);
}

.btn-danger-solid:hover:not(:disabled) {
  background-color: color-mix(in srgb, var(--danger) 25%, transparent);
}

.btn-sm {
  height: 38px;
  min-height: 38px;
  padding: 0 var(--space-3);
  font-size: 13px;
  position: relative;
}

/* Ensure 44px tap target on small button */
.btn-sm::after {
  content: '';
  position: absolute;
  top: -3px;
  bottom: -3px;
  left: 0;
  right: 0;
}

.btn-icon {
  width: 44px;
  height: 44px;
  min-height: 44px;
  min-width: 44px;
  padding: 0;
  border-radius: var(--radius-pill);
}

.btn-hero {
  height: 48px;
  min-height: 48px;
  padding: 0 28px;
  font-size: 15px;
}

.btn-hero-sec {
  height: 48px;
  min-height: 48px;
  padding: 0 24px;
  font-size: 15px;
}

.btn:disabled, .btn[aria-disabled="true"] {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: var(--radius-pill);
  animation: spin 0.7s linear infinite;
}

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

/* 2. INPUTS & FORMS (16px base font size to prevent iOS zoom) */
.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.form-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.input, .select, .textarea {
  width: 100%;
  height: 44px;
  min-height: 44px;
  padding: 0 var(--space-4);
  background-color: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: 16px; /* 16px prevents iOS Safari form zoom */
  line-height: 1.4;
  transition: border-color var(--dur-fast), background-color var(--dur-fast);
}

.textarea {
  height: auto;
  min-height: 100px;
  padding: var(--space-3) var(--space-4);
  resize: vertical;
}

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

.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  background-color: var(--surface);
  box-shadow: 0 0 0 1px var(--accent);
}

.input-addon-group {
  display: flex;
  align-items: stretch;
  background-color: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  height: 44px;
  min-height: 44px;
}

.input-addon-group:hover {
  border-color: var(--line-strong);
}

.input-addon-group:focus-within {
  border-color: var(--accent);
  background-color: var(--surface);
  box-shadow: 0 0 0 1px var(--accent);
}

.input-addon {
  display: inline-flex;
  align-items: center;
  padding: 0 var(--space-3);
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 14px;
  user-select: none;
}

.input-addon-group .input {
  background: transparent;
  border: none;
  padding-left: 0;
  box-shadow: none;
}

.input-addon-group .input:focus {
  border: none;
  background: transparent;
  box-shadow: none;
}

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

.input-with-action .input {
  padding-right: 48px;
}

.password-toggle-btn {
  position: absolute;
  right: 2px;
  top: 50%;
  transform: translateY(-50%);
}

.input-error-msg {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-size: 12px;
  color: var(--danger);
  margin-top: 2px;
}

/* 3. STATUS CHIPS */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
  border: 1px solid transparent;
}

.chip-active, .chip-success {
  background-color: color-mix(in srgb, var(--accent) 15%, transparent);
  color: var(--accent-ink);
  border-color: color-mix(in srgb, var(--accent) 25%, transparent);
}

.chip-paused, .chip-neutral {
  background-color: color-mix(in srgb, var(--muted) 14%, transparent);
  color: var(--muted);
  border-color: color-mix(in srgb, var(--muted) 24%, transparent);
}

.chip-warning {
  background-color: color-mix(in srgb, var(--warning) 16%, transparent);
  color: var(--warning);
  border-color: color-mix(in srgb, var(--warning) 30%, transparent);
}

.chip-danger {
  background-color: color-mix(in srgb, var(--danger) 16%, transparent);
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 30%, transparent);
}

/* 4. SWITCH */
.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  cursor: pointer;
  vertical-align: middle;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.switch-track {
  position: absolute;
  inset: 0;
  background-color: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-pill);
  transition: background-color var(--dur-base), border-color var(--dur-base);
}

.switch-track::before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 2px;
  bottom: 2px;
  background-color: var(--muted);
  border-radius: var(--radius-pill);
  transition: transform var(--dur-base) var(--ease-out), background-color var(--dur-base);
}

.switch input:checked + .switch-track {
  background-color: color-mix(in srgb, var(--accent) 22%, var(--surface));
  border-color: var(--accent);
}

.switch input:checked + .switch-track::before {
  transform: translateX(20px);
  background-color: var(--accent);
}

.switch input:focus-visible + .switch-track {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* 5. SEGMENTED CONTROL */
.segmented-control {
  display: inline-flex;
  padding: 3px;
  background-color: var(--surface-2);
  border-radius: var(--radius-pill);
  border: 1px solid var(--line);
  position: relative;
  user-select: none;
}

.segmented-option {
  position: relative;
  z-index: 1;
  min-height: 38px;
  padding: 6px 18px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border-radius: var(--radius-pill);
  border: none;
  background: transparent;
  transition: color var(--dur-fast), background-color var(--dur-fast);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.segmented-option.is-active {
  color: var(--text);
  background-color: var(--surface);
  box-shadow: var(--shadow-sm);
}

/* 6. METER */
.meter {
  width: 100%;
  height: 6px;
  background-color: var(--surface-2);
  border-radius: var(--radius-pill);
  overflow: hidden;
}

.meter-fill {
  width: var(--w, 0%);
  height: 100%;
  background-color: var(--accent);
  border-radius: var(--radius-pill);
  transition: width var(--dur-slow) var(--ease-out);
}

/* 7. DIALOG & BOTTOM SHEET */
dialog {
  margin: auto;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text);
  max-width: min(92vw, 480px);
  width: 100%;
}

dialog::backdrop {
  background-color: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fade-in var(--dur-base) var(--ease-out);
}

.dialog-panel {
  background-color: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-elevated);
  padding: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  transform: scale(0.97);
  opacity: 0;
  transition: transform var(--dur-base) var(--ease-out), opacity var(--dur-base) var(--ease-out);
}

dialog[open] .dialog-panel {
  transform: scale(1);
  opacity: 1;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-2);
}

.dialog-body-text {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

/* Mobile bottom sheet for dialogs */
@media (max-width: 640px) {
  dialog {
    margin: auto auto 0 auto;
    max-width: 100vw;
    width: 100%;
  }

  .dialog-panel {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    border-top-left-radius: var(--radius-lg);
    border-top-right-radius: var(--radius-lg);
    border-bottom: none;
    padding: var(--space-5) var(--space-4) max(var(--space-6), env(safe-area-inset-bottom));
    transform: translateY(100%);
  }

  dialog[open] .dialog-panel {
    transform: translateY(0);
  }

  .dialog-sheet-handle {
    width: 36px;
    height: 4px;
    border-radius: var(--radius-pill);
    background-color: var(--line-strong);
    margin: 0 auto var(--space-2);
  }
}

/* 8. TOAST NOTIFICATIONS */
.toast-container {
  position: fixed;
  bottom: var(--space-6);
  right: var(--space-6);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  pointer-events: none;
}

@media (max-width: 768px) {
  .toast-container {
    bottom: auto;
    top: var(--space-4);
    left: var(--space-4);
    right: var(--space-4);
    align-items: center;
  }
}

.toast {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background-color: var(--surface);
  color: var(--text);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-card);
  border: 1px solid var(--line);
  font-size: 13px;
  font-weight: 500;
  animation: toast-in var(--dur-base) var(--ease-out);
}

.toast-success .toast-icon {
  color: var(--accent-ink);
}

.toast-error .toast-icon {
  color: var(--danger);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* 9. EMPTY STATE */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-8) var(--space-4);
  gap: var(--space-3);
  color: var(--muted);
}

.empty-state-illustration {
  width: 88px;
  height: 88px;
  margin-bottom: var(--space-2);
  display: grid;
  place-items: center;
}

.empty-state-illustration svg {
  width: 100%;
  height: 100%;
}

.empty-state h2 {
  color: var(--text);
  font-size: 18px;
}

.empty-state p {
  max-width: 400px;
  font-size: 14px;
  line-height: 1.6;
}

/* 10. SKELETON PLACEHOLDER COMPONENT */
.skeleton {
  background: linear-gradient(
    90deg,
    var(--surface-2) 0%,
    color-mix(in srgb, var(--text) 8%, var(--surface)) 50%,
    var(--surface-2) 100%
  );
  background-size: 200% 100%;
  border-radius: var(--radius-sm);
  animation: skeleton-shimmer 1.8s ease-in-out infinite;
}

@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton-text {
  height: 14px;
  margin-bottom: var(--space-2);
  border-radius: 4px;
}

.skeleton-title {
  height: 24px;
  width: 60%;
  margin-bottom: var(--space-3);
  border-radius: 6px;
}

.skeleton-circle {
  border-radius: var(--radius-pill);
}

/* CARDS */
.card {
  background-color: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: var(--space-6);
  position: relative;
}

.card-sm {
  padding: var(--space-4);
  border-radius: var(--radius-md);
}

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

.card-header-bar {
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

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

.card-padded {
  padding: 24px;
}

/* TABLES (Clean Cloudflare density) */
.table-container {
  width: 100%;
  overflow-x: auto;
}

.table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

.table th {
  padding: 12px 16px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

.table td {
  padding: 13px 16px;
  font-size: 14px;
  border-bottom: 1px solid var(--line);
  color: var(--text);
  vertical-align: middle;
}

.table tbody tr {
  transition: background-color var(--dur-fast);
}

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

.table-empty {
  text-align: center;
  color: var(--muted);
  padding: var(--space-6);
}

/* Utilities (Strictly tokens-based, zero inline style requirements) */
.text-right { text-align: right; }
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-muted { color: var(--muted); }
.text-accent { color: var(--accent-ink); }
.text-danger { color: var(--danger); }
.text-bold { font-weight: 600; }
.text-sm { font-size: 13px; }
.text-xs { font-size: 12px; }
.text-body { font-size: 14px; line-height: 1.6; }

.flex-row { display: flex; align-items: center; gap: var(--space-2); }
.flex-row-between { display: flex; align-items: center; justify-content: space-between; }
.flex-inline { display: inline-flex; align-items: center; gap: var(--space-2); }
.flex-col { display: flex; flex-direction: column; gap: var(--space-2); }
.gap-xs { gap: var(--space-1); }
.gap-sm { gap: var(--space-2); }
.gap-md { gap: var(--space-3); }
.gap-lg { gap: var(--space-4); }

.w-full { width: 100%; }
.m-0 { margin: 0; }
.mt-xs { margin-top: var(--space-1); }
.mt-sm { margin-top: var(--space-2); }
.mt-md { margin-top: var(--space-4); }
.mt-lg { margin-top: var(--space-6); }
.mb-xs { margin-bottom: var(--space-1); }
.mb-sm { margin-bottom: var(--space-2); }
.mb-md { margin-bottom: var(--space-4); }
.mb-lg { margin-bottom: var(--space-6); }

.banner-error {
  background-color: color-mix(in srgb, var(--danger) 12%, var(--surface)) !important;
  border-color: var(--danger) !important;
}

.banner-success {
  background-color: color-mix(in srgb, var(--accent) 12%, var(--surface)) !important;
  border-color: var(--accent) !important;
}

.stat-tile-value,
.plan-card-price,
.details-summary {
  font-family: var(--font-display);
}

.api-key-box,
.rotation-source-pill {
  font-family: var(--font-mono);
}

/* Reduced motion overrides */
@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;
  }
}
