:root {
  --bg: #e8edf2;
  --surface: #f7f9fb;
  --paper: #ffffff;
  --ink: #1a1f26;
  --muted: #5a6572;
  --line: #d3dbe4;
  --sidebar: #171a1f;
  --sidebar-ink: #e8edf2;
  --sidebar-mute: #8b95a3;
  --sidebar-hover: #232830;
  --accent: #0f766e;
  --accent-ink: #f4fffd;
  --unread: #0f766e;
  --selected: #dce8e6;
  --danger: #9f2a2a;
  --radius: 8px;
  --sans: "Segoe UI", ui-sans-serif, system-ui, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body[data-view="login"] #shell,
body[data-view="login"] #compose-dialog {
  display: none !important;
}

body[data-view="app"] #auth {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
}

h1,
h2,
.brand strong,
.brand-mark {
  font-weight: 650;
  letter-spacing: -0.03em;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

.skip {
  position: absolute;
  left: 12px;
  top: -40px;
  background: var(--ink);
  color: var(--paper);
  padding: 8px 12px;
  z-index: 20;
}

.skip:focus {
  top: 12px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.auth {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 32px 16px;
  background: var(--bg);
}

.auth-card {
  width: min(400px, 100%);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 32px 28px;
}

.brand-mark {
  margin: 0 0 20px;
  color: var(--accent);
  font-size: 13px;
}

.auth-card h1 {
  margin: 0 0 8px;
  font-size: 28px;
}

.lede,
.hint,
.meta,
.state p,
.compose-note {
  color: var(--muted);
}

.auth-card form,
.sheet form {
  display: grid;
  gap: 6px;
}

label {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 6px;
  padding: 10px 12px;
}

textarea {
  resize: vertical;
  min-height: 180px;
}

input:focus,
select:focus,
textarea:focus,
.compose-editor:focus,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 10px 14px;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

button.quiet,
button.ghost {
  background: transparent;
  color: var(--muted);
}

button.quiet {
  text-align: left;
  padding-left: 0;
}

.form-error {
  color: var(--danger);
  margin: 8px 0 0;
}

.form-error.form-ok {
  color: var(--accent);
}

.shell {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 220px 1fr;
  background: var(--bg);
}

.rail {
  background: var(--sidebar);
  color: var(--sidebar-ink);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.brand {
  display: grid;
  gap: 2px;
  padding: 0 8px;
}

.brand span,
.rail-foot,
.compose-from {
  color: var(--sidebar-mute);
  font-size: 12px;
}

.compose {
  width: 100%;
  font-weight: 600;
}

.rail nav {
  display: grid;
  gap: 2px;
}

.folder {
  background: transparent;
  color: var(--sidebar-ink);
  text-align: left;
  padding: 8px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.folder:hover {
  background: var(--sidebar-hover);
}

.folder.is-active {
  background: var(--sidebar-hover);
  box-shadow: inset 3px 0 0 var(--accent);
}

.count {
  color: var(--sidebar-mute);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}

.rail-foot {
  margin-top: auto;
  display: grid;
  gap: 10px;
}

.current-mailbox {
  margin: 0;
  word-break: break-all;
}

.rail select {
  background: var(--sidebar-hover);
  color: var(--sidebar-ink);
  border-color: #323843;
}

.workspace {
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.settings-page {
  min-width: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

.settings-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.settings-top h2 {
  margin: 0;
  font-size: 18px;
}

.settings-back {
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--line);
}

.settings-layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 200px 1fr;
}

.settings-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 8px;
  background: var(--paper);
  border-right: 1px solid var(--line);
}

.settings-nav button {
  background: transparent;
  color: var(--ink);
  text-align: left;
  padding: 10px 12px;
}

.settings-nav button:hover {
  background: #eef3f6;
}

.settings-nav button.is-active {
  background: var(--selected);
  box-shadow: inset 3px 0 0 var(--accent);
}

.settings-main {
  overflow: auto;
  padding: 28px 36px 48px;
}

.settings-mailbox-bar {
  max-width: 560px;
  display: grid;
  gap: 6px;
  margin-bottom: 24px;
}

.settings-mailbox-bar label {
  margin-top: 0;
}

.settings-panel {
  max-width: 560px;
}

.settings-panel,
.settings-panel form,
#mailbox-password-block,
#add-mailbox-block {
  display: grid;
  gap: 6px;
}

.settings-panel h3 {
  margin: 0 0 4px;
  font-size: 22px;
}

.settings-panel .compose-note {
  margin: 0 0 8px;
}

.settings-actions {
  padding: 0;
  margin: 16px 0 0;
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  gap: 8px;
}

.settings-actions .ghost {
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--line);
}

#settings-forward {
  min-height: 96px;
}

#panel-api {
  max-width: 720px;
}

.api-token-once {
  margin-top: 16px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  display: grid;
  gap: 8px;
}

.api-token-once[hidden] {
  display: none;
}

.api-token-once p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.api-token-once code {
  word-break: break-all;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 12px;
}

.api-example {
  margin: 20px 0 0;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 12px;
  white-space: pre-wrap;
  overflow: auto;
}

.rail-foot .quiet.is-active {
  color: var(--sidebar-ink);
  font-weight: 650;
}

body[data-screen="settings"] .folder.is-active,
body[data-screen="contacts"] .folder.is-active {
  background: transparent;
  box-shadow: none;
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 16px;
  align-items: center;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.topbar-title {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.topbar-title h2,
.reader-head h2 {
  margin: 0;
  font-size: 18px;
}

.search input {
  max-width: 420px;
  background: var(--paper);
}

.panes {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
}

.list-pane,
.reader {
  min-width: 0;
  min-height: 0;
  background: var(--surface);
}

.list-pane {
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}

.messages {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  flex: 1;
}

.message-btn {
  width: 100%;
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 12px 16px 12px 14px;
  border-radius: 0;
  display: grid;
  grid-template-columns: 32px 1fr auto;
  gap: 10px 12px;
  align-items: start;
  border-left: 3px solid transparent;
}

.message-btn:hover {
  background: #eef3f6;
}

.message-btn.is-selected {
  background: var(--selected);
}

.message-btn.is-unread {
  border-left-color: var(--unread);
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: #c5d4d2;
  color: #1d3d3a;
  font-size: 12px;
  font-weight: 650;
}

.message-main {
  min-width: 0;
}

.message-btn strong,
.message-btn .from,
.message-btn .snippet {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-btn .from {
  color: var(--muted);
  font-size: 12px;
}

.message-btn.is-unread .from,
.message-btn.is-unread strong {
  font-weight: 700;
  color: var(--ink);
}

.message-btn .snippet {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.message-time {
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.reader {
  display: flex;
  flex-direction: column;
  background: var(--paper);
}

.reader article {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

.reader-head {
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--line);
}

.reader-who {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 14px;
}

.add-contact-inline {
  margin-left: auto;
  flex-shrink: 0;
  border: 1px solid var(--line);
  color: var(--ink);
}

.add-contact-inline:disabled,
#add-contact-action:disabled {
  color: var(--muted);
}

.message-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: stretch;
}

.add-contact-chip {
  align-self: center;
  margin-right: 10px;
  background: transparent;
  color: var(--accent);
  padding: 4px 8px;
  font-size: 12px;
}

.add-contact-chip:disabled {
  color: var(--muted);
}

.reader-who p {
  margin: 0;
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.actions button {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
}

.attachments {
  padding: 10px 24px;
  border-bottom: 1px solid var(--line);
}

.attachments a {
  color: var(--accent);
  margin-right: 12px;
}

#reader-frame {
  flex: 1;
  width: 100%;
  min-height: 420px;
  border: 0;
  background: var(--paper);
}

.state {
  padding: 48px 28px;
}

.reader-empty {
  margin: auto;
  text-align: left;
}

.sheet {
  width: min(560px, calc(100vw - 24px));
  margin: auto 16px 16px auto;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: var(--radius);
  padding: 0;
  box-shadow: 0 18px 50px rgb(23 26 31 / 0.18);
}

.sheet::backdrop {
  background: rgb(23 26 31 / 0.28);
}

.sheet form {
  padding: 16px 18px 18px;
}

.sheet header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sheet header h2 {
  margin: 0;
  font-size: 16px;
}

.compose-from {
  margin: 4px 0 0;
}

.compose-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.compose-tools .ghost {
  padding: 8px 10px;
}

.compose-files {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.compose-files li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}

.compose-files img {
  width: 36px;
  height: 36px;
  object-fit: cover;
  border-radius: 4px;
}

.compose-files span {
  min-width: 0;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.compose-files small {
  color: var(--muted);
  margin-right: 8px;
}

.compose-files button {
  background: transparent;
  color: var(--danger);
  padding: 4px 8px;
}

.compose-note {
  margin: 8px 0 0;
  font-size: 12px;
}

.compose-secondary {
  background: transparent;
  color: var(--sidebar-ink);
  border: 1px solid #3a424c;
}

.compose-secondary:hover {
  background: var(--sidebar-hover);
}

.sheet menu {
  padding: 0;
  margin: 14px 0 0;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.settings-sheet {
  width: min(480px, calc(100vw - 24px));
}

.settings-sheet h3 {
  margin: 16px 0 0;
  font-size: 14px;
}

.import-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.import-row .ghost,
.sheet menu .ghost {
  padding: 8px 10px;
}

#compose-recipients {
  min-height: 120px;
}

.compose-editor {
  width: 100%;
  min-height: 220px;
  max-height: 420px;
  overflow: auto;
  border: 1px solid var(--line);
  background: var(--paper);
  border-radius: 6px;
  padding: 10px 12px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.55;
}

.compose-editor:empty::before,
.compose-editor.is-empty::before {
  content: attr(data-placeholder);
  color: var(--muted);
}

.compose-editor img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 8px 0;
  border-radius: 4px;
}

.compose-secondary.is-active {
  background: var(--sidebar-hover);
  box-shadow: inset 3px 0 0 var(--accent);
}

.contacts-panel {
  max-width: 720px;
}

.contacts-panel h3 {
  margin: 0 0 4px;
  font-size: 22px;
}

.contacts-search {
  display: block;
  margin-top: 16px;
}

.contacts-search input {
  max-width: none;
}

.contacts-empty {
  margin-top: 24px;
}

.contacts-empty p {
  color: var(--muted);
  margin: 0 0 12px;
}

.contacts-split {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 28px 0 16px;
}

.contact-list {
  list-style: none;
  margin: 16px 0 0;
  padding: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
}

.contact-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.contact-list li:last-child {
  border-bottom: 0;
}

.contact-list .who {
  min-width: 0;
}

.contact-list .who strong,
.contact-list .who span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contact-list .who span {
  color: var(--muted);
  font-size: 12px;
}

.contact-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.contact-actions button {
  padding: 6px 10px;
}

.contact-actions .write {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
}

.contact-actions .remove {
  background: transparent;
  color: var(--danger);
}

.settings-sheet hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 18px 0 8px;
}

.addr-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.addr-row span {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 13px;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  color: var(--ink);
}

.check input {
  width: auto;
}

#compose-mode {
  background: var(--paper);
}

.mobile-only {
  display: none;
}

@media (max-width: 960px) {
  .shell {
    grid-template-columns: 1fr;
  }

  .rail {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
  }

  .brand {
    flex: 1;
  }

  .compose {
    width: auto;
  }

  .rail nav {
    grid-auto-flow: column;
    width: 100%;
  }

  .rail-foot {
    margin-top: 0;
    width: 100%;
    grid-template-columns: 1fr auto;
    align-items: center;
  }

  .topbar {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .panes {
    grid-template-columns: 1fr;
  }

  .settings-layout {
    grid-template-columns: 1fr;
  }

  .settings-nav {
    flex-direction: row;
    overflow-x: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .settings-nav button {
    flex: 0 0 auto;
  }

  .settings-nav button.is-active {
    box-shadow: inset 0 -3px 0 var(--accent);
  }

  .settings-main {
    padding: 20px 16px 40px;
  }

  .settings-page {
    min-height: auto;
  }

  .mobile-only {
    display: inline-flex;
  }

  .reader {
    position: fixed;
    inset: 0;
    z-index: 10;
    transform: translateX(100%);
    transition: transform 160ms ease;
  }

  .reader.is-open {
    transform: none;
  }

  .sheet {
    width: calc(100vw - 16px);
    margin: auto 8px 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .reader {
    transition: none;
  }
}
