/* ==========================================================================
   AgentLab Mail - Design System & UI Stylesheet
   Follows ui-ux-pro-max guidelines & MASTER.md tokens
   ========================================================================== */

:root {
  /* Color Palette - Light Mode */
  --color-primary: #2563EB;
  --color-primary-hover: #1D4ED8;
  --color-primary-light: #EFF6FF;
  --color-primary-ring: rgba(37, 99, 235, 0.25);
  --color-accent: #DC2626;
  --color-accent-light: #FEF2F2;

  --color-background: #F8FAFC;
  --color-surface: #FFFFFF;
  --color-surface-hover: #F1F5F9;
  --color-surface-active: #E0E7FF;
  --color-surface-selected: #EFF6FF;

  --color-foreground: #0F172A;
  --color-foreground-muted: #64748B;
  --color-foreground-subtle: #94A3B8;

  --color-border: #E2E8F0;
  --color-border-subtle: #F1F5F9;

  --color-success: #10B981;
  --color-success-bg: #ECFDF5;
  --color-warning: #F59E0B;
  --color-warning-bg: #FFFBEB;
  --color-danger: #EF4444;

  /* Typography */
  --font-sans: 'Plus Jakarta Sans', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Courier New', monospace;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;

  /* Elevation */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.04);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.04);

  /* Radii */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-full: 9999px;

  /* Layout dimensions */
  --sidebar-width: 290px;
  --list-width: 380px;
  --header-height: 64px;
}

/* Dark Mode Theme */
[data-theme="dark"] {
  --color-primary: #3B82F6;
  --color-primary-hover: #60A5FA;
  --color-primary-light: rgba(59, 130, 246, 0.15);
  --color-primary-ring: rgba(59, 130, 246, 0.35);
  --color-accent: #EF4444;
  --color-accent-light: rgba(239, 68, 68, 0.15);

  --color-background: #0B0F19;
  --color-surface: #111827;
  --color-surface-hover: #1F2937;
  --color-surface-active: #1E3A8A;
  --color-surface-selected: rgba(59, 130, 246, 0.12);

  --color-foreground: #F9FAFB;
  --color-foreground-muted: #9CA3AF;
  --color-foreground-subtle: #6B7280;

  --color-border: #1F2937;
  --color-border-subtle: #182234;

  --color-success-bg: rgba(16, 185, 129, 0.15);
  --color-warning-bg: rgba(245, 158, 11, 0.15);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.5);
}

/* Reset & Global */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--color-background);
  color: var(--color-foreground);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
}

/* Custom Scrollbars */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: var(--radius-full);
}
::-webkit-scrollbar-thumb:hover {
  background: var(--color-foreground-muted);
}

/* Button & Interactive Resets */
button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  background: none;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  transition: all 180ms cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
}

button:focus-visible, input:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* App Container */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ==========================================================================
   Top Header Bar
   ========================================================================== */
.top-header {
  height: var(--header-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-lg);
  gap: var(--space-md);
  z-index: 20;
  flex-shrink: 0;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: var(--sidebar-width);
}

.brand-logo-wrap {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, #2563EB, #1D4ED8);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 4px 8px rgba(37, 99, 235, 0.25);
  flex-shrink: 0;
}

.brand-titles {
  display: flex;
  flex-direction: column;
}

.brand-name {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--color-foreground);
  display: flex;
  align-items: center;
  gap: 6px;
}

.brand-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: var(--radius-full);
  background: var(--color-primary-light);
  color: var(--color-primary);
  letter-spacing: normal;
}

.brand-domain {
  font-size: 11px;
  color: var(--color-foreground-muted);
}

/* Search Bar in Header */
.search-bar-wrap {
  flex: 1;
  max-width: 620px;
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  background: var(--color-background);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 4px 6px 4px 12px;
  transition: border-color 180ms ease, box-shadow 180ms ease;
}

.search-bar-wrap:focus-within {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-ring);
  background: var(--color-surface);
}

.search-icon {
  color: var(--color-foreground-muted);
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 14px;
  outline: none;
  color: var(--color-foreground);
}

.search-input::placeholder {
  color: var(--color-foreground-subtle);
}

.btn-fetch {
  background: var(--color-primary);
  color: white;
  font-weight: 600;
  font-size: 13px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
}

.btn-fetch:hover {
  background: var(--color-primary-hover);
  transform: translateY(-1px);
}

/* Header Controls */
.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.action-icon-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  color: var(--color-foreground-muted);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
}

.action-icon-btn:hover {
  background: var(--color-surface-hover);
  color: var(--color-foreground);
}

.btn-test-mail {
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  color: var(--color-foreground);
}

.btn-test-mail:hover {
  background: var(--color-surface-hover);
  border-color: var(--color-foreground-muted);
}

/* Auto Refresh Switch */
.auto-refresh-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--color-foreground-muted);
  background: var(--color-background);
  padding: 6px 12px;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border);
  cursor: pointer;
  user-select: none;
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-foreground-subtle);
}

.live-dot.active {
  background: var(--color-success);
  box-shadow: 0 0 8px var(--color-success);
  animation: pulse-dot 2s infinite;
}

@keyframes pulse-dot {
  0% { transform: scale(0.95); opacity: 0.8; }
  50% { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(0.95); opacity: 0.8; }
}

/* ==========================================================================
   Main 3-Pane Layout
   ========================================================================== */
.main-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* 1. Left Sidebar */
.sidebar-pane {
  width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
}

.sidebar-section {
  padding: var(--space-md);
  border-bottom: 1px solid var(--color-border-subtle);
}

.sidebar-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-foreground-subtle);
  margin-bottom: var(--space-sm);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.recipient-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.recipient-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  text-align: left;
  width: 100%;
  font-size: 13px;
  color: var(--color-foreground);
  transition: background 150ms ease;
}

.recipient-item:hover {
  background: var(--color-surface-hover);
}

.recipient-item.active {
  background: var(--color-primary-light);
  color: var(--color-primary);
  font-weight: 600;
}

.recipient-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 190px;
}

.count-badge {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: var(--radius-full);
  background: var(--color-border);
  color: var(--color-foreground-muted);
}

.recipient-item.active .count-badge {
  background: var(--color-primary);
  color: white;
}

/* System Stats Card in Sidebar */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.stat-box {
  background: var(--color-background);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px;
}

.stat-val {
  font-size: 18px;
  font-weight: 700;
  color: var(--color-foreground);
}

.stat-lbl {
  font-size: 11px;
  color: var(--color-foreground-muted);
}

/* 2. Middle Message List */
.mail-list-pane {
  width: var(--list-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

.list-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--color-surface);
}

.list-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.list-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--color-foreground);
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.list-filter-input {
  width: 100%;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-background);
  font-size: 12px;
  color: var(--color-foreground);
  outline: none;
}

.list-filter-input:focus {
  border-color: var(--color-primary);
}

.message-items-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* Single Message Card Item */
.message-card {
  padding: 14px 16px;
  border-bottom: 1px solid var(--color-border-subtle);
  cursor: pointer;
  display: flex;
  gap: 12px;
  transition: all 150ms ease;
  position: relative;
  text-align: left;
}

.message-card:hover {
  background: var(--color-surface-hover);
}

.message-card.selected {
  background: var(--color-surface-selected);
}

.message-card.selected::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: var(--color-primary);
}

.sender-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: white;
  flex-shrink: 0;
}

.message-content-col {
  flex: 1;
  min-width: 0;
}

.msg-header-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}

.msg-sender {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-foreground);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg-time {
  font-size: 11px;
  color: var(--color-foreground-subtle);
  white-space: nowrap;
  margin-left: 6px;
}

.msg-subject {
  font-size: 13px;
  font-weight: 500;
  color: var(--color-foreground);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 4px;
}

.msg-snippet {
  font-size: 12px;
  color: var(--color-foreground-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
}

.msg-meta-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}

.attachment-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--color-foreground-muted);
  background: var(--color-border-subtle);
  padding: 1px 6px;
  border-radius: var(--radius-full);
}

/* 3. Right Reading Pane */
.reading-pane {
  flex: 1;
  background: var(--color-background);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 480px;
}

/* Empty State */
.reading-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--color-foreground-muted);
  padding: 40px;
  text-align: center;
}

.empty-icon-wrap {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-foreground-subtle);
}

/* Active Email View */
.email-view-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  background: var(--color-surface);
}

/* Email Subject Header */
.email-header-card {
  padding: 24px 28px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
}

.email-subject-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.email-subject-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--color-foreground);
  line-height: 1.3;
  word-break: break-word;
}

.email-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn-tool {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  font-size: 12px;
  font-weight: 500;
  color: var(--color-foreground-muted);
}

.btn-tool:hover {
  background: var(--color-surface-hover);
  color: var(--color-foreground);
}

/* Sender & Meta details */
.email-meta-card {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.sender-info-wrap {
  display: flex;
  gap: 12px;
  align-items: center;
}

.sender-text-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sender-from-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--color-foreground);
}

.sender-email-line {
  font-size: 13px;
  color: var(--color-foreground-muted);
}

.recipient-row {
  font-size: 12px;
  color: var(--color-foreground-muted);
  margin-top: 2px;
}

.recipient-chip {
  display: inline-block;
  background: var(--color-background);
  border: 1px solid var(--color-border);
  border-radius: 4px;
  padding: 1px 6px;
  color: var(--color-foreground);
  font-family: var(--font-mono);
  font-size: 11px;
}

.email-time-block {
  text-align: right;
}

.email-full-time {
  font-size: 13px;
  color: var(--color-foreground);
  font-weight: 500;
}

.email-rel-time {
  font-size: 11px;
  color: var(--color-foreground-muted);
}

/* Content Tabs */
.content-tabs-bar {
  display: flex;
  align-items: center;
  padding: 0 28px;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  gap: 16px;
}

.tab-btn {
  padding: 12px 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-foreground-muted);
  border-bottom: 2px solid transparent;
  position: relative;
}

.tab-btn:hover {
  color: var(--color-foreground);
}

.tab-btn.active {
  color: var(--color-primary);
  border-bottom-color: var(--color-primary);
}

/* Attachments Bar */
.attachments-wrapper {
  padding: 16px 28px;
  background: var(--color-background);
  border-bottom: 1px solid var(--color-border);
}

.attachments-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-foreground-subtle);
  margin-bottom: 10px;
}

.attachments-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}

.attachment-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  transition: all 150ms ease;
  text-decoration: none;
}

.attachment-card:hover {
  border-color: var(--color-primary);
  box-shadow: var(--shadow-sm);
}

.attach-icon-wrap {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background: var(--color-primary-light);
  color: var(--color-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.attach-info {
  flex: 1;
  min-width: 0;
}

.attach-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-foreground);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attach-size {
  font-size: 11px;
  color: var(--color-foreground-muted);
}

/* Body Area */
.email-body-content {
  flex: 1;
  padding: 28px;
  background: var(--color-surface);
  position: relative;
}

.sandboxed-iframe {
  width: 100%;
  border: none;
  min-height: 500px;
  display: block;
  background: white;
  border-radius: var(--radius-sm);
}

.plain-text-body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.7;
  color: var(--color-foreground);
  white-space: pre-wrap;
  word-break: break-word;
}

/* MIME Headers View */
.headers-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-family: var(--font-mono);
}

.headers-table th, .headers-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--color-border);
  text-align: left;
  vertical-align: top;
}

.headers-table th {
  color: var(--color-primary);
  width: 220px;
  font-weight: 600;
  word-break: break-all;
}

.headers-table td {
  color: var(--color-foreground);
  word-break: break-all;
}

/* Loading Skeletons */
.skeleton {
  background: linear-gradient(90deg, var(--color-border-subtle) 25%, var(--color-surface-hover) 50%, var(--color-border-subtle) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}

@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton-card {
  padding: 16px;
  border-bottom: 1px solid var(--color-border-subtle);
  display: flex;
  gap: 12px;
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}

.toast {
  background: var(--color-foreground);
  color: var(--color-background);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 8px;
  animation: toast-in 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 24px 28px;
  box-shadow: var(--shadow-xl);
  max-width: 520px;
  width: 90%;
  border: 1px solid var(--color-border);
  transform: scale(0.96);
  transition: transform 180ms ease;
}

.modal-overlay.open .modal-card {
  transform: scale(1);
}

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

.modal-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--color-foreground);
}

/* Mobile Back Button */
.mobile-back-btn {
  display: none;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-primary);
  margin-bottom: 12px;
}

/* ==========================================================================
   Responsive Adaptations
   ========================================================================== */
@media (max-width: 1024px) {
  :root {
    --sidebar-width: 240px;
    --list-width: 320px;
  }
}

@media (max-width: 768px) {
  .top-header {
    padding: 0 var(--space-md);
  }
  .brand-titles {
    display: none;
  }
  .brand-section {
    min-width: auto;
  }
  .sidebar-pane {
    position: fixed;
    top: var(--header-height);
    bottom: 0;
    left: -290px;
    z-index: 30;
    transition: left 200ms ease;
  }
  .sidebar-pane.open {
    left: 0;
  }
  .mail-list-pane {
    width: 100%;
  }
  .reading-pane {
    display: none;
    position: fixed;
    top: var(--header-height);
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 25;
    min-width: unset;
  }
  .reading-pane.open {
    display: flex;
  }
  .mobile-back-btn {
    display: flex;
  }
}
