:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --accent: #22c55e;
  --border: #e5e7eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111113;
    --surface: #1c1c1f;
    --text: #f3f4f6;
    --muted: #9ca3af;
    --accent: #22c55e;
    --border: #2c2c31;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.5rem 2rem;
  border-bottom: 1px solid var(--border);
}

h1 { font-size: 1.25rem; margin: 0; }

.header-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.user-name { font-size: 0.85rem; color: var(--muted); }

.logout-link {
  font-size: 0.8rem;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--border);
  padding: 0.25rem 0.65rem;
  border-radius: 6px;
}
.logout-link:hover { border-color: var(--accent); color: var(--text); }

.badge {
  font-size: 0.75rem;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  background: var(--border);
  color: var(--muted);
}
.badge.ready { background: #dcfce7; color: #166534; }
.badge.waiting { background: #fef3c7; color: #92400e; }
.badge.error { background: #fee2e2; color: #991b1b; }

main { max-width: 720px; margin: 0 auto; padding: 2rem; }

.hidden { display: none; }

#qr-section { text-align: center; }
#qr-image { width: 260px; height: 260px; margin-top: 1rem; border-radius: 8px; }

.meta-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 1rem;
}

.topic-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  margin-bottom: 0.75rem;
  cursor: pointer;
  transition: border-color 0.15s ease;
}
.topic-card:hover, .topic-card:focus-visible {
  border-color: var(--accent);
  outline: none;
}

.topic-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.topic-header h3 { margin: 0; font-size: 1rem; }
.count { font-size: 0.75rem; color: var(--muted); }
.summary { color: var(--muted); font-size: 0.875rem; margin: 0.5rem 0; }

.bar {
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin: 0.5rem 0;
}
.bar-fill { height: 100%; background: var(--accent); }

.topic-footer {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--muted);
}

#empty-section { color: var(--muted); text-align: center; margin-top: 3rem; }

#backfill-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  margin-bottom: 1.5rem;
}

.backfill-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
}

.backfill-row input {
  width: 4rem;
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

.backfill-row button {
  margin-left: auto;
  padding: 0.4rem 0.9rem;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #052e14;
  font-size: 0.875rem;
  cursor: pointer;
}

.backfill-row button:disabled { opacity: 0.6; cursor: not-allowed; }

.backfill-status { color: var(--muted); font-size: 0.8rem; margin: 0.5rem 0 0; }

.modal-panel {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.75rem;
  max-width: 560px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
}

.modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0.25rem 0.5rem;
}

.modal-panel h2 { margin: 0 0 0.5rem; font-size: 1.15rem; padding-right: 2rem; }
.modal-panel h3 { margin: 1.25rem 0 0.5rem; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); }

.modal-meta {
  display: flex;
  gap: 0.75rem;
  font-size: 0.75rem;
  color: var(--muted);
  flex-wrap: wrap;
}

.modal-summary { font-size: 0.9rem; line-height: 1.5; white-space: pre-wrap; }

.modal-documents { display: flex; flex-direction: column; gap: 0.5rem; }
.modal-documents-empty { color: var(--muted); font-size: 0.85rem; }

.document-row {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
}
.document-row:hover { border-color: var(--accent); }
.document-name { font-size: 0.875rem; font-weight: 500; }
.document-meta { font-size: 0.75rem; color: var(--muted); }
.document-row audio { width: 100%; margin-top: 0.3rem; }

.login-main {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
}

.login-card h1 { font-size: 1.1rem; margin: 0 0 0.4rem; }
.login-subtitle { color: var(--muted); font-size: 0.85rem; margin: 0 0 1.5rem; }

.login-card label { font-size: 0.8rem; color: var(--muted); margin: 0.75rem 0 0.3rem; }

.login-card input {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
}

.login-card button {
  margin-top: 1.5rem;
  padding: 0.6rem;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #052e14;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.login-error { color: #dc2626; font-size: 0.8rem; margin: 0.75rem 0 0; }

/* Circular floating action button, always bottom-right regardless of
   scroll position — this is what replaces the old header "Ask" button. */
.ask-fab {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 101; /* above .ask-panel, so it's never covered by it */
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #052e14;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}
.ask-fab:hover { filter: brightness(1.05); }
.ask-fab.hidden { display: none; }

/* Floating panel, not a centered/backdropped modal: fixed bottom-right, no
   backdrop element at all, so the rest of the page stays fully visible and
   interactive (clickable/scrollable) while this is open. */
.ask-panel {
  /* Overrides .modal-panel's centered-modal assumptions (position:
     relative, width: 100%, max-height: 85vh) with fixed bottom-right
     placement — set explicitly rather than relying on cascade order so
     this stays correct regardless of where .modal-panel's rule moves to. */
  position: fixed;
  bottom: 5.75rem; /* sits just above the FAB rather than overlapping it */
  right: 1.5rem;
  z-index: 100;
  width: min(380px, calc(100vw - 3rem));
  max-width: min(380px, calc(100vw - 3rem));
  height: min(600px, calc(100vh - 8rem));
  max-height: min(600px, calc(100vh - 8rem));
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
}
.ask-panel.hidden { display: none; }
.ask-subtitle { color: var(--muted); font-size: 0.85rem; margin: 0 0 1rem; }

.ask-thread {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.25rem;
  margin-bottom: 1rem;
}

.ask-message {
  max-width: 85%;
  padding: 0.6rem 0.85rem;
  border-radius: 10px;
  font-size: 0.875rem;
  line-height: 1.45;
  white-space: pre-wrap;
}
.ask-message-user {
  align-self: flex-end;
  background: var(--accent);
  color: #052e14;
}
.ask-message-bot {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--border);
  /* Rendered markdown (see renderMarkdown in app.js) carries its own block
     spacing via <p>/<ul>/<br> — pre-wrap on top of that would double up
     whitespace, so this overrides the general .ask-message rule above. */
  white-space: normal;
}
.ask-message-bot p { margin: 0 0 0.5em; }
.ask-message-bot p:last-child { margin-bottom: 0; }
.ask-message-bot ul,
.ask-message-bot ol {
  margin: 0.25em 0 0.5em;
  padding-left: 1.25em;
}
.ask-message-bot li { margin-bottom: 0.15em; }
.ask-message-bot code {
  background: var(--border);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-size: 0.85em;
}
.ask-message-bot strong { font-weight: 600; }
.ask-message-documents {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 0.6rem;
}
.ask-message-error { color: #dc2626; }

.ask-form { display: flex; gap: 0.5rem; }

.ask-form input {
  flex: 1;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
}

.ask-form button {
  padding: 0.6rem 1.1rem;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #052e14;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
}
.ask-form button:disabled { opacity: 0.6; cursor: not-allowed; }

#matter-view h3 {
  margin: 1.25rem 0 0.5rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}

.back-link {
  display: inline-block;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.875rem;
  margin-bottom: 1.25rem;
}
.back-link:hover { color: var(--text); }

#matter-page-title { margin: 0 0 0.5rem; font-size: 1.3rem; }

.matter-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 1.5rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.75rem;
}

.matter-tab-btn {
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.8rem;
  cursor: pointer;
}
.matter-tab-btn:hover { border-color: var(--accent); color: var(--text); }
.matter-tab-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #052e14;
  font-weight: 600;
}

.matter-tab-empty { color: var(--muted); font-size: 0.85rem; }

.chat-history-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.chat-history-entry {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.9rem;
}
.chat-history-entry p { margin: 0.25rem 0 0; font-size: 0.875rem; line-height: 1.45; }
.chat-history-date { font-size: 0.75rem; color: var(--muted); }
