:root {
  --bg: #0e1116;
  --bg-2: #141922;
  --bg-3: #1b2230;
  --bg-hover: #1f2836;
  --line: #232c3b;
  --line-soft: #1a212c;
  --text: #e6ecf5;
  --text-2: #97a3b6;
  --text-3: #63708a;
  --accent: #4f8cff;
  --accent-soft: rgba(79, 140, 255, 0.14);
  --danger: #ff6b6b;
  --star: #f5b942;
  --radius: 10px;
  --radius-lg: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --bg-2: #f7f9fc;
    --bg-3: #eef2f8;
    --bg-hover: #e9eff7;
    --line: #dde4ee;
    --line-soft: #e9eef5;
    --text: #131822;
    --text-2: #56637a;
    --text-3: #8593a8;
    --accent: #2563eb;
    --accent-soft: rgba(37, 99, 235, 0.1);
    --danger: #d92d20;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ───────────────────────────────────────────────── buttons ── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  cursor: pointer;
  font-weight: 550;
  line-height: 1.2;
  transition: background 0.12s, border-color 0.12s, opacity 0.12s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-ghost { color: var(--text-2); border-color: var(--line); }
.btn-ghost:hover:not(:disabled) { background: var(--bg-hover); color: var(--text); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; border-radius: 8px; }
.btn-block { width: 100%; }
.btn.danger { color: var(--danger); }
.btn.danger:hover:not(:disabled) { background: rgba(255, 107, 107, 0.12); }

.icon-btn {
  display: grid;
  place-items: center;
  width: 32px; height: 32px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.link-btn {
  border: 0; background: none; color: var(--accent);
  cursor: pointer; padding: 0 4px; font-size: 12.5px; white-space: nowrap;
}
.link-btn:hover { text-decoration: underline; }

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

.auth {
  display: grid; place-items: center;
  min-height: 100%;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 50% -10%, var(--accent-soft), transparent 70%),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 34px 30px 28px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.25);
}
.auth-mark {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 12px;
  background: var(--accent);
  color: #fff; font-size: 20px;
  margin-bottom: 18px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 21px; letter-spacing: -0.02em; }
.auth-sub { margin: 0 0 24px; color: var(--text-2); font-size: 13.5px; }

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  font-size: 12px; font-weight: 600;
  color: var(--text-2);
  margin-bottom: 6px;
}
.field > span em {
  font-style: normal; font-weight: 500;
  color: var(--text-3);
  text-transform: none;
}
.field input, .field select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  outline: none;
  transition: border-color 0.12s, box-shadow 0.12s;
}
.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.auth-error {
  background: rgba(255, 107, 107, 0.12);
  border: 1px solid rgba(255, 107, 107, 0.35);
  color: var(--danger);
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 14px;
}
.auth-hint {
  margin: 16px 0 0;
  text-align: center;
  font-size: 12.5px;
  color: var(--text-3);
  line-height: 1.5;
}

.invite-address {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px 14px;
  font-family: var(--mono);
  font-size: 13.5px;
  text-align: center;
  margin-bottom: 18px;
  word-break: break-all;
}

/* one-time secret reveal (invite links, app passwords) */
.secret {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-3);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin: 10px 0;
}
.secret code {
  flex: 1; min-width: 0;
  font-family: var(--mono); font-size: 12.5px;
  word-break: break-all; user-select: all;
}
.secret .btn { flex: none; }

.pill {
  font-size: 10.5px; font-weight: 650;
  padding: 2px 7px; border-radius: 20px;
  text-transform: uppercase; letter-spacing: 0.03em;
  background: var(--bg-3); color: var(--text-3);
}
.pill.pending  { background: rgba(79,140,255,.16); color: var(--accent); }
.pill.accepted { background: rgba(52,199,89,.16); color: #34c759; }
.pill.expired, .pill.revoked { background: rgba(255,107,107,.14); color: var(--danger); }

.setting-note {
  background: var(--bg-3);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text-2);
}
.setting-note table { width: 100%; border-collapse: collapse; }
.setting-note td { padding: 3px 0; vertical-align: top; }
.setting-note td:first-child { color: var(--text-3); width: 96px; }

/* ───────────────────────────────────────────────────── app ── */

.app {
  display: grid;
  grid-template-columns: 232px minmax(300px, 400px) 1fr;
  height: 100%;
}

/* sidebar */

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  padding: 16px 12px 12px;
  min-height: 0;
}
.brand {
  display: flex; align-items: center; gap: 9px;
  padding: 0 6px 16px;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.brand-mark {
  width: 24px; height: 24px;
  display: grid; place-items: center;
  border-radius: 7px;
  background: var(--accent);
  color: #fff; font-size: 12px;
}
.compose-btn { width: 100%; margin-bottom: 16px; }

.folders { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; flex: 1; min-height: 0; }
.folder {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border: 0; background: none;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-2);
  text-align: left;
  width: 100%;
}
.folder:hover { background: var(--bg-hover); color: var(--text); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folder-name { flex: 1; text-transform: capitalize; }
.folder-count {
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  background: var(--bg-3);
  color: var(--text-2);
  padding: 1px 7px;
  border-radius: 20px;
}
.folder.active .folder-count { background: var(--accent); color: #fff; }

.sidebar-foot { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 8px; }
.account { display: flex; align-items: center; gap: 10px; padding: 0 4px 10px; min-width: 0; }
.avatar {
  width: 30px; height: 30px; flex: none;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  font-weight: 650; font-size: 12.5px;
  text-transform: uppercase;
}
.account-text { min-width: 0; }
.account-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-mail { font-size: 11.5px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-actions { display: flex; gap: 6px; }
.sidebar-actions .btn { flex: 1; }

/* list pane */

.list-pane {
  display: flex; flex-direction: column;
  border-right: 1px solid var(--line);
  min-height: 0; min-width: 0;
  background: var(--bg);
}
.list-head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 12px;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.search-wrap { position: relative; flex: 1; min-width: 0; }
.search-icon {
  position: absolute; left: 10px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-3); pointer-events: none;
}
#search {
  width: 100%;
  padding: 8px 10px 8px 31px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--bg-2);
  outline: none;
}
#search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.bulk-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px;
  background: var(--accent-soft);
  border-bottom: 1px solid var(--line);
  font-size: 12.5px; font-weight: 600;
  flex: none; flex-wrap: wrap;
}
.bulk-actions { display: flex; gap: 4px; flex-wrap: wrap; }

.list { flex: 1; overflow-y: auto; min-height: 0; }
.list-more { padding: 10px; text-align: center; flex: none; }

.row {
  display: flex; gap: 10px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--line-soft);
  cursor: pointer;
  position: relative;
}
.row:hover { background: var(--bg-2); }
.row.active { background: var(--accent-soft); }
.row.unread::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0;
  width: 3px; background: var(--accent);
}
.row-check { flex: none; padding-top: 2px; }
.row-check input { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.row-main { flex: 1; min-width: 0; }
.row-top { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.row-from { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; color: var(--text-2); }
.row.unread .row-from { color: var(--text); font-weight: 650; }
.row-date { font-size: 11.5px; color: var(--text-3); flex: none; }
.row-subject {
  font-size: 13px; margin-bottom: 3px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--text-2);
}
.row.unread .row-subject { color: var(--text); font-weight: 600; }
.row-snippet {
  font-size: 12.5px; color: var(--text-3);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.row-side { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: none; }
.star { background: none; border: 0; cursor: pointer; color: var(--text-3); padding: 0; line-height: 1; }
.star.on { color: var(--star); }
.clip { color: var(--text-3); }

.list-empty, .list-loading {
  padding: 48px 24px;
  text-align: center;
  color: var(--text-3);
  font-size: 13px;
}

/* read pane */

.read-pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; overflow-y: auto; }
.empty {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 10px; color: var(--text-3);
}
.empty-mark { font-size: 40px; opacity: 0.35; }

.reader { display: flex; flex-direction: column; min-height: 0; }
.reader-head {
  padding: 20px 26px 16px;
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0;
  background: var(--bg);
  z-index: 2;
}
.reader-subject { margin: 0 0 14px; font-size: 19px; letter-spacing: -0.02em; line-height: 1.35; }
.reader-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.msg { border-bottom: 1px solid var(--line-soft); }
.msg-head {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 16px 26px 12px;
}
.msg-avatar {
  width: 34px; height: 34px; flex: none;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--bg-3); color: var(--text-2);
  font-weight: 650; font-size: 13px; text-transform: uppercase;
}
.msg-meta { flex: 1; min-width: 0; }
.msg-from { font-weight: 620; font-size: 13.5px; }
.msg-from span { font-weight: 400; color: var(--text-3); }
.msg-to { font-size: 12px; color: var(--text-3); margin-top: 2px; word-break: break-word; }
.msg-date { font-size: 12px; color: var(--text-3); flex: none; }

.auth-badges { display: flex; gap: 5px; margin-top: 6px; flex-wrap: wrap; }
.badge {
  font-size: 10.5px; font-weight: 650;
  padding: 1.5px 6px; border-radius: 5px;
  text-transform: uppercase; letter-spacing: 0.03em;
  background: var(--bg-3); color: var(--text-3);
}
.badge.pass { background: rgba(52, 199, 89, 0.15); color: #34c759; }
.badge.fail { background: rgba(255, 107, 107, 0.15); color: var(--danger); }

.image-notice {
  margin: 0 26px 12px;
  padding: 8px 12px;
  background: var(--bg-3);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--text-2);
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}

.msg-body { padding: 4px 26px 20px; }
.msg-body iframe {
  width: 100%; border: 0; display: block;
  background: #fff;
  border-radius: 8px;
  min-height: 60px;
}
.msg-body pre.plain {
  margin: 0;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: var(--font);
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--text);
}

.attachments { padding: 0 26px 20px; display: flex; flex-wrap: wrap; gap: 8px; }
.attachment {
  display: flex; align-items: center; gap: 9px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-2);
  text-decoration: none; color: var(--text);
  font-size: 12.5px;
  max-width: 260px;
}
.attachment:hover { background: var(--bg-hover); }
.attachment.dead { opacity: 0.55; pointer-events: none; }
.att-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-size { color: var(--text-3); font-size: 11.5px; flex: none; }

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

.modal-back {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: grid; place-items: center;
  padding: 20px;
  z-index: 50;
  backdrop-filter: blur(3px);
}
.compose, .settings {
  width: 100%; max-width: 680px;
  max-height: min(760px, 92vh);
  display: flex; flex-direction: column;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}
.compose-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 14px 13px 18px;
  border-bottom: 1px solid var(--line);
  font-weight: 620;
  flex: none;
}
.compose-body { padding: 6px 18px 14px; overflow-y: auto; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.compose-row {
  display: flex; align-items: center; gap: 10px;
  border-bottom: 1px solid var(--line-soft);
  padding: 9px 0;
  flex: none;
}
.compose-row label {
  width: 56px; flex: none;
  font-size: 12px; font-weight: 600; color: var(--text-3);
}
.compose-row input, .compose-row select {
  flex: 1; min-width: 0;
  border: 0; background: none; outline: none; padding: 2px 0;
}
#c-body {
  flex: 1;
  min-height: 220px;
  margin-top: 14px;
  border: 0; background: none; outline: none;
  resize: vertical;
  line-height: 1.65;
  font-size: 13.5px;
}
.attach-list { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 10px; flex: none; }
.chip {
  display: inline-flex; align-items: center; gap: 7px;
  background: var(--bg-3);
  border-radius: 20px;
  padding: 4px 6px 4px 11px;
  font-size: 12px;
}
.chip button { border: 0; background: none; cursor: pointer; color: var(--text-3); line-height: 1; padding: 2px 4px; }
.chip button:hover { color: var(--danger); }

.compose-foot {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--line);
  flex: none;
}
.compose-status { flex: 1; font-size: 12.5px; color: var(--text-3); }
.compose-status.error { color: var(--danger); }

/* settings */

.settings-body { padding: 18px; overflow-y: auto; }
.settings-body h3 { margin: 0 0 4px; font-size: 14px; }
.settings-body p.muted { margin: 0 0 14px; color: var(--text-3); font-size: 12.5px; line-height: 1.55; }
.settings-section { padding-bottom: 22px; margin-bottom: 22px; border-bottom: 1px solid var(--line); }
.settings-section:last-child { border: 0; margin-bottom: 0; padding-bottom: 0; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.inline-form .field { flex: 1; min-width: 150px; margin-bottom: 0; }
.user-table { width: 100%; border-collapse: collapse; font-size: 12.5px; margin-bottom: 14px; }
.user-table th { text-align: left; color: var(--text-3); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; padding-bottom: 8px; }
.user-table td { padding: 7px 0; border-top: 1px solid var(--line-soft); }
.mono { font-family: var(--mono); font-size: 12px; }

/* toasts */

.toast-wrap { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: var(--bg-3);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 20px;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  animation: rise 0.18s ease-out;
}
.toast.error { border-color: rgba(255, 107, 107, 0.5); color: var(--danger); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ────────────────────────────────────────────── responsive ── */

.only-mobile { display: none; }

@media (max-width: 1000px) {
  .app { grid-template-columns: 200px 1fr; }
  .read-pane { display: none; }
  .app.reading .list-pane { display: none; }
  .app.reading .read-pane { display: flex; }
}

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0;
    width: 240px; z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.4);
  }
  .sidebar.open { transform: none; }
  .msg-head, .msg-body, .attachments, .reader-head, .image-notice { padding-left: 16px; padding-right: 16px; }
  .image-notice { margin-left: 16px; margin-right: 16px; }
}
