:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e6e8eb;
  --text: #1a1d21;
  --text-muted: #6b7280;
  --accent: #1f7a5a;
  --accent-soft: #e7f3ee;
  --user-bubble: #1f7a5a;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

.logo {
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--accent);
  letter-spacing: -0.02em;
}

/* ---------- Login ---------- */

.login-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 10;
}

.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 2.5rem;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  text-align: center;
}

.login-sub {
  color: var(--text-muted);
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
}

.login-card input,
.chat-form input,
#emailInput {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem 0.9rem;
  font-size: 0.95rem;
  outline: none;
}

.login-card input:focus,
.chat-form input:focus {
  border-color: var(--accent);
}

.login-card button,
.new-chat-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 0.7rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

.login-error {
  color: #c0392b;
  font-size: 0.85rem;
  min-height: 1rem;
}

/* ---------- App layout ---------- */

.app {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  grid-template-rows: 100vh;
  height: 100vh;
  max-width: 100vw;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}

.new-chat-btn {
  padding: 0.4rem 0.7rem;
  font-size: 0.8rem;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.chat-item {
  display: flex;
  align-items: center;
  border-radius: 8px;
}

.chat-item:hover {
  background: var(--bg);
}

.chat-item.active {
  background: var(--accent-soft);
}

.chat-item-title {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 8px 0 0 8px;
  padding: 0.6rem 0.7rem;
  font-size: 0.85rem;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item.active .chat-item-title {
  color: var(--accent);
  font-weight: 600;
}

.chat-delete-btn {
  flex: none;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.3rem 0.55rem;
  margin-right: 0.15rem;
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition:
    opacity 0.12s ease,
    background 0.12s ease,
    color 0.12s ease;
}

.chat-item:hover .chat-delete-btn,
.chat-item.active .chat-delete-btn {
  opacity: 1;
}

.chat-delete-btn:hover {
  background: var(--bg-elev, rgba(0, 0, 0, 0.06));
  color: var(--danger, #d64545);
}

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 0.75rem 1rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.35rem;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.78rem;
}

/* ---------- Panels ---------- */

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.chat-panel {
  background: var(--bg);
}

.chat-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  min-width: 0;
  padding: 1.5rem;
}

.welcome {
  max-width: 520px;
  margin: 3rem auto;
  text-align: center;
}

.welcome h2 {
  margin-bottom: 0.5rem;
}

.welcome p {
  color: var(--text-muted);
}

.quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-top: 1.5rem;
}

.quick-btn,
.chip {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.5rem 0.9rem;
  font-size: 0.82rem;
  cursor: pointer;
  color: var(--text);
}

.quick-btn:hover,
.chip:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 0;
}

.message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
  max-width: 100%;
}

.message.user {
  align-items: flex-end;
}

.message-id {
  margin-top: 0.2rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.65rem;
  color: var(--text-muted);
  opacity: 0.7;
  user-select: all;
}
.message-id:empty {
  display: none;
}

.message-bubble {
  position: relative;
  max-width: 80%;
  padding: 0.7rem 1rem;
  border-radius: 14px;
  font-size: 0.9rem;
  line-height: 1.5;
  background: var(--panel);
  border: 1px solid var(--border);
}

.message.user .message-bubble {
  background: var(--user-bubble);
  color: #fff;
  border-color: var(--user-bubble);
}

/* Parked in the gutter beside the bubble: assistant bubbles hug the left, so
   their button goes right, and vice versa for the user's. */
.copy-btn {
  position: absolute;
  bottom: 0;
  left: calc(100% + 0.4rem);
  width: 1.5rem;
  height: 1.5rem;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transition:
    opacity 0.12s ease,
    color 0.12s ease;
}

.message.user .copy-btn {
  left: auto;
  right: calc(100% + 0.4rem);
}

.message:hover .copy-btn,
.copy-btn:focus-visible {
  opacity: 1;
}

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

.copy-btn.copied {
  opacity: 1;
  color: var(--accent);
  border-color: var(--accent);
}

/* Nothing worth copying while a turn is still just a progress label. */
.message-bubble.is-activity .copy-btn {
  display: none;
}

.input-area {
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 0.75rem 1rem 1rem;
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.6rem;
}

.chat-form {
  display: flex;
  gap: 0.5rem;
}

.chat-form input {
  flex: 1;
}

.chat-form button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  width: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.chat-form button:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ---------- Product carousel (inline in chat) ---------- */

.product-carousel {
  position: relative;
  margin: 0.25rem 0 0.25rem;
  min-width: 0;
  max-width: 100%;
}

.carousel-track {
  display: flex;
  gap: 0.75rem;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scroll-padding: 0.25rem;
  padding: 0.25rem 0.25rem 0.5rem;
  scrollbar-width: thin;
}

.carousel-track::-webkit-scrollbar {
  height: 8px;
}

.carousel-track::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
}

.carousel-arrow {
  position: absolute;
  top: calc(50% - 18px);
  transform: translateY(-50%);
  z-index: 3;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
}

.carousel-arrow:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.carousel-arrow.prev {
  left: -6px;
}

.carousel-arrow.next {
  right: -6px;
}

@media (hover: none) {
  .carousel-arrow {
    display: none;
  }
}

.product-card {
  position: relative;
  flex: 0 0 auto;
  width: 168px;
  scroll-snap-align: start;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--panel);
}

.product-link {
  text-decoration: none;
  color: inherit;
  display: block;
}

.product-img-wrap {
  position: relative;
  aspect-ratio: 1;
  background: var(--bg);
}

.product-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-badge,
.product-seller-badge {
  position: absolute;
  bottom: 6px;
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
}

.product-badge {
  left: 6px;
}

.product-seller-badge {
  right: 6px;
  background: var(--accent);
}

.fav-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 2;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.9);
  color: #c0392b;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.fav-btn.on {
  background: #c0392b;
  color: #fff;
}

.product-details {
  padding: 0.6rem;
}

.product-brand {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
}

.product-title {
  font-size: 0.82rem;
  margin: 0.15rem 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.product-price {
  font-weight: 700;
  font-size: 0.9rem;
}

.product-meta {
  margin-top: 0.3rem;
  font-size: 0.72rem;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

/* Live, node-driven progress indicator shown while a turn runs. */
.activity {
  color: var(--text-muted);
  font-style: italic;
  display: inline-block;
  animation: activity-pulse 1.2s ease-in-out infinite;
}

@keyframes activity-pulse {
  0%,
  100% {
    opacity: 0.55;
  }
  50% {
    opacity: 1;
  }
}

/* ---------- Image upload & detected items ---------- */

/* The upload control sits inline in the chat form, styled as a muted outline
   button so it reads as secondary to the accent Send button. */
.chat-form button.upload-btn {
  background: var(--panel);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.chat-form button.upload-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* One image per chat: the button is disabled once an image is present. Dim it and
   drop the accent hover so it clearly reads as unavailable. */
.chat-form button.upload-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.chat-form button.upload-btn:disabled:hover {
  border-color: var(--border);
  color: var(--text-muted);
}

/* Wrapper around the upload button. A disabled button emits no pointer events, so
   the wrapper carries the hover + the "one image per chat" tooltip instead. */
.upload-wrap {
  display: inline-flex;
  position: relative;
}

.upload-wrap.disabled {
  cursor: not-allowed;
}

/* The CSS tooltip, shown only when the wrapper is in its disabled state and hovered.
   `data-tooltip` holds the text so the copy lives with the JS that sets the state. */
.upload-wrap.disabled[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  z-index: 10;
  width: max-content;
  max-width: 220px;
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  background: var(--text, #1a1a1a);
  color: #fff;
  font-size: 0.75rem;
  line-height: 1.3;
  white-space: normal;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  opacity: 0;
  transform: translateY(4px);
  pointer-events: none;
  transition:
    opacity 0.12s ease,
    transform 0.12s ease;
}

.upload-wrap.disabled[data-tooltip]:hover::after {
  opacity: 1;
  transform: translateY(0);
}

/* Retry / Replace controls shown under an image turn whose recognition failed after
   all automatic attempts. */
.image-retry-bar {
  display: flex;
  gap: 0.4rem;
  margin: 0.15rem 0 0.5rem;
}

.image-retry-btn {
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 0.4rem 0.9rem;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.15s ease;
}

.image-retry-btn:hover {
  filter: brightness(1.05);
}

.image-retry-btn.secondary {
  background: var(--panel);
  color: var(--text-muted);
  border-color: var(--border);
}

.image-retry-btn.secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
  filter: none;
}

/* An uploaded image rendered inside a chat bubble. */
.chat-image {
  display: block;
  max-width: min(320px, 80%);
  max-height: 320px;
  border-radius: 12px;
  border: 1px solid var(--border);
  object-fit: cover;
}

.message.user .chat-image {
  margin-left: auto;
}

/* The tappable Detected-item chips, rendered under an assistant detected-items-message. */
.detected-item-rail {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.15rem 0 0.35rem;
}

.detected-item {
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 999px;
  padding: 0.45rem 0.85rem;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 0.15s ease,
    color 0.15s ease;
}

.detected-item:hover {
  background: var(--accent);
  color: #fff;
}

/* A detected item showing its crop: a small image-card with the label as a caption. The
   class is dropped at runtime if the crop 404s, leaving a plain text chip. */
.detected-item.detected-item-thumb {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.35rem;
  padding: 0.35rem;
  width: 108px;
  border-radius: 14px;
  background: var(--accent-soft);
}

.detected-item.detected-item-thumb:hover {
  background: var(--accent);
}

.detected-item.detected-item-thumb:hover .detected-item-label {
  color: #fff;
}

.detected-item-img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  background: var(--border);
}

.detected-item-thumb .detected-item-label {
  font-size: 0.74rem;
  line-height: 1.15;
  text-align: center;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* The refinement UX (issue #29): tapping a detected item attaches it to the composer. */

/* The tapped/attached source detected item: highlighted like a selected chip. */
.detected-item.detected-item-armed,
.detected-item.detected-item-thumb.detected-item-armed {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.detected-item.detected-item-armed .detected-item-label {
  color: #fff;
}

/* The attached-detected-item chip shown above the chat input. */
.attached-detected-item {
  margin-bottom: 0.4rem;
}

.attached-detected-item[hidden] {
  display: none;
}

.attached-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  max-width: 100%;
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 0.3rem 0.4rem 0.3rem 0.4rem;
}

.attached-chip-img {
  width: 28px;
  height: 28px;
  object-fit: cover;
  border-radius: 999px;
  flex-shrink: 0;
  background: var(--border);
}

.attached-chip-label {
  font-size: 0.82rem;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-left: 0.15rem;
}

.attached-chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.attached-chip-remove:hover {
  filter: brightness(1.05);
}
