:root {
  --bg: #f3f5f9;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --border: #e3e8ef;
  --text: #0f172a;
  --muted: #64748b;
  --brand: #1d4ed8;
  --brand-2: #1e40af;
  --brand-soft: #dbeafe;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --ok: #16a34a;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --unread: #0f172a;
  --shadow: 0 10px 30px rgba(15, 23, 42, .12);
  --radius: 12px;
  --sidebar-w: 250px;
  --list-w: 400px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220; --panel: #111a2e; --panel-2: #0f172a; --border: #223050; --text: #e5e9f2; --muted: #93a0b8;
    --brand: #3b82f6; --brand-2: #2563eb; --brand-soft: #1e3a8a; --danger: #f87171; --danger-soft: #3f1d1d; --warn-soft: #3b2a09; --unread: #ffffff;
    --shadow: 0 10px 30px rgba(0,0,0,.5);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif; color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; }
code { background: var(--panel-2); padding: 1px 4px; border-radius: 4px; border: 1px solid var(--border); }

/* ---------- buttons ---------- */
.btn { border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 8px 12px; cursor: pointer; white-space: nowrap; transition: background .15s, border-color .15s; }
.btn:hover { background: var(--panel-2); border-color: #c9d2e0; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.sm { padding: 5px 9px; font-size: 12.5px; }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.wide { width: 100%; }
.icon-btn { border: 0; background: transparent; border-radius: 8px; width: 34px; height: 34px; cursor: pointer; font-size: 16px; color: var(--muted); }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.link-btn { border: 0; background: none; color: var(--brand); cursor: pointer; padding: 0; text-decoration: underline; }
.only-mobile { display: none; }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; padding: 14px 16px; }
.brand .logo { width: 34px; height: 34px; border-radius: 10px; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 800; font-size: 18px; }
.brand.big .logo { width: 48px; height: 48px; font-size: 26px; border-radius: 14px; }
.brand strong { display: block; font-size: 15px; }
.brand small { color: var(--muted); }
.brand.big { padding: 0 0 18px; }

/* ---------- login ---------- */
#login { min-height: 100%; display: grid; place-items: center; padding: 24px; background: radial-gradient(1200px 600px at 10% -10%, var(--brand-soft), transparent), var(--bg); }
.login-card { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.login-card label { display: block; margin-bottom: 14px; font-weight: 600; font-size: 13px; }
.login-card input[type=text], .login-card input[type=password] { display: block; width: 100%; margin-top: 6px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); }
.login-card input:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500 !important; }
.error { background: var(--danger-soft); color: var(--danger); padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }

/* ---------- app layout ---------- */
#app { height: 100%; display: grid; grid-template-columns: var(--sidebar-w) var(--list-w) 1fr; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.compose-btn { margin: 4px 16px 14px; padding: 10px; border-radius: 10px; font-weight: 600; }
.folders { list-style: none; margin: 0; padding: 0 10px; overflow: auto; flex: 1; }
.folders li { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; cursor: pointer; color: var(--text); }
.folders li:hover { background: var(--panel-2); }
.folders li.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.folders li .ico { width: 20px; text-align: center; }
.folders li .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folders li .badge { background: var(--brand); color: #fff; border-radius: 999px; padding: 1px 7px; font-size: 11px; font-weight: 600; }
.folders li.active .badge { background: var(--brand-2); }
.folders li.junk .badge { background: var(--warn); }
.spam-card { margin: 10px 16px; padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel-2); font-size: 12.5px; }
.spam-title { font-weight: 600; display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.pill { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: #dcfce7; color: #166534; font-weight: 600; }
.pill.off { background: var(--danger-soft); color: var(--danger); }
.spam-stats { color: var(--muted); margin-bottom: 6px; }
.user-box { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--border); }
.user-info { flex: 1; min-width: 0; }
.user-info strong, .user-info small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-info small { color: var(--muted); }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; flex: none; text-transform: uppercase; }

/* ---------- list pane ---------- */
.list-pane { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.pane-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px 6px; }
.pane-title { flex: 1; min-width: 0; }
.pane-title h1 { margin: 0; font-size: 18px; }
.pane-title small { color: var(--muted); }
.search-row { display: flex; gap: 6px; padding: 6px 14px; }
.search-row input { flex: 1; padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); }
.toolbar { display: flex; align-items: center; gap: 6px; padding: 6px 14px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.toolbar .actions { display: none; gap: 4px; flex-wrap: wrap; }
.toolbar.has-sel .actions { display: flex; }
.toolbar.has-sel #emptyFolderBtn { display: none; }
.sel-count { color: var(--muted); font-size: 12px; flex: 1; }
#emptyFolderBtn { margin-left: auto; }
.msg-list { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; }
.msg-list li { display: grid; grid-template-columns: 24px 36px 1fr auto; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer; align-items: start; }
.msg-list li:hover { background: var(--panel-2); }
.msg-list li.active { background: var(--brand-soft); }
.msg-list li.unread .from, .msg-list li.unread .subject { font-weight: 700; color: var(--unread); }
.msg-list li .from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-list li .subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.msg-list li .meta { text-align: right; color: var(--muted); font-size: 11.5px; white-space: nowrap; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.msg-list li .icons { font-size: 11px; }
.msg-list li .flag { color: var(--warn); }
.msg-list li .avatar { width: 36px; height: 36px; font-size: 13px; }
.msg-list .empty-row { padding: 40px 20px; text-align: center; color: var(--muted); cursor: default; display: block; }
.pager { display: flex; justify-content: space-between; align-items: center; padding: 8px 14px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; }

/* ---------- read pane ---------- */
.read-pane { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.empty { flex: 1; display: grid; place-items: center; color: var(--muted); }
.empty-icon { font-size: 56px; text-align: center; opacity: .4; }
.message { flex: 1; display: flex; flex-direction: column; min-height: 0; margin: 14px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.msg-head { padding: 16px 20px 12px; border-bottom: 1px solid var(--border); }
.msg-head h2 { margin: 0 0 10px; font-size: 20px; line-height: 1.3; word-break: break-word; }
.msg-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.msg-meta { display: flex; gap: 12px; align-items: flex-start; }
.meta-text { flex: 1; min-width: 0; word-break: break-word; }
.notice { margin-top: 10px; background: var(--warn-soft); padding: 8px 12px; border-radius: 8px; font-size: 12.5px; }
.attachments { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 8px; }
.attachments a { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; text-decoration: none; color: var(--text); background: var(--panel-2); font-size: 12.5px; max-width: 260px; }
.attachments a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachments a small { color: var(--muted); }
.msg-body { flex: 1; border: 0; width: 100%; background: #fff; }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.dialog { width: 100%; max-width: 760px; max-height: 95vh; background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; }
.dialog > header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--panel-2); }
.dialog-body { padding: 16px; overflow: auto; }
.dialog-body section { margin-bottom: 20px; }
.dialog-body h3 { margin: 0 0 8px; font-size: 14px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.dialog textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px; background: var(--panel-2); resize: vertical; }
.compose form { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.field { display: flex; align-items: center; gap: 8px; padding: 6px 16px; border-bottom: 1px solid var(--border); }
.field label { width: 60px; color: var(--muted); font-size: 13px; flex: none; }
.field input { flex: 1; border: 0; background: transparent; padding: 6px 0; outline: none; min-width: 0; }
.editor-toolbar { display: flex; gap: 4px; padding: 6px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; align-items: center; }
.editor-toolbar button, .attach-btn { border: 1px solid transparent; background: transparent; border-radius: 6px; padding: 4px 8px; cursor: pointer; font-size: 13px; }
.editor-toolbar button:hover, .attach-btn:hover { background: var(--panel-2); border-color: var(--border); }
.editor { flex: 1; min-height: 220px; max-height: 50vh; overflow: auto; padding: 14px 16px; outline: none; line-height: 1.5; }
.editor blockquote, .quote blockquote { border-left: 3px solid var(--border); margin: 8px 0; padding-left: 10px; color: var(--muted); }
.attach-list { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 8px; }
.attach-list span { border: 1px solid var(--border); border-radius: 6px; padding: 3px 8px; font-size: 12px; background: var(--panel-2); display: inline-flex; gap: 6px; align-items: center; }
.attach-list button { border: 0; background: none; cursor: pointer; color: var(--muted); }
.compose footer { display: flex; gap: 8px; align-items: center; padding: 10px 16px; border-top: 1px solid var(--border); background: var(--panel-2); }
.table-wrap { overflow: auto; max-height: 260px; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--panel-2); position: sticky; top: 0; }

/* ---------- toasts / busy ---------- */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13px; animation: pop .2s ease; }
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }
@keyframes pop { from { transform: translateY(10px); opacity: 0 } }
.busy { position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 200; background: linear-gradient(90deg, transparent, var(--brand), transparent); background-size: 200% 100%; animation: bar 1s linear infinite; }
@keyframes bar { to { background-position: -200% 0 } }
.spinner { display: none; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) { :root { --list-w: 340px; --sidebar-w: 220px; } }
@media (max-width: 860px) {
  .only-mobile { display: inline-flex; }
  #app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  #app.sidebar-open .sidebar { transform: none; }
  .read-pane { display: none; }
  #app.reading .read-pane { display: flex; }
  #app.reading .list-pane { display: none; }
  .message { margin: 0; border: 0; border-radius: 0; }
  .two-col { grid-template-columns: 1fr; }
  .msg-list li { grid-template-columns: 24px 1fr auto; }
  .msg-list li .avatar { display: none; }
}
