:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --ink: #14161c;
  --muted: #6b7280;
  --line: #e6e8ec;
  --brand: #4f46e5;
  --brand-ink: #4338ca;
  --ok: #059669;
  --ok-soft: #d1fae5;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --err: #dc2626;
  --err-soft: #fee2e2;
  --shadow: 0 1px 2px rgba(16, 18, 28, 0.04), 0 8px 24px rgba(16, 18, 28, 0.06);
  --radius: 14px;
}
:root[data-theme="dark"] {
  --bg: #0b0c10;
  --surface: #14161c;
  --surface-2: #1b1e26;
  --ink: #e9eaee;
  --muted: #9aa1ac;
  --line: #262a33;
  --brand: #8b85f0;
  --brand-ink: #a5a0f5;
  --ok: #34d399;
  --ok-soft: #0c3a2e;
  --warn: #fbbf24;
  --warn-soft: #3a2c0c;
  --err: #f87171;
  --err-soft: #3a1414;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.35);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0c10;
    --surface: #14161c;
    --surface-2: #1b1e26;
    --ink: #e9eaee;
    --muted: #9aa1ac;
    --line: #262a33;
    --brand: #8b85f0;
    --brand-ink: #a5a0f5;
    --ok: #34d399;
    --ok-soft: #0c3a2e;
    --warn: #fbbf24;
    --warn-soft: #3a2c0c;
    --err: #f87171;
    --err-soft: #3a1414;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; overflow-x: hidden; }
body {
  margin: 0;
  font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color: var(--ink);
  background: var(--bg);
  overflow-x: hidden;
}
code {
  background: var(--surface-2);
  border: 1px solid var(--line);
  padding: 0.5px 5px;
  border-radius: 5px;
  font-size: 12px;
}
.muted { color: var(--muted); }
.err { color: var(--err); }

/* ---------- header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-left)) 10px max(16px, env(safe-area-inset-right));
  padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 9px; }
.brand-mark { border-radius: 8px; display: block; }
.brand-name { font-weight: 700; letter-spacing: -0.01em; font-size: 15px; }
.header-actions { display: flex; align-items: center; gap: 6px; }

/* ---------- buttons ---------- */
.btn {
  font: inherit;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 9px;
  border: 1px solid var(--brand);
  background: var(--brand);
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: var(--surface-2); }
.icon-btn {
  font: inherit;
  font-size: 15px;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
}
.pill .dot {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: currentColor;
}
.pill.live { color: var(--ok); background: var(--ok-soft); border-color: transparent; }
.pill.live .dot { animation: pulse 1.8s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* ---------- layout ---------- */
.wrap {
  max-width: 1040px;
  margin: 24px auto 64px;
  padding: 0 max(16px, env(safe-area-inset-left)) 0 max(16px, env(safe-area-inset-right));
}

.gate {
  max-width: 380px;
  margin: 10vh auto 0;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 32px 28px;
  box-shadow: var(--shadow);
}
.gate h1 { font-size: 20px; margin: 12px 0 6px; }
.gate p { font-size: 13px; }
#gateForm { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }

input[type="password"], input[type="search"], input[type="text"] {
  font: inherit;
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
}
input:focus { outline: 2px solid color-mix(in srgb, var(--brand) 45%, transparent); outline-offset: 1px; }

/* ---------- stats ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stat-num { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.stat-label { font-size: 12px; color: var(--muted); }

/* ---------- toolbar ---------- */
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.toolbar input { flex: 1; }

/* ---------- table ---------- */
.table-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--line); }
th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  background: var(--surface-2);
  font-weight: 700;
}
tbody tr { cursor: pointer; transition: background 0.12s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.c-subject { font-weight: 600; }
.subject-cell { font-weight: 600; }
.recips { color: var(--muted); }
.empty { padding: 28px 16px; text-align: center; margin: 0; }

/* ---------- status checks (Mailtrack-style) ---------- */
.status { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.status svg { width: 20px; height: 14px; display: block; }
.status.unopened { color: var(--muted); }
.status.opened { color: var(--ok); }
.status.ignored { color: var(--muted); opacity: 0.7; }
.status .count {
  font-size: 11px;
  font-weight: 700;
  background: var(--ok-soft);
  color: var(--ok);
  border-radius: 999px;
  padding: 1px 6px;
}
.status-text { font-size: 12px; font-weight: 600; }

.row-flash { animation: flash 1.6s ease-out; }
@keyframes flash {
  from { background: color-mix(in srgb, var(--brand) 22%, transparent); }
  to { background: transparent; }
}

/* ---------- drawer ---------- */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 18, 0.45);
  z-index: 40;
}
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 50;
  width: min(480px, 100%);
  height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: -12px 0 40px rgba(0, 0, 0, 0.18);
  overflow-y: auto;
  overscroll-behavior: contain;
}
.drawer-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.drawer-head strong {
  font-size: 15px;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.drawer-close {
  flex: none;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-size: 15px;
  cursor: pointer;
}
.drawer-close:hover { background: var(--surface-2); }
.drawer-body { padding: 16px 18px calc(28px + env(safe-area-inset-bottom)); }

.drawer-meta {
  margin: 0 0 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface-2);
}
.drawer-meta .meta-row {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 12px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.drawer-meta .meta-row:last-child { border-bottom: none; }
.drawer-meta dt { color: var(--muted); }
.drawer-meta dd { margin: 0; overflow-wrap: anywhere; }
.drawer-meta .status { font-size: 13px; }

.drawer h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 18px 0 8px;
}
.hint { font-size: 12px; margin: 6px 0 10px; }
.ignore {
  display: flex;
  gap: 10px;
  align-items: center;
  font-size: 13px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
}
.ignore input { width: 18px; height: 18px; flex: none; }

details.raw { margin-top: 8px; }
details.raw > summary {
  cursor: pointer;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  padding: 8px 0;
  list-style: none;
}
details.raw > summary::-webkit-details-marker { display: none; }
details.raw > summary::before { content: "▸ "; }
details.raw[open] > summary::before { content: "▾ "; }

.events { list-style: none; padding: 0; margin: 0; }
.events li {
  padding: 10px 12px;
  margin-bottom: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
  background: var(--surface);
}
.events .when { font-weight: 600; }
.events .sub { color: var(--muted); margin-top: 3px; }

/* ---------- responsive: table -> cards ---------- */
@media (max-width: 720px) {
  .wrap { margin-top: 16px; }
  .stats { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat { padding: 12px 14px; }
  .stat-num { font-size: 20px; }
  .brand-name { display: none; }
  .toolbar { flex-wrap: wrap; }
  .toolbar input { flex-basis: 100%; }
  .app-header { padding-left: 12px; padding-right: 12px; flex-wrap: wrap; }
  .header-actions { gap: 5px; flex-wrap: wrap; justify-content: flex-end; }
  .pill .pill-label { display: none; }
  .pill { padding: 6px; }
  #signout { padding: 8px 10px; }

  table, thead, tbody, tr, td { display: block; }
  thead { display: none; }
  tbody tr {
    padding: 13px 14px;
    border-bottom: 1px solid var(--line);
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "subject status"
      "recips  recips"
      "meta    meta";
    column-gap: 12px;
    row-gap: 3px;
    align-items: start;
  }
  tbody td { border: none; padding: 0; min-width: 0; overflow-wrap: anywhere; }
  td[data-c="subject"] {
    grid-area: subject;
    font-weight: 600;
    font-size: 14px;
    line-height: 1.35;
  }
  td[data-c="status"] { grid-area: status; justify-self: end; }
  td[data-c="status"] .status-text { display: none; }
  td[data-c="status"] .status svg { width: 22px; height: 15px; }
  td[data-c="recips"] { grid-area: recips; font-size: 12.5px; color: var(--muted); }
  td[data-c="sent"] { grid-area: meta; font-size: 12px; color: var(--muted); margin-top: 2px; }
  td[data-c="sent"]::before { content: "Sent "; }
  td[data-c="sent"]::after { content: " · opened " attr(data-extra); }
  td[data-c="opens"], td[data-c="last"] { display: none; }

  /* full-screen detail sheet */
  .drawer {
    width: 100%;
    border-left: none;
  }
  .drawer-body { padding-left: 14px; padding-right: 14px; }
  .drawer-meta .meta-row { grid-template-columns: 84px 1fr; }
}
