:root {
  --rail: #1e222b;
  --rail-ink: #f4f1ea;
  --rail-mute: #c5cad3;
  --rail-line: #323844;
  --rail-hover: #2a303b;
  --rail-active: #3a4658;
  --list: #f3f4f7;
  --paper: #fbfaf7;
  --ink: #1b1d22;
  --mute: #66707c;
  --line: #e2e5eb;
  --mark: #2553c7;
  --gold: #8a6a12;
  --gold-wash: #f4ead0;
  --stamp: #9b2c2c;
  --miss: #f6e4e2;
  --slip: #1f5a3d;
  --slip-paper: #e6f2ea;
  --select: #fff;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  overflow: hidden;
  background: var(--list);
  color: var(--ink);
  font-family: "Be Vietnam Pro", "Segoe UI", sans-serif;
  font-size: 13.5px;
  line-height: 1.45;
}

.gate {
  display: none;
}
body.gated {
  background: var(--rail);
  color: var(--rail-ink);
}
body.gated :focus-visible { outline-color: var(--rail-ink); }
body.gated .rail,
body.gated .app,
body.gated .overlay,
body.gated .drawer { display: none !important; }
body.gated .gate {
  display: flex;
  width: 100%;
  min-height: 100%;
  padding: 48px 40px 56px;
  align-items: flex-end;
}
.gate-copy { width: min(22rem, 100%); }
.gate h1 {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 4rem;
  font-weight: 600;
  letter-spacing: -0.04em;
  line-height: 0.9;
}
#gate-lead {
  margin-top: 14px;
  max-width: 28ch;
  color: var(--rail-mute);
}
.gate .field { margin-top: 16px; }
.gate .field input {
  background: var(--rail-hover);
  border-color: var(--rail-line);
  color: var(--rail-ink);
}
.gate .btn.primary {
  margin-top: 18px;
  background: var(--rail-ink);
  border-color: var(--rail-ink);
  color: var(--rail);
}
.gate .btn.primary:hover { background: #fff; }
.gate .form-error { color: #f0b4b0; }

h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }

:focus-visible {
  outline: 2px solid var(--mark);
  outline-offset: 2px;
}

::selection { background: #d5e0f5; }

.rail {
  width: 228px;
  flex: none;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: var(--rail);
  color: var(--rail-ink);
}

.rail-head,
.menu-btn,
#close-menu { display: none; }
.rail-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--rail);
}
.rail-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  flex: none;
}
.rail-title {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1;
}

.brand { padding: 22px 20px 18px; }
.brand h1 {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 1.75rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1;
}
#account {
  margin-top: 7px;
  color: var(--rail-mute);
  font-size: 0.78rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-scroll {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
}

.nav-block {
  padding: 10px 12px 4px;
}
.nav-block + .nav-block {
  margin-top: 8px;
  padding-top: 14px;
  border-top: 1px solid var(--rail-line);
}
.nav-block[hidden] { display: none; }
.nav-label {
  padding: 0 10px 8px;
  color: var(--rail-mute);
  font-size: 0.78rem;
}
#tag-filters, #filters {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item, .nav-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--rail-ink);
  text-align: left;
  padding: 8px 10px;
  border-radius: 7px;
  cursor: pointer;
}
.nav-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.nav-item:hover, .nav-btn:hover { background: var(--rail-hover); }
.nav-item[aria-pressed="true"],
.nav-btn[aria-expanded="true"] { background: var(--rail-active); }
.nav-count {
  color: var(--rail-mute);
  font-variant-numeric: tabular-nums;
  font-size: 0.78rem;
}
.nav-item[aria-pressed="true"] .nav-count { color: var(--rail-ink); }

.rail-foot {
  margin-top: auto;
  padding: 12px 12px 16px;
  border-top: 1px solid var(--rail-line);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.app {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 10px 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.topbar #status {
  color: var(--mute);
  font-size: 0.8rem;
  white-space: nowrap;
}
@media (max-width: 1100px) {
  .topbar #status { display: none; }
}

.search {
  flex: 1;
  min-width: 0;
  max-width: 560px;
  border: 0;
  background: var(--list);
  padding: 9px 14px;
  border-radius: 999px;
}
.search:focus {
  outline: none;
  background: #fff;
  box-shadow: 0 0 0 2px var(--mark);
}

.banner {
  margin: 0;
  padding: 9px 20px;
  background: var(--miss);
  color: var(--stamp);
}
.banner[hidden] { display: none; }

.workspace {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

.list {
  width: min(400px, 36vw);
  min-width: 300px;
  flex: none;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--list);
}

.list-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 10px 12px 8px 8px;
}
.list-copy { flex: 1; min-width: 0; }
.list-head.choosing .list-copy { display: none; }
.list-head h2 {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.list-meta {
  color: var(--mute);
  font-size: 0.76rem;
  font-variant-numeric: tabular-nums;
}

.bulk {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  flex: 1;
  min-width: 0;
}
.bulk[hidden] { display: none; }
#bulk-count { font-weight: 600; }

.pick {
  display: grid;
  place-items: center;
  width: 28px;
  height: 32px;
  margin-top: 2px;
  flex: none;
}
.pick.all { margin: 0; height: auto; }
.row-check, #select-all {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--mark);
  cursor: pointer;
}
#select-all:disabled { opacity: 0.4; cursor: default; }

.btn {
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: #f4f5f7; }
.btn.primary {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}
.btn.primary:hover { background: #2a2e36; }
.btn:disabled { opacity: 0.5; cursor: wait; }
.btn[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

.rows { overflow-x: hidden; overflow-y: auto; flex: 1; min-height: 0; }

.row {
  position: relative;
  display: grid;
  grid-template-columns: 28px 36px 1fr auto;
  grid-template-areas: "check avatar from date" "check avatar subject subject" "check avatar preview preview";
  column-gap: 8px;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  padding: 11px 16px 11px 6px;
  cursor: pointer;
}
.row .pick { grid-area: check; }
.row:hover { background: rgba(255, 255, 255, 0.72); }
.row.checked { background: #e4ebf7; }
.row[aria-current="true"] {
  background: var(--select);
  box-shadow: inset 3px 0 0 var(--mark);
}
.row[aria-current="true"].checked { background: #d9e4f6; }
.row.unread .row-from,
.row.unread .row-subject { font-weight: 600; }
.row.unread::before {
  content: "";
  position: absolute;
  left: 0;
  top: 12px;
  bottom: 12px;
  width: 3px;
  background: var(--mark);
  border-radius: 0 2px 2px 0;
}
.row[aria-current="true"].unread::before { display: none; }
.row.important .row-date { color: var(--gold); font-weight: 600; }

.row .avatar { grid-area: avatar; }
.avatar {
  width: 32px;
  height: 32px;
  margin-top: 2px;
  border-radius: 50%;
  background: #d5dae3;
  color: #3d4450;
  display: grid;
  place-items: center;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0;
  line-height: 1;
  white-space: nowrap;
}
.avatar.lg { width: 40px; height: 40px; font-size: 0.8rem; margin: 0; }

.row-from, .row-subject, .row-preview {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-from { grid-area: from; min-width: 0; }
.row-date {
  grid-area: date;
  color: var(--mute);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  font-size: 0.78rem;
}
.row-subject { grid-area: subject; margin-top: 1px; }
.row-preview { grid-area: preview; margin-top: 2px; color: var(--mute); font-size: 0.82rem; }

.tag {
  display: inline-block;
  margin-right: 6px;
  padding: 0 7px;
  border-radius: 999px;
  background: #e7eaef;
  color: #4c5562;
  font-size: 0.72rem;
  line-height: 1.6;
}
.tag.spam, .tag.marketing { background: var(--miss); color: var(--stamp); }
.tag.important, .tag.security { background: var(--gold-wash); color: var(--gold); }
.tag.work, .tag.finance, .tag.receipt { background: #e4ecf8; color: #274a86; }

.empty-list { margin: 28px 18px; max-width: 34ch; color: var(--mute); }

.letter {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--paper);
}

.letter-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-height: 44px;
  padding: 6px 20px;
  border-bottom: 1px solid transparent;
}
.letter:has(#letter-empty:not([hidden])) .letter-bar {
  min-height: 0;
  padding: 0;
  overflow: hidden;
}
.letter:has(#letter-content:not([hidden])) .letter-bar {
  border-bottom-color: var(--line);
  background: rgba(255, 255, 255, 0.55);
}
.back {
  display: none;
  border: 1px solid var(--line);
  background: #fff;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.letter-actions {
  display: flex;
  flex: 1;
  flex-wrap: nowrap;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.letter-actions .btn[hidden] { display: none; }
#compose-open[hidden] { display: none; }
.letter-more-btn { display: none; }
.letter-more-menu { display: contents; }
.views { display: flex; gap: 6px; margin-left: auto; }
.views[hidden] { display: none; }

.letter-empty {
  margin: auto;
  padding: 24px;
  text-align: center;
}
.letter-empty p:first-child {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 1.55rem;
  font-weight: 500;
}
.letter-empty .quiet { margin-top: 8px; }

.letter-empty[hidden], .letter-status[hidden], .letter-scroll[hidden] { display: none; }
.letter-status { margin: 28px 32px; color: var(--mute); }

.letter-scroll {
  flex: 1;
  overflow: auto;
  min-height: 0;
  padding: 18px 40px 64px;
}

.letter-subject {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: clamp(1.5rem, 2vw, 1.95rem);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: -0.02em;
  margin: 0 0 18px;
  text-wrap: pretty;
  max-width: 22em;
}

.who {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 12px;
  align-items: start;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}
.who-copy { min-width: 0; }
.letter-from { font-weight: 600; }
.letter-email, .letter-date, .quiet { color: var(--mute); }
.letter-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.letter-date { font-variant-numeric: tabular-nums; }

.letter-tags { margin-top: 14px; }
.letter-tags[hidden] { display: none; }
.letter-tags .quiet { margin-top: 8px; }
.tag-actions { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 10px; }

.slip {
  margin-top: 14px;
  padding: 10px 14px;
  background: var(--slip-paper);
  border-radius: 8px;
  border-left: 3px solid var(--slip);
}
.slip.miss { background: var(--miss); border-left-color: var(--stamp); }
.slip p + p { margin-top: 2px; }
.slip-title { font-weight: 600; color: var(--slip); }
.slip.miss .slip-title { color: var(--stamp); }

.files { list-style: none; margin: 16px 0 0; padding: 0; }
.files li { margin: 6px 0; }
.files a { color: var(--mark); text-underline-offset: 3px; }

.message-frame {
  width: 100%;
  min-height: 0;
  margin-top: 18px;
  border: 0;
  background: transparent;
}
.message-frame[hidden], .letter-text[hidden] { display: none; }

.letter-text {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 1.12rem;
  line-height: 1.65;
  max-width: 66ch;
  white-space: pre-wrap;
  margin-top: 18px;
}

.overlay:not([hidden]) {
  position: fixed;
  inset: 0;
  z-index: 2;
  background: rgba(18, 20, 26, 0.28);
}

.drawer:not([hidden]) {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(420px, 100%);
  z-index: 4;
  display: flex;
  background: #fff;
  border-left: 1px solid var(--line);
  box-shadow: -18px 0 48px rgba(18, 20, 26, 0.14);
}
.compose-drawer:not([hidden]) { width: min(600px, 100%); }

.ask {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.ask-field textarea {
  min-height: 64px;
}
.ask-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}
.ask-row .text-btn { color: var(--mute); }
.ask-row .text-btn:hover { color: var(--ink); }
#ask-status { margin-top: 8px; }
#ask-status[hidden] { display: none; }

.quote-fold {
  margin-top: 12px;
  color: var(--mute);
}
.quote-fold[hidden] { display: none; }
.quote-fold summary { cursor: pointer; }
.quote-fold pre {
  margin: 8px 0 0;
  max-height: 180px;
  overflow: auto;
  white-space: pre-wrap;
  font: inherit;
  color: var(--mute);
}

.rules-scroll {
  overflow: auto;
  width: 100%;
  padding: 22px 24px 48px;
}

.drawer-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}
.drawer h2 {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 1.7rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.icon-btn {
  border: 0;
  background: none;
  padding: 0;
  color: var(--mute);
  cursor: pointer;
}
.icon-btn:hover { color: var(--ink); }
.intro { margin-top: 8px; max-width: 38ch; color: var(--mute); }

.section-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-top: 28px;
}
.drawer h3 { font-size: 0.92rem; margin-top: 28px; }
.section-row h3 { margin-top: 0; }
#inbox-editor .smtp-head { margin-top: 22px; }

.caught {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  padding: 12px 0;
  cursor: pointer;
}
.caught-value {
  font-family: "Source Serif 4", Georgia, serif;
  font-size: 2rem;
  line-height: 1;
}

.rule { padding: 14px 0; border-bottom: 1px solid var(--line); }
.rule.off { opacity: 0.5; }
.rule h3 { margin: 0 0 4px; font-size: 0.95rem; }
.rule p { max-width: 38ch; color: var(--mute); }
.rule-controls, .form-actions { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 8px; }

.text-btn {
  border: 0;
  background: none;
  padding: 0;
  color: var(--mark);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

#editor, #inbox-editor { margin-top: 14px; }
#editor[hidden], #inbox-editor[hidden] { display: none; }
#editor h3, #inbox-editor h3 { margin-top: 0; }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 12px;
}
.field[hidden] { display: none; }
.field input, .field select, .field textarea {
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px 10px;
  border-radius: 8px;
  width: 100%;
}
.field textarea {
  min-height: 220px;
  resize: vertical;
  line-height: 1.5;
}

.check { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.check[hidden] { display: none; }
.form-error { color: var(--stamp); margin-top: 10px; }
.form-error[hidden] { display: none; }
.activity { padding: 10px 0; border-bottom: 1px solid var(--line); }
.activity.miss .activity-summary { color: var(--stamp); }
#run-saved { margin-top: 18px; }
#run-result { margin-top: 8px; }

@media (max-width: 960px) {
  .rail { width: 196px; }
  .list { width: min(340px, 42vw); min-width: 260px; }
  .letter-scroll { padding: 16px 20px 40px; }
}

@media (max-width: 800px) {
  body { flex-direction: column; }
  .rail {
    z-index: 3;
    width: auto;
    flex: none;
    overflow: visible;
    background: var(--rail);
  }
  .rail-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    position: relative;
    z-index: 2;
    min-height: 48px;
    padding: 0 10px 0 16px;
    background: var(--rail);
  }
  .menu-btn {
    display: inline-flex;
    align-items: center;
    border: 1px solid var(--rail-line);
    background: var(--rail-hover);
    color: var(--rail-ink);
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    white-space: nowrap;
  }
  .menu-btn[aria-expanded="true"] { background: var(--rail-active); }
  .rail-panel {
    position: fixed;
    top: 48px;
    right: auto;
    bottom: 0;
    left: 0;
    z-index: 1;
    width: min(20rem, 82vw);
    max-width: 320px;
    flex: none;
    overflow: hidden;
    padding-bottom: env(safe-area-inset-bottom);
    border-right: 1px solid var(--rail-line);
    box-shadow: 18px 0 48px rgba(18, 20, 26, 0.28);
    transform: translateX(-105%);
    visibility: hidden;
    pointer-events: none;
    transition: transform 0.22s ease, visibility 0.22s;
  }
  body.menu-open .rail-panel {
    transform: none;
    visibility: visible;
    pointer-events: auto;
  }
  #close-menu {
    display: inline;
    flex: none;
    padding: 22px 16px 0 0;
    color: var(--rail-mute);
  }
  #close-menu:hover { color: var(--rail-ink); }
  .nav-item, .nav-btn { min-height: 44px; padding: 12px 10px; }
  .topbar {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    min-height: 0;
    padding: 8px;
  }
  .search {
    grid-column: 1 / -1;
    max-width: none;
  }
  .topbar .btn { padding: 10px 12px; }
  .list { width: 100%; min-width: 0; border-right: 0; }
  .row { padding: 14px 16px 14px 6px; }
  body:not(.reading) .letter { display: none; }
  body.reading .list { display: none; }
  body.reading .back { display: inline-flex; }
  .letter-bar {
    flex-wrap: nowrap;
    justify-content: flex-start;
    align-items: center;
    gap: 6px;
    min-height: 0;
    padding: 6px 10px;
  }
  .letter-actions {
    flex-wrap: nowrap;
    gap: 6px;
  }
  .letter-bar .btn,
  .letter-bar .back {
    padding: 6px 9px;
    font-size: 0.8rem;
    border-radius: 6px;
  }
  .letter-actions { position: relative; }
  .letter-more-btn { display: inline-flex; }
  .letter-more-btn.empty { display: none; }
  .letter-more-menu {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 5;
    min-width: 11rem;
    padding: 8px;
    background: #fff;
    border: 1px solid var(--line);
    box-shadow: 0 16px 40px rgba(18, 20, 26, 0.14);
  }
  .letter-actions.more-open .letter-more-menu {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
  }
  .letter-more-menu .btn,
  .letter-more-menu .views {
    width: 100%;
    margin-left: 0;
    justify-content: flex-start;
  }
  .letter-more-menu .views { flex-direction: column; }
  .letter-scroll { padding: 16px 16px calc(40px + env(safe-area-inset-bottom)); }
  .letter-subject { font-size: 1.4rem; max-width: none; }
  .who {
    grid-template-columns: auto 1fr;
  }
  .letter-date { grid-column: 2; }
  body.rules-open .list,
  body.rules-open .letter,
  body.inboxes-open .list,
  body.inboxes-open .letter,
  body.password-open .list,
  body.password-open .letter,
  body.compose-open .list,
  body.compose-open .letter { display: none; }
  body.gated .gate { padding: 28px 20px calc(36px + env(safe-area-inset-bottom)); }
  body.gated .gate h1 { font-size: 3rem; }
  .drawer:not([hidden]) { left: 0; width: auto; }
  .rules-scroll { padding: 18px 16px calc(48px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto; }
  .rail-panel { transition: none; }
}
