* {
  box-sizing: border-box;
}

:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1a1d21;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --open: #d97706;
  --completed: #16a34a;
  --mark-bg: #fef08a;
  --card-hover: #f8fafc;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --fg: #edeef0;
    --muted: #9aa1ab;
    --border: #2a2d33;
    --accent: #5b8def;
    --open: #f59e0b;
    --completed: #4ade80;
    --mark-bg: #7c6a00;
    --card-hover: #1c1f24;
  }
}

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

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px;
}

.search-bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: sticky;
  top: 0;
  background: var(--bg);
  padding-bottom: 8px;
  z-index: 5;
}

#query {
  width: 100%;
  padding: 10px 12px;
  font-size: 15px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
}

#query:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.status-filter {
  display: flex;
  gap: 6px;
}

.status-btn {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}

.status-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.hint {
  color: var(--muted);
  padding: 8px 2px;
  font-size: 13px;
}

.results {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.result-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
  transition: background 0.1s ease;
}

.result-card:hover {
  background: var(--card-hover);
}

.result-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 4px;
}

.result-contact {
  font-weight: 600;
}

.result-date {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.result-meta {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
}

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
}

.badge.open {
  background: var(--open);
}

.badge.completed {
  background: var(--completed);
}

.result-snippet {
  color: var(--fg);
  line-height: 1.4;
}

.result-snippet mark {
  background: var(--mark-bg);
  color: inherit;
  padding: 0 1px;
  border-radius: 2px;
}

.pagination {
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 16px 0;
}

.pagination button {
  padding: 6px 14px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  border-radius: 6px;
  cursor: pointer;
}

.pagination button:disabled {
  opacity: 0.4;
  cursor: default;
}

.thread-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.thread-overlay.hidden {
  display: none;
}

.thread-panel {
  background: var(--bg);
  color: var(--fg);
  width: min(560px, 92vw);
  max-height: 86vh;
  border-radius: 10px;
  padding: 16px;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.thread-close {
  position: absolute;
  top: 8px;
  right: 10px;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--muted);
}

.thread-header {
  padding-right: 24px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}

.thread-header .name {
  font-weight: 600;
  font-size: 15px;
}

.thread-header .sub {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.thread-header .open-link {
  display: inline-block;
  margin-top: 8px;
  color: var(--accent);
  text-decoration: none;
  font-size: 13px;
}

.thread-messages {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.thread-msg {
  padding: 6px 10px;
  border-radius: 8px;
  max-width: 85%;
  font-size: 13px;
  line-height: 1.4;
}

.thread-msg.out {
  background: var(--accent);
  color: #fff;
  align-self: flex-end;
}

.thread-msg.in {
  background: var(--card-hover);
  border: 1px solid var(--border);
  align-self: flex-start;
}

.thread-msg .ts {
  display: block;
  font-size: 10px;
  opacity: 0.75;
  margin-top: 2px;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 24px 0;
}
