:root {
  --bg: #0b0f14;
  --surface: #141b24;
  --surface-2: #1a2330;
  --border: #2a3544;
  --text: #e8edf4;
  --muted: #8b9bb0;
  --accent: #3b82f6;
  --accent-soft: rgba(59, 130, 246, 0.15);
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --nav-height: 72px;
  --header-height: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --touch-min: 44px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body {
  padding-bottom: calc(var(--nav-height) + var(--safe-bottom) + 12px);
}

a {
  color: var(--accent);
  word-break: break-word;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: var(--header-height);
  padding: calc(10px + var(--safe-top)) calc(12px + var(--safe-right)) 10px calc(12px + var(--safe-left));
  background: rgba(11, 15, 20, 0.94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.brand h1 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand p {
  margin: 0;
  font-size: 0.7rem;
  color: var(--muted);
  display: none;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  padding: 8px 10px;
  min-height: var(--touch-min);
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.7rem;
  color: var(--muted);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}

.status-dot.ok { background: var(--success); }
.status-dot.error { background: var(--danger); }

/* Mobile approach badge */
.mobile-approach-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 12px 12px;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1px solid rgba(59, 130, 246, 0.35);
  font-size: 0.82rem;
}

.mobile-approach-badge .icon {
  font-size: 1.1rem;
}

.mobile-approach-badge span {
  color: var(--muted);
  font-size: 0.75rem;
}

/* Layout */
.layout {
  display: grid;
  gap: 12px;
  padding: 12px;
  padding-left: calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
  max-width: 1200px;
  margin: 0 auto;
}

.sidebar {
  display: none;
}

.main-panel {
  min-width: 0;
}

@media (min-width: 900px) {
  body {
    padding-bottom: 24px;
  }

  .brand p {
    display: block;
  }

  .mobile-approach-badge {
    display: none;
  }

  .layout {
    grid-template-columns: 280px 1fr;
    align-items: start;
    padding: 20px;
    gap: 16px;
  }

  .sidebar {
    display: block;
    position: sticky;
    top: calc(var(--header-height) + 16px);
  }

  .mobile-nav {
    display: none !important;
  }

  .btn-mobile-full {
    width: auto;
  }

  .sticky-actions {
    position: static;
    box-shadow: none;
    border: none;
    padding: 0;
    background: transparent;
  }
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card-spaced {
  margin-top: 12px;
}

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

.card-header h2,
.card-header h3 {
  margin: 0;
  font-size: 0.95rem;
}

.card-header p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.4;
}

.card-body {
  padding: 14px 16px;
}

/* Approaches */
.approach-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.approach-btn {
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 12px;
  padding: 14px;
  min-height: var(--touch-min);
  cursor: pointer;
  transition: 0.15s ease;
  touch-action: manipulation;
}

.approach-btn:active {
  transform: scale(0.98);
}

.approach-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.approach-btn strong {
  display: block;
  font-size: 0.88rem;
  margin-bottom: 4px;
}

.approach-btn span {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1.35;
}

/* Form fields — 16px prevents iOS input zoom */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.field label {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font: inherit;
  font-size: 16px;
  min-height: var(--touch-min);
  -webkit-appearance: none;
  appearance: none;
}

.field textarea {
  min-height: 120px;
  resize: vertical;
  line-height: 1.45;
}

.question-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.question-input-wrap {
  position: relative;
}

.question-input-wrap textarea {
  padding-right: 52px;
}

.voice-btn {
  position: absolute;
  right: 8px;
  bottom: 8px;
  width: var(--touch-min);
  height: var(--touch-min);
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  transition: background 0.2s, border-color 0.2s, transform 0.1s;
}

.voice-btn:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.voice-btn:active {
  transform: scale(0.95);
}

.voice-btn.listening {
  border-color: var(--danger);
  background: rgba(239, 68, 68, 0.15);
  color: var(--danger);
  animation: voice-pulse 1.2s ease-in-out infinite;
}

.voice-btn.listening .voice-icon {
  animation: none;
}

@keyframes voice-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.45); }
  50% { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
}

.voice-status {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--danger);
  white-space: nowrap;
}

.voice-status.processing {
  color: var(--warning);
}

.voice-hint {
  margin: 6px 0 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.4;
}

.voice-hint strong {
  color: var(--text);
  font-weight: 600;
}

.voice-unsupported {
  margin: 6px 0 0;
  font-size: 0.75rem;
  color: var(--warning);
  line-height: 1.4;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
}

@media (min-width: 521px) {
  .field-row {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }
}

/* Collapsible advanced section */
details.advanced-panel {
  margin-bottom: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  overflow: hidden;
}

details.advanced-panel summary {
  padding: 14px 16px;
  min-height: var(--touch-min);
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  touch-action: manipulation;
}

details.advanced-panel summary::-webkit-details-marker {
  display: none;
}

details.advanced-panel summary::after {
  content: "▾";
  color: var(--muted);
  transition: transform 0.2s;
}

details.advanced-panel[open] summary::after {
  transform: rotate(180deg);
}

details.advanced-panel .advanced-body {
  padding: 0 16px 16px;
  border-top: 1px solid var(--border);
}

/* Chips */
.chip-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 10px 14px;
  min-height: var(--touch-min);
  font-size: 0.8rem;
  line-height: 1.3;
  cursor: pointer;
  touch-action: manipulation;
  text-align: left;
  max-width: 100%;
}

.chip:active {
  transform: scale(0.97);
}

/* Agents */
.agent-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

@media (min-width: 400px) {
  .agent-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.agent-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  min-height: var(--touch-min);
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 0.85rem;
  touch-action: manipulation;
}

.agent-toggle input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

/* Actions */
.actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 8px;
}

@media (min-width: 521px) {
  .actions {
    flex-direction: row;
    flex-wrap: wrap;
  }
}

.sticky-actions {
  position: sticky;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 8px);
  z-index: 15;
  padding: 12px 0 4px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  min-height: var(--touch-min);
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s;
}

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

.btn-mobile-full {
  width: 100%;
}

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

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

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

/* Results */
.result-box {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.6;
  font-size: 0.95rem;
}

.meta-grid {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.meta-accordion {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  overflow: hidden;
}

.meta-accordion summary {
  padding: 14px 16px;
  min-height: var(--touch-min);
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  list-style: none;
  touch-action: manipulation;
}

.meta-accordion summary::-webkit-details-marker {
  display: none;
}

.meta-accordion .meta-content {
  padding: 0 16px 14px;
  border-top: 1px solid var(--border);
}

.meta-item pre {
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.45;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  white-space: pre-wrap;
  word-break: break-word;
}

.source-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.source-item {
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  line-height: 1.4;
}

.source-item .type {
  display: inline-block;
  margin-bottom: 4px;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

.empty-state {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.55;
}

.history-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.btn-compact {
  padding: 10px 14px;
  min-height: var(--touch-min);
  font-size: 0.85rem;
  flex-shrink: 0;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-item {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  padding: 12px 14px;
  cursor: pointer;
  touch-action: manipulation;
  transition: border-color 0.15s, background 0.15s;
}

.history-item:active {
  transform: scale(0.99);
}

.history-item:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.history-item-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.history-time {
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
}

.history-badge {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 999px;
  flex-shrink: 0;
}

.history-badge.ok {
  color: var(--success);
  background: rgba(34, 197, 94, 0.15);
}

.history-badge.fail {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.15);
}

.history-question {
  font-size: 0.9rem;
  line-height: 1.45;
  margin: 0 0 6px;
  word-break: break-word;
}

.history-meta {
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.35;
}

.history-preview {
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Bottom nav */
.mobile-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(6, minmax(52px, 1fr));
  gap: 2px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px calc(4px + var(--safe-left)) calc(6px + var(--safe-bottom)) calc(4px + var(--safe-right));
  background: rgba(11, 15, 20, 0.97);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
}

.mobile-nav button {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.62rem;
  padding: 6px 2px;
  min-height: var(--touch-min);
  border-radius: 10px;
  touch-action: manipulation;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.mobile-nav button.active {
  color: var(--accent);
  background: var(--accent-soft);
}

.mobile-nav button:active {
  transform: scale(0.95);
}

.mobile-nav .icon {
  font-size: 1.15rem;
  line-height: 1;
}

.mobile-nav .label {
  line-height: 1.1;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hidden {
  display: none !important;
}

.loading {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.9rem;
  padding: 8px 0;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

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

.dev-banner {
  margin-top: 12px;
  padding: 12px;
  border-radius: 10px;
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.35);
  color: #f8d57f;
  font-size: 0.8rem;
  line-height: 1.5;
  word-break: break-word;
}

.dev-banner code {
  font-size: 0.75rem;
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
  .spinner {
    animation: none;
  }
  .btn:active,
  .chip:active,
  .approach-btn:active,
  .mobile-nav button:active {
    transform: none;
  }
}
