:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f7;
  --border: #e3e6ee;
  --text: #1c2033;
  --muted: #6b7189;
  --accent: #4f46e5;
  --accent-soft: #eceafe;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --unread: #1c2033;
  --selected: #e8e7fd;
  --shadow: 0 10px 40px rgba(20, 24, 50, .18);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141c;
    --surface: #1a1d28;
    --surface-2: #222634;
    --border: #2d3142;
    --text: #e6e8f0;
    --muted: #9298ad;
    --accent: #818cf8;
    --accent-soft: #2a2c4a;
    --accent-text: #0f1020;
    --danger: #f87171;
    --unread: #ffffff;
    --selected: #2b2d4d;
    --shadow: 0 10px 40px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.spacer { flex: 1; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }

/* ---------- Botones ---------- */
.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.btn:hover { filter: brightness(.97); }
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.danger { color: var(--danger); }
.icon-btn {
  border: 0;
  background: transparent;
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 17px;
  display: inline-grid;
  place-items: center;
  flex: none;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.active { color: #f5b301; }
.icon-btn.active .ico { fill: currentColor; }

.ico {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
  vertical-align: -4px;
}
.btn-sm .ico { width: 16px; height: 16px; vertical-align: -3px; }
.folders .ico { margin-right: 6px; opacity: .85; }
.link-btn { border: 0; background: none; color: var(--accent); cursor: pointer; font: inherit; font-size: 12.5px; }
.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Login ---------- */
.login-page { align-items: center; justify-content: center; }
.login-card {
  background: var(--surface);
  padding: 32px;
  border-radius: 16px;
  box-shadow: var(--shadow);
  width: min(360px, calc(100% - 32px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: var(--surface);
  color: var(--text);
}
.login-card .btn { justify-content: center; }

/* ---------- Topbar ---------- */
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.brand-lg { font-size: 22px; }
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { width: 206px; }
.search { flex: 1; max-width: 640px; }
.search input {
  width: 100%;
  padding: 9px 14px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}
.search input:focus { outline: none; border-color: var(--accent); background: var(--surface); }

/* ---------- Layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 230px;
  flex: none;
  padding: 14px 10px;
  overflow-y: auto;
  background: var(--bg);
}
.compose-btn { width: 100%; justify-content: center; padding: 11px; margin-bottom: 14px; font-size: 15px; }
.sidebar-title { font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 12px 6px; }
.folders a {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}
.folders a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folders a:hover { background: var(--surface-2); }
.folders a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.folders b { font-size: 12px; background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 0 7px; }
.folders b.soft { background: var(--surface-2); color: var(--muted); }
.folders b:empty { display: none; }

.list-pane {
  width: 400px;
  flex: none;
  background: var(--surface);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.list-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 10px; border-bottom: 1px solid var(--border); }
.list-header h2 { margin: 0; font-size: 17px; }
.email-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.email-item {
  padding: 11px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 10px;
  position: relative;
}
.email-item:hover { background: var(--surface-2); }
.email-item.selected { background: var(--selected); }
.email-item .who, .email-item .subj, .email-item .snip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.email-item .who { color: var(--muted); }
.email-item .date { color: var(--muted); font-size: 12px; text-align: right; }
.email-item .subj { grid-column: 1 / -1; }
.email-item .snip { grid-column: 1 / -1; color: var(--muted); font-size: 13px; }
.email-item.unread::before { content: ""; position: absolute; left: 5px; top: 17px; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.email-item.unread .who, .email-item.unread .subj { color: var(--unread); font-weight: 650; }
.email-item .flags { display: inline-flex; gap: 2px; vertical-align: -3px; }
.email-item .flags .ico { width: 14px; height: 14px; }
.ico.starred { color: #f5b301; fill: currentColor; }
.ico.warn { color: var(--danger); }
.draft-tag { color: var(--danger); font-weight: 600; }
.chip.uploading { opacity: .7; }
.list-empty { padding: 40px 16px; text-align: center; color: var(--muted); }
.load-more { margin: 12px auto; }

.reader-pane { flex: 1; min-width: 0; overflow-y: auto; background: var(--surface); }
.reader-empty { height: 100%; display: grid; place-content: center; text-align: center; color: var(--muted); }
.reader-empty-icon .ico { width: 56px; height: 56px; stroke-width: 1.2; opacity: .5; margin-bottom: 8px; }
.reader { padding: 8px 24px 32px; max-width: 1000px; }
.reader-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  background: var(--surface);
  padding: 8px 0;
  z-index: 1;
  border-bottom: 1px solid var(--border);
}
.reader-subject { font-size: 21px; margin: 18px 0 14px; font-weight: 650; word-break: break-word; }
.reader-meta { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 14px; }
.reader-meta-text { flex: 1; min-width: 0; word-break: break-word; }
.reader-date { text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  color: #fff;
  flex: none;
  text-transform: uppercase;
}
.status { font-size: 11.5px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.status.delivered { background: #dcfce7; color: #166534; }
.status.bounced, .status.failed, .status.complained { background: #fee2e2; color: #991b1b; }
.status.delayed { background: #fef3c7; color: #92400e; }
.remote-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 12px;
  font-size: 13px;
}
.reader-body { width: 100%; border: 1px solid var(--border); border-radius: var(--radius); min-height: 80px; background: #fff; display: block; }
.reader-body { transition: opacity .12s ease; }
.reader-body.loading { opacity: 0; transition: none; }
.reader-body.themed { border: 0; border-radius: 0; background: transparent; }

/* ---------- Autocompletado ---------- */
.autocomplete {
  position: fixed;
  z-index: 10;
  list-style: none;
  margin: 0;
  padding: 4px;
  max-height: 300px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.autocomplete li { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 7px; cursor: pointer; }
.autocomplete li.active, .autocomplete li:hover { background: var(--accent-soft); }
.autocomplete .ac-name { font-weight: 500; }

/* ---------- Visor de adjuntos ---------- */
.viewer {
  width: min(1100px, calc(100% - 32px));
  height: calc(100dvh - 48px);
  max-width: none;
  max-height: none;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.viewer[open] { display: flex; flex-direction: column; }
.viewer::backdrop { background: rgba(5, 6, 15, .75); }
.viewer-header { display: flex; align-items: center; gap: 6px; padding: 10px 12px 10px 18px; border-bottom: 1px solid var(--border); }
.viewer-title { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px; }
.viewer-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-title .small { white-space: nowrap; }
.viewer-body { flex: 1; min-height: 0; display: grid; place-items: center; overflow: auto; background: var(--bg); }
.viewer-body img { max-width: 100%; max-height: 100%; object-fit: contain; background: repeating-conic-gradient(#8881 0 25%, transparent 0 50%) 0 0 / 20px 20px; }
.viewer-body iframe { width: 100%; height: 100%; border: 0; background: #fff; }
.viewer-body[data-kind="pdf"] { display: block; overflow: hidden; }
.viewer-body video { max-width: 100%; max-height: 100%; }
.viewer-body audio { width: min(480px, 90%); }
.viewer-body[data-kind="text"] { display: block; }
.viewer-body pre { margin: 0; padding: 18px 22px; white-space: pre-wrap; word-break: break-word; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.viewer-empty { text-align: center; color: var(--muted); }
.viewer-empty .file-badge { width: 72px; height: 72px; font-size: 16px; margin: 0 auto; border-radius: 12px; }
.viewer-empty .btn { margin-top: 4px; }
a.btn { text-decoration: none; }

.attachments { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.attachment {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  max-width: 280px;
}
.attachment:hover { background: var(--surface-2); }
.attachment .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.file-badge {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  flex: none;
}
.attachment img { width: 40px; height: 40px; object-fit: cover; border-radius: 4px; }

/* ---------- Ajustes ---------- */
.sidebar { display: flex; flex-direction: column; }
.sidebar-bottom { margin-top: auto; padding-top: 14px; }
.settings { width: min(620px, 100%); }
.settings-body { padding: 18px 22px 22px; overflow-y: auto; }
.settings-body h4 { margin: 0 0 4px; font-size: 15px; }
.settings-body > p { margin: 0 0 14px; }
.sender-list { list-style: none; margin: 0 0 16px; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); }
.sender-list li { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.sender-list li:last-child { border-bottom: 0; }
.sender-list li.empty { display: block; padding: 18px; text-align: center; }
.sender-info { flex: 1; min-width: 0; }
.sender-info .small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sender-name {
  width: 100%;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 3px 6px;
  margin-left: -7px;
  font: inherit;
  font-weight: 600;
  background: transparent;
  color: var(--text);
}
.sender-name:hover { border-color: var(--border); }
.sender-name:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.avatar-sm { width: 32px; height: 32px; font-size: 13px; }
.badge { font-size: 12px; padding: 3px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; white-space: nowrap; }
.sender-form { display: flex; gap: 8px; flex-wrap: wrap; }
.sender-form input, .sender-form select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: var(--surface);
  color: var(--text);
  min-width: 0;
}
.sender-form > input { flex: 1 1 170px; }
.email-input { display: flex; flex: 2 1 260px; }
.email-input input { flex: 1; border-radius: 8px 0 0 8px; }
.email-input select { border-left: 0; border-radius: 0 8px 8px 0; background: var(--surface-2); }
.email-input select:disabled { opacity: 1; color: var(--text); appearance: none; }

.settings-section { margin-top: 26px !important; }
.switch-row { display: flex; gap: 12px; align-items: flex-start; margin-top: 10px; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer; }
.switch-row > span { display: flex; flex-direction: column; gap: 2px; }
.switch-row input {
  appearance: none;
  flex: none;
  width: 36px;
  height: 20px;
  margin: 1px 0 0;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  transition: background .15s;
}
.switch-row input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch-row input:checked { background: var(--accent); }
.switch-row input:checked::after { transform: translateX(16px); }
.switch-row input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Redactar ---------- */
.compose {
  width: min(760px, 100%);
  max-height: min(860px, 100%);
  padding: 0;
  border: 0;
  border-radius: 14px;
  box-shadow: var(--shadow);
  background: var(--surface);
  color: var(--text);
}
.compose::backdrop { background: rgba(10, 12, 25, .35); }
.compose > form { display: flex; flex-direction: column; max-height: min(860px, 100dvh); }
.compose-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px 10px 18px; background: var(--surface-2); }
.compose-header h3 { margin: 0; font-size: 15px; }
.compose-fields label {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
}
.compose-fields input, .compose-fields select {
  flex: 1;
  border: 0;
  padding: 11px 0;
  font: inherit;
  font-size: 14px;
  background: transparent;
  color: var(--text);
  min-width: 0;
}
.compose-fields input:focus, .compose-fields select:focus { outline: none; }
.compose-fields select option { background: var(--surface); }
.editor-toolbar { display: flex; flex-wrap: wrap; gap: 2px; padding: 6px 12px; border-bottom: 1px solid var(--border); }
.editor-toolbar button {
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
}
.editor-toolbar button:hover { background: var(--surface-2); }
.editor-toolbar button.active { background: var(--accent-soft); color: var(--accent); }
.editor-toolbar button.active:hover { background: var(--accent-soft); }
.editor-toolbar .sep { width: 1px; background: var(--border); margin: 4px 4px; }
.editor {
  flex: 1;
  min-height: 260px;
  overflow-y: auto;
  padding: 14px 18px;
  outline: none;
  word-wrap: break-word;
}
.editor blockquote { margin: 0 0 0 .8ex; border-left: 2px solid var(--border); padding-left: 1ex; color: var(--muted); }
.editor img { max-width: 100%; }
.compose-attachments { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 18px; }
.compose-attachments:not(:empty) { padding: 8px 18px; border-top: 1px solid var(--border); }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--surface-2); padding: 4px 6px 4px 10px; border-radius: 999px; font-size: 12.5px; }
.chip .ico { width: 14px; height: 14px; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); display: inline-grid; padding: 0; }
.compose-footer { display: flex; align-items: center; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: #1c2033;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 100;
  max-width: calc(100% - 32px);
}
.toast.error { background: var(--danger); }

.only-mobile { display: none; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .list-pane { width: 330px; }
  .sidebar { width: 200px; }
  .topbar .brand { width: auto; }
}
@media (max-width: 820px) {
  .only-mobile { display: inline-grid; }
  .topbar { padding: 8px 10px; gap: 6px; }
  .topbar .brand span { display: none; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    width: 260px;
    transform: translateX(-100%);
    transition: transform .2s;
    background: var(--surface);
  }
  body.menu-open .sidebar { transform: none; box-shadow: var(--shadow); }
  .list-pane { width: 100%; border: 0; }
  .reader-pane { display: none; }
  body.reading .list-pane { display: none; }
  body.reading .reader-pane { display: block; }
  .reader { padding: 4px 14px 24px; }
  .reader-meta { flex-wrap: wrap; }
  .reader-date { order: 3; width: 100%; flex-direction: row; align-items: center; justify-content: flex-start; padding-left: 52px; text-align: left; }
  .reader-toolbar { flex-wrap: nowrap; }
  .reader-toolbar .lbl { display: none; }
  .reader-toolbar .btn-sm { padding: 6px 9px; border: 0; }
  .viewer { width: 100%; height: 100%; border-radius: 0; }
  .viewer-header .lbl { display: none; }
  .compose { width: 100%; height: 100%; max-height: 100%; max-width: 100%; border-radius: 0; margin: 0; }
  .compose > form { height: 100%; max-height: 100%; }
}
.settings { display: none; flex-direction: column; }
.settings[open] { display: flex; max-height: min(860px, 100dvh); }
