:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --surface-hover: #f7f8fa;
  --text: #1a1a1e;
  --muted: #6b7280;
  --faint: #9ca3af;
  --border: #e4e6eb;
  --border-strong: #d1d5db;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --shadow: 0 1px 2px rgba(0,0,0,0.04), 0 4px 16px rgba(0,0,0,0.06);
  --shadow-lg: 0 8px 40px rgba(0,0,0,0.12);
  --radius: 12px;

  --cat-applied: #3b82f6;
  --cat-screening: #8b5cf6;
  --cat-assessment: #f59e0b;
  --cat-interview: #14b8a6;
  --cat-offer: #10b981;
  --cat-rejected: #ef4444;
  --cat-other: #94a3b8;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1013;
    --surface: #1a1b1f;
    --surface-2: #232428;
    --surface-hover: #26272c;
    --text: #ececee;
    --muted: #9aa0ab;
    --faint: #6b7280;
    --border: #2a2c31;
    --border-strong: #3a3d44;
    --accent: #6366f1;
    --accent-hover: #818cf8;
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 4px 16px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 40px rgba(0,0,0,0.6);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: none;
  border-radius: 9px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, transform 0.05s, opacity 0.15s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.55; cursor: default; }
.btn-lg { padding: 12px 24px; font-size: 15px; border-radius: 11px; }
.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border-strong);
  text-decoration: none;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.icon-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.sync-icon { transition: transform 0.4s; }
.btn.syncing .sync-icon { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Connect screen ---------- */
.connect-screen {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.connect-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow-lg);
  padding: 48px 40px;
  max-width: 420px;
  text-align: center;
}
.connect-card h1 { font-size: 24px; margin: 20px 0 8px; }
.connect-card p { color: var(--muted); line-height: 1.5; margin: 0 0 28px; }
.logo-mark {
  width: 64px; height: 64px;
  margin: 0 auto;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--accent), #a855f7);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 30px;
}
.logo-mark.small { width: 36px; height: 36px; border-radius: 10px; font-size: 18px; margin: 0; }

/* ---------- App shell ---------- */
.app { height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 16px;
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-title { font-weight: 700; font-size: 15px; line-height: 1.1; }
.brand-sub { font-size: 12px; color: var(--faint); }
.topbar-actions { display: flex; align-items: center; gap: 12px; }

.search-wrap { position: relative; display: flex; align-items: center; }
.search-icon { position: absolute; left: 10px; color: var(--faint); pointer-events: none; }
#search {
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 12px 8px 32px;
  font-size: 13px;
  color: var(--text);
  width: 240px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}
#search:focus { border-color: var(--accent); background: var(--surface); }
.status { font-size: 12px; color: var(--muted); min-width: 0; max-width: 260px; }

/* ---------- Layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }

/* Sidebar */
.sidebar {
  width: 220px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  overflow-y: auto;
  background: var(--surface);
}
.total-card {
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  color: #fff;
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.total-num { font-size: 30px; font-weight: 700; line-height: 1; }
.total-label { font-size: 12px; opacity: 0.85; margin-top: 4px; }

.cat-nav { display: flex; flex-direction: column; gap: 2px; }
.cat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  transition: background 0.12s;
  user-select: none;
}
.cat-item:hover { background: var(--surface-2); }
.cat-item.active { background: var(--surface-2); font-weight: 600; }
.cat-dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.cat-label { flex: 1; }
.cat-count {
  font-size: 12px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 1px 8px;
  min-width: 24px;
  text-align: center;
}
.cat-item.active .cat-count { background: var(--accent); color: #fff; border-color: transparent; }

/* Email list */
.list-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
}
.list-header {
  padding: 14px 20px;
  font-size: 13px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  font-weight: 500;
}
.email-list { flex: 1; overflow-y: auto; padding: 8px; }

.email-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color 0.12s, box-shadow 0.12s, transform 0.05s;
  border-left: 3px solid var(--cat, var(--cat-other));
}
.email-card:hover { box-shadow: var(--shadow); border-color: var(--border-strong); }
.email-card.selected { border-color: var(--accent); box-shadow: var(--shadow); }
.email-card.completed { opacity: 0.65; }
.email-card.completed:hover, .email-card.completed.selected { opacity: 1; }
.card-top { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.card-company { font-weight: 700; font-size: 14px; }
.card-date { font-size: 11px; color: var(--faint); white-space: nowrap; flex-shrink: 0; }
.card-role { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.card-subject { font-size: 13px; margin-top: 8px; color: var(--text); line-height: 1.35; }
.card-snippet {
  font-size: 12px; color: var(--faint); margin-top: 4px;
  line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card-badges { display: flex; align-items: center; gap: 6px; margin-top: 8px; }
.card-badge {
  display: inline-block;
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 8px;
  border-radius: 6px;
  color: #fff;
  background: var(--cat, var(--cat-other));
}
.done-badge { background: var(--cat-offer); }
.notes-indicator { font-size: 12px; }

/* Reading pane */
.reader-pane {
  width: 46%;
  min-width: 360px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
}
.reader-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.reader-head { padding: 20px 24px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.reader-subject { font-size: 19px; font-weight: 700; line-height: 1.3; margin-bottom: 12px; }
.reader-meta-row { display: flex; align-items: center; gap: 10px; font-size: 12.5px; margin-bottom: 4px; }
.reader-meta-label { color: var(--faint); width: 44px; flex-shrink: 0; }
.reader-meta-val { color: var(--text); }
.reader-tags { display: flex; gap: 8px; margin-top: 12px; }

.reader-tracking {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.completed-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  margin-bottom: 10px;
}
.completed-toggle input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--cat-offer);
  cursor: pointer;
}
.notes-field {
  width: 100%;
  min-height: 64px;
  resize: vertical;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  line-height: 1.5;
  outline: none;
  transition: border-color 0.15s;
}
.notes-field:focus { border-color: var(--accent); }
.notes-field::placeholder { color: var(--faint); }
.notes-status { font-size: 11px; color: var(--faint); margin-top: 4px; height: 14px; }

.reader-body { flex: 1; overflow: hidden; }
.reader-body iframe { width: 100%; height: 100%; border: none; background: #fff; }
.reader-text { padding: 24px; white-space: pre-wrap; line-height: 1.6; font-size: 13.5px; overflow-y: auto; height: 100%; }

/* Empty / loading states */
.empty {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100%;
  color: var(--faint);
  text-align: center;
  padding: 40px;
  gap: 8px;
}
.empty-icon { font-size: 40px; opacity: 0.5; }
.spinner {
  width: 22px; height: 22px;
  border: 2.5px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ---------- Chat assistant ---------- */
.chat-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--accent), #a855f7);
  color: #fff;
  font-size: 24px;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
  transition: transform 0.15s;
}
.chat-fab:hover { transform: scale(1.06); }
.chat-fab:active { transform: scale(0.96); }

.chat-panel {
  position: fixed;
  bottom: 90px;
  right: 24px;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: 520px;
  max-height: calc(100vh - 120px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 90;
}
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  font-weight: 700;
  flex-shrink: 0;
}
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chat-msg {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: 14px;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
}
.chat-msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.bot {
  align-self: flex-start;
  background: var(--surface-2);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.chat-msg.loading { opacity: 0.6; font-style: italic; }
.chat-msg.error { background: color-mix(in srgb, var(--cat-rejected) 15%, var(--surface-2)); color: var(--text); }

.chat-input-row {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
#chatInput {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 13px;
  color: var(--text);
  font-family: inherit;
  outline: none;
  min-width: 0;
}
#chatInput:focus { border-color: var(--accent); }

/* Responsive */
@media (max-width: 900px) {
  .sidebar { width: 180px; }
  .reader-pane {
    position: fixed;
    top: 0; right: 0; bottom: 0;
    width: 100%;
    z-index: 50;
    box-shadow: var(--shadow-lg);
  }
  #search { width: 140px; }
  .chat-panel {
    right: 12px;
    left: 12px;
    width: auto;
    bottom: 84px;
  }
  .chat-fab { bottom: 16px; right: 16px; }
}
