:root {
  --bg: #14171a;
  --bg-card: #1c2024;
  --bg-card-hi: #232830;
  --fg: #e6e6e6;
  --fg-dim: #9aa0a6;
  --border: #2a2f36;
  --accent: #8ad0a0;
  --accent-blue: #7aaadc;
  --warn: #d8a85c;
  --danger: #d87878;
  --mono: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

a { color: var(--accent-blue); text-decoration: none; }
a:hover { text-decoration: underline; }

header {
  padding: 24px 32px 12px;
  border-bottom: 1px solid var(--border);
}

header h1 {
  font-family: var(--mono);
  font-size: 20px;
  margin: 0 0 4px;
  letter-spacing: 0.5px;
}

header .sub { margin: 0; color: var(--fg-dim); font-size: 12px; }

main { padding: 24px 32px; max-width: 1200px; }

footer {
  padding: 16px 32px 32px;
  border-top: 1px solid var(--border);
  margin-top: 32px;
}

.muted { color: var(--fg-dim); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; font-family: var(--mono); }
.empty { color: var(--fg-dim); font-style: italic; }

.back { font-family: var(--mono); color: var(--fg-dim); }
.back:hover { color: var(--accent-blue); }

/* table */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}

th, td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

th {
  background: var(--bg-card-hi);
  font-family: var(--mono);
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--fg-dim);
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg-card-hi); }

.users td .label { font-weight: 500; }
.users td .muted { margin-left: 8px; font-size: 12px; }
.users td .more { font-family: var(--mono); font-size: 12px; }

/* status dots / pills */
.dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}
.dot.online { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.dot.offline { background: #555; }

.status-pill {
  display: inline-block;
  font-family: var(--mono);
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  margin-left: 8px;
  vertical-align: middle;
}
.status-pill.online { background: rgba(138,208,160,0.15); color: var(--accent); border: 1px solid var(--accent); }
.status-pill.offline { background: rgba(120,120,120,0.15); color: #aaa; border: 1px solid #555; }

/* badges */
.badge {
  display: inline-block;
  font-family: var(--mono);
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.badge.ok { background: rgba(138,208,160,0.15); color: var(--accent); }
.badge.warn { background: rgba(216,168,92,0.15); color: var(--warn); }

/* Inline per-contact timezone selector on the index page */
.tz-form { display: inline-block; margin: 0; }
.tz-select {
  font-family: var(--mono);
  font-size: 11px;
  padding: 1px 4px;
  background: rgba(255,255,255,0.04);
  color: #bbb;
  border: 1px solid #333;
  border-radius: 3px;
  max-width: 18ch;
}
.tz-select:focus { outline: 1px solid #555; }

/* Per-column timezone sub-label in the sessions table header */
.tz-tag {
  display: block;
  font-family: var(--mono);
  font-size: 9px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: #777;
  margin-top: 2px;
}

.day-delta {
  font-family: var(--mono);
  font-size: 9px;
  color: #777;
  margin-left: 2px;
  vertical-align: super;
}

/* banner */
.banner {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 10px 14px;
  margin: 12px 0 0;
  background: var(--bg-card);
  font-size: 13px;
}
.banner.warn { border-color: var(--warn); color: #e8c89a; background: rgba(216,168,92,0.05); }
.banner.ok { border-color: var(--accent); color: #c9e9d4; background: rgba(138,208,160,0.05); }

/* user page cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 18px 20px;
  margin-bottom: 24px;
}
.card h2 {
  margin: 0 0 14px;
  font-family: var(--mono);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--fg-dim);
  font-weight: 500;
}

.chart-wrap {
  position: relative;
  height: 260px;
}
.chart-wrap.tall { height: 360px; }

/* sessions table */
.sessions th, .sessions td { font-size: 13px; }

/* storage banner */
.storage-banner {
  padding: 6px 32px;
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.4px;
  border-bottom: 1px solid var(--border);
}
.storage-banner .storage-label { color: var(--fg-dim); margin-right: 6px; }
.storage-banner .storage-num { font-weight: 600; }
.storage-banner .storage-pct { color: var(--fg-dim); margin-left: 8px; }
.storage-banner.level-ok { background: rgba(138,208,160,0.08); color: var(--accent); }
.storage-banner.level-warn { background: rgba(216,168,92,0.10); color: var(--warn); }
.storage-banner.level-danger { background: rgba(216,120,120,0.15); color: var(--danger); }

/* extra status pill variant */
.status-pill.msg {
  background: rgba(122,170,220,0.15);
  color: var(--accent-blue);
  border: 1px solid var(--accent-blue);
  cursor: pointer;
}

/* badge danger variant */
.badge.danger { background: rgba(216,120,120,0.18); color: var(--danger); }

/* messages filter form */
.filter-form {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: end;
}
.filter-form label {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-family: var(--mono);
}
.filter-form input,
.filter-form select {
  background: var(--bg-card-hi);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 6px 8px;
  border-radius: 3px;
  font-family: inherit;
  font-size: 13px;
  margin-top: 4px;
}
.filter-form button {
  background: var(--accent-blue);
  color: var(--bg);
  border: none;
  padding: 7px 14px;
  border-radius: 3px;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.filter-form button:hover { filter: brightness(1.1); }

/* chat bubbles */
.chat {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
/* Infinite-scroll-up sentinel: invisible by default; an IntersectionObserver
   on this element triggers the next "load older" fetch. The .loading class
   surfaces a muted hint while the request is in flight. */
.older-loader {
  height: 40px;
  flex: 0 0 auto;
}
.older-loader[data-has-more="false"] { display: none; }
.older-loader.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--fg-dim);
  font-size: 12px;
}
.older-loader.loading::before { content: "loading older messages…"; }
.bubble {
  max-width: 70%;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-card-hi);
  position: relative;
}
.bubble.in {
  align-self: flex-start;
  border-bottom-left-radius: 2px;
  background: #182533;
  border-color: #1f2c3b;
}
.bubble.out {
  align-self: flex-end;
  background: #2b5278;
  border-color: #36679a;
  border-bottom-right-radius: 2px;
}
.bubble.out .bubble-meta { color: rgba(255, 255, 255, 0.7); }
.bubble.deleted {
  opacity: 0.55;
  text-decoration: line-through;
  background: rgba(216,120,120,0.06);
  border-color: rgba(216,120,120,0.3);
}
.bubble.deleted .bubble-text { text-decoration: line-through; }
.bubble .bubble-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 11px;
  color: var(--fg-dim);
  margin-bottom: 4px;
  font-family: var(--mono);
}
.bubble .bubble-meta .time { font-variant-numeric: tabular-nums; }
.bubble .bubble-meta .checks {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1;
  color: rgba(255, 255, 255, 0.55);
  letter-spacing: -2px;
  padding-right: 2px;
  cursor: help;
}
.bubble .bubble-meta .checks.read { color: #5eb5ff; }
.bubble .bubble-meta .listened {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: rgba(255, 255, 255, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.2);
  cursor: help;
}
.bubble .bubble-meta .listened.played {
  color: #5eb5ff;
  border-color: rgba(94, 181, 255, 0.45);
  background: rgba(94, 181, 255, 0.08);
}
.bubble .bubble-meta .listened + .checks { margin-left: 6px; }
/* inline reply quote (Telegram-style quoted block above the bubble text).
   Translucent-black background + a contrasting left bar so it stays legible
   on both the dark .in bubble and the blue .out bubble. */
.bubble .reply-quote {
  display: block;
  max-width: 100%;
  overflow: hidden;
  margin-bottom: 6px;
  padding: 4px 8px;
  font-size: 12px;
  border-left: 3px solid var(--accent-blue);
  border-radius: 0 6px 6px 0;
  background: rgba(0, 0, 0, 0.25);
}
.bubble a.reply-quote { color: inherit; text-decoration: none; }
.bubble a.reply-quote:hover { background: rgba(0, 0, 0, 0.38); }
.bubble .reply-quote .reply-sender {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-blue);
}
.bubble .reply-quote .reply-snippet {
  display: block;
  color: var(--fg-dim);
  white-space: normal;
  overflow-wrap: anywhere;
}
.bubble .reply-quote.muted-quote {
  border-left-color: var(--border);
}
.bubble .reply-quote.muted-quote .reply-snippet { font-style: italic; }
.bubble .bubble-text {
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 14px;
}
.bubble .badge.edited { cursor: help; }

/* edit history timeline (expandable) */
.filter-form label.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  text-transform: uppercase;
}
.filter-form label.checkbox input { margin-top: 0; }

.bubble .edit-history {
  margin-top: 8px;
  border-top: 1px dashed var(--border);
  padding-top: 6px;
}
.bubble .edit-history > summary {
  cursor: pointer;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--warn);
  list-style: none;
  user-select: none;
}
.bubble .edit-history > summary::-webkit-details-marker { display: none; }
.bubble .edit-history[open] > summary { color: var(--accent-blue); }

.bubble .edit-versions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}
.bubble .edit-version {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 10px;
}
.bubble .edit-version.current {
  border-color: var(--accent);
  background: rgba(138,208,160,0.06);
}
.bubble .edit-version.original {
  border-color: var(--warn);
  background: rgba(216,168,92,0.05);
}
.bubble .edit-version-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 4px;
  font-family: var(--mono);
  font-size: 10px;
}
.bubble .edit-version-text {
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 13px;
}
.bubble .edit-version.prior .edit-version-text,
.bubble .edit-version.original .edit-version-text {
  text-decoration: line-through;
  color: var(--fg-dim);
}
.bubble .media-photo {
  display: block;
  margin-top: 6px;
  max-width: 300px;
  max-height: 300px;
  border-radius: 4px;
}
.bubble .media-video {
  display: block;
  margin-top: 6px;
  max-width: 400px;
  border-radius: 4px;
}
.bubble .media-video.round {
  width: 240px;
  height: 240px;
  border-radius: 50%;
  object-fit: cover;
}
.bubble audio {
  display: block;
  margin-top: 6px;
  width: 280px;
}
.bubble .media-doc {
  display: inline-block;
  margin-top: 6px;
  padding: 6px 10px;
  border: 1px dashed var(--border);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 12px;
}
.bubble .media-placeholder {
  margin-top: 6px;
  font-style: italic;
  color: var(--fg-dim);
  font-size: 12px;
}

/* message formatting */
.bubble-text code {
  background: rgba(255,255,255,0.07);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: var(--mono);
  font-size: 12px;
}
.bubble-text pre {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 8px 10px;
  margin: 6px 0;
  overflow-x: auto;
}
.bubble-text pre code { background: transparent; padding: 0; }
.bubble-text blockquote {
  border-left: 3px solid var(--accent);
  margin: 4px 0;
  padding: 2px 0 2px 10px;
  color: var(--fg-dim);
}
.bubble-text a { color: var(--accent); text-decoration: underline; }
.bubble-text .spoiler {
  background: var(--fg-dim);
  color: transparent;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.bubble-text .spoiler:hover,
.bubble-text .spoiler:active { background: transparent; color: inherit; }

/* flash messages (e.g. rescan-contacts feedback) */
.flash {
  padding: 8px 12px;
  border-radius: 6px;
  margin: 0 0 8px 0;
  font-size: 14px;
}
.flash-ok   { background: #1f3a1f; color: #aeeaae; border: 1px solid #2f5a2f; }
.flash-warn { background: #3a2a1f; color: #f0c98a; border: 1px solid #5a402a; }

.inline-form {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 0;
}
.inline-form button {
  background: var(--accent-blue);
  color: var(--bg);
  border: none;
  padding: 6px 12px;
  border-radius: 3px;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.inline-form button:hover { filter: brightness(1.1); }

/* contacts page */
.contact-search {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 18px;
}
.contact-search input[type="search"] {
  flex: 1;
  background: var(--bg-card-hi);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 8px 10px;
  border-radius: 3px;
  font-size: 13px;
}
.contact-search button {
  background: var(--accent-blue);
  color: var(--bg);
  border: none;
  padding: 8px 14px;
  border-radius: 3px;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.contacts-grid {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}
.contacts-row {
  display: grid;
  grid-template-columns: 2.2fr 0.7fr 0.5fr 0.5fr 0.5fr 0.9fr 0.5fr 0.7fr;
  gap: 10px;
  align-items: center;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
}
.contacts-row:last-child { border-bottom: none; }
.contacts-row.contacts-head {
  background: var(--bg-card-hi);
  font-family: var(--mono);
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--fg-dim);
}
.contacts-row.monitored { background: rgba(138,208,160,0.04); }
.contacts-row .label { font-weight: 500; }
.contacts-row .cell-label .muted { margin-left: 8px; font-size: 12px; }
.contacts-row .cell-label .more { font-family: var(--mono); font-size: 12px; margin-left: 8px; }
.contacts-row input[type="number"].threshold {
  width: 70px;
  background: var(--bg-card-hi);
  border: 1px solid var(--border);
  color: var(--fg);
  padding: 4px 6px;
  border-radius: 3px;
  font-family: var(--mono);
  font-size: 12px;
}
.contacts-row button {
  background: var(--accent);
  color: var(--bg);
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.contacts-row button:hover { filter: brightness(1.1); }

/* login page */
.login-card { max-width: 380px; margin: 40px auto; }
.login-form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.login-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-family: var(--mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--fg-dim);
}
.login-form input[type="text"],
.login-form input[type="password"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--fg);
  padding: 8px 10px;
  font-family: var(--mono);
  font-size: 13px;
}
.login-form input:focus { outline: none; border-color: var(--accent); }
.login-form button {
  background: var(--accent);
  color: var(--bg);
  border: none;
  padding: 9px 14px;
  border-radius: 3px;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-top: 4px;
}
.login-form button:hover { filter: brightness(1.1); }

/* mobile */
@media (max-width: 720px) {
  header { padding: 16px 14px 10px; }
  main { padding: 14px; }
  footer { padding: 14px; margin-top: 20px; }
  .storage-banner { padding: 6px 14px; }

  header h1 { font-size: 18px; }
  .status-pill { display: inline-block; margin-left: 0; margin-top: 4px; }

  /* Index table: hide low-priority columns on phones.
     Visible: User (1), Now (2), Today (3), Msgs (6), view (8). */
  .users th:nth-child(4),
  .users td:nth-child(4),
  .users th:nth-child(5),
  .users td:nth-child(5),
  .users th:nth-child(7),
  .users td:nth-child(7) { display: none; }

  th, td { padding: 8px 8px; }
  th { font-size: 10px; }
  .users td .muted { display: block; margin-left: 0; }

  /* Sessions table scrolls horizontally rather than hiding columns. */
  .card .sessions { display: block; overflow-x: auto; white-space: nowrap; }

  .card { padding: 14px; margin-bottom: 16px; }
  .chart-wrap { height: 220px; }
  .chart-wrap.tall { height: 300px; }

  /* Filter form: stack inputs to full width. */
  .filter-form { flex-direction: column; gap: 10px; align-items: stretch; }
  .filter-form label { width: 100%; }
  .filter-form input,
  .filter-form select { width: 100%; }
  .filter-form button { width: 100%; padding: 10px; }

  /* Chat bubbles + media: keep within viewport. */
  .bubble { max-width: 88%; }
  .bubble .media-photo { max-width: 100%; max-height: none; }
  .bubble .media-video { max-width: 100%; }
  .bubble .media-video.round { width: 200px; height: 200px; }
  .bubble audio { width: 100%; }

  /* Contacts page: tighten spacing, narrow threshold input. */
  .contacts-row {
    grid-template-columns: 1.6fr 0.5fr 0.5fr 0.5fr 0.5fr 0.85fr 0.45fr 0.65fr;
    gap: 6px;
    padding: 8px 10px;
    font-size: 12px;
  }
  .contacts-row input[type="number"].threshold { width: 56px; }
  .contacts-row .cell-label .muted { display: block; margin-left: 0; }
}
