/* Dark theme (default) */
:root {
  color-scheme: dark;
  --bg: #0c0b09;
  --surface: #161412;
  --border: #2a2620;
  --text: #f2ebe3;
  --muted: #8a8278;
  --accent: #e8a54b;
  --accent-dim: #c4842e;
  --accent-hover: #f0b35c;
  --on-accent: #0c0b09;
  --error-bg: #2a1414;
  --error-text: #f5a8a8;
  --focus-ring: rgba(232, 165, 75, 0.12);
  --grain-opacity: 0.04;
  --preview-bg: #ffffff;
  --spinner-track: rgba(12, 11, 9, 0.25);
  --spinner-fg: #0c0b09;
  --sidebar-width: min(400px, 36vw);
  --radius: 12px;
  --font-display: "Instrument Serif", Georgia, serif;
  --font-ui: "Outfit", system-ui, sans-serif;
  --font-mono: "Geist Mono", ui-monospace, monospace;
}

/* Light theme — follows system preference */
@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f5f1ea;
    --surface: #ffffff;
    --border: #ddd6cb;
    --text: #1c1916;
    --muted: #6b645c;
    --accent: #d4922a;
    --accent-dim: #b8781e;
    --accent-hover: #e8a54b;
    --on-accent: #1c1916;
    --error-bg: #fce8e6;
    --error-text: #9b2c2c;
    --focus-ring: rgba(184, 120, 30, 0.22);
    --grain-opacity: 0.035;
    --preview-bg: #ffffff;
    --spinner-track: rgba(28, 25, 22, 0.18);
    --spinner-fg: #1c1916;
  }
}

/* Author display rules (e.g. .btn-primary) must not override [hidden] */
[hidden] {
  display: none !important;
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-ui);
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: var(--grain-opacity);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 0;
}

.workspace {
  position: relative;
  z-index: 1;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.workspace-sidebar {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.header {
  padding: 2rem 1.5rem 1.5rem;
  width: 100%;
}

.header-logo {
  display: block;
  width: 100%;
  max-width: 320px;
  height: auto;
  margin: 0 auto 1.25rem;
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
}

.brand {
  text-align: center;
}

.logo-text {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.85rem;
  font-weight: 400;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.tagline {
  margin: 0.65rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.5;
}

.main {
  flex: 1;
  padding: 0 1.5rem 1.5rem;
  width: 100%;
  min-height: 0;
}

/* Split view: chat sidebar + full-height preview */
body.has-preview {
  height: 100dvh;
  overflow: hidden;
}

body.has-preview .workspace {
  flex: 1;
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 0;
  overflow: hidden;
}

body.has-preview .workspace-sidebar {
  max-width: none;
  margin: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--bg);
}

body.has-preview.has-chat .workspace-sidebar {
  overflow: hidden;
}

.sidebar-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

body.has-preview.has-chat .sidebar-body {
  overflow: hidden;
}

body.has-preview.has-chat .main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

body.has-preview.has-chat .setup-panel {
  display: none !important;
}

body.has-preview.has-chat .chat-panel {
  display: flex !important;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

body.has-preview.has-chat .chat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.75rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

body.has-preview.has-chat .chat-composer {
  display: block;
  flex-shrink: 0;
  padding: 0.75rem 1rem 0.5rem;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

body.has-preview.has-chat .chat-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 0.5rem;
}

body.has-preview.has-chat .chat-suggestions[hidden] {
  display: none !important;
}

.chat-suggestion-chip {
  padding: 0.3rem 0.55rem;
  font-family: var(--font-ui);
  font-size: 0.72rem;
  line-height: 1.3;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  text-align: left;
  transition:
    color 0.15s ease,
    border-color 0.15s ease,
    background 0.15s ease;
}

.chat-suggestion-chip:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--bg);
}

body.has-preview.has-chat .chat-form {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  align-items: stretch;
}

body.has-preview.has-chat .chat-prompt-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
}

body.has-preview.has-chat .chat-prompt-ghost {
  position: absolute;
  inset: 0;
  padding: 0.75rem 0.85rem;
  font-family: var(--font-ui);
  font-size: 0.95rem;
  line-height: 1.55;
  color: var(--muted);
  opacity: 0.5;
  pointer-events: none;
  white-space: pre-wrap;
  word-break: break-word;
  overflow: hidden;
  z-index: 0;
}

body.has-preview.has-chat .chat-prompt-ghost[hidden] {
  display: none !important;
}

body.has-preview.has-chat #chat-prompt {
  position: relative;
  z-index: 1;
  width: 100%;
  flex: 1;
  min-height: 5.5rem;
  max-height: 14rem;
  resize: vertical;
  margin: 0;
  padding: 0.75rem 0.85rem;
  font-family: var(--font-ui);
  font-size: 0.95rem;
  line-height: 1.55;
  background: transparent;
  color: var(--text);
}

body.has-preview.has-chat #chat-prompt::placeholder {
  color: transparent;
}

body.has-preview.has-chat .chat-suggestions.is-loading .chat-suggestion-chip {
  opacity: 0.45;
  pointer-events: none;
}

body.has-preview.has-chat .chat-send {
  flex-shrink: 0;
  align-self: stretch;
  width: 100%;
  min-width: 0;
  margin: 0;
}

body.has-preview.has-chat .chat-header-meta {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.65rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}

body.has-preview.has-chat .page-select-label {
  display: block;
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

body.has-preview.has-chat .page-select {
  width: 100%;
  margin-bottom: 0.5rem;
  padding: 0.45rem 0.6rem;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}

body.has-preview.has-chat .chat-header-meta .preview-link {
  margin: 0;
  font-size: 0.75rem;
}

body.has-preview.has-chat .chat-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

body.has-preview.has-chat .btn-compact {
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
}

body.has-preview.has-chat .tagline {
  display: none;
}

body.has-preview.has-chat .footer {
  flex-shrink: 0;
  margin-top: 0;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  max-width: 100%;
  animation: fadeUp 0.3s ease both;
}

.chat-msg--user {
  align-self: flex-end;
  align-items: flex-end;
}

.chat-msg--assistant,
.chat-msg--system {
  align-self: flex-start;
  align-items: flex-start;
}

.chat-msg-bubble {
  padding: 0.65rem 0.9rem;
  border-radius: 1rem;
  font-size: 0.9rem;
  line-height: 1.5;
  max-width: 100%;
  word-break: break-word;
}

.chat-msg--user .chat-msg-bubble {
  background: var(--accent);
  color: var(--on-accent);
  border-bottom-right-radius: 0.25rem;
}

.chat-msg--assistant .chat-msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 0.25rem;
}

.chat-msg--system .chat-msg-bubble {
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: 0.8rem;
  font-family: var(--font-mono);
}

.chat-msg-meta {
  margin: 0.25rem 0 0;
  font-size: 0.65rem;
  color: var(--muted);
  font-family: var(--font-mono);
}

.chat-msg--user .chat-msg-meta {
  text-align: right;
}

.chat-msg-summary {
  margin: 0;
  white-space: pre-wrap;
}

.chat-msg-expand {
  margin-top: 0.45rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  font-family: var(--font-ui);
}

.chat-msg-expand:hover {
  text-decoration: underline;
}

.chat-msg-details {
  margin-top: 0.5rem;
  padding: 0.65rem 0.75rem;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  line-height: 1.45;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: min(40vh, 320px);
  overflow-y: auto;
}

.chat-msg--thinking .chat-msg-bubble {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.chat-thinking-dot {
  width: 0.4rem;
  height: 0.4rem;
  border-radius: 50%;
  background: var(--accent);
  animation: discoverPulse 1.2s ease-in-out infinite;
}

.chat-thinking-dot:nth-child(2) {
  animation-delay: 0.15s;
}

.chat-thinking-dot:nth-child(3) {
  animation-delay: 0.3s;
}

.chat-discover-slot {
  width: 100%;
}

.discover--in-chat {
  margin-top: 0;
}

.discover--in-chat.discover-thinking {
  padding: 1rem;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

body.has-preview .header {
  padding: 1.25rem 1.25rem 0.75rem;
}

body.has-preview .header-logo {
  max-width: 200px;
  margin-bottom: 0.75rem;
}

body.has-preview .logo-text {
  font-size: 1.35rem;
}

body.has-preview .tagline {
  font-size: 0.85rem;
  margin-top: 0.4rem;
}

body.has-preview .main {
  padding: 0 1.25rem 1rem;
}

body.has-preview textarea {
  min-height: 100px;
  resize: vertical;
}

body.has-preview .form-actions .hint {
  flex: 1 1 100%;
}

body.has-preview .footer {
  margin-top: auto;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
}

body.has-preview .workspace-preview {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100dvh;
  background: var(--preview-bg);
}

body.has-preview .preview-frame {
  flex: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  border: none;
  border-radius: 0;
  background: var(--preview-bg);
}

@media (max-width: 900px) {
  body.has-preview {
    height: auto;
    overflow: auto;
  }

  body.has-preview .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: auto min(52dvh, 560px);
    overflow: visible;
  }

  body.has-preview .workspace-sidebar {
    height: auto;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  body.has-preview .workspace-preview {
    height: min(52dvh, 560px);
    position: sticky;
    bottom: 0;
  }
}

.build-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.label {
  font-size: 0.8rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

textarea {
  width: 100%;
  padding: 1rem 1.15rem;
  font-family: var(--font-ui);
  font-size: 1rem;
  line-height: 1.55;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
  min-height: 140px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

textarea::placeholder {
  color: var(--muted);
  opacity: 0.7;
}

textarea:focus,
.improve-url:focus {
  outline: none;
  border-color: var(--accent-dim);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  margin-top: 0.25rem;
}

.btn-primary,
.btn-secondary {
  font-family: var(--font-ui);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  border-radius: 999px;
  transition: transform 0.15s, opacity 0.15s, background 0.15s;
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  position: relative;
  min-width: 160px;
  padding: 0.75rem 1.5rem;
  background: var(--accent);
  color: var(--on-accent);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: wait;
}

.btn-secondary {
  padding: 0.55rem 1.1rem;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  border-color: var(--muted);
  background: var(--surface);
}

.btn-spinner {
  width: 1rem;
  height: 1rem;
  border: 2px solid var(--spinner-track);
  border-top-color: var(--spinner-fg);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.btn-spinner:not([hidden]) {
  display: inline-block;
}

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

.hint {
  font-size: 0.8rem;
  color: var(--muted);
}

.result-meta[hidden] {
  display: none !important;
}

.result-meta:not([hidden]) {
  margin-top: 1.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
  animation: fadeUp 0.5s ease both;
}

.workspace-preview[hidden] {
  display: none !important;
}

body.has-preview .workspace-preview:not([hidden]) {
  animation: previewReveal 0.45s ease both;
}

@keyframes previewReveal {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.result-label {
  margin: 0 0 0.35rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.preview-link {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--accent);
  word-break: break-all;
  text-decoration: none;
  margin-bottom: 1rem;
}

.preview-link:hover {
  text-decoration: underline;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-bottom: 0;
}

.error {
  margin-top: 1.5rem;
  padding: 1rem 1.15rem;
  background: var(--error-bg);
  color: var(--error-text);
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.5;
  animation: fadeUp 0.35s ease both;
}

.footer {
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

.footer strong {
  color: var(--text);
  font-weight: 500;
}

.build-stamp {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.02em;
  opacity: 0.75;
}

.build-status {
  margin: 1rem 0 0;
  padding: 0.75rem 1rem;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.5;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.build-status:not([hidden]) {
  animation: fadeUp 0.35s ease both;
}

.token-summary {
  margin: 0 0 1rem;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--accent);
}

.discover[hidden] {
  display: none !important;
}

.discover {
  margin-top: 2rem;
  animation: fadeUp 0.45s ease both;
}

.discover-thinking {
  padding: 2rem 1.25rem;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.discover-thinking-dots {
  display: inline-flex;
  gap: 0.45rem;
  margin-bottom: 1rem;
}

.discover-thinking-dots span {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: var(--accent);
  animation: discoverPulse 1.2s ease-in-out infinite;
}

.discover-thinking-dots span:nth-child(2) {
  animation-delay: 0.15s;
}

.discover-thinking-dots span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes discoverPulse {
  0%,
  80%,
  100% {
    opacity: 0.25;
    transform: scale(0.85);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}

.discover-thinking-title {
  margin: 0 0 0.5rem;
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 400;
}

.discover-thinking-sub {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
  max-width: 28rem;
  margin-inline: auto;
}

.discover-label {
  margin: 0 0 0.75rem;
  font-size: 0.8rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.discover-brief {
  margin: 0 0 1rem;
  padding: 0.85rem 1rem;
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  border-left: 3px solid var(--accent-dim);
}

.discover-progress {
  margin: 0 0 1.25rem;
}

.discover-progress-track {
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.discover-progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent-dim), var(--accent));
  border-radius: 999px;
  transition: width 0.45s cubic-bezier(0.4, 0, 0.2, 1);
}

.discover-progress-label {
  margin: 0.5rem 0 0;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.discover-step {
  margin-bottom: 1.1rem;
  animation: fadeUp 0.35s ease both;
}

.discover-form {
  display: flex;
  flex-direction: column;
  gap: 0;
  margin: 0;
  border: none;
  padding: 0;
}

.discover-field {
  margin: 0;
  padding: 0;
  border: none;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.discover-q {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
  line-height: 1.4;
}

.discover-hint {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.4;
}

.discover-field textarea {
  min-height: 4rem;
  font-size: 0.95rem;
}

.discover-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  margin-top: 0.25rem;
}

.improve-panel[hidden] {
  display: none !important;
}

.improve-panel {
  margin-top: 1.25rem;
  padding: 1.15rem 1.2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  animation: fadeUp 0.35s ease both;
}

.improve-url {
  width: 100%;
  padding: 0.75rem 1rem;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.65rem;
}

.improve-hint {
  margin: 0 0 1rem;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--muted);
}

.improve-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
}