:root {
  --ink:        #0f1419;   /* deep slate background */
  --panel:      #161c24;   /* raised panels */
  --panel-2:    #1d2530;   /* hover / nested */
  --line:       #2a323d;   /* hairline borders */
  --text:       #e6edf3;   /* primary text */
  --muted:      #8b97a6;   /* secondary text */
  --signal:     #3fd17a;   /* live / connected accent */
  --signal-dim: #1f6e42;
  --amber:      #e8b84b;   /* counts / attention */
  --danger:     #e5616b;
  --mono: "SF Mono", "JetBrains Mono", "Menlo", Consolas, monospace;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--sans);
  background: var(--ink);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---- top bar ---- */
.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand h1 { font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.brand .ver { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.status {
  margin-left: auto; display: flex; align-items: center; gap: 8px;
  font-family: var(--mono); font-size: 12px; color: var(--muted);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--signal);
  box-shadow: 0 0 0 0 var(--signal); animation: pulse 2s infinite; }
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(63,209,122,0.5); }
  70%  { box-shadow: 0 0 0 6px rgba(63,209,122,0); }
  100% { box-shadow: 0 0 0 0 rgba(63,209,122,0); }
}
.token-field {
  background: var(--ink); border: 1px solid var(--line); color: var(--text);
  font-family: var(--mono); font-size: 12px; padding: 6px 10px; border-radius: 6px;
  width: 160px;
}

/* ---- layout ---- */
.layout { display: grid; grid-template-columns: 340px 1fr; height: calc(100vh - 56px); }

/* ---- group list (left) ---- */
.sidebar { border-right: 1px solid var(--line); overflow-y: auto; background: var(--panel); }
.sidebar-head {
  padding: 14px 16px 10px; display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 1;
}
.sidebar-head h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.search {
  width: 100%; background: var(--ink); border: 1px solid var(--line); color: var(--text);
  padding: 8px 12px; font-size: 13px; border-radius: 6px;
}
.search-wrap { padding: 10px 12px; position: sticky; top: 46px; background: var(--panel); z-index: 1; }

.group {
  padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer;
  display: flex; flex-direction: column; gap: 4px; transition: background 0.12s;
}
.group:hover { background: var(--panel-2); }
.group.active { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--signal); }
.group-name { font-size: 14px; font-weight: 500; }
.group-meta { display: flex; gap: 10px; font-family: var(--mono); font-size: 11px; color: var(--muted); }
.chip { color: var(--amber); }
.chip-live { color: var(--signal); }

/* ---- detail (right) ---- */
.detail { overflow-y: auto; padding: 0; }
.detail-empty {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); gap: 8px; text-align: center; padding: 40px;
}
.detail-empty .big { font-size: 15px; color: var(--text); }

.detail-head {
  padding: 20px 24px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  position: sticky; top: 0; background: var(--ink); z-index: 2;
}
.detail-head h2 { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; }
.detail-head .sub { font-family: var(--mono); font-size: 12px; color: var(--muted); margin-top: 4px; }

.btn {
  background: var(--signal-dim); color: var(--text); border: 1px solid var(--signal);
  padding: 9px 16px; border-radius: 7px; font-size: 13px; font-weight: 500; cursor: pointer;
  display: inline-flex; align-items: center; gap: 8px; transition: background 0.12s;
  white-space: nowrap;
}
.btn:hover { background: var(--signal); color: var(--ink); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; align-items: center; }
.deliver-toggle { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); font-family: var(--mono); }

.section { padding: 20px 24px; }
.section h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted);
  margin-bottom: 14px; display: flex; align-items: center; gap: 8px;
}
.section h3::after { content: ""; flex: 1; height: 1px; background: var(--line); }

/* summary cards */
.summary-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 16px 18px; margin-bottom: 12px;
}
.summary-card .when {
  font-family: var(--mono); font-size: 11px; color: var(--muted);
  display: flex; gap: 12px; margin-bottom: 10px;
}
.summary-card .trigger { color: var(--signal); }
.summary-body { font-size: 14px; line-height: 1.6; white-space: pre-wrap; }
.summary-body b { color: var(--amber); font-weight: 600; }

/* message rows */
.msg { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; gap: 12px; font-size: 13px; }
.msg:last-child { border-bottom: none; }
.msg .t { font-family: var(--mono); font-size: 11px; color: var(--muted); flex-shrink: 0; width: 48px; }
.msg .a { color: var(--signal); flex-shrink: 0; width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .b { color: var(--text); word-break: break-word; }
.msg .media { color: var(--muted); font-style: italic; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  padding: 12px 20px; border-radius: 8px; font-size: 13px; opacity: 0;
  transition: opacity 0.2s, transform 0.2s; pointer-events: none; z-index: 10;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
.toast.err { border-color: var(--danger); }

.spinner {
  width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.3);
  border-top-color: var(--text); border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading { padding: 24px; color: var(--muted); font-family: var(--mono); font-size: 13px; }

/* ---- v3 additions: login, delete, date range ---- */
.login-overlay {
  position: fixed; inset: 0; background: var(--ink);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.login-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 32px; width: 320px; max-width: calc(100vw - 32px); display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { font-size: 18px; font-weight: 600; }
.login-sub { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.login-card input {
  background: var(--ink); border: 1px solid var(--line); color: var(--text);
  padding: 11px 13px; border-radius: 8px; font-size: 14px;
}
.login-err { color: var(--danger); font-size: 12px; min-height: 16px; }

.btn-ghost {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  padding: 7px 12px; border-radius: 7px; font-size: 12px; cursor: pointer; margin-left: 12px;
}
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }

/* back-to-list button: hidden on desktop, shown on mobile inside the detail header */
.back-btn {
  display: none; background: transparent; border: 1px solid var(--line); color: var(--muted);
  padding: 7px 11px; border-radius: 7px; font-size: 13px; cursor: pointer;
  margin-right: 12px; align-items: center; gap: 4px;
}
.back-btn:hover { color: var(--text); border-color: var(--muted); }

.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.date-field {
  background: var(--ink); border: 1px solid var(--line); color: var(--text);
  font-family: var(--mono); font-size: 12px; padding: 7px 10px; border-radius: 6px;
}
.controls label { font-size: 12px; color: var(--muted); font-family: var(--mono); }
.group-actions { margin-left: auto; }
.del-btn {
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  font-size: 16px; padding: 2px 6px; border-radius: 4px;
}
.del-btn:hover { color: var(--danger); background: rgba(229,97,107,0.1); }
.group { position: relative; }

.report-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 18px 20px; margin-bottom: 14px;
}
.report-card .when {
  font-family: var(--mono); font-size: 11px; color: var(--muted);
  display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px;
}
.report-body { font-size: 13.5px; line-height: 1.65; white-space: pre-wrap; }
.report-body b { color: var(--amber); font-weight: 600; }
.report-body .h { color: var(--signal); font-weight: 600; display: block; margin-top: 12px; }

/* ---- context picker ---- */
.context-picker { margin-top: 12px; }
.context-label { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-bottom: 8px; }
.context-item {
  display: inline-flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text);
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 10px; margin: 0 8px 8px 0; cursor: pointer;
}
.context-item:hover { border-color: var(--muted); }
.context-item input { accent-color: var(--signal); }

/* ---- v3.1: master view, clusters, schedules, exports ---- */
.view-toggle { display: flex; gap: 4px; margin-left: 8px; }
.btn-ghost.active { color: var(--text); border-color: var(--signal-dim); background: var(--panel-2); }

/* scroll fix: panes need an explicit height to scroll under the fixed topbar */
#masterView, #settingsView { height: calc(100vh - 56px); overflow-y: auto; }

.cluster-pick { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }

.group-chip {
  display: inline-block; padding: 0 6px; margin: 0 1px;
  border: 1px solid var(--signal-dim); border-radius: 4px;
  color: var(--signal); background: rgba(63,209,122,0.08);
  text-decoration: none; font-size: 12px; cursor: pointer;
}
.group-chip:hover { background: rgba(63,209,122,0.18); }

.when .dl { margin-left: auto; display: inline-flex; gap: 8px; }
.dl-btn {
  font-family: var(--mono); font-size: 11px; color: var(--amber);
  text-decoration: none; border: 1px solid var(--line); border-radius: 4px; padding: 0 5px;
}
.dl-btn:hover { border-color: var(--amber); }

.row {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border: 1px solid var(--line); border-radius: 6px;
  margin-bottom: 6px; background: var(--panel); font-size: 13px;
}
.row .chip { font-family: var(--mono); font-size: 11px; }
.assign-name { flex: 1; }
.assign-sel { max-width: 180px; }
.sch-del { margin-left: auto; }

/* ---- v3.2: rendered reports, metrics, progress, exports ---- */
.hint { font-size: 12px; color: var(--muted); margin-bottom: 12px; line-height: 1.5; }
.hint b { color: var(--text); }

/* report selector toolbar + download buttons */
.report-toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.report-select {
  background: var(--ink); border: 1px solid var(--line); color: var(--text);
  font-size: 13px; padding: 8px 11px; border-radius: 7px; min-width: 260px; flex: 1;
}
.dl-bar { display: flex; gap: 8px; }
.dl-btn2 {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 8px 14px; border-radius: 7px; font-size: 13px; font-weight: 500; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; transition: background 0.12s, border-color 0.12s;
}
.dl-btn2:hover { border-color: var(--muted); background: var(--line); }
.dl-btn2.primary { border-color: var(--signal-dim); color: var(--signal); }
.dl-btn2.primary:hover { background: var(--signal-dim); color: var(--text); }

/* rendered markdown report */
.md-report { font-size: 13.5px; line-height: 1.6; }
.md-report .md-h { color: var(--signal); margin: 16px 0 8px; }
.md-report h2.md-h { font-size: 16px; }
.md-report h3.md-h { font-size: 14px; }
.md-report h4.md-h { font-size: 13px; color: var(--text); }
.md-report .md-p { margin: 8px 0; }
.md-report .md-ul { margin: 8px 0 8px 20px; }
.md-report .md-ul li { margin: 3px 0; }
.md-report strong { color: var(--amber); font-weight: 600; }
.md-report em { color: var(--text); font-style: italic; }
.md-report .md-pre {
  background: var(--ink); border: 1px solid var(--line); border-radius: 6px;
  padding: 10px 12px; font-family: var(--mono); font-size: 11px; color: var(--muted);
  overflow-x: auto; white-space: pre-wrap;
}

/* tables */
.md-table-wrap { overflow-x: auto; margin: 10px 0; }
.md-table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
.md-table th, .md-table td {
  border: 1px solid var(--line); padding: 7px 9px; text-align: left; vertical-align: top;
}
.md-table th { background: var(--panel-2); color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.03em; font-size: 11px; }
.md-table tr:nth-child(even) td { background: rgba(255,255,255,0.02); }

/* metrics grid */
.metrics {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px; margin: 12px 0;
}
.metric {
  background: var(--ink); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; display: flex; flex-direction: column; gap: 3px;
}
.metric .v { font-size: 20px; font-weight: 600; color: var(--amber); font-family: var(--mono); }
.metric .k { font-size: 11px; color: var(--muted); }

/* progress panel */
.progress-panel {
  background: var(--panel); border: 1px solid var(--signal-dim); border-radius: 8px;
  padding: 14px 16px; margin-top: 14px;
}
.progress-label { font-size: 13px; color: var(--text); margin-bottom: 10px; }
.progress-bar { height: 8px; background: var(--ink); border-radius: 5px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--signal); transition: width 0.4s ease; }
.progress-fill.err { background: var(--danger); }

/* new-since awareness tag */
.new-since {
  background: rgba(232,184,75,0.1); border: 1px solid var(--amber); color: var(--amber);
  padding: 9px 13px; border-radius: 8px; font-size: 13px; margin-bottom: 14px;
}

.chip-master { color: var(--signal) !important; }

/* v3.3: metrics cards above the rendered report body */
.report-cards:not(:empty) { margin: 4px 0 16px; }
.report-cards .metrics { margin: 0; }

/* password show/hide toggle */
.pass-wrap { position: relative; display: flex; }
.pass-wrap input { flex: 1; padding-right: 44px; }
.pass-toggle {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  padding: 6px 8px; line-height: 0; border-radius: 6px;
  display: inline-flex; align-items: center;
}
.pass-toggle:hover { color: var(--text); }
/* hidden state (default): crossed-out eye; revealed state: open eye */
.pass-toggle .icon-eye { display: none; }
.pass-toggle .icon-eye-off { display: inline; }
.pass-toggle.revealed .icon-eye { display: inline; }
.pass-toggle.revealed .icon-eye-off { display: none; }

/* ---- mobile (kept last so these reliably override the desktop rules above) ---- */
@media (max-width: 760px) {
  /* topbar: compact single row that fits a phone (drop the wordmark, keep nav) */
  .topbar { gap: 8px; padding: 0 12px; }
  .brand { display: none; }
  .view-toggle { gap: 6px; margin-left: 0; }
  .btn-ghost { margin-left: 0; padding: 8px 12px; }
  .status { gap: 0; }
  #statusText { display: none; }            /* keep the live dot, drop the label */
  #logout { margin-left: 4px; }

  /* groups: list and detail become full-screen panes you swap between */
  #groupsView { grid-template-columns: 1fr; }
  #groupsView .sidebar { display: block; border-right: none; }
  #groupsView #detail { display: none; }
  #groupsView.show-detail .sidebar { display: none; }
  #groupsView.show-detail #detail { display: block; }
  .back-btn { display: inline-flex; }

  /* tighter spacing + stacked form controls so nothing is cramped or clipped */
  .detail-head { padding: 14px 16px; flex-wrap: wrap; }
  .detail-head h2 { font-size: 16px; }
  .section { padding: 16px; }
  .controls { gap: 8px; }
  .controls label { width: 100%; font-size: 12px; }
  .controls label > input:not([type="checkbox"]),
  .controls label > select { display: block; width: 100%; margin-top: 4px; }
  .controls .btn { width: 100%; justify-content: center; }
  .date-field, .report-select { width: 100%; }
  .report-select { min-width: 0; }
  .msg .a { width: 96px; }
}
