:root {
  --bg: #0f1115;
  --panel: #161920;
  --panel-2: #1c2029;
  --line: #262b36;
  --text: #e4e7ee;
  --muted: #8a92a3;
  --accent: #5b8cff;
  --accent-2: #2f4f99;
  --danger: #e5484d;
  --unread: #ffffff;
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
code, pre { font-family: ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace; }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 0; }
.small-text { font-size: 12px; }

button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 12px;
  cursor: pointer;
}
button:hover { border-color: #3a4152; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #4a7bef; }
button.ghost { background: transparent; }
button.danger { background: transparent; color: var(--danger); border-color: #4a2427; }
button.danger:hover { background: #2a1416; }
button.small { padding: 4px 9px; font-size: 12px; }
button:disabled { opacity: .5; cursor: default; }

input[type=text], input[type=password], input[type=search], input:not([type]) {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 10px;
  width: 100%;
  outline: none;
}
input:focus { border-color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; white-space: nowrap; }
.check.small { font-size: 12px; color: var(--muted); }

/* --- login --- */
.login { height: 100%; display: grid; place-items: center; }
.login-card {
  width: min(380px, 92vw);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 28px;
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0; }

/* --- layout --- */
.app {
  height: 100%;
  display: grid;
  grid-template-columns: 260px minmax(320px, 420px) 1fr;
}
.sidebar, .list-pane { border-right: 1px solid var(--line); min-height: 0; }
.sidebar { background: var(--panel); display: flex; flex-direction: column; }
.brand {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 14px 10px; font-weight: 600;
}
.sidebar-actions { padding: 0 14px 10px; display: grid; gap: 8px; }
.sidebar-foot { padding: 10px 14px; font-size: 12px; border-top: 1px solid var(--line); }

.mailboxes, .messages { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.mailboxes li {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 14px; cursor: pointer; border-left: 3px solid transparent;
}
.mailboxes li:hover { background: var(--panel-2); }
.mailboxes li.active { background: var(--panel-2); border-left-color: var(--accent); }
.mailboxes .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailboxes li.has-unread .name { font-weight: 600; color: var(--unread); }
.badge {
  font-size: 11px; padding: 1px 7px; border-radius: 10px;
  background: var(--accent-2); color: #dfe8ff;
}
.badge.dim { background: transparent; color: var(--muted); padding: 1px 0; }
.tag { font-size: 10px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }

.list-pane { display: flex; flex-direction: column; background: var(--bg); }
.pane-head { padding: 12px 14px 8px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.pane-title { display: flex; flex-direction: column; min-width: 0; }
.pane-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pane-title .muted { font-size: 12px; }
.toolbar { display: flex; gap: 6px; flex-wrap: wrap; }
.search-row { display: flex; gap: 8px; align-items: center; padding: 8px 14px; border-bottom: 1px solid var(--line); }

.messages li {
  padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 10px;
}
.messages li:hover { background: var(--panel); }
.messages li.active { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--accent); }
.messages .from, .messages .subject, .messages .rcpt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.messages .from { color: var(--muted); }
.messages .date { color: var(--muted); font-size: 12px; }
.messages .subject { grid-column: 1 / -1; }
.messages .rcpt { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.messages li.unread .from, .messages li.unread .subject { color: var(--unread); font-weight: 600; }
.messages li.unread { box-shadow: inset 3px 0 0 var(--accent-2); }
.messages li.unread.active { box-shadow: inset 3px 0 0 var(--accent); }

.empty { display: grid; place-items: center; padding: 40px 20px; text-align: center; flex: 1; }

/* --- message view --- */
.view-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.view { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.view-head { padding: 16px 20px 10px; border-bottom: 1px solid var(--line); }
.view-head h2 { margin: 0 0 10px; font-size: 18px; word-break: break-word; }
.meta { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 0; font-size: 13px; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; word-break: break-all; }
.view-actions { display: flex; gap: 6px; margin-top: 10px; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 10px 20px 0; }
.chip {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 15px; letter-spacing: .06em;
  background: var(--panel-2); border: 1px solid var(--accent-2); color: #dfe8ff;
}
.links { padding: 10px 20px 0; font-size: 13px; }
.links summary { cursor: pointer; color: var(--muted); }
.links ul { margin: 6px 0 0; padding-left: 18px; max-height: 160px; overflow-y: auto; }
.links li { word-break: break-all; margin: 2px 0; }
a { color: var(--accent); }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; padding: 10px 20px 0; }

.tabs { display: flex; gap: 4px; align-items: center; padding: 10px 20px 0; border-bottom: 1px solid var(--line); }
.tab { border: none; border-bottom: 2px solid transparent; border-radius: 0; background: transparent; color: var(--muted); padding: 6px 10px; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.images-toggle { margin-left: auto; }

.body { flex: 1; min-height: 0; display: flex; }
.body iframe { flex: 1; border: 0; background: #fff; width: 100%; }
.body pre {
  flex: 1; margin: 0; padding: 16px 20px; overflow: auto;
  white-space: pre-wrap; word-break: break-word; font-size: 13px;
}
#v-raw { font-size: 12px; color: #c3c8d4; }

/* --- dialogs --- */
dialog {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 22px; width: min(440px, 92vw);
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
dialog h3 { margin: 0 0 14px; }
dialog form { display: grid; gap: 12px; }
dialog label { display: grid; gap: 6px; font-size: 13px; }
.addr-input { display: flex; align-items: center; gap: 6px; }
.addr-input input { flex: 1; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.creds { display: grid; grid-template-columns: auto 1fr auto; gap: 8px 10px; align-items: center; }
.creds code { background: var(--bg); padding: 4px 8px; border-radius: 5px; word-break: break-all; user-select: all; }

.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 14px; box-shadow: 0 6px 24px rgba(0, 0, 0, .4); z-index: 10;
}
.toast.bad { border-color: var(--danger); color: #ffb4b6; }

/* --- narrow screens: one pane at a time --- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .app .sidebar, .app .list-pane, .app .view-pane { display: none; }
  .app[data-pane=mailboxes] .sidebar,
  .app[data-pane=messages] .list-pane,
  .app[data-pane=view] .view-pane { display: flex; }
  .back { display: inline-block !important; }
}
.back { display: none; }
