:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1a1d23;
  --muted: #5f6774;
  --border: #dde1e7;
  --accent: #2357d9;
  --accent-text: #ffffff;
  --accent-soft: #e6edfc;
  --danger: #c4312b;
  --danger-soft: #fbeaea;
  --ok: #1f7a43;
  --score-lo: #8a6d00;
  --score-hi: #c4312b;
  --shadow: 0 10px 30px rgba(15, 20, 30, .12);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121418;
    --surface: #1b1e24;
    --surface-2: #23272f;
    --text: #e7e9ee;
    --muted: #9aa2b1;
    --border: #2f343e;
    --accent: #6d95ff;
    --accent-text: #0d1220;
    --accent-soft: #1f2a45;
    --danger: #ff7a73;
    --danger-soft: #3a1d1d;
    --ok: #5fcf8e;
    --score-lo: #e2c15a;
    --score-hi: #ff7a73;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

[hidden] { display: none !important; }

code { font-size: .9em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

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

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.brand svg { width: 24px; height: 24px; color: var(--accent); }
.session { display: flex; align-items: center; gap: 10px; min-width: 0; }
.session .user { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 40px; }

/* Buttons & inputs */
.btn {
  font: inherit; font-weight: 500; cursor: pointer;
  padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon { padding: 6px 10px; }

input[type="text"], input[type="email"], input[type="search"], select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-width: 0;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, .tab:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

/* Login */
.login { display: flex; justify-content: center; padding-top: 6vh; }
.card {
  width: 100%; max-width: 420px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 28px 24px;
}
.card h1 { margin: 0 0 6px; font-size: 1.4rem; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.stack label { font-weight: 500; font-size: .9rem; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); margin: 22px 0 14px; font-size: .85rem; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
.msg { margin: 16px 0 0; padding: 10px 12px; border-radius: 8px; background: var(--accent-soft); }
.msg.error { background: var(--danger-soft); color: var(--danger); }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tab {
  font: inherit; background: none; border: 0; color: var(--muted); cursor: pointer;
  padding: 10px 14px; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

/* Toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.toolbar input[type="search"] { flex: 1 1 220px; }

.bulkbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 8px 12px; margin-bottom: 12px; border-radius: 8px; background: var(--accent-soft);
}
.bulkbar span { font-weight: 500; margin-right: auto; }

/* Mail table */
.tablewrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.mails { width: 100%; border-collapse: collapse; table-layout: fixed; }
.mails th, .mails td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.mails th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; background: var(--surface-2); }
.mails tbody tr { cursor: pointer; }
.mails tbody tr:hover { background: var(--surface-2); }
.mails tbody tr:last-child td { border-bottom: 0; }
.mails tbody tr.unseen .subj { font-weight: 600; }
.mails td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-check { width: 44px; }
.c-date { width: 140px; }
.c-from { width: 28%; }
.c-score { width: 80px; text-align: right !important; }
.from-addr { color: var(--muted); font-size: .85em; }
.score { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--score-lo); }
.score.high { color: var(--score-hi); }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; margin: 0; }

@media (max-width: 720px) {
  .mails thead { display: none; }
  .mails, .mails tbody, .mails tr, .mails td { display: block; width: 100%; }
  .mails tr { display: grid; grid-template-columns: 32px 1fr auto; grid-template-areas: "chk from score" "chk subj subj" "chk date date"; column-gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
  .mails td { padding: 0; border: 0; }
  .mails td.c-check { grid-area: chk; }
  .mails td.c-from { grid-area: from; font-weight: 500; }
  .mails td.c-subj { grid-area: subj; }
  .mails td.c-date { grid-area: date; color: var(--muted); font-size: .85em; }
  .mails td.c-score { grid-area: score; }
  .from-addr { display: none; }
}

/* Address lists */
.addform { display: flex; gap: 8px; margin: 12px 0 16px; }
.addform input { flex: 1; }
.addrlist { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.addrlist li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 8px 8px 14px; border-bottom: 1px solid var(--border); word-break: break-all; }
.addrlist li:last-child { border-bottom: 0; }
.addrlist li.empty { color: var(--muted); padding: 20px 14px; justify-content: center; }

/* Drawer */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 10; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 11;
  width: min(760px, 100%); background: var(--surface); box-shadow: var(--shadow);
  display: flex; flex-direction: column; padding: 16px; gap: 12px;
}
.drawer-head { display: flex; align-items: flex-start; gap: 8px; }
.drawer-head h2 { margin: 0; font-size: 1.1rem; flex: 1; overflow-wrap: anywhere; }
.meta { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: .9rem; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; overflow-wrap: anywhere; }
.drawer-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.spaminfo { font-size: .85rem; }
.spaminfo summary { cursor: pointer; color: var(--muted); }
.spaminfo table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.spaminfo td { padding: 3px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
.spaminfo td:first-child { text-align: right; font-variant-numeric: tabular-nums; width: 60px; }
.spaminfo .rule { font-family: ui-monospace, monospace; font-size: .9em; }
#pv-body { flex: 1; width: 100%; min-height: 200px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }

/* Dialog */
dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); max-width: min(440px, calc(100% - 32px)); padding: 20px; }
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog p { margin: 0 0 16px; overflow-wrap: anywhere; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 20;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 8px;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow);
}
.toast.error { background: var(--danger); color: #fff; }
