:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1b1f27;
  --muted: #667085;
  --border: #e3e6eb;
  --accent: #2f5bea;
  --accent-soft: #e8eefe;
  --danger: #d0342c;
  --hover: #f0f2f5;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --panel: #1a1d24;
    --text: #e6e8ec;
    --muted: #98a0ae;
    --border: #2a2e37;
    --accent: #6d8dff;
    --accent-soft: #232c47;
    --danger: #ff6b62;
    --hover: #22262e;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn {
  border: 0; background: transparent; cursor: pointer;
  width: 34px; height: 34px; border-radius: 8px; font-size: 18px;
}
.icon-btn:hover { background: var(--hover); }
.error { color: var(--danger); min-height: 1em; margin: 0; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; }
.logo {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent);
  mask: none; position: relative;
}
.logo::after {
  content: ""; position: absolute; inset: 7px 6px;
  border: 2px solid #fff; border-top-color: transparent; border-radius: 2px;
}

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 14px; padding: 28px;
  display: grid; gap: 14px;
}
.login-card .brand { justify-content: center; font-size: 20px; margin-bottom: 6px; }
.login-card label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
input, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg); color: var(--text);
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Layout */
.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 210px minmax(280px, 380px) 1fr;
  grid-template-rows: 56px 1fr;
  grid-template-areas: "top top top" "side list reader";
}
.topbar {
  grid-area: top; display: flex; align-items: center; gap: 12px;
  padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.topbar .user { margin-left: auto; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar { grid-area: side; padding: 14px 10px; border-right: 1px solid var(--border); overflow-y: auto; }
.compose { width: 100%; margin-bottom: 14px; padding: 10px; }
.folders { list-style: none; margin: 0; padding: 0; }
.folders li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
}
.folders li:hover { background: var(--hover); }
.folders li.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folders .count { font-size: 12px; font-weight: 600; color: var(--accent); }

.list-pane {
  grid-area: list; display: flex; flex-direction: column; min-height: 0;
  background: var(--panel); border-right: 1px solid var(--border);
}
.list-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.list-head h2 { margin: 0; font-size: 16px; }
.messages { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.messages li { padding: 10px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.messages li:hover { background: var(--hover); }
.messages li.active { background: var(--accent-soft); }
.messages li.unread .who, .messages li.unread .subj { font-weight: 650; }
.messages li.unread .who::before {
  content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); margin-right: 6px; vertical-align: middle;
}
.messages .row1 { display: flex; justify-content: space-between; gap: 8px; }
.messages .who, .messages .subj { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.messages .date { color: var(--muted); font-size: 12px; flex-shrink: 0; }
.messages .subj { color: var(--muted); }
.messages .empty { color: var(--muted); text-align: center; cursor: default; padding: 40px 14px; border: 0; }
.messages .empty:hover { background: none; }
.pager { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; }

.reader { grid-area: reader; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }
.reader-empty { margin: auto; color: var(--muted); }
.reader article { display: flex; flex-direction: column; min-height: 0; height: 100%; padding: 16px 20px; gap: 10px; }
.reader-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.reader h1 { font-size: 20px; margin: 6px 0 0; overflow-wrap: anywhere; }
.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; overflow-wrap: anywhere; }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; }
.attachments:empty { display: none; }
.attachments a {
  padding: 5px 10px; border: 1px solid var(--border); border-radius: 999px;
  color: var(--text); text-decoration: none; font-size: 13px; background: var(--panel);
}
.attachments a:hover { background: var(--hover); }
.body-frame { flex: 1; min-height: 300px; width: 100%; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }

/* Escrever */
.compose-dialog {
  width: min(720px, calc(100vw - 32px)); max-height: calc(100dvh - 32px);
  border: 1px solid var(--border); border-radius: 14px; padding: 0;
  background: var(--panel); color: var(--text);
}
.compose-dialog::backdrop { background: rgb(0 0 0 / .4); }
.compose-dialog form { display: grid; gap: 10px; padding: 16px 20px 20px; }
.compose-head { display: flex; justify-content: space-between; align-items: center; }
.compose-head h2 { margin: 0; font-size: 17px; }
.field { display: grid; grid-template-columns: 80px 1fr; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.field input[type=file] { border: 0; padding: 0; background: none; }
textarea { resize: vertical; min-height: 200px; }
.compose-foot { display: flex; align-items: center; gap: 8px; }
.compose-foot .error { margin-right: auto; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  box-shadow: 0 6px 20px rgb(0 0 0 / .2); z-index: 10;
}

.only-mobile { display: none; }

/* Celular: uma coluna por vez */
@media (max-width: 800px) {
  .only-mobile { display: inline-block; }
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
  .topbar { padding: 0 8px; gap: 6px; }
  .topbar .user { display: none; }
  .topbar #logout-btn { margin-left: auto; }
  .sidebar, .list-pane, .reader { grid-area: main; border-right: 0; }
  .sidebar { display: none; background: var(--bg); z-index: 2; }
  .app.show-folders .sidebar { display: block; }
  .app.view-list .reader { display: none; }
  .app.view-reader .list-pane { display: none; }
  .reader article { padding: 12px 16px; }
  .field { grid-template-columns: 1fr; gap: 4px; }
}
