:root {
  color-scheme: light dark;
  --bg: #f6f5f1;
  --surface: #ffffff;
  --ink: #1b1d21;
  --muted: #666b74;
  --line: #e2e0d9;
  --accent: #1f4fd8;
  --accent-ink: #ffffff;
  --accent-soft: #e9eefc;
  --ok: #17663a;
  --ok-soft: #e1f3e8;
  --warn: #7a5200;
  --warn-soft: #fbf0d3;
  --bad: #a32020;
  --bad-soft: #fbe6e4;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131417;
    --surface: #1c1e22;
    --ink: #ecedef;
    --muted: #9a9fa8;
    --line: #2e3138;
    --accent: #7c9cff;
    --accent-ink: #0d1020;
    --accent-soft: #232b47;
    --ok: #7fd8a2;
    --ok-soft: #173424;
    --warn: #f0c768;
    --warn-soft: #3a2f12;
    --bad: #ff9b93;
    --bad-soft: #401c1a;
  }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
h1 { font-size: 1.5rem; line-height: 1.25; margin: 0; letter-spacing: -0.01em; overflow-wrap: anywhere; }
code { font-family: var(--mono); font-size: 0.9em; }
.muted { color: var(--muted); }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar__inner {
  max-width: 960px; margin: 0 auto; padding: 10px 20px;
  display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
}
.topbar__end { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.whoami { color: var(--muted); font-size: 0.9rem; }
.brand {
  display: inline-flex; align-items: center; gap: 9px;
  font-weight: 650; color: var(--ink); text-decoration: none; letter-spacing: -0.01em;
}
.brand--large { font-size: 1.1rem; margin: 0 0 20px; }
.brand__mark {
  width: 18px; height: 13px; border: 2px solid var(--accent); border-radius: 3px;
  position: relative; display: inline-block;
}
.brand__mark::after {
  content: ""; position: absolute; left: 2px; right: 2px; top: -3px; height: 9px;
  border: 2px solid var(--accent); border-top: 0; border-left-color: transparent; border-right-color: transparent;
  transform: perspective(20px) rotateX(-40deg); border-radius: 0 0 6px 6px;
}
.topnav { display: flex; gap: 4px; }
.topnav a {
  padding: 6px 10px; border-radius: 7px; color: var(--muted); text-decoration: none; font-weight: 500;
}
.topnav a:hover { color: var(--ink); }
.topnav a[aria-current="page"] { color: var(--ink); background: var(--bg); }

/* Page */
.page { max-width: 960px; margin: 0 auto; padding: 28px 20px 64px; }
.pagehead {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px;
}
.pagehead__actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.centered .page { max-width: none; min-height: 100vh; display: grid; place-content: center; padding: 20px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px;
}
.card--narrow { width: min(380px, calc(100vw - 40px)); }
.card__title { font-size: 1.3rem; margin-bottom: 4px; }
.card--narrow .muted { margin: 0 0 20px; }
.stack { display: grid; gap: 16px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 38px; padding: 0 16px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 550; text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn--primary:hover { filter: brightness(1.08); border-color: var(--accent); }
.btn--quiet { border-color: transparent; background: transparent; color: var(--muted); padding: 0 10px; }
.btn--quiet:hover { color: var(--ink); border-color: transparent; }
.btn--danger { color: var(--bad); }
.btn--block { width: 100%; }
form[aria-busy="true"] .btn { opacity: 0.6; pointer-events: none; }

/* Flash messages */
.flashes { margin-bottom: 20px; display: grid; gap: 8px; }
.flash {
  margin: 0 0 16px; padding: 11px 14px; border-radius: 8px;
  background: var(--accent-soft); color: var(--ink); overflow-wrap: anywhere;
}
.flashes .flash { margin: 0; }
.flash--success { background: var(--ok-soft); color: var(--ok); }
.flash--warning { background: var(--warn-soft); color: var(--warn); }
.flash--error { background: var(--bad-soft); color: var(--bad); }
.flash a { color: inherit; font-weight: 600; }

/* List toolbar */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs a {
  padding: 6px 12px; border-radius: 999px; text-decoration: none; color: var(--muted); font-weight: 550;
}
.tabs a:hover { color: var(--ink); }
.tabs a[aria-current="page"] { background: var(--ink); color: var(--bg); }
.tabs__count { font-variant-numeric: tabular-nums; opacity: 0.7; margin-left: 2px; }
.search input { width: 260px; max-width: 100%; }

/* Mail list */
.maillist {
  list-style: none; margin: 0; padding: 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.maillist li + li { border-top: 1px solid var(--line); }
.mailrow {
  display: grid; grid-template-columns: 76px minmax(0, 1.1fr) minmax(0, 2fr) auto;
  align-items: center; gap: 14px; padding: 13px 18px; color: inherit; text-decoration: none;
}
.mailrow:hover { background: var(--bg); }
.mailrow__to, .mailrow__subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailrow__to { font-weight: 600; }
.mailrow__date { color: var(--muted); font-size: 0.87rem; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Inbox rows: a dot instead of a status pill; unread rows are bold. */
.mailrow--inbox { grid-template-columns: 8px minmax(0, 1.1fr) minmax(0, 2fr) auto; }
.mailrow--inbox .mailrow__to { font-weight: 450; }
.mailrow__dot { width: 8px; height: 8px; border-radius: 50%; }
.mailrow--unread .mailrow__dot { background: var(--accent); }
.mailrow--unread .mailrow__to, .mailrow--unread .mailrow__subject { font-weight: 700; }

.pill {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 0.78rem; font-weight: 650; text-align: center;
  background: var(--warn-soft); color: var(--warn);
}
.pill--sent { background: var(--ok-soft); color: var(--ok); }
.pill--failed { background: var(--bad-soft); color: var(--bad); }

.pager { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; margin-top: 18px; }
.pager > :last-child { justify-self: end; }
.pager > :first-child { justify-self: start; }

.empty {
  text-align: center; padding: 56px 20px; color: var(--muted);
  background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius);
}
.empty p { margin: 0 0 16px; }

/* Forms */
.field { display: grid; gap: 6px; }
.field + .field { margin-top: 18px; }
.field__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
label { font-weight: 600; font-size: 0.9rem; }
input[type="text"], input[type="password"], input[type="email"], input[type="search"], select, textarea {
  width: 100%; padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
textarea { font-family: var(--mono); font-size: 0.88rem; line-height: 1.55; resize: vertical; min-height: 260px; }
.hint { margin: 0; color: var(--muted); font-size: 0.85rem; }
.field--error input, .field--error select, .field--error textarea { border-color: var(--bad); }
.field__error { margin: 0; color: var(--bad); font-size: 0.87rem; font-weight: 550; }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.segmented button {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 0.85rem; font-weight: 550;
  padding: 4px 12px; cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--ink); color: var(--bg); }

.preview {
  width: 100%; height: 420px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
.preview--detail { height: 520px; margin-top: 20px; }

.actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 22px; }
.actions__end { margin-left: auto; }

.subhead { font-size: 0.95rem; margin: 22px 0 8px; }
.attachments { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.attachments li { overflow-wrap: anywhere; }
.attachments .muted { font-size: 0.87rem; margin-left: 6px; }
.card > .flash { margin: 18px 0 0; }
.card > .flash + .preview--detail { margin-top: 12px; }

/* Detail meta */
.meta { margin: 0; display: grid; gap: 10px; }
.meta > div { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 12px; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; overflow-wrap: anywhere; }

@media (max-width: 640px) {
  .page { padding: 20px 14px 48px; }
  .card { padding: 18px; }
  .topbar__inner { gap: 12px; padding: 10px 14px; }
  .whoami { display: none; }
  .mailrow {
    grid-template-columns: auto 1fr; grid-template-areas: "to date" "subject subject" "pill pill";
    gap: 4px 10px; padding: 12px 14px;
  }
  .mailrow--inbox { grid-template-columns: 8px 1fr auto; grid-template-areas: "dot to date" ". subject subject"; }
  .mailrow__dot { grid-area: dot; }
  .mailrow__to { grid-area: to; }
  .mailrow__date { grid-area: date; }
  .mailrow__subject { grid-area: subject; }
  .mailrow .pill { grid-area: pill; justify-self: start; margin-top: 4px; }
  .search, .search input { width: 100%; }
  .meta > div { grid-template-columns: 1fr; gap: 0; }
  .actions__end { margin-left: 0; }
  .preview--detail { height: 420px; }
}

.stack .field + .field { margin-top: 0; }
