:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-subtle: #f1f5f9;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-soft: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #eff6ff;
  --ok: #059669;
  --ok-soft: #ecfdf5;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 18px 40px rgba(15, 23, 42, 0.14);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* { box-sizing: border-box; }

.is-hidden { display: none !important; }
.is-static { cursor: default !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Top Bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: linear-gradient(135deg, #2563eb, #1e40af);
  color: #ffffff;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: -0.02em;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  box-shadow: var(--shadow-xs);
}

.brand-title {
  font-weight: 650;
  font-size: 15px;
  letter-spacing: -0.01em;
  color: var(--text);
  white-space: nowrap;
}

.health-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface-subtle);
  color: var(--text-muted);
  border: 1px solid var(--border);
  cursor: pointer;
}

.health-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-soft);
}
.health-dot.ok { background: var(--ok); }
.health-dot.degraded { background: var(--warn); }
.health-dot.error { background: var(--danger); }

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.shop-select-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0 10px;
  height: 36px;
  box-shadow: var(--shadow-xs);
}

.shop-select-wrap label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.shop-select {
  border: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font);
  font-size: 13.5px;
  font-weight: 500;
  padding-right: 4px;
  outline: none;
  cursor: pointer;
  max-width: 270px;
}

.locked-shop-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 13px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent-hover);
  border: 1px solid #bfdbfe;
  white-space: nowrap;
}

.meta-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface-subtle);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.meta-badge.warn {
  background: var(--warn-soft);
  color: #92400e;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 13px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
  white-space: nowrap;
}
.btn:hover:not(:disabled) {
  background: var(--surface-subtle);
  border-color: #94a3b8;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn-sm {
  height: 30px;
  padding: 0 10px;
  font-size: 12.5px;
}

/* Main Chat Area */
.main {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.chat-column {
  width: 100%;
  max-width: 860px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.welcome-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 26px;
  box-shadow: var(--shadow-sm);
  margin-top: 20px;
}

.welcome-card h1 {
  margin: 0 0 6px;
  font-size: 22px;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.welcome-card p {
  margin: 0 0 18px;
  color: var(--text-muted);
  font-size: 14.5px;
}

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 14px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  margin-bottom: 16px;
  background: var(--warn-soft);
  border: 1px solid #fde68a;
  color: #92400e;
}
.banner.error {
  background: var(--danger-soft);
  border-color: #fecaca;
  color: #991b1b;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 13px;
  border-radius: 999px;
  font-size: 13.5px;
  font-family: var(--font);
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
  text-align: left;
}
.chip:hover {
  background: var(--accent-soft);
  border-color: #bfdbfe;
  color: var(--accent-hover);
  transform: translateY(-1px);
}

/* Messages */
.message {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 100%;
}
.message.user { align-items: flex-end; }
.message.assistant { align-items: flex-start; }

.bubble {
  border-radius: var(--radius-lg);
  padding: 13px 17px;
  max-width: 92%;
  word-break: break-word;
}

.message.user .bubble {
  background: var(--accent);
  color: #ffffff;
  border-bottom-right-radius: 5px;
  box-shadow: var(--shadow-xs);
  white-space: pre-wrap;
}

.message.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-xs);
  width: 100%;
  max-width: 100%;
}

.status-line {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-soft);
  margin-bottom: 6px;
}

.pulse-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.9); }
  50% { opacity: 1; transform: scale(1.15); }
}

.md-body > :first-child { margin-top: 0; }
.md-body > :last-child { margin-bottom: 0; }
.md-body p { margin: 0 0 10px; }
.md-body ul, .md-body ol { margin: 6px 0 10px; padding-left: 20px; }
.md-body li { margin: 3px 0; }
.md-body code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--surface-subtle);
  padding: 1px 5px;
  border-radius: 5px;
}
.md-body a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.md-table-wrap {
  overflow-x: auto;
  margin: 8px 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.md-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
  font-variant-numeric: tabular-nums;
}
.md-body th, .md-body td {
  padding: 7px 10px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border);
}
.md-body th + th, .md-body td + td { border-left: 1px solid var(--border); }
.md-body th {
  background: var(--surface-subtle);
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}
.md-body tbody tr:last-child td { border-bottom: none; }
.md-body tbody tr:nth-child(even) td { background: #f8fafc; }

.error-inline {
  margin-top: 6px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: #991b1b;
  border: 1px solid #fecaca;
  font-size: 13.5px;
}

/* Product Source Cards */
.sources-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sources-label {
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-soft);
  padding-left: 2px;
}

.sources-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 10px;
  width: 100%;
}

.product-card {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-xs);
  transition: border-color 140ms ease, box-shadow 140ms ease;
}
.product-card:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-sm);
}

.product-thumb-wrap {
  height: 120px;
  background: var(--surface-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 8px;
}

.product-thumb {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.product-thumb-fallback {
  font-size: 24px;
  font-weight: 700;
  color: var(--text-soft);
  letter-spacing: -0.03em;
}

.product-info {
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.product-name {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.35;
}

.product-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: auto;
}

.product-price {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.stock-pill {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
}
.stock-pill.in { background: var(--ok-soft); color: var(--ok); }
.stock-pill.out { background: var(--danger-soft); color: var(--danger); }

.product-link {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  margin-top: 2px;
  display: inline-flex;
  align-items: center;
  gap: 3px;
}
.product-link:hover { text-decoration: underline; }

/* Debug & Feedback Strip */
.meta-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  width: 100%;
  padding: 2px 2px 0;
  font-size: 12px;
  color: var(--text-soft);
}

.meta-left, .meta-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.fb-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 26px;
  padding: 0 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 12px;
  font-family: var(--font);
  cursor: pointer;
}
.fb-btn:hover:not(:disabled) {
  border-color: var(--border-strong);
  color: var(--text);
}
.fb-btn.active-up {
  background: var(--ok-soft);
  border-color: #a7f3d0;
  color: var(--ok);
  font-weight: 600;
}
.fb-btn.active-down {
  background: var(--danger-soft);
  border-color: #fecaca;
  color: var(--danger);
  font-weight: 600;
}

.prompt-pill {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-subtle);
  border: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-muted);
}

.trace-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}
.trace-link:hover { text-decoration: underline; }

/* Composer */
.composer-wrap {
  position: sticky;
  bottom: 0;
  background: linear-gradient(to top, var(--bg) 72%, rgba(248, 250, 252, 0));
  padding: 12px 16px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.composer {
  width: 100%;
  max-width: 860px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  padding: 8px 10px 8px 14px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  box-shadow: var(--shadow-sm);
}
.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.composer textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  color: var(--text);
  background: transparent;
  max-height: 160px;
  min-height: 24px;
  padding: 5px 0;
}

.composer-footer {
  width: 100%;
  max-width: 860px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 4px 0;
  font-size: 11.5px;
  color: var(--text-soft);
  gap: 8px;
  flex-wrap: wrap;
}

/* Drawer Modal */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.36);
  z-index: 40;
  display: none;
  justify-content: flex-end;
}
.drawer-backdrop.open { display: flex; }

.drawer {
  width: 100%;
  max-width: 430px;
  height: 100%;
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}
.drawer-head h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
}

.panel {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.panel h3 {
  margin: 0;
  font-size: 13.5px;
  font-weight: 650;
  color: var(--text);
}
.panel p {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.field label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}
.field input[type="text"],
.field input[type="number"],
.field input[type="url"],
.field select {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 13.5px;
  color: var(--text);
  background: var(--surface);
}

.quick-caps {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.cap-btn {
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-subtle);
  color: var(--text-muted);
  font-size: 12px;
  cursor: pointer;
}
.cap-btn:hover { border-color: var(--accent); color: var(--accent); }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

.result-box {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-subtle);
  font-size: 12.5px;
  font-family: var(--mono);
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  display: none;
}
.result-box.show { display: block; }
.result-box.ok { background: var(--ok-soft); color: #065f46; }
.result-box.err { background: var(--danger-soft); color: #991b1b; }

details summary {
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-soft);
  user-select: none;
}
details[open] {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

@media (max-width: 680px) {
  .topbar { padding: 10px 12px; }
  .brand-title { font-size: 14px; }
  .meta-badge { display: none; }
  .welcome-card { padding: 20px 16px; margin-top: 6px; }
  .sources-grid { grid-template-columns: 1fr; }
}
