:root {
  --bg: #E9F1E5;
  --surface: #F2F7EF;
  --ink: #16202C;
  --muted: #505C68;
  --faint: #6B7783;
  --line: #D3DECF;
  --hover: #E0EBDB;
  --select: #D5E6CC;
  --accent: #6CC24A;
  --accent-ink: #0F141A;
  --accent-text: #2F6A1D;
  --danger: #C8102E;
  --focus: #2F6FD6;
  --shadow: 0 12px 40px rgba(22, 32, 44, .18);
  --radius: 10px;
  --side-w: 240px;
  --list-w: 380px;
  --font: system-ui, -apple-system, "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0F141A; --surface: #161D25; --ink: #E6EBF1; --muted: #9AA6B4; --faint: #6C7887;
    --line: #26303B; --hover: #1C2530; --select: #1F2D3D; --accent: #6CC24A; --accent-ink: #0F141A; --accent-text: #6CC24A; --leaf-opacity: .12; --danger: #E5484D; --focus: #6EA2F5;
    --shadow: 0 12px 40px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0F141A; --surface: #161D25; --ink: #E6EBF1; --muted: #9AA6B4; --faint: #6C7887;
  --line: #26303B; --hover: #1C2530; --select: #1F2D3D; --accent: #6CC24A; --accent-ink: #0F141A; --accent-text: #6CC24A; --leaf-opacity: .12; --danger: #E5484D; --focus: #6EA2F5;
  --shadow: 0 12px 40px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.45 var(--font); -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent; overscroll-behavior: none;
}
[hidden] { display: none !important; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 38px; padding: 0 16px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn:disabled { opacity: .6; cursor: default; }
.btn svg { width: 18px; height: 18px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 8px;
  border: 0; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn.sm { width: 28px; height: 28px; }
.icon-btn.sm svg { width: 16px; height: 16px; }
.icon-btn.on { color: #D99A00; }
.icon-btn.on svg { fill: currentColor; }
.link-btn { border: 0; background: none; padding: 6px 4px; color: var(--muted); font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }
.link-btn:hover { color: var(--ink); }
.link-btn.danger:hover { color: var(--danger); }

/* ---------- sign-in ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px 24px; display: grid; gap: 14px;
}
.login-mark { width: 44px; height: 44px; border-radius: 12px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.login h1 { margin: 4px 0 0; font-size: 24px; letter-spacing: -.01em; }
.login-note { margin: -8px 0 6px; color: var(--muted); }
.login label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.login input {
  height: 44px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font-size: 16px;
}
#login-code { letter-spacing: .3em; font-variant-numeric: tabular-nums; }
.login-error { margin: 0; color: var(--danger); font-size: 14px; }
.login-error:empty { display: none; }
.login .btn { height: 44px; }
.login-step { display: grid; gap: 14px; }
.login-step .link-btn { justify-self: center; }
.login-offer { margin: 0; color: var(--ink); }
.side-actions { display: flex; flex-wrap: wrap; column-gap: 12px; }

/* ---------- layout ---------- */
.app {
  height: 100%; display: grid;
  grid-template-columns: var(--side-w) var(--list-w) minmax(0, 1fr);
  padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right);
}
.sidebar { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--bg); padding-top: env(safe-area-inset-top); }
.side-head { padding: 14px 14px 8px; }
.compose-wide { width: 100%; }
.side-scroll { flex: 1; overflow: auto; padding: 4px 8px 12px; }
.side-heading { font-size: 13px; font-weight: 600; color: var(--faint); margin: 18px 10px 6px; }
.nav-list { list-style: none; margin: 0; padding: 0; }
.nav-item {
  width: 100%; display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 0; border-radius: 8px; background: none; text-align: left; color: var(--ink);
}
.nav-item:hover { background: var(--hover); }
.nav-item[aria-current="true"] { background: var(--select); font-weight: 600; }
.nav-item svg { color: var(--muted); width: 19px; height: 19px; }
.nav-item .count { margin-left: auto; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.nav-item .count.has { color: var(--accent-text); font-weight: 600; }
.nav-item .name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; margin: 0 5px; }
.side-foot { display: flex; flex-wrap: wrap; row-gap: 2px; align-items: center; justify-content: space-between; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.kbd-hint { flex-basis: 100%; padding: 0 4px; font-size: 12px; color: var(--faint); }

.list-pane { position: relative; display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--surface); padding-top: env(safe-area-inset-top); }
.list-head { display: flex; align-items: center; gap: 4px; padding: 10px 8px 4px 16px; }
.list-title { flex: 1; margin: 0; font-size: 22px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search { position: relative; margin: 4px 12px 8px; display: flex; align-items: center; }
.search > svg { position: absolute; left: 10px; width: 17px; height: 17px; color: var(--faint); pointer-events: none; }
.search input {
  width: 100%; height: 36px; padding: 0 34px 0 34px; border-radius: 8px; border: 1px solid transparent;
  background: var(--bg); font-size: 15px; -webkit-appearance: none; appearance: none;
}
.search input::-webkit-search-cancel-button { display: none; }
.search input:focus { border-color: var(--line); outline: none; background: var(--surface); }
.search .icon-btn { position: absolute; right: 4px; }
.banner { margin: 0 12px 8px; padding: 8px 12px; border-radius: 8px; font-size: 13px; background: color-mix(in srgb, var(--accent) 10%, var(--surface)); color: var(--ink); }

.message-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; overscroll-behavior: contain; }
.msg { position: relative; border-bottom: 1px solid var(--line); background: #1E8A5B; cursor: default; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.msg::after { /* revealed behind the row while swiping */
  content: "Archive"; position: absolute; right: 20px; top: 50%; transform: translateY(-50%); color: #fff; font-weight: 600; font-size: 14px;
}
.msg .row {
  position: relative; z-index: 1; display: grid; grid-template-columns: 1fr auto; gap: 1px 10px;
  padding: 11px 16px 12px 22px; background: var(--surface);
}
.msg .row::before { /* account colour edge */
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--edge, transparent);
}
.msg:hover .row { background: var(--hover); }
.msg[aria-selected="true"] .row { background: var(--select); }
.msg .who { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .when { font-size: 13px; color: var(--faint); font-variant-numeric: tabular-nums; text-align: right; }
.msg .subj { grid-column: 1; font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; gap: 6px; align-items: center; }
.msg .subj span { overflow: hidden; text-overflow: ellipsis; }
.msg .subj svg { width: 14px; height: 14px; color: var(--faint); }
.msg .subj svg.star { color: #D99A00; fill: currentColor; }
/* Mini bin under the date: a 32px tap area that doesn't make the row taller. */
.row-bin { grid-column: 2; justify-self: end; display: grid; place-items: center; width: 32px; height: 28px; margin: -5px -11px -5px 0;
  border: 0; border-radius: 6px; background: none; color: var(--faint); cursor: pointer; }
.row-bin svg { width: 16px; height: 16px; }
.row-bin:hover, .row-bin:focus-visible { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.msg.unread .who { font-weight: 700; }
.msg.unread .subj { color: var(--ink); }
.msg.unread .row::after { /* unread dot */
  content: ""; position: absolute; left: 10px; top: 19px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent-text);
}
.list-foot { padding: 12px; text-align: center; color: var(--muted); font-size: 14px; }
.list-foot:empty { display: none; }
.empty-state { padding: 48px 24px; text-align: center; color: var(--muted); }
.empty-state svg { width: 32px; height: 32px; color: var(--faint); }
.fab {
  position: absolute; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 5; width: 56px; height: 56px; border-radius: 16px; border: 0; background: var(--accent); color: var(--accent-ink);
  box-shadow: var(--shadow); display: grid; place-items: center;
}
.fab svg { width: 24px; height: 24px; }

/* ---------- reader ---------- */
.reader { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--surface); padding-top: env(safe-area-inset-top); }
.reader-empty { margin: auto; text-align: center; color: var(--faint); }
/* Faint leaf emblem in the empty reading area */
.reader-empty .leaf-mark { width: 320px; max-width: 60vw; height: auto; aspect-ratio: 1; display: block; margin: 0 auto;
  color: var(--accent); opacity: var(--leaf-opacity, .15); stroke-width: 2; }
.reader-body { flex: 1; overflow-y: auto; min-height: 0; overscroll-behavior: contain; }
.toolbar {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 2px;
  padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.toolbar .grow { flex: 1; }
.toolbar .back { display: none; }
.read-head { padding: 20px 28px 8px; max-width: 900px; }
.read-subject { margin: 0 0 16px; font-size: 22px; line-height: 1.25; letter-spacing: -.01em; overflow-wrap: anywhere; }
.read-meta { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 12px; align-items: start; }
.avatar { grid-row: span 2; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 15px; }
.read-from { font-weight: 600; overflow-wrap: anywhere; }
.read-from .addr { font-weight: 400; color: var(--muted); }
.read-to { grid-column: 2; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.read-date { font-size: 13px; color: var(--faint); white-space: nowrap; grid-row: 1; grid-column: 3; }
.acct-tag { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-top: 10px; }
.acct-tag i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.images-bar {
  margin: 12px 28px 0; max-width: 844px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 12px; border-radius: 8px; background: var(--bg); font-size: 13px; color: var(--muted);
}
.images-bar svg { width: 18px; height: 18px; }
.images-bar .link-btn { color: var(--focus); font-weight: 600; font-size: 13px; padding: 2px; }
.read-content { padding: 16px 28px 32px; max-width: 900px; }
.mail-frame { width: 100%; border: 0; display: block; background: #fff; border-radius: 6px; min-height: 60px; }
.mail-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; line-height: 1.55; margin: 0; font-family: var(--font); }
.mail-text a { color: var(--focus); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.read-files { padding: 0 28px 24px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; max-width: 280px; padding: 7px 12px;
  border: 1px solid var(--line); border-radius: 8px; font-size: 13px; color: var(--ink); text-decoration: none; background: var(--surface);
}
.chip:hover { background: var(--hover); }
.chip svg { width: 16px; height: 16px; color: var(--muted); }
.chip .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip .fsize { color: var(--faint); white-space: nowrap; }
.chip button { border: 0; background: none; padding: 0; color: var(--muted); display: grid; }
.quick-reply { padding: 0 28px calc(28px + env(safe-area-inset-bottom)); display: flex; gap: 8px; flex-wrap: wrap; }
.loading { padding: 40px; text-align: center; color: var(--faint); }

/* ---------- compose ---------- */
.compose { position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; justify-content: flex-end; padding: 16px; pointer-events: none; }
.compose-card {
  pointer-events: auto; width: 600px; max-width: 100%; height: min(640px, calc(100vh - 32px));
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.compose-head { display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 6px; border-bottom: 1px solid var(--line); }
.compose-head h2 { flex: 1; margin: 0; font-size: 16px; }
.field { display: flex; align-items: center; gap: 8px; padding: 0 14px; border-bottom: 1px solid var(--line); min-height: 44px; }
.field label { width: 62px; color: var(--muted); font-size: 14px; flex: none; }
.field input, .field select { flex: 1; min-width: 0; border: 0; background: none; height: 42px; font-size: 15px; outline: none; }
.field select { -webkit-appearance: none; appearance: none; cursor: pointer; }
#c-body { flex: 1; border: 0; resize: none; padding: 14px; background: none; outline: none; font: 15px/1.55 var(--font); }
#c-files { padding: 0 14px 8px; }
#c-files:empty { display: none; }
.compose-foot { display: flex; align-items: center; gap: 12px; padding: 8px 14px calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.attach-btn { cursor: pointer; }
.c-note { flex: 1; font-size: 13px; color: var(--faint); }

/* ---------- toast / help ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50;
  display: flex; align-items: center; gap: 16px; padding: 10px 12px 10px 16px; border-radius: 10px;
  background: var(--ink); color: var(--bg); box-shadow: var(--shadow); font-size: 14px;
  width: max-content; max-width: calc(100vw - 32px); /* fixed + left 50% would otherwise cap it at half the screen */
}
.toast button { white-space: nowrap; border: 0; background: none; color: inherit; font-weight: 700; padding: 4px 6px; text-decoration: underline; text-underline-offset: 3px; }
.help { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.35); display: grid; grid-template-columns: minmax(0, 1fr); place-items: center; padding: 16px; }
.help-card { background: var(--surface); border-radius: 12px; padding: 20px 24px; width: min(380px, 100%); box-shadow: var(--shadow); }
.help-card h2 { margin: 0 0 12px; font-size: 17px; }
.help-card dl { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; margin: 0 0 16px; font-size: 14px; }
.help-card dt { font-weight: 600; font-variant-numeric: tabular-nums; }
.help-card dd { margin: 0; color: var(--muted); }

/* ---------- dialog: new / rename / delete folder, move to ---------- */
.dlg-card { display: grid; gap: 12px; }
.dlg-card h2 { margin: 0; }
.dlg-text { margin: 0; color: var(--muted); font-size: 14px; }
.dlg-text:empty, .dlg-list:empty { display: none; }
#dlg-input { height: 44px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); font-size: 16px; width: 100%; }
.dlg-list { display: grid; gap: 2px; max-height: min(50vh, 360px); overflow-y: auto; margin: 0 -8px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn.danger { background: #C8102E; border-color: #C8102E; color: #fff; } /* 5.9:1 in both themes */

/* ---------- your own folders ---------- */
.nav-sep { height: 1px; background: var(--line); margin: 8px 10px; }
.nav-item.new-folder { color: var(--muted); }
.nav-item .count.unblock { color: var(--danger); font-weight: 600; }
/* ---------- inbox colours ---------- */
.colour-row { display: grid; gap: 6px; padding: 6px 8px 10px; }
.colour-row .who { font-size: 14px; font-weight: 600; display: flex; gap: 8px; align-items: center; }
.colour-row .who small { font-weight: 400; color: var(--muted); }
.palette { display: grid; grid-template-columns: repeat(5, 32px); gap: 6px 8px; }
.palette button { width: 32px; height: 32px; border-radius: 8px; border: 2px solid transparent; padding: 0; display: grid; place-items: center; background: none; }
.palette button i { width: 22px; height: 22px; border-radius: 6px; display: block; }
.palette button[aria-pressed="true"] { border-color: var(--ink); }
.nav-item.drop { background: var(--select); box-shadow: inset 0 0 0 2px var(--accent); }
.side-note { font-size: 13px; color: var(--faint); margin: 4px 10px 0; }
.msg { -webkit-touch-callout: none; }
.msg.dragging { opacity: .5; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 19; }
.only-narrow { display: none; }

/* ---------- medium: sidebar becomes a drawer ---------- */
/* ---------- slim rail (phone and tablet): inboxes and folders, one tap each ---------- */
.rail { display: none; }
@media (max-width: 1100px) {
  .rail {
    display: flex; flex-direction: column; align-items: center; gap: 2px; overflow-y: auto; overscroll-behavior: contain;
    padding: calc(8px + env(safe-area-inset-top)) 0 calc(8px + env(safe-area-inset-bottom));
    background: var(--bg); border-right: 1px solid var(--line); scrollbar-width: none;
  }
  .rail::-webkit-scrollbar { display: none; }
  .rail-btn { position: relative; flex: none; width: 44px; height: 44px; display: grid; place-items: center; border: 0; border-radius: 10px; background: none; color: var(--muted); }
  .rail-btn:hover { background: var(--hover); }
  .rail-btn[aria-current="true"] { background: var(--select); color: var(--ink); }
  .rail-btn svg { width: 21px; height: 21px; }
  .rail-btn .swatch { width: 14px; height: 14px; margin: 0; border-radius: 4px; }
  .rail-btn .dot { position: absolute; top: 6px; right: 6px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent-text); box-shadow: 0 0 0 2px var(--bg); }
  .rail-sep { flex: none; width: 28px; height: 1px; margin: 6px 0; background: var(--line); }
}
@media (max-width: 1100px) {
  .app { grid-template-columns: 52px var(--list-w) minmax(0, 1fr); }
  .sidebar {
    position: fixed; z-index: 20; top: 0; bottom: 0; left: 0; width: min(300px, 86vw);
    transform: translateX(-102%); transition: transform .22s ease, box-shadow .22s ease; background: var(--surface);
  }
  .app.drawer .sidebar { transform: none; box-shadow: var(--shadow); }
  .only-narrow { display: inline-grid; }
  .kbd-hint { display: none; }
  .fab { display: none; }
}

/* ---------- phone: one screen at a time ---------- */
@media (max-width: 720px) {
  .app { grid-template-columns: 52px minmax(0, 1fr); }
  .list-pane { border-right: 0; }
  .fab { display: grid; }
  .reader {
    position: fixed; inset: 0; z-index: 10; transform: translateX(100%); transition: transform .24s ease;
    padding-top: env(safe-area-inset-top);
  }
  .app.reading .reader { transform: none; }
  .toolbar .back { display: inline-grid; }
  .read-head, .read-content, .read-files, .quick-reply { padding-left: 16px; padding-right: 16px; }
  .images-bar { margin-left: 16px; margin-right: 16px; }
  .read-subject { font-size: 20px; }
  .compose { padding: 0; }
  .compose-card { width: 100%; height: 100%; border-radius: 0; border: 0; padding-top: env(safe-area-inset-top); }
  .msg .row { padding-top: 12px; padding-bottom: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
