:root {
  --bg-light: #FEF2E4;
  --bg-dark: #171717;
  --text-light-theme: #171717;
  --text-dark-theme: #FEF2E4;
  --accent: #C60000;

  --panel: #1F1F1F;
  --panel-2: #262626;
  --border: rgba(254, 242, 228, 0.1);
  --text-dim: rgba(254, 242, 228, 0.55);
  --text-faint: rgba(254, 242, 228, 0.35);

  --font-heading: "Onest", sans-serif;
  --font-body: "Inter", sans-serif;

  --space-xs: 8px;
  --space-sm: 14px;
  --space-md: 20px;
  --space-lg: 32px;

  --hot: #E25A2E;
  --warm: #E0A000;
  --cold: rgba(254, 242, 228, 0.4);
  --ai-accent: #6C8EF5;
  --ok: #37C871;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: var(--font-body);
  background: var(--bg-dark);
  color: var(--text-dark-theme);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--text-dark-theme); outline-offset: 2px; }

.dark-section {
  position: relative;
  --glow-size: 380px;
  --glow-color: rgba(198, 0, 0, 0.22);
  background-color: var(--bg-dark);
  overflow: hidden;
}
.dark-section::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image:
    radial-gradient(var(--glow-size) var(--glow-size) at calc(100% - 80px) 80px, var(--glow-color) 0%, transparent 70%),
    radial-gradient(var(--glow-size) var(--glow-size) at 80px calc(100% - 80px), var(--glow-color) 0%, transparent 70%);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  background-color: var(--accent);
  color: var(--text-dark-theme);
  border: none;
  padding: 14px 26px;
  font-family: var(--font-body);
  font-weight: 500;
  font-size: 15px;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: opacity 0.2s ease;
}
.btn:hover { opacity: 0.88; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--text-dark-theme); outline-offset: 3px; }
.btn__icon { width: 16px; height: 16px; flex-shrink: 0; }
.btn--ghost {
  background: var(--panel);
  border: 1px solid var(--border);
}
.btn--ghost:hover { border-color: var(--accent); opacity: 1; }
.btn--block { width: 100%; }
.btn--sm { padding: 9px 16px; font-size: 13px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field__label { font-size: 13px; color: var(--text-dim); }
.field__input, .field__textarea {
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 13px 14px;
  font-size: 15px;
  color: var(--text-dark-theme);
  transition: border-color 0.2s ease;
  width: 100%;
}
.field__input:focus, .field__textarea:focus { outline: none; border-color: var(--accent); }
.field__textarea { resize: vertical; min-height: 220px; line-height: 1.55; }
.field__error { font-size: 13px; color: var(--accent); min-height: 16px; }

.select {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text-dark-theme);
  font-size: 13px;
  padding: 8px 10px;
  cursor: pointer;
}
.select:focus { outline: none; border-color: var(--accent); }

/* ==========================================================================
   MODAL (shared)
   ========================================================================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.modal-overlay[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: var(--space-lg);
  max-height: calc(100dvh - 48px);
  overflow-y: auto;
}
.modal-card--wide { max-width: 620px; }
.modal-card__title { font-family: var(--font-heading); font-size: 18px; font-weight: 700; margin-bottom: 12px; }
.modal-card__text { font-size: 13px; color: var(--text-dim); line-height: 1.5; }
.modal-card__form { display: flex; flex-direction: column; gap: var(--space-md); margin-top: var(--space-md); }
.modal-card__actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: var(--space-md); }
.modal-btn { padding: 10px 18px; font-size: 13px; border: 1px solid var(--border); color: var(--text-dim); }
.modal-btn:hover { border-color: var(--text-faint); color: var(--text-dark-theme); }
.modal-close {
  position: absolute; top: 14px; right: 14px;
  width: 30px; height: 30px; display: flex; align-items: center; justify-content: center;
  color: var(--text-faint);
}
.modal-close:hover { color: var(--accent); }
.modal-card { position: relative; }

/* Toggle (reused for HTML / Plain Text switch) */
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 8px 12px 8px 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  transition: border-color 0.2s ease;
  width: fit-content;
}
.toggle:hover { border-color: var(--text-faint); }
.toggle__input {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.toggle__track {
  position: relative; width: 34px; height: 20px; border-radius: 10px;
  background: rgba(254, 242, 228, 0.14);
  border: 1px solid var(--border);
  flex-shrink: 0;
  transition: background 0.2s ease, border-color 0.2s ease;
}
.toggle__thumb {
  position: absolute; top: 1px; left: 1px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--text-faint);
  transition: transform 0.2s ease, background 0.2s ease;
}
.toggle__input:checked ~ .toggle__track { background: rgba(108, 142, 245, 0.25); border-color: var(--ai-accent); }
.toggle__input:checked ~ .toggle__track .toggle__thumb { transform: translateX(14px); background: var(--ai-accent); }
.toggle__input:focus-visible ~ .toggle__track { outline: 2px solid var(--text-dark-theme); outline-offset: 2px; }
.toggle__label { font-size: 12px; color: var(--text-dim); white-space: nowrap; }
.toggle__input:checked ~ .toggle__label { color: var(--ai-accent); }

/* ==========================================================================
   APP SHELL
   ========================================================================== */
.mail-app {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 280px 340px 1fr;
  position: relative;
  z-index: 1;
}

/* ---------- Sidebar: mailboxes ---------- */
.sidebar {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar__brand {
  padding: var(--space-md) var(--space-md) var(--space-sm);
  font-family: var(--font-heading);
  font-weight: 800;
  font-size: 19px;
  color: var(--accent);
  letter-spacing: -0.01em;
}
.sidebar__compose { margin: 0 var(--space-md) var(--space-md); }
.sidebar__section-label {
  padding: 0 var(--space-md);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 8px;
}
.mailbox-list { flex: 1; overflow-y: auto; padding: 0 8px; display: flex; flex-direction: column; gap: 2px; }
.mailbox-card {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s ease;
  position: relative;
}
.mailbox-card:hover { background: var(--panel); }
.mailbox-card.is-active { background: var(--panel-2); }
.mailbox-card.is-active::before {
  content: "";
  position: absolute; left: 0; top: 8px; bottom: 8px; width: 3px;
  background: var(--accent);
}
.mailbox-card__avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-heading); font-weight: 700; font-size: 13px;
  flex-shrink: 0;
}
.mailbox-card__body { flex: 1; min-width: 0; }
.mailbox-card__email { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mailbox-card__user { font-size: 11px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mailbox-empty { padding: 24px var(--space-md); color: var(--text-faint); font-size: 13px; }

.sidebar__footer { padding: var(--space-md); border-top: 1px solid var(--border); }
.sidebar__new-btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%;
  border: 1px dashed var(--border);
  padding: 11px;
  font-size: 13px;
  color: var(--text-dim);
}
.sidebar__new-btn:hover { border-color: var(--accent); color: var(--text-dark-theme); }

/* ---------- Feed: message list ---------- */
.feed {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.feed__header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: var(--space-md);
  border-bottom: 1px solid var(--border);
}
.feed__title { font-family: var(--font-heading); font-weight: 700; font-size: 16px; }
.feed__conn { display: flex; align-items: center; gap: 7px; font-size: 11px; color: var(--text-faint); }
.feed__conn .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); transition: background 0.2s ease; }
.feed__conn.is-online .dot { background: var(--ok); }
.feed__conn.is-online { color: var(--text-dim); }

.folder-tabs {
  display: flex; gap: 4px;
  padding: var(--space-sm) var(--space-md) 0;
  border-bottom: 1px solid var(--border);
}
.folder-tab {
  padding: 10px 4px;
  font-size: 13px; color: var(--text-faint);
  border-bottom: 2px solid transparent;
  transition: color 0.15s ease;
  margin-right: var(--space-md);
}
.folder-tab:hover { color: var(--text-dim); }
.folder-tab.is-active { color: var(--text-dark-theme); border-bottom-color: var(--accent); }

.feed__list { flex: 1; overflow-y: auto; padding: 8px; }
.feed__empty, .feed__placeholder {
  padding: 40px 20px; text-align: center; color: var(--text-faint); font-size: 13px; line-height: 1.6;
}

.msg-item {
  display: flex; gap: 10px;
  padding: 13px 10px;
  cursor: pointer;
  border-radius: 6px;
  position: relative;
  transition: background 0.15s ease;
  border-bottom: 1px solid var(--border);
}
.msg-item:last-child { border-bottom: none; }
.msg-item:hover { background: var(--panel); }
.msg-item.is-active { background: var(--panel-2); }
.msg-item.is-active::before {
  content: "";
  position: absolute; left: 0; top: 8px; bottom: 8px; width: 3px;
  background: var(--accent);
}
.msg-item__dot {
  width: 8px; height: 8px; border-radius: 50%; margin-top: 5px; flex-shrink: 0;
  background: transparent;
}
.msg-item.is-unread .msg-item__dot { background: var(--accent); }
.msg-item__body { flex: 1; min-width: 0; }
.msg-item__top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.msg-item__from {
  font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  color: var(--text-dim);
}
.msg-item.is-unread .msg-item__from { color: var(--text-dark-theme); font-weight: 700; }
.msg-item__time { font-size: 11px; color: var(--text-faint); flex-shrink: 0; }
.msg-item__subject {
  font-size: 13px; color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 3px;
}
.msg-item.is-unread .msg-item__subject { color: var(--text-dark-theme); }

.skeleton {
  background: linear-gradient(90deg, var(--panel) 0%, var(--panel-2) 50%, var(--panel) 100%);
  background-size: 200% 100%;
  animation: skeleton-sheen 1.4s ease-in-out infinite;
  border-radius: 4px;
}
@keyframes skeleton-sheen { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
.msg-item-skel { display: flex; flex-direction: column; gap: 8px; padding: 13px 10px; }
.msg-item-skel .skeleton:nth-child(1) { width: 55%; height: 12px; }
.msg-item-skel .skeleton:nth-child(2) { width: 85%; height: 11px; }

/* ---------- Reader ---------- */
.reader { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.reader__placeholder {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text-faint); text-align: center; gap: 14px;
}
.reader__placeholder-mark { font-family: var(--font-heading); font-size: 52px; color: var(--accent); font-weight: 800; }
.reader__placeholder-text { font-size: 13px; max-width: 260px; line-height: 1.6; }

.reader__content { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.reader__header {
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 10px;
}
.reader__subject { font-family: var(--font-heading); font-weight: 700; font-size: 19px; }
.reader__meta-row { display: flex; align-items: baseline; gap: 8px; font-size: 13px; }
.reader__meta-label { color: var(--text-faint); flex-shrink: 0; width: 60px; }
.reader__meta-value { color: var(--text-dim); word-break: break-word; }
.reader__attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.attachment-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-dim);
  border: 1px solid var(--border);
  padding: 5px 10px;
  border-radius: 10px;
}
.attachment-chip__size { color: var(--text-faint); }

.reader__body-wrap { flex: 1; min-height: 0; padding: var(--space-lg); overflow-y: auto; }
.reader__body-frame {
  width: 100%;
  border: none;
  background: transparent;
  min-height: 60px;
}
.reader__body-plain {
  white-space: pre-wrap;
  font-size: 14.5px;
  line-height: 1.65;
  color: var(--text-dark-theme);
}
.reader__skel { display: flex; flex-direction: column; gap: 12px; }
.reader__skel .skeleton:nth-child(1) { width: 40%; height: 20px; }
.reader__skel .skeleton:nth-child(2) { width: 60%; height: 12px; }
.reader__skel .skeleton:nth-child(3) { width: 100%; height: 12px; }
.reader__skel .skeleton:nth-child(4) { width: 92%; height: 12px; }
.reader__skel .skeleton:nth-child(5) { width: 96%; height: 12px; }
.reader__skel .skeleton:nth-child(6) { width: 70%; height: 12px; }

/* ---------- Composer modal ---------- */
.composer-form { display: flex; flex-direction: column; gap: var(--space-md); margin-top: var(--space-md); }
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.composer-status { font-size: 13px; min-height: 16px; }
.composer-status.is-error { color: var(--accent); }
.composer-status.is-ok { color: var(--ok); }

/* ---------- Toast ---------- */
.toast-stack {
  position: fixed; bottom: 24px; right: 24px; z-index: 200;
  display: flex; flex-direction: column; gap: 8px;
  align-items: flex-end;
}
.toast {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--ok);
  padding: 12px 16px;
  font-size: 13px;
  max-width: 320px;
  animation: toast-in 0.2s ease;
}
.toast.is-error { border-left-color: var(--accent); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.spinner {
  width: 15px; height: 15px;
  border: 2px solid rgba(254, 242, 228, 0.35);
  border-top-color: var(--text-dark-theme);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

[hidden] { display: none !important; }

/* ==========================================================================
   RESPONSIVE
   ========================================================================== */
@media (max-width: 1200px) {
  .mail-app { grid-template-columns: 240px 300px 1fr; }
}

@media (max-width: 900px) {
  .mail-app { grid-template-columns: 1fr; position: relative; overflow: hidden; }
  .sidebar, .feed, .reader { position: absolute; inset: 0; background: var(--bg-dark); }
  .sidebar { z-index: 3; transform: translateX(0); transition: transform 0.22s ease; }
  .feed { z-index: 2; transform: translateX(0); transition: transform 0.22s ease; }
  .reader { z-index: 1; }
  .mail-app[data-view="feed"] .sidebar { transform: translateX(-100%); }
  .mail-app[data-view="reader"] .sidebar { transform: translateX(-100%); }
  .mail-app[data-view="reader"] .feed { transform: translateX(-100%); }
  .mail-nav-back { display: inline-flex !important; }
}

.mail-nav-back {
  display: none;
  align-items: center; justify-content: center;
  width: 32px; height: 32px;
  color: var(--text-dim);
  margin-right: 4px;
}
.mail-nav-back:hover { color: var(--accent); }
.mail-nav-back svg { width: 18px; height: 18px; }

@media (max-width: 480px) {
  .reader__header, .reader__body-wrap { padding: var(--space-sm); }
  .reader__subject { font-size: 16px; }
  .modal-card { padding: var(--space-md); }
}