:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --panel-2: #f1f1ee;
  --text: #1d1d1b;
  --muted: #6b6b66;
  --line: #e4e4df;
  --accent: #e8590c;
  --accent-text: #ffffff;
  --accent-soft: #fdebdf;
  --danger: #c92a2a;
  --unread: #1d1d1b;
  --focus: #1c7ed6;
  --agent: #6741d9;
  --agent-soft: #efeafe;
  --danger-soft: #fdecec;
  --ok: #2b8a3e;
  --ok-soft: #ebf7ee;
  --warn: #a35a00;
  --warn-soft: #fff4e0;
  --radius: 10px;
  --shadow: 0 10px 40px rgb(0 0 0 / 0.12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413;
    --panel: #1c1c1a;
    --panel-2: #252522;
    --text: #ececea;
    --muted: #9a9a94;
    --line: #30302c;
    --accent: #ff7a33;
    --accent-text: #141413;
    --accent-soft: #3a2518;
    --danger: #ff6b6b;
    --unread: #ffffff;
    --agent: #a58bff;
    --agent-soft: #2a2340;
    --danger-soft: #3a1d1d;
    --ok: #69db7c;
    --ok-soft: #1b2e20;
    --warn: #ffc078;
    --warn-soft: #3a2c16;
    --shadow: 0 10px 40px rgb(0 0 0 / 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141413; --panel: #1c1c1a; --panel-2: #252522; --text: #ececea; --muted: #9a9a94; --line: #30302c;
  --accent: #ff7a33; --accent-text: #141413; --accent-soft: #3a2518; --danger: #ff6b6b; --unread: #ffffff;
  --agent: #a58bff; --agent-soft: #2a2340; --shadow: 0 10px 40px rgb(0 0 0 / 0.5); color-scheme: dark;
  --danger-soft: #3a1d1d; --ok: #69db7c; --ok-soft: #1b2e20; --warn: #ffc078; --warn-soft: #3a2c16;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
button {
  cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 12px;
}
button:hover { background: var(--panel-2); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
button.primary:hover { filter: brightness(1.05); }
button.link { border: 0; background: none; padding: 4px 6px; color: var(--muted); }
button.link:hover { color: var(--text); background: none; text-decoration: underline; }
button.danger, .danger { color: var(--danger) !important; }
.icon-btn { padding: 4px 9px; line-height: 1.2; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
input, select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 8px 10px;
}
textarea { resize: vertical; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.error { color: var(--danger); min-height: 1em; margin: 6px 0 0; }
[hidden] { display: none !important; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; width: min(380px, 100%); display: grid; gap: 12px; box-shadow: var(--shadow); }
.card h1 { margin: 0; font-size: 22px; }
.check { display: flex; gap: 8px; align-items: center; color: var(--muted); }
.check input { width: auto; }

/* Layout */
.app { display: grid; grid-template-columns: 240px minmax(300px, 400px) 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--panel-2); border-right: 1px solid var(--line); padding: 14px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.logo { width: 22px; height: 22px; border-radius: 6px; background: var(--accent); display: inline-block; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-left: auto; }
.live-dot.on { background: #2f9e44; }
.compose-btn { width: 100%; padding: 9px; }
.field-label { font-size: 12px; color: var(--muted); margin-bottom: -6px; }
.folders { display: grid; gap: 2px; }
.folders button, .mailbox-list button {
  display: flex; justify-content: space-between; align-items: center; width: 100%; text-align: left; border: 0; background: none; padding: 7px 10px;
}
.folders button.active { background: var(--accent-soft); font-weight: 600; }
.count { font-size: 12px; color: var(--muted); }
.folders button.active .count, .count.unread { color: var(--accent); font-weight: 700; }
.sidebar-section { border-top: 1px solid var(--line); padding-top: 10px; }
.section-head { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.mailbox-list { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 2px; }
.mailbox-list button { font-size: 13px; overflow: hidden; }
.mailbox-list .addr { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { font-size: 10.5px; padding: 1px 6px; border-radius: 99px; background: var(--agent-soft); color: var(--agent); font-weight: 600; flex: none; }
.sidebar-foot { margin-top: auto; display: flex; justify-content: space-between; }

.list-pane { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; background: var(--panel); }
.list-head { padding: 10px; border-bottom: 1px solid var(--line); display: flex; gap: 8px; }
.bulk-bar { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); background: var(--accent-soft); flex-wrap: wrap; }
.bulk-bar button { padding: 3px 8px; font-size: 12.5px; }
.message-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.message-list li { border-bottom: 1px solid var(--line); display: grid; grid-template-columns: auto 1fr; gap: 8px; padding: 10px 12px; cursor: pointer; }
.message-list li:hover { background: var(--panel-2); }
.message-list li.selected { background: var(--accent-soft); }
.message-list li .sel { margin-top: 2px; width: auto; }
.message-list .row1 { display: flex; justify-content: space-between; gap: 8px; }
.message-list .who { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-list .when { color: var(--muted); font-size: 12px; flex: none; }
.message-list .subj { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-list .snip { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-list li.unread .who, .message-list li.unread .subj { font-weight: 700; color: var(--unread); }
.message-list .tags { display: flex; gap: 4px; margin-top: 3px; }
.star { color: #f59f00; }
.list-foot { padding: 10px; }

.reader-pane { overflow-y: auto; min-width: 0; background: var(--panel); }
.reader-empty { height: 100%; display: grid; place-items: center; }
.reader { padding: 16px 22px 40px; display: flex; flex-direction: column; gap: 10px; min-height: 100%; }
.reader-head { display: flex; gap: 8px; align-items: center; }
.reader-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.reader-actions button { font-size: 12.5px; padding: 4px 10px; }
.reader h2 { margin: 4px 0 0; font-size: 20px; line-height: 1.3; overflow-wrap: anywhere; }
.meta { border-bottom: 1px solid var(--line); padding-bottom: 10px; overflow-wrap: anywhere; }
.delivery-note { border-radius: 8px; padding: 8px 12px; font-size: 13px; background: var(--ok-soft); color: var(--ok); display: grid; gap: 2px; overflow-wrap: anywhere; }
.delivery-note.failed, .delivery-note.complained { background: var(--danger-soft); color: var(--danger); }
.delivery-note.deferred, .delivery-note.sending { background: var(--warn-soft); color: var(--warn); }
.badge.delivery.failed, .badge.delivery.complained { background: var(--danger-soft); color: var(--danger); }
.badge.delivery.deferred { background: var(--warn-soft); color: var(--warn); }
.auth { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.auth-pill { font-size: 10.5px; padding: 1px 6px; border-radius: 99px; font-weight: 600; background: var(--panel-2); color: var(--muted); }
.auth-pill.pass { background: var(--ok-soft); color: var(--ok); }
.auth-pill.fail { background: var(--danger-soft); color: var(--danger); }
.dialog h4 { margin: 10px 0 4px; font-size: 13px; }
.agent-note { background: var(--agent-soft); color: var(--agent); border-radius: 8px; padding: 8px 12px; font-size: 13px; }
.images-bar { background: var(--panel-2); padding: 6px 10px; border-radius: 8px; font-size: 12.5px; }
.body-frame { width: 100%; border: 0; min-height: 300px; background: #fff; border-radius: 6px; flex: 1; }
.body-text { white-space: pre-wrap; overflow-wrap: anywhere; font: inherit; margin: 0; }
.attachments { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.attachments button { font-size: 12.5px; }
.thread ul { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 4px; }
.thread button { width: 100%; text-align: left; font-size: 12.5px; }
.thread summary { cursor: pointer; color: var(--muted); }

/* Dialogs */
.dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); box-shadow: var(--shadow); width: min(480px, calc(100vw - 32px)); }
.dialog.wide { width: min(720px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgb(0 0 0 / 0.35); }
.dialog form { padding: 20px; display: grid; gap: 10px; }
.dialog h3 { margin: 0 0 4px; }
.dialog label { display: grid; gap: 4px; font-size: 12.5px; color: var(--muted); }
.dialog label > input, .dialog label > select, .dialog label > textarea { color: var(--text); font-size: 14px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; }
.dialog-actions .danger { margin-right: auto; }
.row { display: flex; gap: 8px; }
.block-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; max-height: 240px; overflow-y: auto; }
.block-list li { display: flex; justify-content: space-between; align-items: center; padding: 4px 8px; background: var(--panel-2); border-radius: 6px; }

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

.mobile-only { display: none; }

@media (max-width: 960px) {
  .app { grid-template-columns: 220px 1fr; }
  .reader-pane { position: fixed; inset: 0; z-index: 5; display: none; }
  .app.reading .reader-pane { display: block; }
  .app.reading #back-btn { display: inline-block; }
}
@media (max-width: 680px) {
  .app { grid-template-columns: 1fr; }
  .mobile-only { display: inline-block; }
  .sidebar { position: fixed; inset: 0 25% 0 0; z-index: 6; transform: translateX(-105%); visibility: hidden; transition: transform 0.2s, visibility 0.2s; }
  .app.menu-open .sidebar { transform: none; visibility: visible; box-shadow: var(--shadow); }
  .reader { padding: 12px 16px 32px; }
}
