:root {
  --brand: #1a5fb4;
  --brand-strong: #154e95;
  --brand-soft: #e6effa;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8f9fa;
  --hover: #eef1f5;
  --selected: #dce8f8;
  --border: #e1e4e8;
  --text: #1d2125;
  --muted: #5f6873;
  --danger: #c4314b;
  --warn-bg: #fff4ce;
  --warn-text: #5c4400;
  --success: #107c41;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.18);
  --radius: 8px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --brand: #4a90e2;
    --brand-strong: #6aa5ea;
    --brand-soft: #1f3550;
    --bg: #17191c;
    --surface: #1f2226;
    --surface-2: #26292e;
    --hover: #2b2f35;
    --selected: #24384f;
    --border: #353a41;
    --text: #e8eaed;
    --muted: #a0a7b0;
    --danger: #f1707b;
    --warn-bg: #3d3214;
    --warn-text: #f5d77a;
    --success: #5cc48a;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.45 var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--brand); }
h1, h2 { margin: 0; font-weight: 600; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.grow { flex: 1; min-width: 0; }
.strong { font-weight: 600; }
.mono { font-family: ui-monospace, Consolas, monospace; }
.icon { flex-shrink: 0; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
  font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-strong); border-color: var(--brand-strong); }
.btn-danger.btn-primary, .btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-quiet { border-color: transparent; background: transparent; }
.btn-block { width: 100%; justify-content: center; height: 38px; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; border: 0; border-radius: 6px;
  background: transparent; color: inherit; flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn.small { width: 26px; height: 26px; }
.link-btn { border: 0; background: none; color: var(--brand); padding: 0 4px; font-weight: 500; }
.link-btn:hover { text-decoration: underline; }
.input {
  width: 100%; height: 36px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface);
}
.input:focus, .search-input:focus { outline: 2px solid var(--brand); outline-offset: -1px; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; font-weight: 600; }
.check { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.check input { width: 16px; height: 16px; }
.row { display: flex; gap: 8px; align-items: center; }
.addr-row .input { flex: 1; }

/* ---------- Sign-in ---------- */
.auth { display: grid; place-items: center; min-height: 100vh; padding: 16px; overflow: auto; height: 100%; }
.auth-card {
  width: min(400px, 100%); padding: 32px 28px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
}
.auth-card h1 { font-size: 22px; margin: 12px 0 2px; }
.auth-card > .muted { margin: 0 0 22px; }
.brand-mark { width: 48px; height: 48px; border-radius: 12px; display: grid; place-items: center; background: var(--brand); color: #fff; }
.error-text { color: var(--danger); min-height: 1.2em; margin: 4px 0 12px; }

/* ---------- Top bar ---------- */
.topbar {
  height: 48px; display: flex; align-items: center; gap: 12px; padding: 0 12px;
  background: var(--brand); color: #fff;
}
@media (prefers-color-scheme: dark) { .topbar { background: #1b2b40; } }
.brand { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; font-weight: 600; font-size: 16px; width: 216px; }
.nav-toggle { display: none; color: #fff; }
.nav-toggle:hover { background: rgba(255,255,255,0.15); }
.search {
  flex: 1; max-width: 560px; display: flex; align-items: center; gap: 8px;
  height: 32px; padding: 0 10px; border-radius: 6px; background: rgba(255,255,255,0.92); color: #333;
}
.search-input { flex: 1; border: 0; background: transparent; height: 100%; color: #1d2125; min-width: 0; }
.search-input:focus { outline: none; }
.search:focus-within { background: #fff; box-shadow: 0 0 0 2px rgba(255,255,255,0.5); }
.topbar-right { margin-left: auto; display: flex; align-items: center; }
.user-btn { border: 0; background: none; padding: 2px; border-radius: 50%; }
.user-btn:hover { box-shadow: 0 0 0 2px rgba(255,255,255,0.5); }

.avatar {
  display: inline-grid; place-items: center; border-radius: 50%;
  color: #fff; font-weight: 600; flex-shrink: 0; letter-spacing: 0.02em;
}

/* ---------- Layout ---------- */
.shell {
  display: grid;
  grid-template-columns: 232px minmax(300px, 380px) 1fr;
  height: calc(100vh - 48px);
  height: calc(100dvh - 48px);
}
.sidebar-wrap { background: var(--surface-2); border-right: 1px solid var(--border); overflow-y: auto; padding: 12px 8px; }
.new-mail { width: 100%; justify-content: center; height: 36px; margin-bottom: 12px; }
.list-pane { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.reading-pane { background: var(--surface); overflow-y: auto; min-width: 0; }
.admin-pane { grid-column: 2 / 4; overflow-y: auto; background: var(--surface); }
body.admin-mode .list-pane, body.admin-mode .reading-pane { display: none; }
.fab { display: none; }
.scrim { display: none; }

/* ---------- Notifications ---------- */
.notif-banner {
  margin: 0 0 12px; padding: 10px 12px; border-radius: var(--radius);
  background: var(--brand-soft); border: 1px solid color-mix(in srgb, var(--brand) 25%, transparent);
}
.notif-banner-head { display: flex; align-items: center; gap: 8px; color: var(--brand); }
.notif-banner-head strong { flex: 1; font-size: 13px; color: var(--text); }
.notif-banner .notif-state { display: none; }
.notif-banner .row { margin-top: 8px; }
.notif-banner .btn { height: 30px; }
.notif-box .notif-state { margin: 0 0 8px; color: var(--muted); font-size: 13px; }
.notif-box .row { flex-wrap: wrap; }

/* ---------- Sidebar ---------- */
.mailbox { margin-bottom: 10px; }
.mailbox-head { display: flex; align-items: center; }
.mailbox-toggle {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px;
  border: 0; background: none; padding: 6px 4px; font-weight: 600; text-align: left;
}
.mailbox-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folders { list-style: none; margin: 0; padding: 0; }
.folder {
  display: flex; align-items: center; gap: 10px; padding: 0 8px 0 22px; height: 34px;
  border-radius: 6px; color: var(--text); text-decoration: none;
}
.folder:hover { background: var(--hover); }
.folder.active { background: var(--selected); font-weight: 600; }
.folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder.has-unread .folder-name { font-weight: 600; }
.count { color: var(--brand); font-weight: 600; font-size: 12px; }
.folder-more { opacity: 0; }
.folder:hover .folder-more, .folder-more:focus { opacity: 1; }
li.drop .folder { background: var(--brand-soft); outline: 2px dashed var(--brand); }

/* ---------- Message list ---------- */
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 12px 8px 16px; border-bottom: 1px solid var(--border); }
.list-title { min-width: 0; }
.list-title h1 { font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-tools { display: flex; align-items: center; gap: 4px; }
.filter-select { height: 30px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); padding: 0 6px; }
.items { flex: 1; overflow-y: auto; }
.list-empty { padding: 48px 16px; text-align: center; color: var(--muted); }
.item {
  position: relative; display: flex; gap: 12px; padding: 10px 12px 10px 14px;
  border-bottom: 1px solid var(--border); cursor: pointer; border-left: 3px solid transparent;
}
.item:hover { background: var(--hover); }
.item.selected { background: var(--selected); }
.item.unread { border-left-color: var(--brand); }
.item.unread .item-from, .item.unread .item-subject { font-weight: 600; }
.item.unread .item-subject { color: var(--brand); }
.item.flagged { background: color-mix(in srgb, #ffd60a 10%, var(--surface)); }
.item.flagged.selected { background: var(--selected); }
.item-main { flex: 1; min-width: 0; }
.item-row { display: flex; align-items: baseline; gap: 8px; }
.item-from, .item-subject { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.item.unread .item-date { color: var(--brand); font-weight: 600; }
.item-icons { display: flex; gap: 4px; color: var(--muted); }
.flag-on { color: #c50f1f; display: inline-flex; }
.item-snippet { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-tag { display: inline-block; font-size: 11px; padding: 0 6px; border-radius: 4px; background: var(--hover); color: var(--muted); margin-right: 6px; }
.item-quick {
  position: absolute; right: 8px; top: 30px; display: none; gap: 2px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 2px;
}
.item:hover .item-quick { display: flex; }
.load-more { margin: 12px auto; display: flex; }

/* ---------- Reading pane ---------- */
.reading-empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--muted); padding: 24px; text-align: center; }
.big-count { font-size: 40px; font-weight: 600; color: var(--brand); }
.bulk-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 8px; }
.reading-toolbar {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
  padding: 6px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.back-btn { display: none; }
.tool-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 8px;
  border: 0; border-radius: 6px; background: transparent;
}
.tool-btn:hover { background: var(--hover); }
.tool-sep { width: 1px; height: 20px; background: var(--border); margin: 0 6px; }
.reading { padding: 20px 24px 40px; max-width: 1100px; }
.reading-subject { font-size: 20px; line-height: 1.3; margin-bottom: 16px; display: flex; gap: 8px; align-items: center; overflow-wrap: anywhere; }
.sender { display: flex; gap: 12px; margin-bottom: 14px; }
.sender-info { min-width: 0; }
.sender-name { font-weight: 600; overflow-wrap: anywhere; }
.sender-name .muted { font-weight: 400; }
.recipients { font-size: 13px; overflow-wrap: anywhere; }
.banner {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; margin-bottom: 12px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); font-size: 13px;
}
.banner-warn { background: var(--warn-bg); color: var(--warn-text); border-color: transparent; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.att-card {
  display: flex; align-items: center; gap: 4px; padding: 4px 4px 4px 10px; max-width: 280px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface);
}
.att-card:hover { border-color: var(--brand); }
.att-open { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; min-width: 0; flex: 1; }
.att-text { display: flex; flex-direction: column; min-width: 0; }
.att-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.att-size { font-size: 11px; color: var(--muted); }
.att-card .icon-btn { color: var(--muted); }
.mail-frame-wrap { background: #fff; border-radius: 6px; }
@media (prefers-color-scheme: dark) { .mail-frame-wrap { padding: 12px; } }
.mail-frame { width: 100%; border: 0; display: block; min-height: 120px; }
.thread { margin-top: 28px; border-top: 1px solid var(--border); padding-top: 16px; }
.thread h2 { font-size: 14px; margin-bottom: 8px; }
.thread-item {
  width: 100%; display: grid; grid-template-columns: 28px minmax(80px, 180px) 1fr auto; align-items: center; gap: 10px;
  padding: 8px; border: 0; border-radius: 6px; background: none; text-align: left;
}
.thread-item:hover { background: var(--hover); }
.thread-from { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-snippet { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Compose ---------- */
.compose-backdrop {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: rgba(15, 23, 42, 0.35); padding: 16px;
}
.compose {
  width: min(920px, 100%); height: min(88vh, 900px); display: flex; flex-direction: column;
  background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); overflow: hidden;
}
.compose.dropping { outline: 3px dashed var(--brand); outline-offset: -6px; }
.compose-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 0 18px; }
.compose-head h2 { font-size: 16px; }
.compose-actions { display: flex; gap: 8px; padding: 10px 18px; flex-wrap: wrap; }
.compose-fields { padding: 0 18px; }
.rcpt-row { position: relative; display: flex; align-items: center; gap: 8px; min-height: 40px; border-bottom: 1px solid var(--border); }
.rcpt-label { width: 44px; color: var(--muted); flex-shrink: 0; }
.rcpt-field { flex: 1; display: flex; flex-wrap: wrap; align-items: center; gap: 4px; padding: 4px 0; min-width: 0; cursor: text; }
.rcpt-input { flex: 1; min-width: 160px; border: 0; background: transparent; height: 28px; }
.rcpt-input:focus, .subject-input:focus { outline: none; }
.rcpt-toggles { display: flex; gap: 4px; }
.from-select { border: 0; height: 36px; padding: 0; background: transparent; flex: 1; min-width: 0; }
.chips { display: contents; }
.chip {
  display: inline-flex; align-items: center; gap: 2px; max-width: 100%;
  padding: 2px 2px 2px 8px; border-radius: 12px; background: var(--brand-soft); font-size: 13px;
}
.chip-bad { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }
.chip-x { display: inline-grid; place-items: center; width: 20px; height: 20px; border: 0; border-radius: 50%; background: none; color: inherit; padding: 0; }
.chip-x:hover { background: rgba(0,0,0,0.1); }
.suggest {
  position: absolute; left: 52px; top: 100%; z-index: 5; width: min(420px, calc(100% - 52px));
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); padding: 4px;
}
.suggest-item { display: flex; flex-direction: column; padding: 6px 10px; border-radius: 6px; cursor: pointer; }
.suggest-item span { color: var(--muted); font-size: 12px; }
.suggest-item.active, .suggest-item:hover { background: var(--hover); }
.subject-input { flex: 1; border: 0; background: transparent; height: 40px; font-size: 15px; font-weight: 500; }
.compose-attachments { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 18px 0; }
.att-chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 4px 3px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; max-width: 280px; }
.att-chip .att-name { flex: 1; }
.size-note { color: var(--danger); font-size: 13px; padding: 4px 18px 0; }
.size-note:empty { display: none; }
.editor-toolbar { display: flex; align-items: center; gap: 2px; padding: 6px 14px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.tool { width: 32px; height: 30px; border: 0; border-radius: 4px; background: none; display: inline-grid; place-items: center; font-size: 15px; }
.tool:hover { background: var(--hover); }
.editor-wrap { flex: 1; overflow-y: auto; }
.editor { padding: 14px 18px; outline: none; overflow-wrap: anywhere; }
.editor img { max-width: 100%; }
.editor blockquote { margin: 0 0 0 .8ex; border-left: 2px solid var(--border); padding-left: 1ex; }
.signature-editor { border: 1px solid var(--border); border-radius: 6px; }
.signature-editor .editor { max-height: 220px; overflow-y: auto; }
body.composing .fab { display: none !important; }

/* ---------- Dialogs, menus, toasts ---------- */
.dialog {
  border: 0; padding: 0; border-radius: 10px; box-shadow: var(--shadow);
  width: min(460px, calc(100vw - 32px)); background: var(--surface); color: var(--text);
}
.dialog-wide { width: min(640px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(15, 23, 42, 0.35); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 0 20px; }
.dialog-head h2 { font-size: 17px; }
.dialog-body { padding: 16px 20px; max-height: 70vh; overflow-y: auto; }
.dialog-body p { margin: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 0 20px 18px; }
.pw-section summary { cursor: pointer; font-weight: 600; margin-bottom: 12px; }
.member-list { border: 1px solid var(--border); border-radius: 6px; padding: 2px 12px; max-height: 220px; overflow-y: auto; }

.menu {
  position: fixed; z-index: 100; min-width: 200px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
}
.menu-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; border: 0; border-radius: 6px; background: none; text-align: left; }
.menu-item:hover:not(:disabled), .menu-item:focus-visible { background: var(--hover); outline: none; }
.menu-item:disabled { color: var(--muted); cursor: default; font-weight: 600; }
.menu-item.danger { color: var(--danger); }
.menu-icon-space { width: 16px; }
.menu-sep { height: 1px; background: var(--border); margin: 4px 0; }

.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: #2b2f35; color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); max-width: min(520px, calc(100vw - 32px)); transition: opacity .3s; }
.toast-error { background: var(--danger); }
.toast-success { background: var(--success); }
.toast.out { opacity: 0; }

/* ---------- Admin ---------- */
.admin { padding: 24px 28px 48px; max-width: 900px; }
.admin h1 { font-size: 22px; }
.admin > .muted { margin: 4px 0 18px; }
.admin-loading { padding: 40px; color: var(--muted); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tab { border: 0; background: none; padding: 10px 14px; border-bottom: 2px solid transparent; font-weight: 500; white-space: nowrap; }
.tab.active { border-bottom-color: var(--brand); color: var(--brand); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.section-head h2 { font-size: 16px; }
.table { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin-top: 10px; }
.table-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.table-row:last-child { border-bottom: 0; }
.table-row.is-disabled { opacity: 0.6; }
.badge { display: inline-block; margin-left: 8px; padding: 0 8px; border-radius: 10px; background: var(--brand-soft); color: var(--brand); font-size: 11px; font-weight: 600; vertical-align: middle; }
.badge-muted { background: var(--hover); color: var(--muted); }
.empty-small { color: var(--muted); padding: 18px 0; }

/* ---------- Phones & small tablets ---------- */
@media (max-width: 899px) {
  .shell { grid-template-columns: 1fr; }
  .nav-toggle { display: inline-grid; }
  .brand { width: auto; }
  .brand span { display: none; }
  .sidebar-wrap {
    position: fixed; z-index: 40; top: 48px; bottom: 0; left: 0; width: min(300px, 85vw);
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  body.nav-open .sidebar-wrap { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 48px 0 0 0; z-index: 39; background: rgba(0,0,0,0.3); }
  .list-pane { border-right: 0; }
  .reading-pane { display: none; }
  body.show-reading .list-pane { display: none; }
  body.show-reading .reading-pane { display: block; }
  body.show-reading .fab { display: none; }
  .back-btn { display: inline-grid; }
  .admin-pane { grid-column: 1; }
  .tool-label { display: none; }
  .tool-btn { padding: 0 9px; }
  .reading { padding: 16px; }
  .item-quick { display: none !important; }
  .fab {
    display: grid; place-items: center; position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom));
    width: 56px; height: 56px; border-radius: 16px; border: 0; z-index: 30;
    background: var(--brand); color: #fff; box-shadow: var(--shadow);
  }
  .compose-backdrop { padding: 0; }
  .compose { width: 100%; height: 100%; border-radius: 0; }
  .thread-item { grid-template-columns: 28px 1fr auto; }
  .thread-snippet { display: none; }
  .admin { padding: 18px 16px 40px; }
}
@media (min-width: 900px) and (max-width: 1180px) {
  .shell { grid-template-columns: 200px minmax(280px, 330px) 1fr; }
  .brand { width: 184px; }
  .tool-label { display: none; }
}
