:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f9fafb;
  --border: #e3e6ea;
  --text: #1b1f24;
  --muted: #69717c;
  --accent: #2f6fed;
  --accent-soft: #e8effd;
  --accent-text: #ffffff;
  --danger: #d93a3a;
  --danger-soft: #fdecec;
  --unread: #2f6fed;
  --hover: #f0f2f5;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --panel: #171a20;
    --panel-2: #1c2027;
    --border: #2a2f38;
    --text: #e6e8eb;
    --muted: #8d95a1;
    --accent: #5b8cff;
    --accent-soft: #1f2a44;
    --accent-text: #ffffff;
    --danger: #ff6b6b;
    --danger-soft: #3a1f22;
    --unread: #5b8cff;
    --hover: #20242c;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
    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;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
input, select {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  outline: none;
  min-width: 0;
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); margin: 0; font-size: 13px; min-height: 0; }
.error:empty { display: none; }

svg.i { width: 18px; height: 18px; display: block; flex: none; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 500;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; flex: none;
  border: none; background: transparent; border-radius: 8px;
  color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); padding: 28px;
  display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.logo { margin: 0 auto; width: 44px; height: 44px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.logo svg.i { width: 24px; height: 24px; }

/* ---------- Layout ---------- */
.app {
  height: 100%;
  display: grid;
  grid-template-columns: 290px minmax(280px, 380px) 1fr;
}
.sidebar, .list-pane, .reader-pane { min-height: 0; min-width: 0; display: flex; flex-direction: column; }
.sidebar { background: var(--panel-2); border-right: 1px solid var(--border); }
.list-pane { background: var(--panel); border-right: 1px solid var(--border); }
.reader-pane { background: var(--panel); }
.back { display: none; }

/* ---------- Sidebar ---------- */
.brand { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 6px 16px; }
.brand-name { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; }
.brand-name svg.i { color: var(--accent); }

.create {
  margin: 6px 12px 10px; padding: 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  display: flex; flex-direction: column; gap: 8px;
}
.create-label { font-weight: 600; font-size: 13px; }
.addr-input {
  display: flex; align-items: center;
  border: 1px solid var(--border); border-radius: 8px; background: var(--panel);
}
.addr-input:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.addr-input input { flex: 1 1 auto; border: none; box-shadow: none !important; background: transparent; padding: 8px 6px 8px 10px; }
.addr-input .icon-btn { width: 30px; height: 30px; margin-right: 2px; }
#create-domain { width: 100%; cursor: pointer; }
.domain-text { padding: 0 2px; margin-top: -4px; font-size: 13px; overflow-wrap: anywhere; }

.addr-tools { padding: 0 12px 6px; }
.addr-tools input { width: 100%; }

.addr-list { flex: 1; overflow-y: auto; padding: 4px 8px 16px; }
.addr-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 8px 8px 10px; border-radius: 8px; cursor: pointer;
  position: relative;
}
.addr-item:hover { background: var(--hover); }
.addr-item.active { background: var(--accent-soft); }
.addr-item .addr-main { flex: 1; min-width: 0; }
.addr-item .addr-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.addr-item .addr-name .dom { color: var(--muted); font-weight: 400; }
.addr-item .addr-label { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.addr-item.has-unread .addr-name { font-weight: 700; }
.badge {
  flex: none; min-width: 20px; padding: 0 6px; height: 20px; border-radius: 10px;
  background: var(--accent); color: var(--accent-text); font-size: 11.5px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
}
.addr-item .copy { width: 28px; height: 28px; opacity: 0; }
.addr-item:hover .copy, .addr-item:focus-within .copy { opacity: 1; }
.addr-item.all { margin-bottom: 4px; }
.addr-item.all svg.i { color: var(--muted); }
.addr-section { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 10px 10px 4px; }
.addr-empty { color: var(--muted); padding: 12px 10px; font-size: 13px; }

/* ---------- Message list ---------- */
.pane-head { display: flex; align-items: center; gap: 6px; padding: 10px 10px 6px 16px; min-height: 56px; }
.pane-title { flex: 1; min-width: 0; }
.title-text { font-weight: 650; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions { display: flex; gap: 2px; }
.search-row { padding: 0 12px 8px; border-bottom: 1px solid var(--border); }
.search-row input { width: 100%; }
.msg-list { flex: 1; overflow-y: auto; }
.msg-item {
  display: grid; grid-template-columns: 10px 1fr auto; column-gap: 8px; row-gap: 1px;
  padding: 10px 14px 10px 8px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.msg-item:hover { background: var(--hover); }
.msg-item.active { background: var(--accent-soft); }
.msg-item .dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; }
.msg-item.unread .dot { background: var(--unread); }
.msg-item .from { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-item.unread .from, .msg-item.unread .subj { font-weight: 700; }
.msg-item .time { font-size: 12px; color: var(--muted); white-space: nowrap; display: flex; align-items: center; gap: 4px; }
.msg-item .time svg.i { width: 14px; height: 14px; }
.msg-item .subj, .msg-item .snip, .msg-item .to-chip { grid-column: 2 / 4; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-item .snip { color: var(--muted); font-size: 13px; }
.msg-item .to-chip { font-size: 11.5px; color: var(--accent); }
.list-empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.list-empty code { background: var(--hover); padding: 2px 6px; border-radius: 6px; color: var(--text); user-select: all; }
.load-more { display: block; margin: 12px auto; }

/* ---------- Reader ---------- */
.empty { flex: 1; display: grid; place-content: center; justify-items: center; color: var(--muted); gap: 4px; }
.empty-icon svg.i { width: 40px; height: 40px; opacity: .5; }
.reader { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.reader-head { padding: 8px 20px 12px; border-bottom: 1px solid var(--border); }
.reader-bar { display: flex; align-items: center; gap: 2px; margin: 0 -8px; }
.spacer { flex: 1; }
.reader h2 { font-size: 19px; margin: 4px 0 12px; word-break: break-word; }
.meta { display: flex; gap: 10px; align-items: center; }
.meta-text { min-width: 0; word-break: break-word; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 600; color: #fff;
}
.remote-bar {
  margin-top: 12px; padding: 8px 12px; border-radius: 8px;
  background: var(--panel-2); border: 1px solid var(--border);
  display: flex; gap: 10px; align-items: center; justify-content: space-between; font-size: 13px; color: var(--muted);
}
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.attachments:empty { display: none; }
.att {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px;
  color: var(--text); text-decoration: none; font-size: 13px;
}
.att:hover { background: var(--hover); }
.att .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; }
.att.missing { opacity: .6; cursor: default; }
.msg-body { flex: 1; min-height: 0; overflow: auto; }
.msg-body iframe { width: 100%; height: 100%; border: 0; background: #fff; display: block; }
.msg-body pre {
  margin: 0; padding: 20px; white-space: pre-wrap; word-break: break-word;
  font: 14px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .app { grid-template-columns: 260px 320px 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .app .sidebar, .app .list-pane, .app .reader-pane { display: none; border: none; }
  .app[data-view="sidebar"] .sidebar,
  .app[data-view="list"] .list-pane,
  .app[data-view="reader"] .reader-pane { display: flex; }
  .back { display: inline-flex; }
  .pane-head { padding-left: 8px; }
  .reader-head { padding: 8px 16px 12px; }
  .addr-item .copy { opacity: 1; }
}
