:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --danger: #dc2626;
  --warn: #d97706;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(2, 6, 23, 0.12);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--surface-2);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 18px; margin: 6px 0 0; font-size: 13px; }

/* ---------- thin scrollbars for internal scroll areas ---------- */
.mailboxes, .mails, .mail-view .modal-body, .settings-layout {
  scrollbar-width: thin;
  scrollbar-color: #cbd5e1 transparent;
}
.mailboxes::-webkit-scrollbar,
.mails::-webkit-scrollbar,
.mail-view .modal-body::-webkit-scrollbar,
.settings-layout::-webkit-scrollbar {
  width: 8px;
}
.mailboxes::-webkit-scrollbar-track,
.mails::-webkit-scrollbar-track,
.mail-view .modal-body::-webkit-scrollbar-track,
.settings-layout::-webkit-scrollbar-track {
  background: transparent;
}
.mailboxes::-webkit-scrollbar-thumb,
.mails::-webkit-scrollbar-thumb,
.mail-view .modal-body::-webkit-scrollbar-thumb,
.settings-layout::-webkit-scrollbar-thumb {
  background: #cbd5e1; border-radius: 999px;
}
.mailboxes::-webkit-scrollbar-thumb:hover,
.mails::-webkit-scrollbar-thumb:hover,
.mail-view .modal-body::-webkit-scrollbar-thumb:hover,
.settings-layout::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

/* ---------- buttons ---------- */
.btn {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all .15s ease;
  background: var(--surface-2);
  color: var(--text);
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.icon-btn {
  border: none; background: transparent; cursor: pointer;
  font-size: 15px; padding: 4px 6px; border-radius: 8px; color: var(--muted);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
/* Danger icon buttons stay visibly red at rest (not just on hover) so a
   destructive action like "delete mailbox" doesn't blend in with the plain
   icon buttons next to it. */
.icon-btn-danger { color: var(--danger); background: #fee2e2; }
.icon-btn-danger:hover { background: #fecaca; }
.icon-btn-lg {
  width: 32px; height: 32px; padding: 0; font-size: 16px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
}

/* compact square icon button for space-constrained rows (e.g. apply mailbox) */
.icon-btn-primary {
  flex: 0 0 auto; width: 38px; height: 38px;
  border: none; border-radius: 10px; background: var(--primary); color: #fff;
  font-size: 20px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s ease;
}
.icon-btn-primary:hover { background: var(--primary-dark); }
.icon-btn-primary:disabled { opacity: .6; cursor: default; }

.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; font-weight: 600; }
.badge-warn { background: #fef3c7; color: var(--warn); }

/* ---------- auth ---------- */
.auth-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #4f46e5, #0f172a);
  padding: 20px;
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border-radius: 16px;
  padding: 32px;
  box-shadow: var(--shadow);
}
.brand { text-align: center; margin-bottom: 22px; }
.brand h1 { font-size: 22px; margin: 12px 0 2px; }
.brand-mark {
  width: 52px; height: 52px; margin: 0 auto;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #6366f1, #4338ca);
  color: #fff; font-size: 26px; border-radius: 14px;
}
.brand-mark.sm { width: 30px; height: 30px; font-size: 16px; border-radius: 8px; }
.tabs { display: flex; gap: 8px; margin-bottom: 18px; background: var(--surface-2); padding: 4px; border-radius: 10px; }
.tab {
  flex: 1; border: none; background: transparent; padding: 8px; border-radius: 8px;
  cursor: pointer; font-weight: 600; color: var(--muted);
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
form label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 12px; }
form input {
  width: 100%; margin-top: 6px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px; font-size: 14px;
}
form input:focus { outline: 2px solid #c7d2fe; border-color: var(--primary); }

/* ---------- topbar ---------- */
.topbar {
  height: 60px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: 10px; font-size: 16px; }
.topbar-right { display: flex; align-items: center; gap: 14px; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 300px 360px 1fr;
  gap: 18px;
  padding: 18px;
  max-width: 1600px;
  margin: 0 auto;
  height: calc(100vh - 60px); /* 60px = .topbar height; panels stretch to fill it */
  box-sizing: border-box;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.panel-head h2 { font-size: 16px; margin: 0; }

/* apply-row: select + a compact icon button, one row — the icon button never
   wraps regardless of domain label length or column width. */
.apply-row {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 18px; border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.apply-row select {
  flex: 1 1 auto; min-width: 0;
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px;
  font-size: 13px; font-weight: 600; background: var(--surface-2); color: var(--text);
}

/* ---------- domain filter chips ---------- */
/* Horizontally-scrolling strip, not wrap: keeps header height constant no
   matter how many domains get added later. No visible scrollbar — the
   prev/next arrow buttons drive it (scrollIntoView-free scrollBy). */
.domain-filter-wrap {
  display: flex; align-items: center; gap: 2px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.domain-filter {
  display: flex; flex-wrap: nowrap; gap: 6px; padding: 10px 6px;
  overflow-x: auto; overflow-y: hidden;
  flex: 1 1 auto; min-width: 0;
  scrollbar-width: none;
}
.domain-filter::-webkit-scrollbar { display: none; }
.chip-nav {
  flex: 0 0 auto; border: none; background: transparent; color: var(--muted);
  font-size: 20px; line-height: 1; cursor: pointer; padding: 6px 8px;
  border-radius: 8px; margin: 0 2px;
}
.chip-nav:hover { background: var(--surface-2); color: var(--text); }
.chip-nav:disabled { opacity: .3; cursor: default; }
.chip-nav:disabled:hover { background: transparent; }
.chip {
  flex: 0 0 auto; white-space: nowrap;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--muted);
  border-radius: 999px; padding: 5px 12px; font-size: 12px; font-weight: 600; cursor: pointer;
}
.chip:hover { background: #eef2ff; }
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.mb-domain {
  display: inline-block; font-size: 11px; font-weight: 700; color: var(--primary);
  background: #eef2ff; border-radius: 6px; padding: 1px 6px; margin-right: 6px;
}

.sync-status { font-size: 12px; white-space: nowrap; }
.sync-status .dot { color: #16a34a; }
.switch { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; user-select: none; }
.switch input { width: 34px; height: 18px; appearance: none; background: #cbd5e1; border-radius: 999px; position: relative; cursor: pointer; transition: background .15s; flex-shrink: 0; }
.switch input:checked { background: var(--primary); }
.switch input::after { content: ''; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; background: #fff; border-radius: 50%; transition: transform .15s; }
.switch input:checked::after { transform: translateX(16px); }

/* ---------- mailbox list ---------- */
.mailboxes { flex: 1 1 auto; overflow-y: auto; min-height: 0; }
.mailbox-item {
  position: relative;
  padding: 14px 34px 14px 18px; /* extra right padding reserves room for the corner badge */
  border-bottom: 1px solid var(--border); cursor: pointer;
  display: flex; flex-direction: column; gap: 4px;
}
.mailbox-item:hover { background: var(--surface-2); }
.mailbox-item.active { background: #eef2ff; border-left: 3px solid var(--primary); }
.mailbox-item .meta-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.mailbox-item .addr { font-weight: 600; font-size: 14px; word-break: break-all; }
.mailbox-item .sub { font-size: 12px; color: var(--muted); }
.list-empty { padding: 28px 18px; text-align: center; color: var(--muted); font-size: 14px; }

/* WeChat-style unread-count badge, pinned to the item's corner. Shown
   whenever the mailbox has been synced at least once, even at 0 unread
   (see renderMailboxList / unreadCount). */
.mb-badge {
  position: absolute; top: 12px; right: 10px;
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 999px; background: #ef4444; color: #fff;
  font-size: 11px; font-weight: 700; line-height: 18px; text-align: center;
  box-shadow: 0 0 0 2px var(--surface);
}
.mailbox-item.active .mb-badge { box-shadow: 0 0 0 2px #eef2ff; }
.mb-badge.is-zero { background: #cbd5e1; color: #475569; }

/* ---------- detail ---------- */
.empty { flex: 1 1 auto; display: flex; align-items: center; justify-content: center; text-align: center; color: var(--muted); padding: 24px; }
#detail-content, #mail-content { display: flex; flex-direction: column; height: 100%; min-height: 0; }

/* detail-head: four uniform label/value/actions rows (收件箱, 邮箱地址, 密码,
   同步) — each control lives next to the row it belongs to. */
.detail-head { display: flex; flex-direction: column; flex: 0 0 auto; }
.info-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 18px; border-bottom: 1px solid var(--border);
}
.info-label { flex: 0 0 60px; font-size: 12px; font-weight: 700; color: var(--muted); }
.info-spacer { flex: 1 1 auto; }
.info-value {
  flex: 1 1 auto; min-width: 0; font-size: 14px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
code.info-value {
  background: var(--surface-2); padding: 3px 10px; border-radius: 8px;
  border: 1px solid var(--border); font-size: 13px;
}
.info-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }

/* ---------- mails ---------- */
.mails { flex: 1 1 auto; overflow-y: auto; overflow-x: hidden; min-height: 0; }
.mail-item {
  padding: 14px 18px; border-bottom: 1px solid var(--border); cursor: pointer;
  display: flex; flex-direction: column; gap: 3px;
  overflow: hidden; /* long unbroken sender/subject strings never force horizontal scroll */
}
.mail-item:hover { background: var(--surface-2); }
.mail-item.active { background: #eef2ff; border-left: 3px solid var(--primary); }
.mail-item .row1 { display: flex; justify-content: space-between; gap: 12px; min-width: 0; }
/* Truncated here on purpose — the right-hand column already shows the full
   sender/subject, so the middle list doesn't need to wrap or scroll for it. */
.mail-item .from {
  font-weight: 600; font-size: 13px; color: var(--text);
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.mail-item .date { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.mail-item .subject { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-item .preview { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mail-del { align-self: flex-end; }
.pager { display: flex; align-items: center; justify-content: center; gap: 14px; padding: 14px; }

/* ---------- mail content column ---------- */
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 16px; word-break: break-word; }
.modal-meta { border-bottom: 1px solid var(--border); font-size: 13px; display: flex; flex-direction: column; gap: 6px; }
.modal-body { font-size: 14px; line-height: 1.6; }
.mail-view .modal-head { padding: 16px 18px; flex: 0 0 auto; }
.mail-view .modal-head .btn { flex-shrink: 0; }
.mail-view .modal-meta { padding: 14px 18px; flex: 0 0 auto; }
.mail-view .modal-body { padding: 18px; flex: 1 1 auto; overflow-y: auto; min-height: 0; }
.modal-body img { max-width: 100%; height: auto; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; }
.attachments .att { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 4px 10px; font-size: 12px; }
.mail-frame { width: 100%; border: none; min-height: 300px; }

/* ---------- safe mail reading (privacy) ---------- */
.safe-mail-root {
  display: flex; flex-direction: column; gap: 12px;
  min-height: 0;
}

.safe-privacy-banner {
  flex: 0 0 auto;
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 12px;
  background: #eef2ff;
  border: 1px solid #c7d2fe;
  border-radius: 10px;
  padding: 10px 12px;
}
.safe-privacy-title {
  font-size: 12px; font-weight: 700; color: var(--primary);
  background: #fff; border-radius: 6px; padding: 2px 8px;
  border: 1px solid #c7d2fe;
}
.safe-privacy-desc {
  flex: 1 1 200px; font-size: 12px; color: #334155; line-height: 1.5;
}

/*
 * Mail body: let original email CSS work. Only light containment —
 * do NOT force max-height:none / width:auto on all tables (destroys layout).
 */
.safe-body-content {
  flex: 0 0 auto;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: visible;
  word-wrap: break-word;
  /* isolate email styles from the app chrome a bit */
  font-size: 14px;
  line-height: 1.5;
  color: #0f172a;
}
.safe-body-content img { max-width: 100%; height: auto; }
.safe-plain {
  margin: 0; white-space: pre-wrap; font-family: inherit; font-size: 14px;
}

/* Copy-only links: keep author colors/underline; only change cursor + hover hint */
.safe-body-content a.safe-inline-link,
.safe-body-content .safe-inline-link {
  cursor: copy;
}
.safe-body-content a.safe-inline-link:hover {
  outline: 2px dashed #4f46e5;
  outline-offset: 2px;
}

/* Remote image placeholders (layout-preserving) */
.safe-img-ph {
  max-width: 100%;
}

/* ---------- confirm / prompt dialog ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.55);
  display: grid; place-items: center; z-index: 50; padding: 20px;
}
.modal-card {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 720px;
  max-height: 86vh; display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.modal-card.sm { max-width: 420px; }
.modal-card .modal-head { padding: 16px 20px; }
.modal-card .modal-meta { padding: 14px 20px; }
.modal-card .modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--border); }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 11px 20px; border-radius: 10px;
  font-size: 14px; box-shadow: var(--shadow); z-index: 100;
}

/* ---------- domain settings page ---------- */
.settings-layout { max-width: 900px; margin: 0 auto; padding: 24px 18px; }
.settings-hint { padding: 0 18px; margin: 14px 0; font-size: 13px; line-height: 1.6; }
.settings-hint code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; }
.domain-table { padding-bottom: 4px; }
.domain-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.domain-row:last-child { border-bottom: none; }
.domain-row-main { min-width: 0; }
.domain-row-title { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.domain-row-meta { font-size: 13px; margin-top: 4px; word-break: break-all; }
.domain-row-actions { display: flex; gap: 8px; flex-shrink: 0; }

@media (max-width: 1300px) {
  .layout { grid-template-columns: 260px 320px 1fr; }
}

@media (max-width: 980px) {
  /* Stacked single column: let the page scroll naturally instead of locking
     three panels into one viewport-height box. */
  .layout { grid-template-columns: 1fr; height: auto; }
  .mailboxes { flex: none; max-height: 280px; }
  .mails { flex: none; max-height: 320px; }
  /* Keep body scrollable with enough room; avoid hard-clipping long mail */
  .mail-view .modal-body { flex: none; max-height: min(70vh, 720px); }
}
