:root {
  --bg: #f4f5f7; --panel: #ffffff; --panel-2: #f9fafb; --text: #1b2430; --muted: #667085;
  --line: #e3e6eb; --accent: #2563eb; --accent-fg: #ffffff; --accent-soft: #e8f0ff;
  --danger: #d92d20; --ok: #12a05c; --warn: #d97706; --shadow: 0 8px 30px rgba(16, 24, 40, .18);
  --radius: 8px; --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1318; --panel: #171c23; --panel-2: #1c222b; --text: #e6e9ee; --muted: #8b95a5;
    --line: #2a313b; --accent: #5b8def; --accent-fg: #0b1220; --accent-soft: #1d2a44;
    --danger: #f97066; --ok: #34c77b; --warn: #f5a524; --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 var(--font); color: var(--text); background: var(--bg); -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button, .msg { -webkit-tap-highlight-color: transparent; }
body { overscroll-behavior-y: none; }
a { color: var(--accent); }
[hidden] { display: none !important; }

/* ---------- controles ---------- */
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: var(--radius); padding: 7px 12px; line-height: 1.2; white-space: nowrap; }
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.icon-btn { border: 0; background: transparent; border-radius: 6px; padding: 5px 8px; font-size: 16px; line-height: 1; color: var(--muted); }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
input[type=text], input[type=password], input[type=email], input[type=search], input[type=number], input[type=date], input[type=url], select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: var(--radius); padding: 8px 10px; outline: none;
}
input:focus, select:focus, textarea:focus, .editor-area:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
label.field { display: block; margin: 0 0 12px; }
label.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
label.check { display: flex; gap: 8px; align-items: center; margin: 0 0 10px; }
label.check input { width: auto; }
.hint { color: var(--muted); font-size: 12.5px; margin: 4px 0 12px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.row > .shrink { flex: 0 0 auto; }
.badge { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.error-box { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); border-radius: var(--radius); padding: 8px 10px; margin: 8px 0; }
.info-box { background: var(--accent-soft); border-radius: var(--radius); padding: 8px 10px; margin: 8px 0; }
.warn-box { background: color-mix(in srgb, var(--warn) 14%, transparent); border-radius: var(--radius); padding: 8px 10px; margin: 8px 0; }
.muted { color: var(--muted); }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.login form { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 18px; color: var(--muted); }

/* ---------- layout principal ---------- */
.app { height: 100vh; height: 100dvh; display: grid; grid-template-columns: 250px minmax(300px, 400px) minmax(0, 1fr); background: var(--bg); }
.nav { background: var(--panel-2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.nav-top { padding: 14px 12px 8px; }
.nav-top .brand { font-weight: 700; font-size: 15px; margin: 0 4px 12px; display: flex; align-items: center; gap: 8px; }
.nav-top .btn.primary { width: 100%; padding: 9px; }
.nav-scroll { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.nav-section { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 14px 8px 4px; }
.nav-item { display: flex; align-items: center; gap: 8px; width: 100%; border: 0; background: transparent; text-align: left; padding: 6px 8px; border-radius: 6px; }
.nav-item:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.nav-item.danger { color: var(--danger); }
.nav-item:disabled { opacity: .5; cursor: default; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item .lbl { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .cnt { font-size: 12px; font-weight: 700; color: var(--accent); }
.nav-item.sub { padding-left: 26px; }
.nav-account { display: flex; align-items: center; }
.nav-account .nav-item { flex: 1; min-width: 0; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.err { background: var(--danger); } .dot.warn { background: var(--warn); }
.dot.sync { background: var(--accent); animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: .3; } to { opacity: 1; } }
.nav-bottom { border-top: 1px solid var(--line); padding: 8px; display: flex; gap: 6px; }
.nav-bottom .btn { flex: 1; }

.list { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.list-head { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.list-title { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.list-title h2 { margin: 0; font-size: 16px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-row { display: flex; gap: 6px; }
.search-field { display: flex; flex: 1; min-width: 0; gap: 6px; }
.search-field input { flex: 1; min-width: 0; }
.search-field input::-webkit-search-cancel-button { display: none; }
.search-clear { flex: 0 0 44px; min-height: 44px; padding: 0; font-size: 24px; }
.search-clear:hover { color: var(--accent); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.chip { border: 1px solid var(--line); background: transparent; border-radius: 99px; padding: 2px 10px; font-size: 12.5px; color: var(--muted); }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.adv { margin-top: 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.adv label { font-size: 11.5px; color: var(--muted); font-weight: 600; }
.adv input { margin-top: 2px; padding: 5px 8px; }
.adv .full { grid-column: 1 / -1; display: flex; gap: 6px; justify-content: flex-end; }
.list-scroll { flex: 1; overflow-y: auto; }
.msg { padding: 10px 14px 10px 40px; border-bottom: 1px solid var(--line); cursor: pointer; position: relative; }
.msg:hover { background: var(--panel-2); }
.msg.active { background: var(--accent-soft); }
.msg.unread::before { content: ""; position: absolute; left: 4px; top: 16px; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.msg-top { display: flex; gap: 8px; align-items: baseline; }
.msg-from { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg.unread .msg-from, .msg.unread .msg-subject { font-weight: 700; }
.msg-date { font-size: 12px; color: var(--muted); flex: none; }
.msg-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-snippet, .msg-acc { font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-acc { font-size: 11.5px; opacity: .85; }
.star { color: #f5b301; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.more { padding: 12px; text-align: center; }

.detail { background: var(--panel); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.detail-empty { margin: auto; color: var(--muted); text-align: center; padding: 20px; }
.detail-bar { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.detail-bar .spacer { flex: 1; }
.detail-scroll { flex: 1; overflow-y: auto; padding: 16px 20px 30px; }
.detail h1 { font-size: 20px; margin: 0 0 12px; line-height: 1.3; word-break: break-word; }
.meta { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; font-size: 13px; margin-bottom: 14px; }
.meta dt { color: var(--muted); } .meta dd { margin: 0; min-width: 0; word-break: break-word; }
.addr { display: inline-block; margin: 0 6px 2px 0; }
.addr small { color: var(--muted); }
.atts { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 14px; }
.att { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 10px; background: var(--panel-2); max-width: 100%; }
.att b { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 240px; }
.att small { color: var(--muted); }
.email-frame { width: 100%; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; display: block; min-height: 120px; }
.email-text { white-space: pre-wrap; word-break: break-word; font-family: ui-monospace, Consolas, monospace; font-size: 13px; background: var(--panel-2); padding: 12px; border-radius: var(--radius); }
.mobile-only { display: none; }

/* ---------- diálogos ---------- */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); box-shadow: var(--shadow); width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); }
dialog.wide { width: min(860px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(10, 15, 25, .5); }
dialog[open] { display: flex; flex-direction: column; }
.dlg-head { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dlg-head h3 { margin: 0; font-size: 16px; flex: 1; }
.dlg-body { padding: 16px 18px; overflow-y: auto; flex: 1; }
.dlg-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); padding: 0 12px; }
.tab { border: 0; background: transparent; padding: 10px 12px; color: var(--muted); border-bottom: 2px solid transparent; }
.tab.on { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.card { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 10px; background: var(--panel-2); }
.card-head { display: flex; gap: 8px; align-items: center; margin-bottom: 4px; flex-wrap: wrap; }
.card-head b { font-size: 14.5px; }
.order-btns { display: inline-flex; flex-direction: column; margin-right: 2px; }
.order-btns .icon-btn { padding: 0 8px; font-size: 11px; min-height: 0; min-width: 0; }
.order-btns .icon-btn:disabled { opacity: .25; cursor: default; }
@media (max-width: 860px) { .order-btns .icon-btn { min-height: 28px; min-width: 40px; font-size: 13px; } }
.card .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.status { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
table.simple { width: 100%; border-collapse: collapse; font-size: 13px; }
table.simple th, table.simple td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); }

/* ---------- editor ---------- */
.editor { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); overflow: hidden; }
.editor-bar { display: flex; gap: 2px; padding: 4px; border-bottom: 1px solid var(--line); background: var(--panel-2); flex-wrap: wrap; }
.editor-bar button { border: 0; background: transparent; border-radius: 5px; padding: 4px 9px; min-width: 30px; }
.editor-bar button:hover { background: var(--line); }
.editor-area { min-height: 180px; max-height: 45vh; overflow: auto; padding: 10px 12px; outline: none; }
.editor-area:empty::before { content: attr(data-placeholder); color: var(--muted); }
.editor-area img { max-width: 100%; }
.editor-src { width: 100%; min-height: 180px; border: 0; border-radius: 0; font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; }
.chip-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip-list .att { padding: 3px 8px; }

#toasts { position: fixed; right: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 8px; z-index: 1000; max-width: min(420px, calc(100vw - 28px)); }
.toast { background: #1f2937; color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); white-space: pre-wrap; word-break: break-word; }
.toast.error { background: #b42318; } .toast.ok { background: #067647; }

/* ---------- móvil / tablet ---------- */
@media (max-width: 1100px) { .app { grid-template-columns: 220px minmax(280px, 360px) minmax(0, 1fr); } }
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .app > * { display: none; }
  .app[data-pane="nav"] > .nav, .app[data-pane="list"] > .list, .app[data-pane="detail"] > .detail { display: flex; }
  .mobile-only { display: inline-block; }
  .adv { grid-template-columns: 1fr; }
  dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; border-radius: 0; margin: 0; }
  .row { flex-wrap: wrap; }
  .detail-scroll { padding: 14px 14px 30px; }
}

/* ---------- selección múltiple ---------- */
.msg-sel { position: absolute; left: 16px; top: 12px; width: 16px; height: 16px; margin: 0; cursor: pointer; accent-color: var(--accent); opacity: .35; }
.msg:hover .msg-sel, .msg.selected .msg-sel, .list-head:has(.bulk:not([hidden])) ~ .list-scroll .msg-sel { opacity: 1; }
.msg.selected { background: var(--accent-soft); }
.bulk { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin: -2px 0 10px; padding: 6px 8px; border-radius: var(--radius); background: var(--accent-soft); }
.bulk-n { margin-right: 4px; color: var(--accent); }
@media (hover: none) { .msg-sel { opacity: 1; } }

/* ---------- app instalada / móvil ---------- */
.list-scroll, .detail-scroll, .nav-scroll { overscroll-behavior: contain; }
.msg { touch-action: pan-y; }
.msg[data-swipe="wait"] { background: var(--panel-2); }
.msg[data-swipe="archive"] { background: color-mix(in srgb, var(--ok) 25%, var(--panel)); }
.msg[data-swipe="delete"] { background: color-mix(in srgb, var(--danger) 25%, var(--panel)); }
.fab { position: fixed; right: 16px; bottom: calc(18px + env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 50%; border: 0; padding: 0;
  background: var(--accent); color: var(--accent-fg); font-size: 22px; box-shadow: var(--shadow); z-index: 50; }
body.offline::before { content: "Sin conexión: no se pueden cargar los correos"; position: fixed; top: 0; left: 0; right: 0; z-index: 2000; text-align: center;
  background: var(--warn); color: #111; font-size: 12.5px; padding: calc(3px + env(safe-area-inset-top)) 8px 3px; }
@media (max-width: 860px) {
  .fab.mobile-only { display: block; }
  .nav, .list, .detail { padding-bottom: env(safe-area-inset-bottom); }
  .nav { padding-top: env(safe-area-inset-top); }
  input[type=text], input[type=password], input[type=email], input[type=search], input[type=number], input[type=date], input[type=url], select, textarea, .editor-area { font-size: 16px; }
  .btn, .icon-btn { min-height: 40px; }
  .btn.small { min-height: 36px; padding: 6px 11px; }
  .icon-btn { min-width: 40px; font-size: 18px; }
  .nav-item { padding: 11px 10px; }
  .chip { padding: 6px 12px; }
  .msg { padding: 12px 14px 12px 46px; }
  .msg-sel { width: 20px; height: 20px; top: 14px; left: 14px; }
  .detail-bar { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; padding-top: calc(10px + env(safe-area-inset-top)); }
  .detail-bar::-webkit-scrollbar { display: none; }
  .detail-bar .btn { flex: none; }
  .list-head { padding-top: calc(10px + env(safe-area-inset-top)); }
  .dlg-head { padding-top: calc(14px + env(safe-area-inset-top)); }
  .dlg-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .editor-area { max-height: 38vh; }
  #toasts { left: 14px; right: 14px; bottom: auto; top: calc(10px + env(safe-area-inset-top)); max-width: none; }
  .att b { max-width: 55vw; }
}
