:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #1d2129; --muted: #6b7280; --line: #e3e6ea;
  --accent: #2f6fde; --accent-soft: #e8f0fd; --danger: #c0392b; --unread: #111827;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171b; --panel: #1d2026; --text: #e6e8eb; --muted: #9aa1ab; --line: #2c3038;
    --accent: #6ea1ff; --accent-soft: #233150; --danger: #ff7b6b; --unread: #ffffff;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
a { color: var(--accent); }
button {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 12px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.link { border: none; background: none; color: var(--accent); padding: 0; }
button.danger { color: var(--danger); }

/* Header */
.top { display: flex; align-items: center; gap: 16px; height: 48px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; color: var(--text); text-decoration: none; }
.user { min-width: 0; margin-left: auto; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-decoration: none; }
.user:hover { color: var(--accent); }
.switch { white-space: nowrap; text-decoration: none; }
.top form { margin: 0; }

/* Three panes */
.app { display: grid; grid-template-columns: 220px minmax(280px, 380px) minmax(0, 1fr); height: calc(100% - 48px); }
#mailboxes { overflow-y: auto; padding: 8px; border-right: 1px solid var(--line); }
#mailboxes ul { list-style: none; margin: 0; padding: 0; }
.mailbox { display: flex; justify-content: space-between; gap: 8px; padding: 7px 10px; border-radius: 6px; color: var(--text); text-decoration: none; }
.mailbox:hover { background: var(--accent-soft); }
.mailbox.current { background: var(--accent-soft); font-weight: 600; }
.mailbox.depth-1 { padding-left: 24px; } .mailbox.depth-2 { padding-left: 38px; } .mailbox.depth-3 { padding-left: 52px; }
.badge { min-width: 22px; padding: 0 7px; border-radius: 11px; background: var(--accent); color: #fff; font-size: 12px; line-height: 20px; text-align: center; }

#list { display: flex; flex-direction: column; overflow: hidden; background: var(--panel); border-right: 1px solid var(--line); }
.list-head { padding: 12px 16px; border-bottom: 1px solid var(--line); }
.list-head h1 { margin: 0; font-size: 18px; }
.rows { overflow-y: auto; flex: 1; }
.row {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 10px 16px;
  border-bottom: 1px solid var(--line); color: var(--text); text-decoration: none;
}
.row:hover { background: var(--bg); }
.row.selected { background: var(--accent-soft); }
.row .from, .row .subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .subject { grid-column: 1 / -1; color: var(--muted); }
.row time { color: var(--muted); font-size: 13px; }
.row.unread .from, .row.unread .subject { font-weight: 650; color: var(--unread); }
.row.unread { box-shadow: inset 3px 0 var(--accent); }
.star { color: #e3a008; margin-right: 4px; }
.more { display: block; margin: 12px auto; }
.empty, .empty-pane { color: var(--muted); padding: 24px 16px; text-align: center; }
.empty-pane { margin-top: 20vh; }

#pane { overflow-y: auto; background: var(--panel); }
.message { display: flex; flex-direction: column; min-width: 0; min-height: 100%; padding: 12px 20px 20px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.toolbar form { margin: 0; }
.toolbar .back { display: none; margin-right: auto; text-decoration: none; }
.toolbar .raw { margin-left: auto; font-size: 13px; }
.message .subject { margin: 16px 0 8px; font-size: 20px; overflow-wrap: anywhere; }
.headers { display: grid; grid-template-columns: max-content 1fr; gap: 2px 12px; margin: 0 0 12px; font-size: 14px; }
.headers dt { color: var(--muted); }
.headers dd { margin: 0; overflow-wrap: anywhere; }
.address { color: var(--muted); }
.attachments { margin: 0 0 12px; padding: 8px 12px; list-style: none; background: var(--bg); border-radius: 6px; }
.attachments li { padding: 2px 0; }
.attachments .size { color: var(--muted); font-size: 13px; }
.images-note { margin: 0 0 8px; padding: 6px 10px; font-size: 13px; background: var(--bg); border-radius: 6px; color: var(--muted); }
iframe.body { flex: 1; width: 100%; min-height: 60vh; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
pre.text { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; }

/* Login */
.login-page { display: grid; place-items: center; }
.login { display: grid; gap: 14px; width: min(360px, calc(100% - 32px)); padding: 28px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login label { display: grid; gap: 4px; color: var(--muted); font-size: 14px; }
.login input { font: inherit; padding: 8px 10px; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; }
.login button { padding: 9px; background: var(--accent); border-color: var(--accent); color: #fff; }
.error { margin: 0; color: var(--danger); }
.notice { margin: 0; color: var(--accent); }
.login .or { margin: -4px 0; text-align: center; color: var(--muted); font-size: 13px; }
.login a.button { display: block; padding: 9px; text-align: center; text-decoration: none; color: var(--accent); background: var(--panel); border: 1px solid var(--accent); border-radius: 6px; }
.login a.button:hover { background: var(--accent-soft); }
.account h2 { margin: 6px 0 0; font-size: 16px; }
.account p { margin: 0; }
.account .address { color: var(--muted); overflow-wrap: anywhere; }
.account form, .select form { margin: 0; }
.muted { color: var(--muted); }
.account .links { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
.account .links li { display: flex; align-items: baseline; gap: 10px; }
.account .links .who { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.account .nav-links { display: flex; gap: 16px; }
.select p { margin: 0; overflow-wrap: anywhere; }
.select .mailbox-choice { display: grid; gap: 8px; max-height: 60vh; overflow-y: auto; }
.mailbox-choice { margin: 0; padding: 0; list-style: none; }
.mailbox-choice a { display: block; padding: 10px 12px; color: var(--text); text-decoration: none; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailbox-choice a:hover { border-color: var(--accent); }
.select button.link, .account button.link { padding: 0; background: none; border: none; }
.account button.link.danger { color: var(--danger); }
.select button.link { color: var(--accent); }

/* Narrow screens: one pane at a time */
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .top { gap: 10px; padding: 0 12px; }
  #mailboxes { border-right: none; border-bottom: 1px solid var(--line); padding: 6px 8px; }
  #mailboxes ul { display: flex; gap: 4px; overflow-x: auto; }
  .mailbox, .mailbox[class*="depth-"] { padding: 6px 10px; white-space: nowrap; }
  #pane { display: none; }
  .app:has(#pane .message) #mailboxes, .app:has(#pane .message) #list { display: none; }
  .app:has(#pane .message) { grid-template-rows: 1fr; }
  .app:has(#pane .message) #pane { display: block; }
  .toolbar .back { display: inline; }
  .message { padding: 12px 16px; }
}
