/* ============================================================
   ShopMind AI — Styles (Upgraded)
   ============================================================ */

:root {
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --success: #22c55e;
  --danger: #ef4444;
  --warning: #f59e0b;
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --agent-bg: #f0f4ff;
  --user-bg: linear-gradient(135deg, #3b82f6, #4f46e5);
  --user-text: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-xl: 0 8px 24px rgba(0,0,0,0.12);
  --header-grad: linear-gradient(135deg, #1e293b, #334155);
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  line-height: 1.6;
}

/* ── Layout ─────────────────────────────── */
.app-container {
  display: flex;
  height: 100vh;
}

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

.cart-sidebar {
  width: 340px;
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

/* ── Header ─────────────────────────────── */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: var(--header-grad);
  color: #ffffff;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.chat-header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-header .badge {
  font-size: 0.7rem;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  color: white;
  padding: 4px 10px;
  border-radius: 9999px;
  font-weight: 600;
  box-shadow: var(--shadow);
}

.email-badge {
  font-size: 0.75rem;
  background: rgba(59, 130, 246, 0.2);
  color: #e0f2fe;
  padding: 4px 10px;
  border-radius: 9999px;
  font-weight: 500;
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  vertical-align: middle;
  border: 1px solid rgba(59, 130, 246, 0.3);
}

.btn-switch-account {
  font-size: 0.75rem;
  padding: 4px 8px;
  border-radius: 6px;
  background: transparent;
  color: #94a3b8;
  border: 1px solid #475569;
  cursor: pointer;
  transition: all 0.2s;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
}

.btn-switch-account:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border-color: #64748b;
}

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

.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.header-actions a, .header-actions button {
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  border: 1px solid rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.1);
  color: #ffffff;
  transition: all 0.2s;
}

.header-actions a:hover, .header-actions button:hover {
  background: rgba(255,255,255,0.2);
}

.header-actions a:active, .header-actions button:active {
  transform: scale(0.95);
}

/* ── Messages ───────────────────────────── */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  display: flex;
  gap: 12px;
  max-width: 80%;
  animation: slideIn 0.4s cubic-bezier(0.16, 1, 0.3, 1) forwards;
  opacity: 0;
  transform: translateY(10px);
}

@keyframes slideIn {
  to { opacity: 1; transform: translateY(0); }
}

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

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  box-shadow: var(--shadow);
}

.message.agent .message-avatar {
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  color: white;
}

.message.user .message-avatar {
  background: linear-gradient(135deg, #3b82f6, #4f46e5);
  color: white;
}

.message-bubble {
  padding: 14px 18px;
  border-radius: 18px;
  line-height: 1.6;
  font-size: 0.92rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}

.message.agent .message-bubble {
  background: var(--agent-bg);
  color: var(--text);
  border-bottom-left-radius: 4px;
  border: 1px solid rgba(59, 130, 246, 0.1);
}

.message.user .message-bubble {
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

.message-bubble p { margin-bottom: 8px; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble strong { font-weight: 600; }

.message-bubble ul, .message-bubble ol {
  margin: 6px 0;
  padding-left: 22px;
}

.message-bubble li {
  margin-bottom: 4px;
}

.message-bubble li:last-child {
  margin-bottom: 0;
}

.message-bubble code {
  background: rgba(0, 0, 0, 0.06);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85em;
  color: #7c3aed;
}

.message.user .message-bubble code {
  background: rgba(255, 255, 255, 0.2);
  color: #ffffff;
}

/* ── Confirm/Cancel buttons ─────────────── */
.confirmation-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.btn-confirm, .btn-cancel {
  padding: 8px 20px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-confirm {
  background: var(--success);
  color: white;
}
.btn-confirm:hover { background: #16a34a; }
.btn-confirm:active { transform: scale(0.95); }

.btn-cancel {
  background: var(--surface);
  color: var(--danger);
  border: 1px solid var(--danger);
}
.btn-cancel:hover { background: #fef2f2; }
.btn-cancel:active { transform: scale(0.95); }

/* ── Input area ─────────────────────────── */
.input-area {
  padding: 16px 24px 24px;
  background: transparent;
}

.input-wrapper {
  display: flex;
  gap: 8px;
  background: var(--surface);
  border-radius: 9999px;
  padding: 6px 6px 6px 20px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  transition: box-shadow 0.3s ease, border-color 0.3s ease;
}

.input-wrapper:focus-within {
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2), var(--shadow-lg);
  border-color: var(--primary);
}

.input-wrapper input {
  flex: 1;
  padding: 10px 0;
  border: none;
  background: transparent;
  font-size: 0.95rem;
  outline: none;
  color: var(--text);
}

.input-wrapper button {
  padding: 10px 24px;
  background: var(--header-grad);
  color: white;
  border: none;
  border-radius: 9999px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.input-wrapper button:hover {
  box-shadow: 0 4px 12px rgba(30, 41, 59, 0.3);
}

.input-wrapper button:active {
  transform: scale(0.95);
}

.input-wrapper button:disabled {
  background: var(--text-muted);
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* ── Cart sidebar ───────────────────────── */
.cart-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.cart-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
  font-size: 1rem;
}

.btn-recover {
  background: rgba(59, 130, 246, 0.08);
  border: 1px solid rgba(59, 130, 246, 0.2);
  color: var(--primary);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: all 0.2s;
}

.btn-recover:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

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

.cart-items {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.cart-item {
  display: flex;
  justify-content: space-between;
  align-items: start;
  padding: 12px;
  border-radius: 12px;
  background: var(--bg);
  margin-bottom: 12px;
  border: 1px solid var(--border);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.cart-item:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
  border-color: rgba(59, 130, 246, 0.3);
}

.cart-item-name {
  font-weight: 600;
  font-size: 0.88rem;
  margin-bottom: 4px;
}

.cart-item-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.cart-item-price {
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
}

.cart-item .source-badge {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}

.source-badge.ai { background: #ede9fe; color: #7c3aed; }
.source-badge.organic { background: #ecfdf5; color: #059669; }
.source-badge.upsell { background: #fff7ed; color: #ea580c; }

.cart-empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.cart-footer {
  padding: 20px;
  background: var(--header-grad);
  color: white;
  border-top: 1px solid var(--border);
}

.cart-total {
  display: flex;
  justify-content: space-between;
  font-weight: 700;
  font-size: 1.15rem;
  margin-bottom: 16px;
}

.btn-checkout {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #22c55e, #10b981);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.btn-checkout:hover { 
  box-shadow: 0 6px 16px rgba(16, 185, 129, 0.4); 
}

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

.btn-checkout:disabled { 
  background: var(--text-muted); 
  cursor: not-allowed; 
  box-shadow: none; 
  transform: none; 
}

/* ── Typing indicator ───────────────────── */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 12px 16px;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--primary);
  animation: typingPulse 1.4s infinite ease-in-out both;
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes typingPulse {
  0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

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

/* ── Dashboard styles ───────────────────── */
.dashboard-container {
  padding: 24px;
  max-width: 1400px;
  margin: 0 auto;
  overflow-y: auto;
  height: 100vh;
}

.dashboard-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.dashboard-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.metric-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.metric-card .label {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: 4px;
}

.metric-card .value {
  font-size: 1.5rem;
  font-weight: 700;
}

.metric-card .value.success { color: var(--success); }
.metric-card .value.danger { color: var(--danger); }
.metric-card .value.primary { color: var(--primary); }

.panel {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 24px;
  overflow: hidden;
}

.panel-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-body {
  padding: 16px 20px;
}

/* ── AI Actions Trace ───────────────────── */
.action-item {
  display: flex;
  gap: 12px;
  padding: 12px;
  border-radius: 8px;
  margin-bottom: 8px;
  border-left: 3px solid var(--border);
  background: var(--bg);
}

.action-item.success { border-left-color: var(--success); }
.action-item.failure { border-left-color: var(--danger); }
.action-item.pending { border-left-color: var(--warning); }

.action-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  flex-shrink: 0;
}

.action-item.success .action-icon { background: #dcfce7; }
.action-item.failure .action-icon { background: #fee2e2; }
.action-item.pending .action-icon { background: #fef3c7; }

.action-details { flex: 1; min-width: 0; }

.action-tool { font-weight: 600; font-size: 0.85rem; }
.action-decision { font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; }
.action-time { font-size: 0.7rem; color: var(--text-muted); }

/* ── Orders table ───────────────────────── */
.orders-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.orders-table th, .orders-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.orders-table th {
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  color: var(--text-muted);
  background: var(--bg);
}

.status-badge {
  padding: 4px 10px;
  border-radius: 9999px;
  font-size: 0.7rem;
  font-weight: 600;
}

.status-badge.paid { background: #dcfce7; color: #166534; }
.status-badge.failed { background: #fee2e2; color: #991b1b; }
.status-badge.created { background: #fef3c7; color: #92400e; }

/* ── Session selector ───────────────────── */
.session-select {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 0.85rem;
  background: var(--surface);
  outline: none;
}
.session-select:focus { border-color: var(--primary); }

/* ── Failures panel ─────────────────────── */
.failure-item {
  padding: 12px;
  border-radius: 8px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  margin-bottom: 8px;
}

.failure-item .failure-reason {
  color: var(--danger);
  font-weight: 600;
  font-size: 0.85rem;
}

.failure-item .failure-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ── Modal Dialogs (Checkout Info & Cart Recovery) ── */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.7);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
  animation: fadeIn 0.2s ease-out;
}

.modal-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-xl);
  width: 100%;
  max-width: 440px;
  padding: 24px;
  position: relative;
  border: 1px solid var(--border);
}

/* Email Modal Specific */
.email-modal-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-xl);
  width: 100%;
  max-width: 440px;
  padding: 32px 24px;
  position: relative;
  border: 1px solid var(--border);
  overflow: hidden;
  text-align: center;
}

.email-modal-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 4px;
  background: linear-gradient(90deg, #3b82f6, #8b5cf6);
}

.email-modal-enter {
  animation: modalScaleFade 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes modalScaleFade {
  from { opacity: 0; transform: scale(0.95) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.email-modal-card .modal-header h3 {
  font-size: 1.5rem;
  justify-content: center;
  margin-bottom: 8px;
}

.email-modal-card .modal-subtitle {
  font-size: 0.95rem;
  margin-bottom: 24px;
}

.email-modal-card .modal-form input {
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--bg);
  font-size: 1rem;
}

.email-modal-card .modal-btn-primary {
  padding: 12px;
  border-radius: 12px;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  font-size: 1rem;
  margin-top: 12px;
}
.email-modal-card .modal-btn-primary:hover {
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

/* General Modal */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-header h3 {
  font-size: 1.2rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.modal-close-btn {
  background: none;
  border: none;
  font-size: 1.25rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  line-height: 1;
}

.modal-close-btn:hover {
  background: var(--bg);
  color: var(--text);
}

.modal-subtitle {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 16px;
  line-height: 1.5;
}

.modal-form .form-group {
  margin-bottom: 16px;
  text-align: left;
}

.modal-form label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.modal-form input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 0.95rem;
  outline: none;
  font-family: inherit;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.modal-form input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.modal-form .modal-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}

.modal-btn-primary {
  flex: 1;
  padding: 12px 20px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.modal-btn-primary:hover {
  background: var(--primary-dark);
}
.modal-btn-primary:active {
  transform: scale(0.98);
}

.modal-btn-secondary {
  padding: 12px 20px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.modal-btn-secondary:hover {
  background: var(--border);
}
.modal-btn-secondary:active {
  transform: scale(0.98);
}

.modal-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin-top: 12px;
}

/* ── Cart Action & Recovery Buttons ──────── */
.cart-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.cart-recover-link {
  font-size: 0.8rem;
  color: var(--primary);
  text-decoration: none;
  cursor: pointer;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

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

.cart-item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.cart-remove-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  font-size: 1rem;
  transition: all 0.2s;
}

.cart-remove-btn:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

.btn-clear-cart {
  width: 100%;
  margin-top: 8px;
  padding: 10px;
  background: none;
  border: 1px dashed var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-clear-cart:hover {
  border-color: var(--danger);
  color: var(--danger);
  background: rgba(239, 68, 68, 0.04);
}

/* ── Responsive Layout ──────────────────── */
@media (max-width: 840px) {
  .app-container {
    flex-direction: column-reverse;
    height: 100vh;
    overflow: hidden;
  }

  .cart-sidebar {
    width: 100%;
    max-height: 240px;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  .chat-panel {
    flex: 1;
    min-height: 0;
  }

  .message {
    max-width: 92%;
  }

  .metrics-grid {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  }
}
