:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1a1f27;
  --muted: #4b5565;
  --border: #d3d9e2;
  --control-border: #737e8f;
  --accent: #1649c4;
  --accent-text: #ffffff;
  --accent-soft: #e2eafc;
  --danger: #b3261e;
  --danger-solid: #b3261e;
  --danger-solid-text: #ffffff;
  --focus: #1649c4;
  --toast-bg: #1a1f27;
  --toast-text: #ffffff;
  --toast-error: #ffb4ab;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0e1217;
    --surface: #161b22;
    --surface-2: #1f2630;
    --text: #e6e9ee;
    --muted: #a9b3c1;
    --border: #2f3743;
    --control-border: #8591a3;
    --accent: #8ab4ff;
    --accent-text: #0a1220;
    --accent-soft: #1d2b46;
    --danger: #ff9d94;
    --danger-solid: #ffb4ab;
    --danger-solid-text: #2b0a07;
    --focus: #8ab4ff;
    --toast-bg: #e6e9ee;
    --toast-text: #0e1217;
    --toast-error: #8f1d17;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1217;
  --surface: #161b22;
  --surface-2: #1f2630;
  --text: #e6e9ee;
  --muted: #a9b3c1;
  --border: #2f3743;
  --control-border: #8591a3;
  --accent: #8ab4ff;
  --accent-text: #0a1220;
  --accent-soft: #1d2b46;
  --danger: #ff9d94;
  --danger-solid: #ffb4ab;
  --danger-solid-text: #2b0a07;
  --focus: #8ab4ff;
  --toast-bg: #e6e9ee;
  --toast-text: #0e1217;
  --toast-error: #8f1d17;
}

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

[hidden] {
  display: none !important;
}

html {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
}

main {
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

main:focus {
  outline: none;
}

h1,
h2,
h3,
p {
  margin: 0;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

a {
  color: var(--accent);
}

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

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: 0.5rem;
  top: -4rem;
  z-index: 100;
  padding: 0.5rem 0.75rem;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 4px;
}

.skip-link:focus {
  top: 0.5rem;
}

.clip-proxy {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.app-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 1.25rem;
  padding: 0.45rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-size: 1.05rem;
  font-weight: 700;
}

.brand a {
  color: var(--text);
  text-decoration: none;
}

.main-nav {
  display: flex;
  gap: 0.25rem;
}

.main-nav a {
  padding: 0.35rem 0.7rem;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
  border-bottom: 2px solid transparent;
}

.main-nav a:hover {
  background: var(--surface-2);
}

.main-nav a[aria-current="page"] {
  font-weight: 700;
  border-bottom-color: var(--accent);
  background: var(--accent-soft);
}

.header-tools {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.current-user {
  color: var(--muted);
  max-width: 16rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 2rem;
  padding: 0.25rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--control-border);
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: var(--surface-2);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent);
  filter: brightness(1.08);
}

.btn-danger {
  color: var(--danger);
}

.btn-danger-solid {
  background: var(--danger-solid);
  border-color: var(--danger-solid);
  color: var(--danger-solid-text);
  font-weight: 600;
}

.btn-danger-solid:hover:not(:disabled) {
  background: var(--danger-solid);
  filter: brightness(1.08);
}

.btn-sm {
  min-height: 1.85rem;
  padding: 0.15rem 0.6rem;
  font-size: 0.9rem;
}

.btn-xs {
  min-height: 1.6rem;
  padding: 0.05rem 0.5rem;
  font-size: 0.82rem;
}

.btn[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  font-weight: 600;
}

.back-btn {
  display: none;
}

.input {
  width: 100%;
  min-height: 2rem;
  padding: 0.25rem 0.6rem;
  background: var(--surface);
  border: 1px solid var(--control-border);
  border-radius: 6px;
}

.input:disabled {
  opacity: 0.6;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-bottom: 0.75rem;
  min-width: 0;
}

.field label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.form-error {
  min-height: 1.2rem;
  margin-bottom: 0.5rem;
  color: var(--danger);
  font-weight: 600;
  font-size: 0.9rem;
}

.empty,
.empty-inline {
  padding: 1rem;
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1rem;
  z-index: 200;
  transform: translateX(-50%);
  max-width: calc(100vw - 2rem);
  padding: 0.55rem 1rem;
  border-radius: 8px;
  background: var(--toast-bg);
  color: var(--toast-text);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}

.toast.error {
  border-left: 5px solid var(--toast-error);
}

.toast:empty {
  padding: 0;
  opacity: 0;
  box-shadow: none;
}

.dialog {
  width: min(26rem, calc(100vw - 2rem));
  padding: 1.1rem 1.25rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.dialog-title {
  font-size: 1.05rem;
  margin-bottom: 0.6rem;
}

.dialog-text {
  margin-bottom: 1rem;
  overflow-wrap: anywhere;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.input-row {
  display: flex;
  gap: 0.5rem;
}

.mail {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(210px, 270px) minmax(250px, 340px) minmax(0, 1fr);
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.pane-reader {
  border-right: 0;
}

.pane-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: 2.9rem;
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.pane-title {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  font-size: 0.95rem;
  font-weight: 700;
}

.pane-title:focus {
  outline: none;
}

.pane-sub {
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pane-tools {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.pane-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.row {
  position: relative;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.row:hover {
  background: var(--surface-2);
}

.row[data-selected="true"] {
  background: var(--accent-soft);
  box-shadow: inset 4px 0 0 var(--accent);
}

.row-select {
  display: block;
  width: 100%;
  min-width: 0;
  padding: 0.6rem 0.75rem;
  background: none;
  border: 0;
  text-align: left;
  cursor: pointer;
}

.row-select:focus-visible {
  outline-offset: -3px;
}

.mailbox-row .row-select {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding-bottom: 0.4rem;
  min-height: 2.6rem;
}

.mb-address {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.badge {
  flex: none;
  min-width: 1.4rem;
  padding: 0 0.4rem;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.4rem;
  text-align: center;
}

.row-actions {
  display: flex;
  gap: 0.35rem;
  padding: 0 0.75rem 0.55rem;
}

.message-row {
  display: flex;
  align-items: flex-start;
}

.message-row .row-select {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.message-row .row-delete {
  flex: none;
  margin: 0.55rem 0.6rem 0 0;
}

.m-top {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.m-top time {
  flex: none;
  margin-left: auto;
  white-space: nowrap;
}

.m-from {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.dot {
  flex: none;
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: var(--accent);
}

.message-row[data-unread="false"] .dot {
  visibility: hidden;
}

.message-row[data-unread="true"] .m-from,
.message-row[data-unread="true"] .m-subject {
  font-weight: 700;
}

.m-subject {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-left: 0.95rem;
}

.m-att {
  padding-left: 0.95rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.load-more {
  display: flex;
  margin: 0.6rem auto;
}

.reader-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.reader-content {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.reader-head {
  padding: 0.6rem 1rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.reader-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.4rem;
}

.spacer {
  flex: 1;
}

.reader-subject {
  font-size: 1.15rem;
  line-height: 1.3;
  overflow-wrap: anywhere;
  margin-bottom: 0.4rem;
}

.reader-subject:focus {
  outline: none;
}

.meta {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 0.1rem 0.75rem;
  margin: 0;
  font-size: 0.9rem;
}

.meta dt {
  color: var(--muted);
}

.meta dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.attachments {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1rem;
  margin: 0.5rem 0 0;
  padding: 0;
  font-size: 0.9rem;
}

.attachment-size {
  color: var(--muted);
}

.tabs {
  display: flex;
  gap: 0.15rem;
  padding: 0 0.75rem;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.tab {
  padding: 0.5rem 0.8rem;
  background: none;
  border: 0;
  border-bottom: 3px solid transparent;
  cursor: pointer;
  color: var(--muted);
  white-space: nowrap;
}

.tab:hover {
  color: var(--text);
}

.tab[aria-selected="true"] {
  color: var(--text);
  font-weight: 700;
  border-bottom-color: var(--accent);
}

.panels {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.tabpanel {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 0.75rem 1rem;
}

.tabpanel-html {
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

.tabpanel[hidden] {
  display: none;
}

.frame-tools {
  padding: 0.4rem 1rem;
  border-bottom: 1px solid var(--border);
}

.reader-frame {
  flex: 1;
  width: 100%;
  min-height: 12rem;
  border: 0;
  background: #ffffff;
}

.pre {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.85rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.links {
  margin: 0;
  padding: 0;
  list-style: none;
}

.links li {
  padding: 0.3rem 0;
  overflow-wrap: anywhere;
}

.headers {
  display: grid;
  grid-template-columns: minmax(8rem, max-content) minmax(0, 1fr);
  gap: 0.25rem 1rem;
  margin: 0;
  font-size: 0.88rem;
}

.headers dt {
  font-weight: 600;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.headers dd {
  margin: 0;
  font-family: var(--mono);
  overflow-wrap: anywhere;
}

.login-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-card {
  width: min(22rem, 100%);
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.login-title {
  font-size: 1.25rem;
  margin-bottom: 1rem;
}

.login-card .btn {
  width: 100%;
}

.settings {
  width: min(56rem, 100%);
  margin: 0 auto;
  padding: 1rem;
  overflow-y: auto;
}

.settings-section {
  margin-bottom: 1.75rem;
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.settings-section h2 {
  font-size: 1.05rem;
  margin-bottom: 0.75rem;
}

.inline-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0 0.75rem;
}

.inline-form .field {
  flex: 0 1 auto;
}

.inline-form .grow {
  flex: 1 1 12rem;
}

.inline-form .btn {
  margin-bottom: 0.75rem;
}

.secret-box {
  margin-bottom: 1rem;
  padding: 0.75rem;
  background: var(--accent-soft);
  border: 2px solid var(--accent);
  border-radius: 8px;
}

.secret-note {
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.secret-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.secret-value {
  flex: 1 1 14rem;
  min-width: 0;
  padding: 0.35rem 0.5rem;
  background: var(--surface);
  border: 1px solid var(--control-border);
  border-radius: 6px;
  font-family: var(--mono);
  overflow-wrap: anywhere;
  user-select: all;
}

.cards {
  list-style: none;
  margin: 0;
  padding: 0;
}

.card {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 1rem;
  padding: 0.6rem 0;
  border-top: 1px solid var(--border);
}

.card-main {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  flex: 1 1 14rem;
  min-width: 0;
}

.card-title {
  overflow-wrap: anywhere;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem 1rem;
  color: var(--muted);
  font-size: 0.88rem;
}

.card-actions {
  display: flex;
  gap: 0.5rem;
}

.card.is-revoked .card-title,
.card.is-revoked .mono {
  text-decoration: line-through;
}

.mono {
  font-family: var(--mono);
  font-size: 0.88rem;
}

.tag {
  display: inline-block;
  padding: 0.05rem 0.5rem;
  border: 1px solid var(--control-border);
  border-radius: 999px;
  font-size: 0.8rem;
  color: var(--muted);
}

@media (max-width: 900px) {
  body {
    height: auto;
    min-height: 100vh;
  }

  main {
    flex: none;
    min-height: 0;
  }

  .app-header {
    padding: 0.4rem 0.75rem;
  }

  .header-tools {
    margin-left: 0;
    width: 100%;
  }

  .current-user {
    flex: 1 1 100%;
    max-width: none;
  }

  .mail {
    display: flex;
    flex-direction: column;
    flex: none;
  }

  .pane {
    border-right: 0;
    border-bottom: 2px solid var(--border);
  }

  .pane-scroll {
    flex: none;
  }

  .pane-mailboxes .pane-scroll {
    max-height: 15rem;
  }

  .pane-messages .pane-scroll {
    max-height: 22rem;
  }

  .pane-reader {
    min-height: 32rem;
  }

  .back-btn {
    display: inline-flex;
  }

  .reader-frame {
    min-height: 24rem;
  }

  .login-wrap {
    align-items: flex-start;
  }

  .headers {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
