:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #7c3aed;
  --accent-2: #6d28d9;
  --accent-soft: #ede9fe;
  --danger: #dc2626;
  --ok: #16a34a;
  --unread: #0f172a;
  --sel: #ede9fe;
  --hover: #f1f5f9;
  --shadow: 0 10px 30px rgba(15, 23, 42, .15);
  --radius: 10px;
  --row-pad: 10px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0b1120;
  --panel: #111827;
  --panel-2: #0f172a;
  --border: #1f2937;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --accent: #a78bfa;
  --accent-2: #8b5cf6;
  --accent-soft: #2e1065;
  --unread: #ffffff;
  --sel: #312e81;
  --hover: #1e293b;
  --shadow: 0 10px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
:root[data-density="compact"] { --row-pad: 5px; }

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; }
.spacer { flex: 1; }

/* ---------- Botões ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--panel); border-radius: 8px;
  padding: 7px 14px; cursor: pointer; white-space: nowrap; transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--hover); }
.btn.danger { color: var(--danger); }
.btn.link { border: none; background: none; color: var(--accent); padding: 4px 6px; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn.left { justify-content: flex-start; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn {
  border: none; background: transparent; border-radius: 8px; width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; font-size: 16px; flex: none;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn.on { color: #f59e0b; }
.chk { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; user-select: none; }

input[type=text], input[type=email], input[type=password], input[type=search], input[type=number], select, textarea {
  border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 8px 10px; width: 100%;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
label { display: block; }

/* ---------- Login ---------- */
.login-view { min-height: 100%; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(circle at 20% 10%, #7c3aed33, transparent 50%), radial-gradient(circle at 80% 90%, #0ea5e933, transparent 50%), var(--bg); }
.login-card { width: min(380px, 100%); background: var(--panel); padding: 32px; border-radius: 16px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.login-card h1 { margin: 0; font-size: 22px; text-align: center; }
.login-card p { margin: 0; text-align: center; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login-logo { width: 64px; height: 64px; margin: 0 auto; }

/* ---------- Layout principal ---------- */
.app { display: grid; grid-template-columns: 250px minmax(320px, 420px) 1fr; height: 100vh; height: 100dvh; }
.sidebar { background: var(--panel-2); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.sidebar-top { padding: 14px; }
.compose-btn { width: 100%; padding: 11px; font-size: 15px; border-radius: 12px; }
.folders { flex: 1; overflow: auto; padding: 0 8px 8px; }
.sidebar-bottom { padding: 8px; border-top: 1px solid var(--border); display: grid; gap: 2px; }
.me { padding: 4px 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.acc-head { display: flex; align-items: center; gap: 6px; padding: 10px 8px 4px; font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .03em; color: var(--muted); cursor: pointer; user-select: none; }
.acc-head .caret { transition: transform .15s; }
.acc-head.collapsed .caret { transform: rotate(-90deg); }
.acc-head .acc-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.folder { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 8px; cursor: pointer;
  user-select: none; white-space: nowrap; }
.folder:hover { background: var(--hover); }
.folder.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folder .f-ico { width: 18px; text-align: center; flex: none; }
.folder .f-name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.folder .badge { font-size: 11px; background: var(--accent); color: #fff; border-radius: 10px; padding: 0 7px; font-weight: 600; }
.folder.drop-target { outline: 2px dashed var(--accent); }
.folder-error { color: var(--danger); font-size: 12px; padding: 4px 10px; }

.list-pane { display: flex; flex-direction: column; border-right: 1px solid var(--border); background: var(--panel); min-height: 0; min-width: 0; }
.pane-header { display: flex; align-items: center; gap: 8px; padding: 12px 12px 6px; }
.list-title { flex: 1; min-width: 0; }
.list-title .title { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-row { display: flex; gap: 8px; align-items: center; padding: 4px 12px 8px; }
.search-row input { flex: 1; }
.bulk-bar { display: flex; align-items: center; gap: 4px; padding: 4px 12px; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); background: var(--panel-2); }
.bulk-bar span { flex: 1; }
.messages { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; border-top: 1px solid var(--border); }
.msg { display: grid; grid-template-columns: 22px 1fr auto; gap: 2px 8px; padding: var(--row-pad) 12px; border-bottom: 1px solid var(--border);
  cursor: pointer; position: relative; user-select: none; }
.msg:hover { background: var(--hover); }
.msg.selected { background: var(--sel); }
.msg.unread .m-from, .msg.unread .m-subject { font-weight: 700; color: var(--unread); }
.msg.unread::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent); }
.msg .m-check { grid-row: span 2; align-self: center; }
.msg .m-from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .m-date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.msg .m-subject { grid-column: 2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.msg .m-icons { font-size: 12px; color: var(--muted); display: flex; gap: 4px; justify-content: flex-end; }
.msg .m-icons .flag { color: #f59e0b; }
.list-footer { padding: 10px; text-align: center; }
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.skeleton { height: 54px; margin: 1px 0; background: linear-gradient(90deg, var(--hover), var(--panel), var(--hover)); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }

/* ---------- Leitura ---------- */
.reader { min-width: 0; min-height: 0; background: var(--panel); display: flex; }
.reader-empty { margin: auto; text-align: center; }
.big-ico { font-size: 64px; opacity: .25; }
.reader-content { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.reader-toolbar { display: flex; align-items: center; gap: 6px; padding: 10px 14px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.reader-scroll { overflow: auto; flex: 1; padding: 0 22px 22px; display: flex; flex-direction: column; }
.r-subject { font-size: 21px; margin: 18px 0 14px; word-break: break-word; }
.r-head { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 12px; }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; flex: none; text-transform: uppercase; }
.r-meta { flex: 1; min-width: 0; }
.r-from { font-weight: 600; word-break: break-all; }
.r-from .addr { font-weight: 400; color: var(--muted); }
.r-date { white-space: nowrap; }
.banner { background: #fef3c7; color: #78350f; border-radius: 8px; padding: 8px 12px; display: flex; align-items: center; gap: 10px; justify-content: space-between; margin-bottom: 10px; font-size: 13px; }
:root[data-theme="dark"] .banner { background: #422006; color: #fde68a; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.att { display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; max-width: 260px;
  background: var(--panel-2); text-decoration: none; color: inherit; cursor: pointer; }
.att:hover { border-color: var(--accent); }
.att .a-thumb { width: 32px; height: 32px; object-fit: cover; border-radius: 4px; flex: none; display: grid; place-items: center; font-size: 20px; }
.att .a-info { min-width: 0; }
.att .a-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.att .a-size { font-size: 11px; color: var(--muted); }
.att .a-x { border: none; background: none; cursor: pointer; color: var(--muted); font-size: 14px; }
.att.uploading { opacity: .6; }
.r-body { border: none; width: 100%; flex: 1; min-height: 300px; background: #fff; border-radius: 8px; }

/* ---------- Escrever ---------- */
.compose { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: flex-end; pointer-events: none; padding: 0 24px; }
.compose-win { pointer-events: auto; width: min(760px, 100%); height: min(680px, 92vh); background: var(--panel); border-radius: 12px 12px 0 0;
  box-shadow: var(--shadow); display: flex; flex-direction: column; position: relative; border: 1px solid var(--border); border-bottom: none; }
.compose.max { padding: 3vh 4vw; align-items: stretch; background: rgba(15, 23, 42, .45); pointer-events: auto; }
.compose.max .compose-win { width: 100%; height: 100%; border-radius: 12px; border: 1px solid var(--border); }
.compose-head { display: flex; align-items: center; gap: 10px; padding: 8px 10px 8px 16px; background: var(--panel-2); border-radius: 12px 12px 0 0; border-bottom: 1px solid var(--border); }
.compose-fields { padding: 4px 14px; }
.field-row { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--border); min-height: 42px; }
.field-row > label { width: 58px; color: var(--muted); font-size: 13px; flex: none; }
.field-row select, .field-row input[type=text] { border: none; box-shadow: none !important; padding: 8px 0; background: transparent; }
.recips { flex: 1; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; padding: 5px 0; position: relative; min-width: 0; }
.recips input { border: none !important; box-shadow: none !important; padding: 4px 0 !important; flex: 1; min-width: 160px; width: auto !important; background: transparent; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: var(--accent-soft); border-radius: 14px; padding: 2px 4px 2px 10px; font-size: 13px; max-width: 100%; }
.chip.invalid { background: #fee2e2; color: #991b1b; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { border: none; background: none; cursor: pointer; padding: 0 4px; color: inherit; opacity: .7; }
.suggest { position: absolute; top: 100%; left: 0; z-index: 5; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  min-width: 280px; max-width: 100%; overflow: hidden; }
.suggest div { padding: 7px 12px; cursor: pointer; }
.suggest div.active, .suggest div:hover { background: var(--hover); }
.compose-editor { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.compose-editor .tox-tinymce { border: none !important; border-radius: 0 !important; flex: 1; }
.compose-att { padding: 0 14px; margin: 6px 0 0; max-height: 110px; overflow: auto; }
.compose-foot { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.drop-overlay { position: absolute; inset: 0; background: rgba(124, 58, 237, .12); border: 3px dashed var(--accent); border-radius: 12px;
  display: grid; place-items: center; font-size: 20px; font-weight: 700; color: var(--accent); pointer-events: none; }

/* ---------- Modais ---------- */
.modal { position: fixed; inset: 0; z-index: 80; background: rgba(15, 23, 42, .5); display: grid; place-items: center; padding: 16px; }
.modal-win { background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); width: min(460px, 100%); max-height: 92vh; display: flex; flex-direction: column; overflow: hidden; }
.modal-win.wide { width: min(980px, 100%); height: min(760px, 92vh); }
.modal-head { display: flex; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 16px; }
.small-win { padding: 20px; gap: 14px; }
.small-win h3 { margin: 0; }
.small-win .actions { display: flex; justify-content: flex-end; gap: 8px; }
.small-win .list { max-height: 50vh; overflow: auto; border: 1px solid var(--border); border-radius: 8px; }
.small-win .list button { display: block; width: 100%; text-align: left; border: none; background: none; padding: 8px 12px; cursor: pointer; }
.small-win .list button:hover { background: var(--hover); }
.settings-body { display: flex; flex: 1; min-height: 0; }
.tabs { width: 180px; border-right: 1px solid var(--border); padding: 10px; display: flex; flex-direction: column; gap: 2px; flex: none; }
.tabs button { text-align: left; border: none; background: none; padding: 9px 12px; border-radius: 8px; cursor: pointer; }
.tabs button:hover { background: var(--hover); }
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tab-content { flex: 1; overflow: auto; padding: 18px 22px; }
.tab-content h3 { margin-top: 0; }
.card { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin-bottom: 10px; display: flex; align-items: center; gap: 12px; }
.card .grow { flex: 1; min-width: 0; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.form-grid label { font-size: 13px; color: var(--muted); display: grid; gap: 4px; }
.form-grid .full { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; margin: 0 0 12px; }
legend { padding: 0 6px; font-weight: 600; }
.test-result { font-size: 13px; margin-top: 8px; }
.test-result .ok { color: var(--ok); }
.test-result .bad { color: var(--danger); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
table.users { width: 100%; border-collapse: collapse; }
table.users td, table.users th { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); font-size: 13px; }

/* ---------- Menu ---------- */
.menu-wrap { position: relative; }
.menu { position: absolute; right: 0; top: 100%; z-index: 20; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); min-width: 190px; padding: 4px; }
.menu button { display: block; width: 100%; text-align: left; border: none; background: none; padding: 8px 12px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--hover); }

/* ---------- Toasts ---------- */
.toasts { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 100; display: grid; gap: 8px; }
.toast { background: #1e293b; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); display: flex; gap: 12px; align-items: center; animation: tin .2s; max-width: 90vw; }
.toast.err { background: var(--danger); }
.toast button { background: none; border: none; color: #c4b5fd; font-weight: 700; cursor: pointer; }
@keyframes tin { from { opacity: 0; transform: translateY(8px); } }

.only-mobile { display: none !important; }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 30; }

/* ---------- Responsivo ---------- */
@media (max-width: 1150px) {
  .app { grid-template-columns: 220px minmax(280px, 340px) 1fr; }
}
@media (max-width: 900px) {
  .only-mobile { display: inline-flex !important; }
  .only-desktop { display: none !important; }
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; width: min(300px, 85vw); transform: translateX(-100%); transition: transform .2s; }
  .app.menu-open .sidebar { transform: none; }
  .reader { position: fixed; inset: 0; z-index: 20; transform: translateX(100%); transition: transform .2s; }
  .app.reading .reader { transform: none; }
  .reader-toolbar .btn.small { padding: 4px 8px; }
  .reader-scroll { padding: 0 12px 12px; }
  .compose { padding: 0; }
  .compose-win, .compose.max .compose-win { width: 100%; height: 100%; border-radius: 0; }
  .compose.max { padding: 0; }
  .compose-head { border-radius: 0; }
  .settings-body { flex-direction: column; }
  .tabs { width: auto; flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .modal { padding: 0; }
  .modal-win.wide { width: 100%; height: 100%; border-radius: 0; max-height: none; }
  .msg { padding: 12px; }
}
@media (max-width: 520px) {
  .reader-toolbar .btn.small { font-size: 0; gap: 0; }
  .reader-toolbar .btn.small::first-letter { font-size: 16px; }
}
@media print {
  .sidebar, .list-pane, .reader-toolbar, .banner { display: none !important; }
  .app { display: block; }
}
