:root {
  --scale: 1.25;
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --read-row: #f2f5fa;
  --text: #111418;
  --muted: #3c4450;
  --line: #c9d0db;
  --accent: #0b57d0;
  --accent-text: #ffffff;
  --accent-soft: #d3e3fd;
  --me: #0d7a4f;
  --me-soft: #e3f4ec;
  --draft: #b3261e;
  --draft-soft: #fdecea;
  --star: #b06000;
  --focus: #ff9800;
  --hover: #e4e9f2;
  --selected: #c2dbff;
  --shadow: 0 4px 24px rgba(0, 0, 0, .18);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101317; --surface: #1a1e24; --surface-2: #242a32; --read-row: #161a1f;
    --text: #f2f4f7; --muted: #c3cad4; --line: #3a424d; --accent: #8ab4f8; --accent-text: #0b1d3a;
    --accent-soft: #23395d; --me: #6fdcaa; --me-soft: #17322a; --draft: #ff8a80; --draft-soft: #3b1f1d;
    --star: #ffc04d; --hover: #262c35; --selected: #2c4772; --shadow: 0 4px 24px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101317; --surface: #1a1e24; --surface-2: #242a32; --read-row: #161a1f;
  --text: #f2f4f7; --muted: #c3cad4; --line: #3a424d; --accent: #8ab4f8; --accent-text: #0b1d3a;
  --accent-soft: #23395d; --me: #6fdcaa; --me-soft: #17322a; --draft: #ff8a80; --draft-soft: #3b1f1d;
  --star: #ffc04d; --hover: #262c35; --selected: #2c4772; --shadow: 0 4px 24px rgba(0, 0, 0, .6);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { font-size: calc(16px * var(--scale)); }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif; line-height: 1.45;
}
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.btn {
  border: 2px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: .35rem 1.1rem; cursor: pointer; font-weight: 600;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.big { padding: .6rem 1.6rem; }
.btn.danger { color: var(--draft); }
.icon-btn {
  border: none; background: transparent; border-radius: 999px; min-width: 2.4rem; height: 2.4rem;
  cursor: pointer; font-size: 1.15rem; padding: 0 .5rem; line-height: 1; font-weight: 600;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn.small { min-width: 1.8rem; height: 1.8rem; font-size: 1rem; }
.icon-btn[aria-pressed="true"] { color: var(--star); }

/* ---------- login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card {
  background: var(--surface); padding: 2rem; border-radius: 16px; box-shadow: var(--shadow);
  display: grid; gap: .8rem; width: min(26rem, 100%);
}
.login-card h1 { margin: 0 0 .5rem; }
.login-card input {
  padding: .7rem; border: 2px solid var(--muted); border-radius: 10px; background: var(--surface);
  font-size: 1.15rem; width: 100%;
}
.login-card label { font-weight: 700; margin-top: .4rem; }
.login-card form, #login-fields { display: grid; gap: .6rem; }
#login-code, #setup-code { font-size: 1.5rem; letter-spacing: .2em; }
.hint { margin: -.3rem 0 0; color: var(--muted); font-size: .95rem; }
.notice { font-weight: 700; font-size: 1.15rem; margin: 0; }
.error { color: var(--draft); font-weight: 600; min-height: 1.4em; margin: 0; }
.setup-card { width: min(38rem, 100%); }
.setup-card h2 { font-size: 1.25rem; margin: 1.2rem 0 0; }
.setup-card p { margin: .2rem 0; }
.qr-box { display: flex; justify-content: center; padding: .4rem 0; }
.qr { width: min(17rem, 75vw); height: auto; display: block; background: #fff; border-radius: 8px; }
.secret { font-family: ui-monospace, Consolas, monospace; font-size: 1.25rem; font-weight: 700; word-break: break-all; }
.backup-codes { font-family: ui-monospace, Consolas, monospace; font-size: 1.35rem; font-weight: 700; columns: 2; }
.row-buttons { display: flex; flex-wrap: wrap; gap: .8rem; }
a.btn { text-decoration: none; display: inline-block; text-align: center; }
@media print { .btn, .row-buttons { display: none !important; } .login-card { box-shadow: none; } }

/* ---------- layout */
.app {
  display: grid; grid-template-columns: 15.5rem minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr);
  height: 100vh; overflow: hidden;
}
/* The warning banner: its own row on top (no space at all when hidden). */
.alert-banner {
  grid-column: 1 / -1; grid-row: 1; display: flex; flex-wrap: wrap; align-items: center; gap: .8rem 1.2rem;
  background: #8c1d18; color: #fff; padding: .9rem 1.2rem; font-size: 1.15rem; font-weight: 700; line-height: 1.4;
}
.alert-words { flex: 1 1 22rem; }
.alert-words p { margin: 0; }
.alert-words p + p { margin-top: .4rem; }
.alert-banner .btn { background: #fff; color: #8c1d18; border-color: #fff; min-width: 6rem; }
.sidebar, .main { grid-row: 3; }
.topbar {
  grid-row: 2; grid-column: 1 / -1; display: flex; align-items: center; gap: .3rem .6rem; padding: .5rem 1rem;
  background: var(--bg); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 1.2rem; text-decoration: none; color: var(--text); white-space: nowrap; }
.search {
  flex: 1 1 12rem; min-width: 0; max-width: 46rem; display: flex; align-items: center; gap: .4rem;
  background: var(--surface-2); border-radius: 999px; padding: .15rem .6rem; border: 2px solid transparent;
}
.search:focus-within { background: var(--surface); border-color: var(--accent); }
.search input { flex: 1; border: none; background: transparent; padding: .45rem .2rem; outline: none; min-width: 0; }
.search-icon { font-size: 1.2rem; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: .2rem; margin-left: auto; }
.sync-status { color: var(--muted); font-size: .85rem; margin-right: .4rem; white-space: nowrap; }

.sidebar { overflow-y: auto; padding: .6rem .5rem 1rem; }
.compose-btn {
  display: block; margin: .2rem .3rem 1rem; padding: .9rem 1.5rem; border-radius: 18px; border: none;
  background: var(--accent-soft); font-weight: 700; font-size: 1.05rem; cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
}
.compose-btn:hover { box-shadow: var(--shadow); }
.nav { list-style: none; margin: 0; padding: 0; }
.nav a {
  display: flex; align-items: center; gap: .7rem; padding: .45rem .9rem; border-radius: 0 999px 999px 0;
  text-decoration: none; color: var(--text); margin-right: .3rem;
}
.nav a:hover { background: var(--hover); }
.nav a.active { background: var(--selected); font-weight: 700; }
.nav .ico { width: 1.3rem; text-align: center; }
.nav .count { margin-left: auto; font-weight: 700; font-size: .9rem; }
.nav-section {
  display: flex; align-items: center; justify-content: space-between; padding: 1rem .9rem .3rem;
  font-weight: 700; color: var(--muted);
}
.label-dot { width: .8rem; height: .8rem; border-radius: 3px; display: inline-block; flex: none; }
.mode-note { color: var(--muted); font-size: .85rem; padding: 0 .9rem; }

main, .main { min-width: 0; }
.main {
  background: var(--surface); border-radius: 16px 16px 0 0; margin: .5rem .8rem 0 0;
  overflow-y: auto; outline: none; position: relative;
}

/* ---------- toolbar */
.toolbar {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: .2rem; flex-wrap: wrap;
  padding: .4rem .8rem; background: var(--surface); border-bottom: 1px solid var(--line);
}
/* Many rows of buttons (large text, narrow window): it scrolls away with the page. See fitToolbar(). */
.toolbar.tall { position: static; }
.toolbar .spacer { flex: 1; }
.toolbar .pager { color: var(--muted); font-size: .9rem; }
.check { width: 1.25rem; height: 1.25rem; accent-color: var(--accent); cursor: pointer; }

/* ---------- thread list */
.list { list-style: none; margin: 0; padding: 0; }
.row {
  display: grid; grid-template-columns: auto auto minmax(8rem, 15rem) minmax(0, 1fr) auto; align-items: center;
  gap: .7rem; padding: .55rem 1rem; border-bottom: 1px solid var(--line); cursor: pointer;
  background: var(--read-row); position: relative;
}
.density-compact .row { padding: .25rem 1rem; }
.row.unread { background: var(--surface); font-weight: 700; }
.row:hover { box-shadow: inset 0 -1px 0 var(--line), 0 1px 4px rgba(0, 0, 0, .15); z-index: 1; }
.row.selected { background: var(--selected); }
.row.focused::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 6px; background: var(--focus);
}
.star { border: none; background: none; cursor: pointer; font-size: 1.25rem; color: var(--muted); padding: 0 .1rem; }
.star.on { color: var(--star); }
.who { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who .n { color: var(--muted); font-weight: 400; font-size: .9em; }
.who .draft-tag { color: var(--draft); font-weight: 700; }
.what { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.what .snip { color: var(--muted); font-weight: 400; }
.when { white-space: nowrap; font-size: .9rem; text-align: right; display: flex; gap: .4rem; align-items: center; }
.replied {
  display: inline-block; font-size: .78rem; font-weight: 700; color: var(--me); background: var(--me-soft);
  border-radius: 6px; padding: 0 .35rem; margin-right: .35rem; vertical-align: 1px;
}
.chip {
  display: inline-block; font-size: .78rem; font-weight: 700; border-radius: 6px; padding: 0 .4rem;
  margin-right: .3rem; background: var(--surface-2); border: 1px solid var(--line); vertical-align: 1px;
}
.chip.label { color: #fff; border: none; }
.chip.snooze { background: #fff4d6; color: #6a4300; border-color: #e7c66a; }
.empty { padding: 3rem 1rem; text-align: center; color: var(--muted); font-size: 1.1rem; }
.more-btn { display: block; margin: 1rem auto; }

/* ---------- conversation */
.conv { padding: .4rem 1.4rem 3rem; max-width: 64rem; }
.conv-title { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; margin: .6rem 0 1rem; }
.conv-title h1 { font-size: 1.55rem; font-weight: 500; margin: 0; }
.msg {
  border: 1px solid var(--line); border-radius: 12px; margin: .6rem 0; background: var(--surface);
  border-left-width: 6px;
}
.msg.me { border-left-color: var(--me); }
.msg.unread-msg { border-left-color: var(--accent); }
.msg-head {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: .8rem; align-items: center;
  padding: .7rem 1rem; cursor: pointer;
}
.avatar {
  width: 2.5rem; height: 2.5rem; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 1.1rem; flex: none;
}
.msg-who { min-width: 0; }
.msg-who .name { font-weight: 700; }
.msg-who .addr { color: var(--muted); font-size: .9rem; }
.msg-who .you { color: var(--me); font-weight: 700; }
.msg-who .snip { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.msg-who .rcpt { color: var(--muted); font-size: .9rem; display: block; }
.msg-who .reply-to { color: var(--text); font-weight: 600; }
.msg-meta { display: flex; align-items: center; gap: .2rem; white-space: nowrap; color: var(--muted); font-size: .9rem; }
.msg-body { padding: 0 1rem 1rem 4.3rem; overflow-x: auto; }
.msg.collapsed .msg-body, .msg.collapsed .msg-actions, .msg.collapsed .atts { display: none; }
.msg:not(.collapsed) .msg-who .snip { display: none; }
.text-body { white-space: pre-wrap; word-wrap: break-word; margin: 0; font-family: inherit; }
.html-frame { width: 100%; border: none; display: block; min-height: 2rem; background: #fff; border-radius: 6px; }
.quote-toggle {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 6px; cursor: pointer;
  font-weight: 700; padding: 0 .6rem; margin: .5rem 0; letter-spacing: .1em;
}
.quoted { border-left: 3px solid var(--line); padding-left: .8rem; color: var(--muted); margin-top: .4rem; }
.images-banner {
  display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; background: var(--surface-2);
  border-radius: 8px; padding: .5rem .8rem; margin-bottom: .6rem; font-size: .95rem;
}
.atts { display: flex; flex-wrap: wrap; gap: .6rem; padding: 0 1rem 1rem 4.3rem; }
.att {
  display: flex; align-items: center; gap: .5rem; border: 2px solid var(--line); border-radius: 10px;
  padding: .45rem .8rem; text-decoration: none; color: var(--text); max-width: 20rem; background: var(--surface);
}
.att:hover { background: var(--hover); }
.att .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.att .fsize { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.att .x { border: none; background: none; cursor: pointer; font-size: 1.1rem; color: var(--muted); }
.msg-actions { display: flex; gap: .5rem; padding: 0 1rem 1rem 4.3rem; flex-wrap: wrap; }
.older-bar {
  display: block; width: 100%; text-align: center; border: 1px dashed var(--line); background: var(--surface-2);
  border-radius: 12px; padding: .5rem; cursor: pointer; font-weight: 700; color: var(--muted); margin: .6rem 0;
}
.reply-bar { display: flex; gap: .8rem; margin: 1.2rem 0 0 0; flex-wrap: wrap; }
.draft-card {
  border: 2px dashed var(--draft); background: var(--draft-soft); border-radius: 12px; padding: .6rem 1rem;
  margin: .6rem 0; cursor: pointer;
}
.draft-card b { color: var(--draft); }

/* ---------- composer */
.composer {
  border: 2px solid var(--accent); border-radius: 12px; background: var(--surface); margin: .8rem 0;
  box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.composer-head {
  display: flex; align-items: center; gap: .4rem; padding: .4rem .5rem .4rem 1rem; font-weight: 700;
  background: var(--surface-2); border-radius: 10px 10px 0 0;
}
.composer-head .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.field { display: flex; align-items: flex-start; gap: .5rem; border-bottom: 1px solid var(--line); padding: .2rem 1rem; }
.field > label { color: var(--muted); padding-top: .45rem; min-width: 3.6rem; }
.field input.subject { flex: 1; border: none; background: transparent; padding: .45rem 0; outline: none; }
.rcpts { flex: 1; display: flex; flex-wrap: wrap; gap: .3rem; align-items: center; position: relative; padding: .2rem 0; }
.rcpts input { flex: 1; min-width: 8rem; border: none; background: transparent; padding: .25rem 0; outline: none; }
.rchip {
  display: inline-flex; align-items: center; gap: .2rem; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 999px; padding: 0 .2rem 0 .7rem; font-size: .95rem;
}
.rchip.bad { border-color: var(--draft); color: var(--draft); }
.rchip button { border: none; background: none; cursor: pointer; font-size: 1rem; padding: 0 .3rem; }
.cc-toggles { display: flex; gap: .2rem; }
.cc-toggles button { border: none; background: none; color: var(--muted); cursor: pointer; padding: .4rem .3rem; }
.cc-toggles button:hover { color: var(--text); text-decoration: underline; }
.suggest {
  position: absolute; top: 100%; left: 0; z-index: 30; background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow); list-style: none; margin: .2rem 0 0; padding: .3rem 0; min-width: 18rem;
}
.suggest li { padding: .4rem .9rem; cursor: pointer; }
.suggest li.on, .suggest li:hover { background: var(--hover); }
.suggest .sa { color: var(--muted); font-size: .9rem; display: block; }
.composer textarea {
  border: none; resize: vertical; padding: .8rem 1rem; min-height: 9rem; background: transparent;
  outline: none; line-height: 1.5;
}
.composer .atts { padding: 0 1rem .6rem; }
.composer-foot { display: flex; align-items: center; gap: .5rem; padding: .5rem .8rem; flex-wrap: wrap; }
/* The Send row comes before the attached files in the page, so Tab goes from the message to Send. It is shown below them. */
.composer .composer-foot { order: 1; }
.composer-foot .saved { color: var(--muted); font-size: .9rem; }
.composer-foot .spacer { flex: 1; }
.quote-note { color: var(--muted); font-size: .9rem; padding: 0 1rem .4rem; }

/* As wide as the window, so that two open messages share it and neither goes off its left edge. */
.composers {
  position: fixed; left: 1rem; right: 1rem; bottom: 0; display: flex; justify-content: flex-end; gap: 1rem;
  align-items: flex-end; z-index: 40; pointer-events: none;
}
.composers .composer {
  pointer-events: auto; width: min(40rem, calc(100vw - 2rem)); max-height: 85vh; margin: 0;
  border-radius: 12px 12px 0 0; border-bottom: none; overflow-y: auto; overflow-x: hidden;
  scroll-padding: 3.4rem 0 4.8rem;
}
/* The Send row and the title row stay in view whatever the window and text size: the middle scrolls. */
.composers .composer .composer-head { position: sticky; top: 0; z-index: 2; flex: none; }
.composers .composer .composer-foot {
  position: sticky; bottom: 0; z-index: 2; flex: none; background: var(--surface); border-top: 1px solid var(--line);
}
.composers .composer textarea { height: 14rem; min-height: 6rem; }
/* Nothing inside a composer is wider than the composer: fields shrink or wrap, they never scroll sideways. */
.field { flex-wrap: wrap; }
.field, .rcpts, .field input.subject { min-width: 0; }
.rcpts, .field input.subject { flex: 1 1 6rem; }
.rcpts input { min-width: min(8rem, 100%); }
.cc-toggles { margin-left: auto; }
.rchip { max-width: 100%; overflow-wrap: anywhere; }
.suggest { min-width: min(18rem, 100%); max-width: 100%; }
.suggest li { overflow-wrap: anywhere; }
.composer .att { max-width: min(20rem, 100%); min-width: 0; }
.composers .composer.minimized textarea, .composers .composer.minimized .field,
.composers .composer.minimized .composer-foot, .composers .composer.minimized .atts,
.composers .composer.minimized .quote-note { display: none; }
.composers .composer.minimized { width: 20rem; cursor: pointer; }
.composers .composer.full {
  position: fixed; inset: 3vh 5vw; width: auto; max-height: none; border-radius: 12px; border-bottom: 2px solid var(--accent);
}
.composers .composer.full textarea { flex: 1; }

/* ---------- toasts, modal */
.toasts { position: fixed; left: 1.2rem; bottom: 1.2rem; display: grid; gap: .6rem; z-index: 60; }
.toast {
  background: #1f2329; color: #fff; padding: .8rem 1.1rem; border-radius: 10px; box-shadow: var(--shadow);
  display: flex; gap: 1rem; align-items: center; font-size: 1rem; max-width: 34rem;
}
.toast.err { background: #8c1d18; }
.toast button { background: none; border: none; color: #8ab4f8; font-weight: 700; cursor: pointer; font-size: 1rem; }
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center; z-index: 70; padding: 1rem; }
.modal-card {
  background: var(--surface); border-radius: 16px; padding: 1.4rem; width: min(38rem, 100%);
  max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow);
}
.modal-card h2 { margin-top: 0; }
.modal-card label { display: block; font-weight: 600; margin: .8rem 0 .3rem; }
.modal-card label.tick { display: flex; gap: .6rem; align-items: center; margin-top: 1.2rem; cursor: pointer; }
.modal-card label.tick input { width: 1.4rem; height: 1.4rem; flex: none; }
.modal-card input[type=text], .modal-card input[type=datetime-local], .modal-card textarea, .modal-card select {
  width: 100%; padding: .55rem; border: 2px solid var(--line); border-radius: 8px; background: var(--surface);
}
.modal-actions { display: flex; justify-content: flex-end; gap: .6rem; margin-top: 1.2rem; flex-wrap: wrap; }
.pick-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .3rem; }
.pick-list button, .pick-list label {
  width: 100%; text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  padding: .55rem .8rem; cursor: pointer; display: flex; gap: .6rem; align-items: center; margin: 0; font-weight: 400;
}
.pick-list button:hover, .pick-list label:hover { background: var(--hover); }
.pick-list .sub { color: var(--muted); margin-left: auto; font-size: .9rem; }
.keys { display: grid; grid-template-columns: auto 1fr; gap: .3rem 1rem; }
kbd {
  border: 1px solid var(--line); border-bottom-width: 3px; border-radius: 6px; padding: 0 .4rem;
  font-family: inherit; font-weight: 700; background: var(--surface-2);
}

/* ---------- small screens */
@media (max-width: 800px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: 17rem; z-index: 50; background: var(--bg);
    box-shadow: var(--shadow); transform: translateX(-105%); transition: transform .2s;
  }
  .sidebar.open { transform: none; }
  .main { margin: .3rem 0 0; border-radius: 0; }
  .brand, .sync-status, #smaller-btn { display: none; }
  .row { grid-template-columns: auto minmax(0, 1fr) auto; grid-template-areas: "c who when" "c what what"; gap: .1rem .6rem; }
  .row .check { grid-area: c; } .row .star { display: none; }
  .row .who { grid-area: who; }
  .row .what { grid-area: what; white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  .row .when { grid-area: when; }
  .conv { padding: .4rem .7rem 3rem; }
  .msg-body, .atts, .msg-actions { padding-left: 1rem; }
  .msg-head { grid-template-columns: auto minmax(0, 1fr); gap: .3rem .7rem; padding: .6rem .8rem; }
  .msg-meta { grid-column: 1 / -1; justify-content: flex-end; }
  .msg-who .addr { display: block; overflow-wrap: anywhere; font-size: .8rem; }
  .composers { right: 0; left: 0; }
  .composers .composer { width: 100vw; scroll-padding-bottom: 7.2rem; }
}
