:root {
  color-scheme: light dark;

  --bg-light: #f6f7f9;
  --bg-dark: #0e1116;
  --surface-light: #ffffff;
  --surface-dark: #161b22;
  --surface-2-light: #eef1f5;
  --surface-2-dark: #1f2630;
  --border-light: #d8dee6;
  --border-dark: #2d3643;
  --text-light: #161c24;
  --text-dark: #e6edf3;
  --muted-light: #5c6775;
  --muted-dark: #96a2b2;
  --accent-light: #1f5eff;
  --accent-dark: #5c8dff;
  --accent-text-light: #ffffff;
  --accent-text-dark: #0a1224;
  --danger-light: #c92a2a;
  --danger-dark: #ff6b6b;
  --ok-bg-light: #eaf7ef;
  --ok-bg-dark: #142a1d;
  --ok-text-light: #156c3a;
  --ok-text-dark: #7ce2a4;
  --err-bg-light: #fdecec;
  --err-bg-dark: #33191b;
  --err-text-light: #b42318;
  --err-text-dark: #ff9c97;

  --bg: var(--bg-light);
  --surface: var(--surface-light);
  --surface-2: var(--surface-2-light);
  --border: var(--border-light);
  --text: var(--text-light);
  --muted: var(--muted-light);
  --accent: var(--accent-light);
  --accent-text: var(--accent-text-light);
  --danger: var(--danger-light);
  --ok-bg: var(--ok-bg-light);
  --ok-text: var(--ok-text-light);
  --err-bg: var(--err-bg-light);
  --err-text: var(--err-text-light);

  --radius: 10px;
  --font: system-ui, -apple-system, "PingFang SC", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  @media (prefers-color-scheme: dark) {
    --bg: var(--bg-dark);
    --surface: var(--surface-dark);
    --surface-2: var(--surface-2-dark);
    --border: var(--border-dark);
    --text: var(--text-dark);
    --muted: var(--muted-dark);
    --accent: var(--accent-dark);
    --accent-text: var(--accent-text-dark);
    --danger: var(--danger-dark);
    --ok-bg: var(--ok-bg-dark);
    --ok-text: var(--ok-text-dark);
    --err-bg: var(--err-bg-dark);
    --err-text: var(--err-text-dark);
  }

  @supports (color: light-dark(white, black)) {
    --bg: light-dark(var(--bg-light), var(--bg-dark));
    --surface: light-dark(var(--surface-light), var(--surface-dark));
    --surface-2: light-dark(var(--surface-2-light), var(--surface-2-dark));
    --border: light-dark(var(--border-light), var(--border-dark));
    --text: light-dark(var(--text-light), var(--text-dark));
    --muted: light-dark(var(--muted-light), var(--muted-dark));
    --accent: light-dark(var(--accent-light), var(--accent-dark));
    --accent-text: light-dark(var(--accent-text-light), var(--accent-text-dark));
    --danger: light-dark(var(--danger-light), var(--danger-dark));
    --ok-bg: light-dark(var(--ok-bg-light), var(--ok-bg-dark));
    --ok-text: light-dark(var(--ok-text-light), var(--ok-text-dark));
    --err-bg: light-dark(var(--err-bg-light), var(--err-bg-dark));
    --err-text: light-dark(var(--err-text-light), var(--err-text-dark));
  }

  &:has(> head > meta[name="color-scheme"][content="dark"]) {
    color-scheme: dark;
  }

  &:has(> head > meta[name="color-scheme"][content="light"]) {
    color-scheme: light;
  }

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

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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
}

[hidden] {
  display: none !important;
}

.visually-hidden:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  white-space: nowrap !important;
}

.skip-link:focus {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 1000;
  padding: 8px 14px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
}

:where(a:any-link, button, input, select, textarea):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

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

/* Buttons & inputs */
button {
  font: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 8px;
  min-block-size: 34px;
  padding: 6px 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

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

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

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

.btn-danger {
  background: var(--danger);
  color: #ffffff;
  font-weight: 600;
}

.btn-small {
  min-block-size: 28px;
  padding: 4px 10px;
  font-size: 13px;
}

.btn-block {
  width: 100%;
}

input[type="text"],
input[type="password"],
input[type="search"],
select,
textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-block-size: 36px;
}

/* Login view */
.login-shell {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-card h1 {
  margin: 0;
  font-size: 22px;
}

.login-card p {
  margin: 0;
}

.brand-badge {
  display: inline-flex;
  align-self: flex-start;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.stack-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field label {
  font-weight: 600;
  font-size: 13px;
}

.field-hint {
  font-size: 12px;
  color: var(--muted);
}

.password-row {
  display: flex;
  gap: 8px;
}

.password-row input {
  flex: 1;
}

.alert-box {
  background: var(--err-bg);
  color: var(--err-text);
  border: 1px solid currentColor;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
}

/* App shell */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.topbar-left,
.topbar-nav,
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-mark {
  font-weight: 700;
  font-size: 15px;
}

.role-pill {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.nav-tab {
  background: transparent;
  color: var(--muted);
  border-color: transparent;
}

.nav-tab.is-active {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
  font-weight: 600;
}

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

/* Inbox 3-pane layout */
.inbox-layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 230px 360px 1fr;
}

.mailbox-sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-header h2 {
  margin: 0;
  font-size: 14px;
}

.sidebar-note {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  word-break: break-word;
}

.mailbox-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.mailbox-btn {
  width: 100%;
  justify-content: space-between;
  text-align: left;
  background: transparent;
  color: var(--text);
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 13px;
}

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

.mailbox-btn.is-active {
  background: var(--surface-2);
  border-color: var(--border);
  font-weight: 600;
}

.mailbox-addr {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.count-badge {
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}

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

.search-bar {
  display: flex;
  gap: 6px;
  padding: 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.list-status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.pager {
  display: flex;
  align-items: center;
  gap: 6px;
}

.message-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.msg-row {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  text-align: left;
  gap: 4px;
  padding: 11px 14px;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface);
  color: var(--text);
}

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

.msg-row.is-selected {
  background: var(--surface-2);
  box-shadow: inset 3px 0 0 var(--accent);
}

.msg-row-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
}

.msg-sender {
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg-row.is-unread .msg-subject {
  font-weight: 700;
}

.msg-subject {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.msg-meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.msg-snippet {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-state {
  padding: 28px 16px;
  text-align: center;
  color: var(--muted);
}

/* Detail pane */
.detail-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  background: var(--surface);
}

.detail-placeholder {
  margin: auto;
}

.detail-article {
  display: flex;
  flex-direction: column;
  min-height: 100%;
  padding: 16px 20px;
  gap: 14px;
}

.detail-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.detail-toolbar-right {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.detail-header {
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}

.detail-subject {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
  word-break: break-word;
}

.otp-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--ok-bg);
  color: var(--ok-text);
}

.detail-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 6px 16px;
  margin: 0;
  font-size: 13px;
}

.detail-meta > div {
  display: flex;
  gap: 8px;
}

.detail-meta dt {
  color: var(--muted);
  flex-shrink: 0;
}

.detail-meta dd {
  margin: 0;
  word-break: break-all;
}

.attachments-box h3 {
  margin: 0 0 6px;
  font-size: 13px;
}

.attachment-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.attachment-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}

.attachment-link:hover {
  border-color: var(--accent);
}

.view-tabs {
  display: flex;
  gap: 6px;
}

.tab-btn {
  background: var(--surface-2);
  color: var(--muted);
  border-color: var(--border);
}

.tab-btn.is-active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.body-panel {
  flex: 1;
  min-height: 360px;
  display: flex;
  flex-direction: column;
}

.mail-frame {
  width: 100%;
  flex: 1;
  min-height: 460px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  color-scheme: light;
}

.plain-pre {
  margin: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  font-family: var(--mono);
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow: auto;
  flex: 1;
}

.headers-table,
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.headers-table caption,
.data-table caption {
  text-align: left;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--muted);
}

.headers-table th,
.headers-table td,
.data-table th,
.data-table td {
  border-bottom: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
}

.headers-table td:first-child {
  font-family: var(--mono);
  white-space: nowrap;
  color: var(--muted);
}

/* Admin view */
.admin-layout {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.card-header h2 {
  margin: 0 0 4px;
  font-size: 16px;
}

.card-header p {
  margin: 0;
}

.table-wrap {
  overflow-x: auto;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tag {
  font-family: var(--mono);
  font-size: 12px;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* Native dialogs */
dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  width: min(480px, calc(100vw - 24px));
  padding: 22px;
}

dialog[open] {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
  overflow: hidden;
  overscroll-behavior: contain;
}

dialog h1 {
  margin: 0;
  font-size: 18px;
}

.dialog-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 4px;
}

.settings-fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}

.radio-group {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}

.radio-option,
.checkbox-row {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.toast-region {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 900;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--text);
  color: var(--surface);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
}

.mobile-only {
  display: none;
}

@media (max-width: 980px) {
  .inbox-layout {
    grid-template-columns: 1fr;
  }

  .mailbox-sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--border);
    max-height: 170px;
  }

  .message-list-pane {
    border-right: 0;
  }

  .inbox-layout[data-mobile-detail="1"] .mailbox-sidebar,
  .inbox-layout[data-mobile-detail="1"] .message-list-pane {
    display: none;
  }

  .inbox-layout:not([data-mobile-detail="1"]) .detail-pane {
    display: none;
  }

  .mobile-only {
    display: inline-flex;
  }
}
