/* Re-EL Mailer — design tokens + layout.
   Light and dark are driven by [data-theme] on <html>. */

:root {
  --brand: #21396a;
  --brand-600: #2d4a86;
  --brand-050: #eef2fb;
  --muted: #6b7488;
  --border: #dfe4ee;
  --bg: #f6f8fc;
  --surface: #ffffff;
  --text: #1b2233;
  --danger: #b42318;
  --danger-bg: #fdeceb;
  --success: #067647;
  --radius: 10px;
  --radius-lg: 14px;
  --shadow: 0 1px 2px rgb(16 21 34 / 6%), 0 8px 24px rgb(16 21 34 / 6%);
  --row-gap: 14px;
}

[data-theme='dark'] {
  --brand: #7ea2f0;
  --brand-600: #9ab8f5;
  --brand-050: #1a2540;
  --muted: #9aa3b7;
  --border: #2a3348;
  --bg: #101522;
  --surface: #171d2d;
  --text: #e8ebf2;
  --danger: #ff8a80;
  --danger-bg: #3a1f1e;
  --success: #5ddb9a;
  --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 8px 24px rgb(0 0 0 / 30%);
}

[data-density='compact'] { --row-gap: 8px; }

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#root { min-height: 100%; }

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

.muted { color: var(--muted); }
.small { font-size: 13px; }
.loading { padding: 32px; text-align: center; color: var(--muted); }
.empty { padding: 48px 16px; text-align: center; color: var(--muted); }

/* ── Buttons & inputs ────────────────────────────────────────────────── */

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}
.btn:hover { background: var(--brand-050); border-color: var(--brand); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-600); border-color: var(--brand-600); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.08); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 13px; }

.icon-btn {
  border: none; background: none; cursor: pointer;
  font-size: 18px; color: var(--text); padding: 6px 8px; border-radius: 8px;
}
.icon-btn:hover { background: var(--brand-050); }

input, select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 11px;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}
textarea { resize: vertical; }

.field { display: grid; gap: 4px; margin-bottom: 14px; }
.field-label { font-size: 13px; font-weight: 600; color: var(--muted); }

.form-error {
  background: var(--danger-bg); color: var(--danger);
  padding: 9px 11px; border-radius: var(--radius); margin-bottom: 12px;
  font-size: 14px;
}

/* ── Public / auth ───────────────────────────────────────────────────── */

.public-shell { min-height: 100vh; display: flex; flex-direction: column; }
.public-top { position: absolute; top: 12px; right: 12px; }

.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-content: center;
  gap: 22px;
  padding: 24px;
  max-width: 420px;
  margin: 0 auto;
}
.auth-brand { text-align: center; }
.auth-title { margin: 12px 0 2px; font-size: 26px; }
.auth-tagline { color: var(--muted); margin: 0; }
.auth-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
  box-shadow: var(--shadow);
}
.auth-links { display: flex; justify-content: space-between; font-size: 14px; margin-top: 6px; }

/* ── Shell ───────────────────────────────────────────────────────────── */

.shell { display: flex; flex-direction: column; height: 100vh; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); }
.brand:hover { text-decoration: none; }
.mailbox-select { max-width: 260px; margin-left: 8px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 4px; }
.user-menu { display: flex; align-items: center; gap: 8px; margin-left: 8px; }
.user-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--brand); color: #fff;
  display: grid; place-content: center; font-weight: 700; font-size: 13px;
}
.user-name { font-size: 14px; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.view { flex: 1; overflow: hidden; }

/* ── Mail layout ─────────────────────────────────────────────────────── */

.mail-layout { display: grid; grid-template-columns: 240px 1fr; height: 100%; }

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 14px 12px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 18px;
}
.side-compose {
  width: 100%;
  background: var(--brand); color: #fff; border: none;
  padding: 10px; border-radius: var(--radius);
  font: inherit; font-weight: 600; cursor: pointer;
}
.side-compose:hover { background: var(--brand-600); }
.side-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 2px; }
.side-link {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 10px; border-radius: 8px; color: var(--text);
}
.side-link:hover { background: var(--brand-050); text-decoration: none; }
.side-link.active { background: var(--brand); color: #fff; }
.side-link.active .pill { background: rgb(255 255 255 / 25%); color: #fff; }
.pill {
  margin-left: auto; background: var(--brand); color: #fff;
  border-radius: 999px; padding: 1px 7px; font-size: 12px; font-weight: 600;
}
.pill-quiet { background: var(--border); color: var(--muted); }
.label-dot { width: 10px; height: 10px; border-radius: 50%; }
.side-labels h4 { margin: 0; font-size: 12px; text-transform: uppercase; color: var(--muted); }

.mail-main { display: flex; flex-direction: column; overflow: hidden; }

.list-header { padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.list-title-row { display: flex; align-items: center; gap: 12px; }
.list-title { margin: 0; font-size: 20px; }
.search { max-width: 320px; margin-left: auto; }

.list-actions {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px; border-bottom: 1px solid var(--border);
  background: var(--brand-050); font-weight: 600;
}

/* ── Message rows ────────────────────────────────────────────────────── */

.msg-list { overflow-y: auto; flex: 1; }

.msg-row {
  display: flex; align-items: flex-start; gap: 10px;
  padding: var(--row-gap) 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  background: var(--surface);
}
.msg-row:hover { background: var(--brand-050); }
.msg-row.selected { background: var(--brand-050); }
.msg-row.unread .msg-from, .msg-row.unread .msg-subject { font-weight: 700; }
.msg-check { width: auto; margin-top: 3px; }
.msg-avatar {
  width: 34px; height: 34px; flex: none; border-radius: 50%;
  background: var(--brand-050); color: var(--brand);
  display: grid; place-content: center; font-size: 13px; font-weight: 700;
}
.msg-main { flex: 1; min-width: 0; }
.msg-line { display: flex; justify-content: space-between; gap: 8px; }
.msg-from { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-date { color: var(--muted); font-size: 12px; flex: none; }
.msg-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-snippet { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-tags { display: flex; gap: 4px; }
.tag { font-size: 12px; }
.msg-star { border: none; background: none; cursor: pointer; font-size: 17px; color: var(--muted); }
.msg-star.on { color: #f5a623; }

/* ── Reader ──────────────────────────────────────────────────────────── */

.reader { height: 100%; overflow-y: auto; }
.reader-head { padding: 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.reader-toolbar { display: flex; gap: 4px; margin-bottom: 6px; }
.reader-subject { margin: 0 0 8px; font-size: 22px; }
.reader-meta { display: grid; gap: 3px; }
.hdr { display: flex; gap: 8px; font-size: 14px; }
.hdr-label { color: var(--muted); min-width: 44px; }
.hdr-value { font-weight: 500; }
.reader-body { padding: 20px 16px 60px; max-width: 860px; }
.reader-text { white-space: pre-wrap; font: inherit; margin: 0; }
.reader-html { line-height: 1.6; }

/* Message bodies render in a sandboxed iframe with no allow-same-origin, so the
   frame sizes itself from its content rather than being forced to a height. */
.reader-frame {
  width: 100%;
  min-height: 120px;
  border: 0;
  background: transparent;
  display: block;
}

.attachments { margin-top: 24px; }
.attach-list, .attach-chip { display: flex; gap: 6px; align-items: center; }
.attach-chip {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--radius); padding: 6px 10px; cursor: pointer; font: inherit; font-size: 13px;
}
.attach-img { max-width: 100%; border-radius: var(--radius); margin-top: 8px; }

.thread { margin-top: 28px; border-top: 1px solid var(--border); padding-top: 16px; }
.thread-item {
  display: grid; gap: 2px; width: 100%; text-align: left;
  border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--radius); padding: 9px 11px; margin-bottom: 8px;
  cursor: pointer; font: inherit;
}
.thread-item.current { border-color: var(--brand); }
.thread-from { font-weight: 600; }
.thread-date, .thread-snip { color: var(--muted); font-size: 13px; }
.thread-snip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Compose ─────────────────────────────────────────────────────────── */

.compose-wrap { height: 100%; overflow-y: auto; padding: 16px; }
.compose-title { margin: 0 0 14px; font-size: 20px; }
.compose { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 18px; max-width: 900px; }
.compose-body { min-height: 260px; font-family: inherit; }
.attach-input { display: none; }
.attach-list { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.compose-attachments { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.schedule-wrap { display: inline-block; }
.compose-actions { display: flex; gap: 8px; align-items: center; }
.compose-status { margin-top: 10px; }

/* ── Settings / admin ────────────────────────────────────────────────── */

.settings, .admin { height: 100%; overflow-y: auto; padding: 18px; max-width: 900px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 18px; margin-bottom: 16px;
}
.card-title { margin: 0 0 12px; font-size: 17px; }
.session-row, .activity-item {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 0; border-bottom: 1px solid var(--border);
}
.session-row > :first-child, .activity-item > :first-child { flex: 1; }

.admin-tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.admin-tab-btn { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 14px; cursor: pointer; font: inherit; }
.admin-tab-btn.active { background: var(--brand); color: #fff; border-color: var(--brand); }

/* Create/edit rows in the admin console, and the scrollable route/audit panes. */
.form-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-top: 14px; }
.form-row .field { flex: 1 1 160px; margin: 0; }
.form-row .btn { flex: 0 0 auto; }
.domain-picker { max-width: 320px; }
.routes-body, .audit-body { margin-top: 12px; }
.admin-tab code { font-size: 12px; background: var(--brand-050); border-radius: 4px; padding: 1px 5px; }
.table td .muted.small { display: block; }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 18px; }
.stat { background: var(--brand-050); border-radius: var(--radius); padding: 12px; text-align: center; }
.stat-value { font-size: 22px; font-weight: 700; color: var(--brand); }
.stat-label { font-size: 13px; color: var(--muted); }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); }
.table th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.table td select { width: auto; min-width: 110px; padding: 4px 8px; }

/* ── Toasts & modal ──────────────────────────────────────────────────── */

.toasts { position: fixed; bottom: 18px; right: 18px; display: grid; gap: 8px; z-index: 50; }
.toast {
  background: var(--text); color: var(--bg);
  padding: 10px 14px; border-radius: var(--radius);
  box-shadow: var(--shadow); font-size: 14px; max-width: 320px;
  animation: toast-in .18s ease;
}
.toast-success { background: var(--success); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
.toast-out { opacity: 0; transition: opacity .4s ease; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

.modal-backdrop {
  position: fixed; inset: 0; background: rgb(16 21 34 / 45%);
  display: grid; place-content: center; z-index: 60;
}
.modal {
  background: var(--surface); border-radius: var(--radius-lg);
  padding: 22px; width: min(90vw, 400px); box-shadow: var(--shadow);
}
.modal-title { margin: 0 0 16px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

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

@media (max-width: 760px) {
  .mail-layout { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: auto 0 0 0; height: auto; max-height: 45vh; z-index: 10; border-right: none; border-top: 1px solid var(--border); }
  .mail-main { padding-bottom: 45vh; }
  .user-name, .search { display: none; }
  .mailbox-select { max-width: 150px; }
}