:root {
  --bg: #ffffff;
  --bg-sunken: #f6f8fc;
  --bg-raised: #ffffff;
  --border: #dde3ec;
  --text: #1f2328;
  --text-dim: #5b6472;
  --accent: #1a73e8;
  --accent-soft: #e8f0fe;
  --danger: #d93025;
  --unread: #f2f6fd;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 6px 20px rgba(0,0,0,.06);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181d;
    --bg-sunken: #101216;
    --bg-raised: #1d2026;
    --border: #2c313a;
    --text: #e6e9ef;
    --text-dim: #9aa4b2;
    --accent: #7cadf5;
    --accent-soft: #1d2b40;
    --danger: #f2857c;
    --unread: #1b2431;
    --shadow: 0 1px 3px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg-sunken);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- auth ---------- */
.auth {
  /* dvh tracks the mobile URL bar; vh is the fallback for older browsers. */
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  padding-left: max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px;
  box-shadow: var(--shadow);
}
.brand { font-size: 22px; margin: 0 0 4px; font-weight: 600; }
.brand span { color: var(--accent); }
.brand.small { font-size: 16px; white-space: nowrap; }
.tagline { margin: 0 0 24px; color: var(--text-dim); font-size: 13px; }

.tabs { display: flex; gap: 4px; margin-bottom: 20px; background: var(--bg-sunken); padding: 4px; border-radius: 8px; }
.tab {
  flex: 1; padding: 8px; border: 0; background: transparent; border-radius: 6px;
  color: var(--text-dim); font-weight: 500;
}
.tab.active { background: var(--bg-raised); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; }
.auth-form label small { font-weight: 400; color: var(--text-dim); }
.auth-form input,
.field input, textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  outline: none;
}
.auth-form input:focus, .field input:focus, textarea:focus,
.search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.addr-input { display: flex; align-items: center; gap: 0; }
.addr-input input { border-radius: 8px 0 0 8px; flex: 1; min-width: 0; }
.addr-input span {
  padding: 10px 12px; border: 1px solid var(--border); border-left: 0;
  border-radius: 0 8px 8px 0; background: var(--bg-sunken);
  color: var(--text-dim); font-size: 13px; white-space: nowrap;
}

.primary {
  padding: 11px 16px; border: 0; border-radius: 8px;
  background: var(--accent); color: #fff; font-weight: 600;
}
.primary:disabled { opacity: .55; cursor: not-allowed; }
.error { color: var(--danger); font-size: 13px; margin: 12px 0 0; }

/* ---------- app shell ---------- */
.app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--bg-raised);
  border-bottom: 1px solid var(--border);
}
.spacer { flex: 1; }
.account { color: var(--text-dim); font-size: 13px; }
.icon-btn {
  border: 0; background: transparent; color: var(--text-dim);
  font-size: 16px; padding: 6px 9px; border-radius: 6px;
}
.icon-btn:hover { background: var(--bg-sunken); color: var(--text); }
.menu-btn { display: none; }

.search {
  flex: 1; max-width: 520px; padding: 9px 14px;
  border: 1px solid transparent; border-radius: 999px;
  background: var(--bg-sunken); outline: none;
}

.body { flex: 1; display: flex; min-height: 0; }

/* ---------- sidebar ---------- */
.sidebar {
  width: 200px; flex-shrink: 0; padding: 16px 12px;
  background: var(--bg-raised); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 16px;
}
.compose {
  padding: 12px; border: 0; border-radius: 999px;
  background: var(--accent); color: #fff; font-weight: 600;
  box-shadow: var(--shadow);
}
.folders { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.folder {
  width: 100%; text-align: left; display: flex; justify-content: space-between; align-items: center;
  padding: 9px 14px; border: 0; border-radius: 999px;
  background: transparent; color: var(--text);
}
.folder:hover { background: var(--bg-sunken); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.badge { font-size: 12px; color: var(--text-dim); }
.folder.active .badge { color: var(--accent); }
.quota { margin-top: auto; font-size: 11px; color: var(--text-dim); line-height: 1.6; }

/* ---------- list ---------- */
.list-pane {
  width: 380px; flex-shrink: 0; display: flex; flex-direction: column;
  border-right: 1px solid var(--border); background: var(--bg);
}
.list-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.message-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.message-list li {
  padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer;
  display: flex; flex-direction: column; gap: 3px;
}
.message-list li:hover { background: var(--bg-sunken); }
.message-list li.unread { background: var(--unread); }
.message-list li.unread .row-from,
.message-list li.unread .row-subject { font-weight: 700; }
.message-list li.selected { box-shadow: inset 3px 0 0 var(--accent); background: var(--accent-soft); }
.row-top { display: flex; justify-content: space-between; gap: 8px; }
.row-from { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-date { font-size: 11px; color: var(--text-dim); flex-shrink: 0; }
.row-subject { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-preview { font-size: 12px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { padding: 40px 20px; text-align: center; color: var(--text-dim); }

/* ---------- reader ---------- */
.reader { flex: 1; overflow-y: auto; background: var(--bg); padding: 24px 32px; min-width: 0; }
.reader-placeholder { color: var(--text-dim); text-align: center; margin-top: 80px; }
.reader h2 { margin: 0 0 14px; font-size: 20px; font-weight: 600; word-wrap: break-word; }
.meta { display: flex; flex-wrap: wrap; gap: 4px 14px; padding-bottom: 16px; border-bottom: 1px solid var(--border); margin-bottom: 20px; font-size: 13px; color: var(--text-dim); }
.meta strong { color: var(--text); font-weight: 600; }
.reader-actions { display: flex; gap: 8px; margin-bottom: 20px; }
.reader-actions button {
  padding: 7px 14px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--bg-raised); color: var(--text);
}
.reader-actions button:hover { background: var(--bg-sunken); }
.reader-actions .danger { color: var(--danger); }
.mail-body { white-space: pre-wrap; word-wrap: break-word; line-height: 1.7; }
.mail-html { border: 0; width: 100%; min-height: 420px; background: #fff; border-radius: 8px; }
.attachments { margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); }
.attachments h4 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-dim); }
.att { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; margin: 0 8px 8px 0; border: 1px solid var(--border); border-radius: 8px; text-decoration: none; color: var(--text); font-size: 13px; }
.att:hover { background: var(--bg-sunken); }
.att small { color: var(--text-dim); }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 20px; z-index: 50; }
.modal-card { width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto; background: var(--bg-raised); border-radius: 14px; box-shadow: var(--shadow); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 600; }
#compose-form { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; align-items: center; gap: 12px; }
.field label { width: 64px; flex-shrink: 0; font-size: 13px; color: var(--text-dim); }
.field input { flex: 1; min-width: 0; }
.from-addr { font-size: 13px; color: var(--text-dim); }
textarea { resize: vertical; width: 100%; }
.modal-actions { display: flex; align-items: center; gap: 14px; }
.status { font-size: 13px; color: var(--text-dim); }

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

/* ---------- backdrop behind the off-canvas sidebar ---------- */
.backdrop {
  position: fixed; inset: 0; z-index: 15;
  background: rgba(0, 0, 0, .45);
  -webkit-backdrop-filter: blur(1px); backdrop-filter: blur(1px);
}

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .menu-btn { display: block; }

  /* Off-canvas drawer. It slides rather than display:none-ing so the motion
     reads as a panel, and so a stray tap can't land on a hidden control. */
  .sidebar {
    position: fixed; top: 0; left: 0; z-index: 20;
    height: 100vh; height: 100dvh;
    width: 240px;
    padding-top: max(16px, env(safe-area-inset-top));
    padding-left: max(12px, env(safe-area-inset-left));
    box-shadow: var(--shadow);
    transition: transform .22s ease;
  }
  .sidebar.collapsed { transform: translateX(-102%); }

  .list-pane { width: 320px; }
  .reader { padding: 18px; }
}

@media (max-width: 680px) {
  /* Search moves to its own full-width row instead of being hidden —
     an inbox you cannot search on a phone is a broken inbox. */
  .topbar { flex-wrap: wrap; row-gap: 8px; padding: 8px 12px; }
  .topbar { padding-top: max(8px, env(safe-area-inset-top)); }
  .search { order: 10; flex-basis: 100%; max-width: none; }
  .account { display: none; }

  /* One pane at a time: list, or the message you opened. */
  .list-pane { width: 100%; border-right: 0; }
  .body.reading .list-pane { display: none; }
  .body:not(.reading) .reader { display: none; }

  .reader {
    padding: 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  .reader h2 { font-size: 18px; }
  .reader-actions { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 8px 0; margin: -8px 0 12px; }
  .reader-actions button { padding: 9px 14px; }

  /* Comfortable thumb targets. */
  .message-list li { padding: 14px 16px; }
  .icon-btn { padding: 9px 11px; }
  .folder { padding: 12px 16px; }

  /* A full-height sheet beats a cramped centred dialog on a phone. */
  .modal { padding: 0; align-items: stretch; }
  .modal-card {
    max-width: none; width: 100%;
    height: 100vh; height: 100dvh;
    max-height: none; border-radius: 0;
    display: flex; flex-direction: column;
  }
  .modal-head { padding-top: max(14px, env(safe-area-inset-top)); }
  #compose-form { flex: 1; padding-bottom: max(18px, env(safe-area-inset-bottom)); }
  #compose-form textarea { flex: 1; min-height: 180px; }
  .field { align-items: flex-start; gap: 8px; }
  .field label { width: 52px; padding-top: 10px; }

  /* iOS zooms the whole page when a focused input is under 16px. */
  .auth-form input,
  .field input,
  textarea,
  .search { font-size: 16px; }

  .auth-card { padding: 24px 20px; }
  .toast { left: 12px; right: 12px; bottom: max(16px, env(safe-area-inset-bottom)); transform: none; text-align: center; }
}

/* Remote HTML mail is authored for desktop widths and will happily push the
   page sideways; keep any overflow inside the frame. */
@media (max-width: 680px) {
  .mail-html { min-height: 320px; }
  .mail-body, .meta { overflow-wrap: anywhere; }
  .attachments .att { max-width: 100%; }
}

/* The backdrop belongs to the mobile drawer only — never let it cover a
   desktop layout if the class is ever left on. */
@media (min-width: 901px) {
  .backdrop { display: none !important; }
}

/* Respect users who ask for less motion. */
@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}
