:root {
  --bg: #0f1419;
  --surface: #1a222d;
  --surface-2: #242d3a;
  --border: #2f3b4d;
  --text: #e8edf4;
  --text-muted: #8b9cb3;
  --accent: #22c55e;
  --accent-dim: #16a34a;
  --accent-glow: rgba(34, 197, 94, 0.15);
  --user-bubble: #2563eb;
  --assistant-bubble: #242d3a;
  --danger: #ef4444;
  --radius: 14px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
}

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

html,
body {
  height: 100%;
}

body {
  font-family: 'DM Sans', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
  max-height: 100dvh;
}

/* Sidebar */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-icon {
  width: 44px;
  height: 44px;
  background: var(--accent-glow);
  border: 1px solid rgba(34, 197, 94, 0.3);
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 1.4rem;
}

.brand h1 {
  font-size: 1.1rem;
  font-weight: 700;
}

.brand p {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.sidebar-section h2 {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.6rem;
}

.suggestion {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.65rem 0.85rem;
  margin-bottom: 0.4rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.suggestion:hover {
  border-color: var(--accent);
  background: var(--accent-glow);
}

.store-label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.store-select {
  width: 100%;
  padding: 0.55rem 0.65rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
}

.store-add-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.store-add-input {
  width: 100%;
  padding: 0.45rem 0.55rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.8rem;
}

.store-add-btn {
  padding: 0.45rem 0.7rem;
  background: var(--accent-glow);
  border: 1px solid rgba(34, 197, 94, 0.35);
  border-radius: 8px;
  color: var(--accent);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.store-add-btn:hover {
  border-color: var(--accent);
}

.store-hint {
  margin-top: 0.45rem;
  font-size: 0.7rem;
  color: var(--text-muted);
  line-height: 1.35;
}

.status-panel .status-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  padding: 0.35rem 0;
  color: var(--text-muted);
}

.status-panel .status-row span:last-child {
  color: var(--text);
  font-weight: 500;
}

.badge {
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.7rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.badge.waiting {
  background: rgba(234, 179, 8, 0.15);
  border-color: rgba(234, 179, 8, 0.4);
  color: #fbbf24;
}

.badge.completed {
  background: var(--accent-glow);
  border-color: rgba(34, 197, 94, 0.4);
  color: var(--accent);
}

.cart-panel .cart-count {
  display: inline-block;
  min-width: 1.25rem;
  padding: 0.05rem 0.4rem;
  margin-left: 0.25rem;
  border-radius: 999px;
  font-size: 0.65rem;
  background: var(--accent-glow);
  color: var(--accent);
  border: 1px solid rgba(34, 197, 94, 0.35);
  vertical-align: middle;
}

.cart-items {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 280px;
  overflow-y: auto;
}

.cart-item {
  display: grid;
  grid-template-columns: 40px 1fr;
  gap: 0.5rem;
  padding: 0.45rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.75rem;
}

.cart-item img,
.cart-item .cart-item-placeholder {
  width: 40px;
  height: 40px;
  object-fit: contain;
  background: #fff;
  border-radius: 6px;
  display: grid;
  place-items: center;
}

.cart-item-name {
  font-weight: 500;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.cart-item-meta {
  color: var(--text-muted);
  font-size: 0.68rem;
  margin-top: 0.15rem;
}

.cart-item-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.65rem;
}

.cart-item-link:hover {
  text-decoration: underline;
}

.cart-total {
  margin-top: 0.65rem;
  padding-top: 0.55rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
}

.product-card.in-cart {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(34, 197, 94, 0.35);
}

.product-card .in-cart-badge {
  display: inline-block;
  margin-top: 0.2rem;
  font-size: 0.6rem;
  font-weight: 600;
  color: var(--accent);
}

.btn-new-chat {
  margin-top: auto;
  padding: 0.75rem 1rem;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 10px;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-new-chat:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-debug-link {
  display: block;
  text-align: center;
  margin-top: 0.55rem;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.8rem;
  transition: border-color 0.15s, color 0.15s;
}

.btn-debug-link:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Chat area */
.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(34, 197, 94, 0.06) 0%, transparent 50%),
    var(--bg);
}

.chat-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 20, 25, 0.8);
  backdrop-filter: blur(8px);
}

.chat-header h2 {
  font-size: 1.1rem;
  font-weight: 600;
}

.chat-header p {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.welcome {
  text-align: center;
  max-width: 420px;
  margin: auto;
  padding: 2rem 1rem;
}

.welcome-icon {
  font-size: 2.5rem;
  margin-bottom: 0.75rem;
}

.welcome h3 {
  font-size: 1.25rem;
  margin-bottom: 0.5rem;
}

.welcome p {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.message {
  display: flex;
  gap: 0.75rem;
  max-width: 85%;
  animation: fadeIn 0.25s ease;
}

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

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.message.assistant .message-avatar {
  background: var(--accent-glow);
  border: 1px solid rgba(34, 197, 94, 0.3);
}

.message.user .message-avatar {
  background: rgba(37, 99, 235, 0.2);
  border: 1px solid rgba(37, 99, 235, 0.4);
}

.message-bubble {
  padding: 0.85rem 1.1rem;
  border-radius: var(--radius);
  font-size: 0.95rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.assistant .message-bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.message.user .message-bubble {
  background: var(--user-bubble);
  border-bottom-right-radius: 4px;
}

.message.loading .message-bubble {
  display: flex;
  gap: 4px;
  padding: 1rem 1.2rem;
}

.dot {
  width: 7px;
  height: 7px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}

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

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-5px); }
}

/* Quick replies */
.quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0 1.5rem 0.75rem;
}

.quick-reply {
  padding: 0.5rem 1rem;
  background: var(--surface);
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 999px;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.quick-reply:hover {
  background: var(--accent);
  color: #fff;
}

/* Results panel */
.results {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 0.75rem 1.25rem;
  max-height: min(46vh, 480px);
  overflow-y: auto;
}

.result-block {
  margin-bottom: 0.75rem;
}

.result-block:last-child {
  margin-bottom: 0;
}

.result-block h3 {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.45rem;
}

#recipeContent h4 {
  font-size: 0.95rem;
  margin-bottom: 0.25rem;
}

#recipeContent .recipe-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

#recipeContent ul {
  list-style: none;
  font-size: 0.8rem;
  color: var(--text-muted);
}

#recipeContent li {
  padding: 0.15rem 0;
}

#instructionsList {
  padding-left: 1.1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

#instructionsList li {
  padding: 0.2rem 0;
  line-height: 1.4;
}

.ingredient-row {
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--border);
}

.ingredient-row:last-child {
  border-bottom: none;
}

.ingredient-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

.ingredient-name {
  font-size: 0.85rem;
  font-weight: 600;
}

.ingredient-qty {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.ingredient-badge {
  font-size: 0.65rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: rgba(234, 179, 8, 0.15);
  color: #fbbf24;
  border: 1px solid rgba(234, 179, 8, 0.35);
  white-space: nowrap;
}

.ingredient-badge.owned {
  background: var(--accent-glow);
  color: var(--accent);
  border-color: rgba(34, 197, 94, 0.35);
}

.ingredient-products {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.4rem;
}

.ingredient-products.empty {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-style: italic;
}

.product-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  transition: border-color 0.15s;
  min-width: 0;
}

.product-card:hover {
  border-color: var(--accent);
}

.product-card img,
.product-card .product-img-placeholder {
  width: 100%;
  height: 64px;
  aspect-ratio: auto;
  object-fit: contain;
  background: #fff;
  display: grid;
  place-items: center;
  font-size: 1.25rem;
}

.product-info {
  padding: 0.35rem 0.4rem 0.45rem;
}

.product-info .name {
  font-size: 0.68rem;
  font-weight: 500;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.25;
  margin-bottom: 0.15rem;
  min-height: 1.7em;
}

.product-info a.name.product-link {
  color: inherit;
  text-decoration: none;
}

.product-info a.name.product-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

.product-info .product-url {
  display: inline-block;
  margin-top: 0.2rem;
  font-size: 0.62rem;
  font-weight: 500;
  color: var(--accent);
  text-decoration: none;
}

.product-info .product-url:hover {
  text-decoration: underline;
}

.product-info .price {
  font-size: 0.72rem;
  color: var(--accent);
  font-weight: 600;
}

.product-info .pack-size {
  font-size: 0.58rem;
  color: var(--text-muted);
  margin-bottom: 0.1rem;
}

.product-info .qty-field {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  margin: 0.2rem 0 0.25rem;
}

.product-info .qty-label {
  font-size: 0.58rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.product-info .qty-input {
  width: 3.2rem;
  padding: 0.15rem 0.3rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  text-align: center;
  -moz-appearance: textfield;
}

.product-info .qty-input::-webkit-outer-spin-button,
.product-info .qty-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.product-info .qty-unit {
  font-size: 0.62rem;
  color: var(--text-muted);
}

.product-info .line-total {
  font-size: 0.68rem;
  color: var(--text);
  font-weight: 600;
  margin-top: 0.1rem;
}

.product-info .brand {
  font-size: 0.6rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.product-info .unavailable {
  font-size: 0.6rem;
  color: var(--danger);
}

.product-card.compact img,
.product-card.compact .product-img-placeholder {
  height: 56px;
}

.product-card.compact .product-info {
  padding: 0.3rem 0.35rem 0.4rem;
}

.product-card.compact .product-info .name {
  font-size: 0.65rem;
  -webkit-line-clamp: 2;
  min-height: 1.6em;
}

.product-card.compact .product-info .price {
  font-size: 0.7rem;
}

.product-card.compact .product-info .brand {
  font-size: 0.58rem;
}

@media (max-width: 768px) {
  .ingredient-products {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.3rem;
  }

  .product-card img,
  .product-card .product-img-placeholder,
  .product-card.compact img,
  .product-card.compact .product-img-placeholder {
    height: 48px;
  }

  .results {
    max-height: min(42vh, 360px);
  }
}

/* Composer */
.composer {
  display: flex;
  gap: 0.75rem;
  padding: 1rem 1.5rem 1.25rem;
  border-top: 1px solid var(--border);
  background: rgba(15, 20, 25, 0.9);
  backdrop-filter: blur(8px);
}

.composer textarea {
  flex: 1;
  resize: none;
  padding: 0.85rem 1.1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.4;
  max-height: 120px;
  transition: border-color 0.15s;
}

.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.composer textarea::placeholder {
  color: var(--text-muted);
}

.composer button {
  width: 48px;
  height: 48px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s, transform 0.1s;
  flex-shrink: 0;
}

.composer button:hover:not(:disabled) {
  background: var(--accent-dim);
}

.composer button:active:not(:disabled) {
  transform: scale(0.96);
}

.composer button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.75rem 1.25rem;
  background: var(--danger);
  color: #fff;
  border-radius: 10px;
  font-size: 0.9rem;
  box-shadow: var(--shadow);
  z-index: 100;
  animation: fadeIn 0.2s ease;
}

/* Mobile */
@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    padding: 1rem;
    gap: 0.75rem;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .brand {
    width: 100%;
  }

  .sidebar-section:not(.status-panel) {
    display: none;
  }

  .btn-new-chat {
    margin-top: 0;
    width: 100%;
  }

  .message {
    max-width: 95%;
  }
}
