/* ─── Reset & base ─────────────────────────────────────────────────────────── */

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

:root {
  --primary:       #1e4a7c;   /* Diocese blue */
  --primary-light: #2d6bb5;
  --primary-pale:  #e8f0fb;
  --accent:        #c8a84b;   /* Gold accent */
  --text:          #1a1a2e;
  --text-muted:    #6b7280;
  --border:        #dde3ec;
  --bg:            #f4f6fa;
  --surface:       #ffffff;
  --unread-bg:     #eef4ff;
  --unread-dot:    #2563eb;
  --flag-color:    #dc2626;
  --danger:        #dc2626;
  --radius:        8px;
  --shadow:        0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md:     0 4px 12px rgba(0,0,0,.10);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  min-height: 100vh;
}

.hidden { display: none !important; }

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ─── Buttons ───────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, opacity .15s;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--primary-light); }

.btn-secondary {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: var(--bg); }

.btn-large { padding: 12px 28px; font-size: 15px; }

/* ─── Login view ────────────────────────────────────────────────────────────── */

#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}

.login-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  padding: 48px 40px;
  max-width: 420px;
  width: 100%;
  text-align: center;
}

.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 12px;
}

.login-logo img {
  height: 64px;
  margin-bottom: 8px;
  object-fit: contain;
}

.login-logo h1 {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
}

.login-card h2 {
  font-size: 26px;
  font-weight: 700;
  margin-bottom: 10px;
  color: var(--text);
}

.login-card p {
  color: var(--text-muted);
  margin-bottom: 28px;
  line-height: 1.5;
}

/* ─── App layout ────────────────────────────────────────────────────────────── */

#app-view { display: flex; flex-direction: column; min-height: 100vh; }

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left { display: flex; align-items: center; gap: 12px; }
.header-logo-wrap {
  background: #fff;
  border-radius: 8px;
  padding: 4px 10px;
  display: flex;
  align-items: center;
}
.header-logo { height: 44px; }
.app-title { font-size: 17px; font-weight: 700; letter-spacing: -.2px; }
.org-subtitle { font-size: 12px; opacity: .7; margin-left: 4px; }

.header-right { display: flex; align-items: center; gap: 12px; }
#user-name { font-size: 13px; opacity: .9; }
#btn-logout {
  background: rgba(255,255,255,.15);
  color: #fff;
  border: 1px solid rgba(255,255,255,.3);
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s;
}
#btn-logout:hover { background: rgba(255,255,255,.25); }

/* ─── Controls bar ──────────────────────────────────────────────────────────── */

.controls-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 20px;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-group label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text-muted);
}

.radio-group {
  display: flex;
  gap: 14px;
  align-items: center;
}
.radio-group label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  cursor: pointer;
}

input[type="email"], input[type="date"], select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s;
}
input[type="email"]:focus, input[type="date"]:focus, select:focus {
  outline: none;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(30,74,124,.1);
}

#shared-mailbox-input { min-width: 260px; }

.date-presets { display: flex; gap: 6px; flex-wrap: wrap; }

.preset-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  transition: all .15s;
}
.preset-btn:hover { border-color: var(--primary-light); color: var(--primary); }
.preset-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

#custom-dates {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

#btn-run { align-self: flex-end; }

/* ─── Stats bar ─────────────────────────────────────────────────────────────── */

#stats-bar {
  display: flex;
  gap: 1px;
  background: var(--border);
  border-bottom: 1px solid var(--border);
}

.stat-card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 14px 20px;
  background: var(--surface);
  cursor: pointer;
  transition: background .15s;
  gap: 2px;
}
.stat-card:hover { background: var(--primary-pale); }
.stat-card:first-child { border-radius: 0; }

.stat-num {
  font-size: 26px;
  font-weight: 700;
  color: var(--primary);
  line-height: 1;
}
.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .4px;
  font-weight: 500;
}

/* Highlight active stat */
.stat-card[data-filter="unread"] .stat-num { color: var(--unread-dot); }
.stat-card[data-filter="flagged"] .stat-num { color: var(--flag-color); }
.stat-card[data-filter="categorized"] .stat-num { color: #7c3aed; }

/* ─── Filter bar ────────────────────────────────────────────────────────────── */

#filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 56px;
  z-index: 90;
}

.filter-tabs { display: flex; gap: 4px; }

.filter-tab {
  padding: 6px 16px;
  border: 1px solid transparent;
  border-radius: 20px;
  background: none;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: all .15s;
}
.filter-tab:hover { background: var(--bg); color: var(--text); }
.filter-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.filter-tab-triage {
  background: linear-gradient(135deg, #1e4a7c, #7c3aed);
  color: #fff !important;
  border-color: transparent !important;
}
.filter-tab-triage:hover { opacity: .85; }
.filter-tab-triage.active { opacity: 1; box-shadow: 0 0 0 3px rgba(124,58,237,.25); }

/* ─── Triage sections ───────────────────────────────────────────────────────── */

.triage-section { margin-bottom: 8px; }

.triage-section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
  transition: background .15s;
}
.triage-section-header:hover { background: var(--primary-pale); }

.triage-chevron {
  font-size: 16px;
  transition: transform .2s;
  color: var(--text-muted);
  display: inline-block;
}
.triage-chevron.collapsed { transform: rotate(-90deg); }

.triage-icon { font-size: 16px; }

.triage-label { font-weight: 700; font-size: 14px; }

.triage-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.triage-unread-badge {
  background: var(--unread-dot);
  color: #fff;
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 11px;
  font-weight: 600;
}

.triage-section-actions {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.triage-move-btn, .triage-markread-btn {
  padding: 4px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s;
  border: 1px solid var(--border);
}
.triage-move-btn {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.triage-move-btn:hover { background: var(--primary-light); }
.triage-markread-btn { background: var(--surface); color: var(--text); }
.triage-markread-btn:hover { background: var(--bg); }
.triage-markread-btn:disabled { opacity: .5; cursor: not-allowed; }

.triage-section-body {
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  overflow: hidden;
}
.triage-section-body .email-row {
  border-radius: 0;
  border-left: none;
  border-right: none;
  border-top: none;
  border-bottom: 1px solid var(--border);
}
.triage-section-body .email-row:last-child { border-bottom: none; }
.triage-section-body .email-row.unread { border-left: 3px solid var(--unread-dot); }
.triage-section-body .email-row.flagged { border-left: 3px solid var(--flag-color); }

/* ─── Move modal ────────────────────────────────────────────────────────────── */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 24px;
}

.modal-box {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow-md);
  padding: 28px 32px;
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-box h3 { font-size: 18px; font-weight: 700; }
.modal-box p { color: var(--text-muted); font-size: 14px; }
.modal-box label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; color: var(--text-muted); }
.modal-box select { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; }

.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

.modal-progress { display: flex; flex-direction: column; gap: 6px; }
.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  transition: width .2s;
  width: 0%;
}
#progress-label { font-size: 12px; color: var(--text-muted); text-align: center; }

.filter-extras {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.filter-extras select, .filter-extras input {
  padding: 5px 9px;
  font-size: 13px;
}
#search-filter { min-width: 180px; }

.results-label {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}
#results-count { font-weight: 600; color: var(--text); }

#btn-export-csv {
  padding: 6px 12px;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  transition: background .15s;
}
#btn-export-csv:hover { background: var(--bg); }

/* ─── Email list ────────────────────────────────────────────────────────────── */

#email-list {
  flex: 1;
  padding: 12px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.email-row {
  display: grid;
  grid-template-columns: 24px 1fr auto;
  gap: 10px;
  align-items: start;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: box-shadow .15s, background .15s;
  position: relative;
}
.email-row:hover { box-shadow: var(--shadow-md); }
.email-row.unread { background: var(--unread-bg); border-left: 3px solid var(--unread-dot); }
.email-row.flagged { border-left: 3px solid var(--flag-color); }
.email-row.unread.flagged { border-left: 3px solid var(--flag-color); }

.unread-dot {
  display: block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--unread-dot);
  margin-top: 5px;
  flex-shrink: 0;
}
.read-dot {
  display: block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: transparent;
  margin-top: 5px;
}

.email-body { min-width: 0; }

.email-row-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 3px;
  flex-wrap: wrap;
}

.email-sender {
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 220px;
}
.email-row.unread .email-sender { color: var(--text); }

.email-badges { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }

.badge-importance {
  display: inline-block;
  background: #fee2e2;
  color: var(--danger);
  font-weight: 800;
  font-size: 11px;
  padding: 0 5px;
  border-radius: 4px;
}
.badge-attach { font-size: 13px; opacity: .6; }
.badge-flag { font-size: 13px; }

.badge-category {
  display: inline-block;
  font-size: 11px;
  font-weight: 500;
  padding: 1px 7px;
  border-radius: 10px;
  white-space: nowrap;
}

.email-date {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: auto;
  white-space: nowrap;
}

.email-subject {
  font-size: 14px;
  font-weight: 400;
  color: var(--text);
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.email-row.unread .email-subject { font-weight: 600; }

.email-preview {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-height: 1.4em;
  transition: max-height .2s, white-space .2s;
}
.email-row.expanded .email-preview {
  white-space: normal;
  max-height: 200px;
  overflow: auto;
}

/* Action buttons */
.email-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  opacity: 0;
  transition: opacity .15s;
}
.email-row:hover .email-actions { opacity: 1; }

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-muted);
  transition: background .15s, color .15s;
  text-decoration: none;
}
.action-btn:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.action-btn.active { color: var(--flag-color); background: #fee2e2; }
.action-btn-delete:hover { background: #fee2e2; color: #dc2626; }

/* ─── Loading ────────────────────────────────────────────────────────────────── */

#loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 64px;
  color: var(--text-muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Empty state ───────────────────────────────────────────────────────────── */

#empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 64px;
  color: var(--text-muted);
  font-size: 15px;
}

/* ─── Error banner ──────────────────────────────────────────────────────────── */

#error-banner {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1a1a2e;
  color: #fff;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  box-shadow: var(--shadow-md);
  z-index: 999;
  max-width: 480px;
  text-align: center;
}

/* ─── Responsive ────────────────────────────────────────────────────────────── */

@media (max-width: 768px) {
  .controls-bar { padding: 12px 16px; gap: 12px; }
  #email-list { padding: 8px 12px; }
  .email-sender { max-width: 140px; }
  #filter-bar { padding: 8px 12px; flex-direction: column; align-items: flex-start; }
  .filter-extras { width: 100%; }
  #search-filter { flex: 1; min-width: 0; }
  #stats-bar { flex-wrap: wrap; }
  .stat-card { min-width: 50%; }
}

@media (max-width: 480px) {
  .app-title { font-size: 15px; }
  .login-card { padding: 32px 20px; }
}

/* ─── Print ─────────────────────────────────────────────────────────────────── */

@media print {
  .app-header, .controls-bar, #filter-bar, .email-actions, #btn-export-csv { display: none !important; }
  #email-list { padding: 0; }
  .email-row { box-shadow: none; border: 1px solid #ccc; margin-bottom: 4px; }
  .email-row:hover .email-actions { opacity: 0; }
  body { background: #fff; }
}
