/* Theme tokens live on <html data-theme="dark|light"> so light mode always wins over :root (same specificity bug). */
:root {
  --radius: 14px;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #000000;
  --panel: #000000;
  --card: #000000;
  --border: #1a1a1a;
  --surface-raised: #0a0a0a;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #6366f1;
  --ok: #22c55e;
  --danger: #f87171;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.75);
  --meta-muted: #64748b;
  --meta-code: #cbd5e1;
  --card-heading: #cbd5e1;
  --label-color: #cbd5e1;
  --input-border: #2a2a2a;
  --focus-ring: rgba(56, 189, 248, 0.15);
  --focus-border: rgba(56, 189, 248, 0.65);
  --ghost-text: #e2e8f0;
  --ghost-bg: #0a0a0a;
  --ghost-border: #2a2a2a;
  --ghost-hover-border: #404040;
  --quick-border: #262626;
  --quick-text: #e2e8f0;
  --quick-hover-bg: #141414;
  --quick-hover-border: #404040;
  --narrative-heading: #94a3b8;
  --primary-btn-bg: #151515;
  --primary-btn-bg-hover: #1e1e1e;
  --primary-btn-border: #2e2e2e;
  --primary-btn-text: #e8edf4;
  --primary-shadow: rgba(0, 0, 0, 0.5);
  --primary-hover-shadow: rgba(0, 0, 0, 0.65);
  --map-frame-bg: #000000;
  /* Sun/moon theme toggle */
  --theme-switch-track-bg: #18181b;
  --theme-switch-track-border: #3f3f46;
  --theme-switch-thumb-dark-bg: #09090b;
  --theme-switch-thumb-dark-border: rgba(244, 244, 245, 0.45);
  --theme-switch-thumb-light-bg: #fafafa;
  --theme-switch-thumb-light-border: #27272a;
  --theme-switch-moon-color: #f4f4f5;
  --theme-switch-sun-color: #64748b;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f1f5f9;
  --panel: #ffffff;
  --card: #ffffff;
  --border: #e2e8f0;
  --surface-raised: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #0284c7;
  --accent-2: #4f46e5;
  --shadow: 0 12px 40px rgba(15, 23, 42, 0.08);
  --meta-muted: #64748b;
  --meta-code: #334155;
  --card-heading: #475569;
  --label-color: #475569;
  --input-border: #cbd5e1;
  --focus-ring: rgba(2, 132, 199, 0.2);
  --focus-border: #0284c7;
  --ghost-text: #334155;
  --ghost-bg: #eef2f7;
  --ghost-border: #cbd5e1;
  --ghost-hover-border: #94a3b8;
  --quick-border: #e2e8f0;
  --quick-text: #1e293b;
  --quick-hover-bg: #e8eef4;
  --quick-hover-border: #94a3b8;
  --narrative-heading: #64748b;
  --primary-btn-bg: #e2e8f0;
  --primary-btn-bg-hover: #d8dee9;
  --primary-btn-border: #cbd5e1;
  --primary-btn-text: #0f172a;
  --primary-shadow: rgba(15, 23, 42, 0.08);
  --primary-hover-shadow: rgba(15, 23, 42, 0.12);
  --map-frame-bg: #e2e8f0;
  --theme-switch-track-bg: #e4e4e7;
  --theme-switch-track-border: #18181b;
  --theme-switch-thumb-dark-bg: #18181b;
  --theme-switch-thumb-dark-border: rgba(255, 255, 255, 0.25);
  --theme-switch-thumb-light-bg: #ffffff;
  --theme-switch-thumb-light-border: #0f172a;
  --theme-switch-moon-color: #fafafa;
  --theme-switch-sun-color: #64748b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "DM Sans", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  height: 100vh;
  min-height: 100vh;
  max-height: 100vh;
  gap: 0;
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 22px 20px 28px;
  border-right: 1px solid var(--border);
  background: var(--panel);
  box-shadow: var(--shadow);
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
}

.brand-logo {
  display: block;
  width: auto;
  max-width: min(100%, 280px);
  height: auto;
  max-height: 88px;
  margin: 0 0 12px;
  object-fit: contain;
  object-position: left top;
  background: transparent;
}

/* Transparent PNG — subtle lift on light panels so artwork stays readable */
html[data-theme="light"] .brand-logo {
  filter: drop-shadow(0 1px 2px rgba(15, 23, 42, 0.06));
}

.brand h1 {
  margin: 0 0 6px;
  font-size: 1.35rem;
  letter-spacing: -0.03em;
}

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

.muted {
  color: var(--muted);
}

.meta {
  margin: 10px 0 0;
  font-size: 0.78rem;
  color: var(--meta-muted);
}

.meta code {
  color: var(--meta-code);
  font-size: 0.78rem;
  word-break: break-all;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 16px;
}

.card.grow {
  flex: 1;
  min-height: 180px;
}

.card-title {
  margin: 0 0 10px;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--card-heading);
}

.label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--label-color);
  margin-bottom: 8px;
}

textarea {
  width: 100%;
  resize: vertical;
  border-radius: 12px;
  border: 1px solid var(--input-border);
  background: var(--surface-raised);
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
  line-height: 1.45;
  outline: none;
}

textarea:focus {
  border-color: var(--focus-border);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
  align-items: center;
}

.btn {
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  border-radius: 11px;
  padding: 10px 14px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.12s ease, background 0.12s ease, border-color 0.12s ease;
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
}

.btn:active {
  transform: translateY(1px);
}

.btn.primary {
  color: var(--primary-btn-text);
  background-color: var(--primary-btn-bg);
  border-color: var(--primary-btn-border);
  border-style: solid;
  border-width: 1px;
  box-shadow: 0 8px 22px var(--primary-shadow);
}

.btn.primary:hover {
  background-color: var(--primary-btn-bg-hover);
  box-shadow: 0 10px 28px var(--primary-hover-shadow);
}

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

.btn.ghost {
  color: var(--ghost-text);
  background-color: var(--ghost-bg);
  border-color: var(--ghost-border);
  border-style: solid;
  border-width: 1px;
}

.btn.ghost:hover {
  border-color: var(--ghost-hover-border);
}

.btn.sm {
  padding: 8px 12px;
  font-size: 0.85rem;
}

/* Inline status inside Response card (replaces old chat statusLine). */
.response-status {
  min-height: 1.15em;
  margin: 0 0 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 0.82rem;
  color: var(--muted);
}

.response-status:empty {
  display: none;
}

.response-status.error {
  color: var(--danger);
  border-bottom-color: rgba(248, 113, 113, 0.35);
}

.response-status.ok {
  color: var(--ok);
  border-bottom-color: rgba(34, 197, 94, 0.35);
}

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

.response-body {
  min-height: 0;
}

.response-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 140px;
  padding: 20px 12px 8px;
  color: var(--muted);
  font-size: 0.88rem;
}

.response-loading[hidden] {
  display: none !important;
}

.response-spinner {
  width: 28px;
  height: 28px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: response-spin 0.65s linear infinite;
}

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

.quick-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 320px;
  overflow: auto;
  padding-right: 4px;
}

.quick-btn {
  text-align: left;
  border-radius: 12px;
  border: 1px solid var(--quick-border);
  background: var(--surface-raised);
  color: var(--quick-text);
  padding: 10px 12px;
  font: inherit;
  font-size: 0.86rem;
  line-height: 1.35;
  cursor: pointer;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.quick-btn:hover {
  border-color: var(--quick-hover-border);
  background: var(--quick-hover-bg);
}

.narrative h3 {
  margin: 0 0 6px;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--narrative-heading);
}

.narrative + .narrative {
  margin-top: 14px;
}

.narrative p,
.narrative li {
  font-size: 0.9rem;
  line-height: 1.5;
}

ul {
  padding-left: 18px;
  margin: 0;
}

.empty:empty::before {
  content: "";
}

.map-shell {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  background: var(--bg);
  overflow: hidden;
}

.map-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.map-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.map-open-tab {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
}

.map-open-tab:hover {
  text-decoration: underline;
}

.map-title {
  display: block;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.map-caption {
  display: block;
  margin-top: 4px;
  font-size: 0.82rem;
  color: var(--muted);
}

.map-frame {
  flex: 1 1 0;
  width: 100%;
  min-width: 0;
  min-height: 0;
  border: 0;
  background: var(--map-frame-bg);
}

/* Light / dark toggle — pill track + thumb with moon (dark) / sun (light) icons */
.theme-switch {
  position: relative;
  display: inline-block;
  width: 56px;
  height: 30px;
  vertical-align: middle;
  flex-shrink: 0;
  cursor: pointer;
  user-select: none;
}

.theme-switch-input {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 56px;
  height: 30px;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  border: none;
  background: transparent;
}

.theme-switch-track {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  pointer-events: none;
  background: var(--theme-switch-track-bg);
  border: 1px solid var(--theme-switch-track-border);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.theme-switch-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--theme-switch-thumb-dark-bg);
  border: 1px solid var(--theme-switch-thumb-dark-border);
  box-shadow:
    0 2px 6px rgba(0, 0, 0, 0.35),
    0 1px 2px rgba(0, 0, 0, 0.2);
  transition:
    transform 0.28s cubic-bezier(0.34, 1.2, 0.64, 1),
    background 0.2s ease,
    border-color 0.2s ease,
    box-shadow 0.2s ease;
}

.theme-switch-icon {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
}

.theme-switch-icon--moon {
  display: block;
  color: var(--theme-switch-moon-color);
}

.theme-switch-icon--sun {
  display: none;
  color: var(--theme-switch-sun-color);
}

.theme-switch-input:checked + .theme-switch-track .theme-switch-thumb {
  transform: translateX(28px);
  background: var(--theme-switch-thumb-light-bg);
  border-color: var(--theme-switch-thumb-light-border);
  box-shadow:
    0 2px 8px rgba(15, 23, 42, 0.18),
    0 1px 3px rgba(15, 23, 42, 0.1);
}

.theme-switch-input:checked + .theme-switch-track .theme-switch-icon--moon {
  display: none;
}

.theme-switch-input:checked + .theme-switch-track .theme-switch-icon--sun {
  display: block;
}

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

@media (max-width: 960px) {
  .app {
    grid-template-columns: 1fr;
    height: auto;
    max-height: none;
    min-height: 100vh;
    overflow: visible;
  }

  .panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: none;
  }

  .map-shell {
    min-height: 55vh;
    height: auto;
  }

  .map-frame {
    flex: 1 1 auto;
    min-height: 45vh;
  }
}
