:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #667085;
  --line: #e3e6ec;
  --accent: #2f6fed;
  --accent-soft: #e8f0ff;
  --accent-text: #ffffff;
  --unread: #eef4ff;
  --danger: #d0353f;
  --ok: #1f9d55;
  --warn: #c27c0e;
  --code: #f6f8fa;
  --overlay: rgba(10, 14, 22, .45);
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  --radius: 10px;
  color-scheme: light;
  font-size: 14px;
}
/* Темна тема: автоматично за налаштуванням системи/браузера (якщо не вибрано світлу)… */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111318;
    --panel: #1a1d24;
    --text: #e6e8ee;
    --muted: #98a2b3;
    --line: #2a2f3a;
    --accent: #6b97ff;
    --accent-soft: #1f2a44;
    --accent-text: #0b1220;
    --unread: #18233a;
    --danger: #f0646c;
    --ok: #3ccb7f;
    --warn: #e3a33b;
    --code: #141821;
    --overlay: rgba(0, 0, 0, .6);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}
/* …або вибрана вручну перемикачем. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111318;
  --panel: #1a1d24;
  --text: #e6e8ee;
  --muted: #98a2b3;
  --line: #2a2f3a;
  --accent: #6b97ff;
  --accent-soft: #1f2a44;
  --accent-text: #0b1220;
  --unread: #18233a;
  --danger: #f0646c;
  --ok: #3ccb7f;
  --warn: #e3a33b;
  --code: #141821;
  --overlay: rgba(0, 0, 0, .6);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0 0 12px; font-weight: 600; }
h1 { font-size: 20px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
code, pre, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
pre { background: var(--code); border: 1px solid var(--line); border-radius: 8px; padding: 10px; overflow: auto; white-space: pre-wrap; word-break: break-all; margin: 6px 0; }
.hidden { display: none !important; }
.mt { margin-top: 22px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.ok { color: var(--ok); }
.bad { color: var(--danger); }
.warn { color: var(--warn); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.spacer { flex: 1; }

input, select, textarea, button {
  font: inherit;
  color: inherit;
}
input[type=text], input[type=number], input[type=email], input[type=password], input[type=search], select, textarea {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { width: 100%; resize: vertical; }
button, .btn {
  background: var(--accent);
  color: var(--accent-text);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 500;
  text-decoration: none;
  display: inline-block;
}
button:hover { filter: brightness(1.07); }
button:disabled { opacity: .55; cursor: default; }
button.secondary, .btn.secondary { background: transparent; color: var(--text); border-color: var(--line); }
button.danger { background: var(--danger); }
button.link { background: none; color: var(--accent); padding: 2px 4px; border: none; }
button.small, .btn.small { padding: 4px 9px; font-size: 12.5px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.stack > * + * { margin-top: 12px; }
.msg { padding: 8px 12px; border-radius: 8px; margin: 8px 0; }
.msg.error { background: rgba(208, 53, 63, .1); color: var(--danger); }
.msg.success { background: rgba(31, 157, 85, .1); color: var(--ok); }
.badge { display: inline-block; font-size: 11.5px; padding: 1px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.badge.ok { background: rgba(31, 157, 85, .12); color: var(--ok); }
.badge.bad { background: rgba(208, 53, 63, .12); color: var(--danger); }
.badge.gray { background: var(--line); color: var(--muted); }

table.grid { width: 100%; border-collapse: collapse; }
table.grid th, table.grid td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.grid th { font-weight: 600; color: var(--muted); font-size: 12.5px; }
table.kv td:first-child { color: var(--muted); white-space: nowrap; padding-right: 14px; }
table.kv td { padding: 3px 0; vertical-align: top; word-break: break-word; }

/* ---------- вхід ---------- */
.center-page { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-box { width: 100%; max-width: 380px; }
.login-box form > * { display: block; width: 100%; margin-top: 10px; }
.logo { font-weight: 700; font-size: 22px; letter-spacing: -.3px; }
.logo span { color: var(--accent); }

/* ---------- вебпошта ---------- */
.app { display: flex; flex-direction: column; height: 100vh; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.topbar .who { color: var(--muted); }
.main { flex: 1; display: flex; min-height: 0; }
.sidebar { width: 380px; max-width: 45%; border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; gap: 4px; padding: 8px; border-bottom: 1px solid var(--line); }
.tabs button { background: transparent; color: var(--muted); border: none; padding: 6px 12px; }
.tabs button.active { background: var(--accent-soft); color: var(--accent); }
.search { padding: 8px; border-bottom: 1px solid var(--line); }
.search input { width: 100%; }
.list { flex: 1; overflow-y: auto; }
.item { padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; border-left: 3px solid transparent; }
.item:hover { background: var(--bg); }
.item.active { background: var(--accent-soft); }
.item.unread { background: var(--unread); border-left-color: var(--accent); }
.item.unread .i-from, .item.unread .i-subj { font-weight: 700; }
.item .i-top { display: flex; gap: 8px; }
.item .i-from { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .i-date { color: var(--muted); font-size: 12px; white-space: nowrap; }
.item .i-subj { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); display: inline-block; margin-right: 6px; vertical-align: middle; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.reader { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: auto; }
.reader-head { padding: 16px 20px; background: var(--panel); border-bottom: 1px solid var(--line); }
.reader-head h2 { font-size: 19px; word-break: break-word; }
.reader-head .meta td:first-child { width: 90px; }
details.spoiler { margin-top: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
details.spoiler > summary { cursor: pointer; padding: 8px 12px; font-weight: 500; user-select: none; }
details.spoiler > div { padding: 0 12px 12px; overflow-x: auto; }
details.spoiler table.kv td { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.attachments { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.attachments a { font-size: 12.5px; border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; text-decoration: none; background: var(--panel); }
.body-frame { flex: 1; width: 100%; min-height: 420px; border: none; background: var(--panel); }

.modal-bg { position: fixed; inset: 0; background: var(--overlay); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 10; }
.modal { width: 100%; max-width: 680px; max-height: 92vh; overflow: auto; }
.modal label { display: block; margin-top: 10px; font-weight: 500; }
.modal input[type=text] { width: 100%; }

/* ---------- адмінка ---------- */
.admin-wrap { max-width: 1200px; margin: 0 auto; padding: 18px; }
.admin-nav { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 16px; }
.admin-nav button { background: transparent; color: var(--muted); border: 1px solid transparent; }
.admin-nav button.active { background: var(--panel); color: var(--accent); border-color: var(--line); box-shadow: var(--shadow); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.stat .v { font-size: 24px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 12.5px; }
.stat.warn { border-color: var(--danger); }
.stat.warn .v, .stat.warn .l { color: var(--danger); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.split { display: grid; grid-template-columns: 340px 1fr; gap: 16px; align-items: start; }
.scroll-list { max-height: 70vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.admin-reader { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--panel); display: flex; flex-direction: column; min-height: 520px; }
.dns-value { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; word-break: break-all; }

@media (max-width: 860px) {
  .main { flex-direction: column; }
  .sidebar { width: 100%; max-width: none; height: 42vh; border-right: none; border-bottom: 1px solid var(--line); }
  .two, .split { grid-template-columns: 1fr; }
}
.w-days { width: 110px; flex: 0 0 auto; }

/* Перемикач теми */
.theme-toggle { white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.theme-toggle svg { flex: none; }
.theme-corner { position: fixed; top: 12px; right: 12px; z-index: 5; }
