:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #7c3aed;
  --primary-hover: #6d28d9;
  --primary-tint: #f5f3ff;
  --danger: #dc2626;
  --sent-bg: #f3f4f6;   --sent-fg: #4b5563;
  --maybe-bg: #fef3c7;  --maybe-fg: #92400e;
  --opened-bg: #dcfce7; --opened-fg: #166534;
  --clicked-bg: #ede9fe;--clicked-fg: #5b21b6;
  --shadow: 0 1px 2px rgba(17, 24, 39, .06), 0 4px 16px rgba(17, 24, 39, .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --surface: #171a23;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #2a2f3c;
    --primary: #8b5cf6;
    --primary-hover: #7c3aed;
    --primary-tint: #221a3a;
    --danger: #f87171;
    --sent-bg: #262a35;   --sent-fg: #c3c7d1;
    --maybe-bg: #3b2f12;  --maybe-fg: #fcd34d;
    --opened-bg: #12301f; --opened-fg: #86efac;
    --clicked-bg: #2a1f4a;--clicked-fg: #c4b5fd;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.strong { font-weight: 600; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }

.brand { display: flex; align-items: baseline; gap: 0; font-size: 20px; font-weight: 700; letter-spacing: -.01em; }
.brand b { color: var(--primary); }
.brand small { font-size: 10px; font-weight: 600; letter-spacing: .12em; color: var(--muted); margin-left: 8px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 20px; box-shadow: var(--shadow); min-width: 0; }

label { display: block; font-weight: 500; margin-bottom: 12px; }
label small { font-weight: 400; color: var(--muted); }
input, textarea, select {
  display: block; width: 100%; margin-top: 4px; padding: 9px 11px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text);
  font: inherit;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer;
}
.btn:hover { border-color: var(--primary); }
.btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.mono { font: 12px/1.4 ui-monospace, Consolas, monospace; margin-top: 10px; color: var(--muted); }
input.mono:focus { color: var(--text); }
.stat em::before { content: " "; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; }
.login-card .brand { margin-bottom: 20px; }
.login-card .btn { width: 100%; }
.error { color: var(--danger); margin: 0 0 12px; }

/* App */
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--surface); }
.wrap { max-width: 1200px; margin: 0 auto; padding: 24px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.stat span { display: block; color: var(--muted); font-size: 12px; }
.stat b { font-size: 24px; font-variant-numeric: tabular-nums; }
.stat em { font-style: normal; color: var(--muted); margin-left: 6px; }

.grid { display: grid; grid-template-columns: 340px 1fr; gap: 20px; align-items: start; }
.result { margin-top: 16px; padding: 14px; border-radius: 10px; background: var(--primary-tint); }
.hint { margin: 0 0 10px; }
.links { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 6px; }
.links li { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }

.list-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.list-head h2 { margin: 0; }
.filters { display: flex; gap: 8px; }
.filters input, .filters select { margin: 0; width: auto; }
.filters input { width: 200px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; }
th { text-align: left; font-size: 12px; font-weight: 500; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--border); }
th:first-child { width: 40%; }
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; font-variant-numeric: tabular-nums; }
tbody tr { cursor: pointer; }
tbody tr:hover, tbody tr:focus { background: var(--primary-tint); outline: none; }
.empty { text-align: center; color: var(--muted); padding: 32px 0; margin: 0; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.st-sent { background: var(--sent-bg); color: var(--sent-fg); }
.st-maybe { background: var(--maybe-bg); color: var(--maybe-fg); }
.st-opened { background: var(--opened-bg); color: var(--opened-fg); }
.st-clicked { background: var(--clicked-bg); color: var(--clicked-fg); }
.tag { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 4px; border: 1px solid var(--border); font-size: 11px; color: var(--muted); }

/* Drawer */
.drawer { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); display: flex; justify-content: flex-end; z-index: 10; }
.drawer-panel { width: min(480px, 100%); height: 100%; overflow-y: auto; border-radius: 0; }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.drawer-head h2 { margin: 0; word-break: break-all; }
.drawer-head p { margin: 2px 0 0; }
.d-meta { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; margin: 14px 0; font-size: 12px; }
.timeline { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.timeline li { border-left: 3px solid var(--primary); padding: 4px 0 4px 12px; display: grid; gap: 2px; justify-items: start; }
.timeline li.uncounted { border-left-color: var(--border); opacity: .75; }
.ev-line { display: flex; flex-wrap: wrap; align-items: center; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; z-index: 20; max-width: calc(100% - 32px); }
.toast.bad { background: var(--danger); color: #fff; }

@media (max-width: 900px) {
  .grid { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
  .wrap { padding: 16px; }
  .topbar { padding: 12px 16px; }
  th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5) { display: none; }
  th:first-child { width: 60%; }
  .filters { width: 100%; }
  .filters input { flex: 1; width: auto; }
}
