:root {
  --font: "Inter", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 12px 32px rgb(0 0 0 / 0.08);
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --bg: #09090b;
  --bg-elev: #111113;
  --surface: rgb(24 24 27 / 0.72);
  --surface-2: #18181b;
  --border: rgb(255 255 255 / 0.08);
  --border-strong: rgb(255 255 255 / 0.14);
  --text: #fafafa;
  --text-dim: #a1a1aa;
  --text-faint: #71717a;
  --accent: #818cf8;
  --accent-strong: #6366f1;
  --accent-soft: rgb(99 102 241 / 0.14);
  --green: #34d399;
  --green-soft: rgb(52 211 153 / 0.12);
  --warn: #fbbf24;
  --warn-soft: rgb(251 191 36 / 0.1);
  --red: #fb7185;
  --red-soft: rgb(251 113 133 / 0.1);
  --glow: rgb(99 102 241 / 0.28);
  --kbd-bg: #27272a;
  --placeholder: #52525b;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f4f4f5;
  --bg-elev: #ffffff;
  --surface: rgb(255 255 255 / 0.8);
  --surface-2: #fafafa;
  --border: rgb(24 24 27 / 0.08);
  --border-strong: rgb(24 24 27 / 0.14);
  --text: #09090b;
  --text-dim: #52525b;
  --text-faint: #71717a;
  --accent: #4f46e5;
  --accent-strong: #4338ca;
  --accent-soft: rgb(79 70 229 / 0.1);
  --green: #059669;
  --green-soft: rgb(5 150 105 / 0.1);
  --warn: #b45309;
  --warn-soft: rgb(180 83 9 / 0.1);
  --red: #e11d48;
  --red-soft: rgb(225 29 72 / 0.08);
  --glow: rgb(79 70 229 / 0.16);
  --kbd-bg: #e4e4e7;
  --placeholder: #a1a1aa;
  --shadow: 0 1px 2px rgb(24 24 27 / 0.04), 0 16px 40px rgb(24 24 27 / 0.06);
  color-scheme: light;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.ambient {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(900px 420px at 12% -10%, var(--glow), transparent 55%),
    radial-gradient(700px 360px at 92% 0%, rgb(168 85 247 / 0.12), transparent 50%);
}

[data-theme="light"] .ambient {
  background:
    radial-gradient(900px 420px at 12% -10%, rgb(99 102 241 / 0.14), transparent 55%),
    radial-gradient(700px 360px at 92% 0%, rgb(244 114 182 / 0.1), transparent 50%);
}

.app {
  position: relative;
  z-index: 1;
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 20px 0 72px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.hidden {
  display: none !important;
}

/* Nav */
.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0 28px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  color: #fff;
  background: linear-gradient(145deg, #818cf8, #4f46e5 60%, #7c3aed);
  box-shadow: 0 8px 20px rgb(79 70 229 / 0.28);
}

.brand-text {
  font-weight: 650;
  letter-spacing: -0.03em;
  font-size: 1.02rem;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px 6px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-dim);
  font-size: 0.75rem;
  font-weight: 500;
  backdrop-filter: blur(12px);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 4px var(--green-soft);
}

.icon-btn,
.btn-ghost,
.btn-primary,
.seg-btn {
  font-family: inherit;
  cursor: pointer;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text-dim);
  backdrop-filter: blur(12px);
  transition: color 0.2s var(--ease), border-color 0.2s var(--ease), transform 0.2s var(--ease), background 0.2s var(--ease);
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--border-strong);
  transform: translateY(-1px);
}

[data-theme="dark"] .icon-moon,
[data-theme="light"] .icon-sun {
  display: none;
}

/* Hero */
.hero {
  max-width: 680px;
  margin-bottom: 28px;
}

.eyebrow {
  display: inline-flex;
  margin-bottom: 10px;
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.hero h1 {
  font-size: clamp(1.7rem, 3.4vw, 2.45rem);
  font-weight: 650;
  letter-spacing: -0.045em;
  line-height: 1.15;
}

.lede {
  margin-top: 10px;
  max-width: 54ch;
  color: var(--text-dim);
  font-size: 1.02rem;
}

/* Alerts */
.alert {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 16px;
  padding: 12px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
}

.alert code {
  font-family: var(--mono);
  font-size: 0.8em;
}

.alert-warn {
  background: var(--warn-soft);
  border-color: rgb(251 191 36 / 0.28);
  color: var(--warn);
}

.alert-error {
  background: var(--red-soft);
  border-color: rgb(251 113 133 / 0.28);
  color: var(--red);
}

/* Layout */
.workspace {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  margin-bottom: 16px;
}

@media (min-width: 900px) {
  .workspace {
    grid-template-columns: 1.05fr 0.95fr;
    align-items: start;
  }

  #error-panel,
  #skeleton-panel {
    grid-column: 1 / -1;
  }

  .result-panel,
  .input-panel {
    min-height: 100%;
  }
}

.panel {
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.panel-kicker {
  color: var(--text-faint);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* Segmented control */
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 14px;
  padding: 5px;
  border-radius: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.seg-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 12px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  text-align: left;
  transition: background 0.2s var(--ease), color 0.2s var(--ease), border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.seg-title {
  color: var(--text);
  font-size: 0.92rem;
  font-weight: 600;
}

.seg-sub {
  font-size: 0.75rem;
}

.seg-btn:hover {
  color: var(--text);
}

.seg-btn.active {
  background: var(--bg-elev);
  border-color: var(--border);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.08);
}

.seg-btn.active .seg-title {
  color: var(--accent);
}

textarea {
  width: 100%;
  min-height: 180px;
  padding: 14px;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.55;
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

textarea::placeholder {
  color: var(--placeholder);
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.input-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.meta-row {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-faint);
  font-size: 0.8rem;
}

.kbd-hint {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

kbd {
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--kbd-bg);
  font-family: var(--mono);
  font-size: 0.72rem;
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: linear-gradient(180deg, #818cf8, var(--accent-strong));
  color: #fff;
  font-size: 0.92rem;
  font-weight: 600;
  box-shadow: 0 8px 20px rgb(79 70 229 / 0.28);
  transition: transform 0.2s var(--ease), filter 0.2s var(--ease), opacity 0.2s var(--ease);
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.06);
}

.btn-primary:active:not(:disabled) {
  transform: translateY(0);
}

.btn-primary:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.btn-ghost {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 500;
  transition: color 0.2s var(--ease), border-color 0.2s var(--ease), background 0.2s var(--ease);
}

.btn-ghost:hover {
  color: var(--text);
  border-color: var(--border-strong);
  background: var(--surface-2);
}

.btn-ghost.copied {
  color: var(--green);
  border-color: rgb(52 211 153 / 0.4);
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgb(255 255 255 / 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Result */
.result-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.result-header h2 {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.result-panel {
  animation: rise 0.45s var(--ease);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

#output {
  max-height: 360px;
  overflow: auto;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}

.token-stats {
  display: grid;
  grid-template-columns: 1fr auto 1fr 1fr;
  gap: 10px;
  align-items: stretch;
  margin-top: 14px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
}

.stat-label {
  color: var(--text-faint);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.stat-value {
  font-family: var(--mono);
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: -0.03em;
}

.stat-arrow {
  display: grid;
  place-items: center;
  color: var(--text-faint);
}

.stat.savings {
  background: var(--green-soft);
  border-color: rgb(52 211 153 / 0.22);
}

.stat.savings .stat-value {
  color: var(--green);
}

.stat.savings.negative {
  background: var(--accent-soft);
  border-color: rgb(99 102 241 / 0.22);
}

.stat.savings.negative .stat-value {
  color: var(--accent);
}

/* Skeleton */
.skeleton-panel {
  display: grid;
  gap: 10px;
}

.skel {
  border-radius: 8px;
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border-strong) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s ease infinite;
}

.skel-title { height: 18px; width: 40%; }
.skel-line { height: 12px; width: 100%; }
.skel-line.short { width: 64%; }
.skel-row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin-top: 8px; }
.skel-stat { height: 58px; }

@keyframes shimmer {
  to { background-position: -200% 0; }
}

/* History */
.history-panel {
  margin-top: 4px;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  list-style: none;
}

.history-empty {
  padding: 18px 8px;
  color: var(--text-faint);
  font-size: 0.88rem;
  text-align: center;
}

.history-item {
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  cursor: pointer;
  transition: border-color 0.2s var(--ease), transform 0.2s var(--ease), background 0.2s var(--ease);
}

.history-item:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.hi-idea {
  overflow: hidden;
  font-size: 0.9rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hi-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-top: 6px;
  color: var(--text-faint);
  font-size: 0.74rem;
}

.hi-mode {
  display: inline-flex;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  text-transform: capitalize;
}

.site-footer {
  margin-top: 28px;
  text-align: center;
}

.credits {
  margin: 0 auto 18px;
  padding: 22px 20px;
  max-width: 420px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.credits-kicker {
  color: var(--text-faint);
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.credits-name {
  margin-top: 4px;
  font-size: 1.15rem;
  font-weight: 650;
  letter-spacing: -0.03em;
}

.credits-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 14px;
}

.credits-links a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 500;
  text-decoration: none;
  transition: color 0.2s var(--ease), border-color 0.2s var(--ease), transform 0.2s var(--ease), background 0.2s var(--ease);
}

.credits-links a:hover {
  color: var(--text);
  border-color: var(--accent);
  transform: translateY(-1px);
}

.footer-note {
  color: var(--text-faint);
  font-size: 0.8rem;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (max-width: 720px) {
  .app {
    width: min(1120px, calc(100% - 24px));
  }

  .status-pill {
    display: none;
  }

  .kbd-hint {
    display: none;
  }

  .token-stats {
    grid-template-columns: 1fr 1fr;
  }

  .stat-arrow {
    display: none;
  }

  .hero h1 {
    letter-spacing: -0.035em;
  }

  .input-footer {
    flex-direction: column;
    align-items: stretch;
  }

  .btn-primary {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
