:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c2330;
  --muted: #667085;
  --border: #d8dee8;
  --accent: #1f5fbf;
  --accent-dark: #17498f;
  --accent-soft: #e8f0fd;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --ok: #067647;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 6px 20px rgba(16, 24, 40, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c;
    --panel: #171d26;
    --text: #e6eaf0;
    --muted: #98a2b3;
    --border: #2a3341;
    --accent: #6aa2ff;
    --accent-dark: #8bb8ff;
    --accent-soft: #1c2b45;
    --danger: #ff8b7e;
    --danger-soft: #3a1d1a;
    --ok: #6dd3a3;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
}

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

h1 { font-size: 1.5rem; margin: 0 0 0.5rem; }
h2 { font-size: 1.15rem; margin: 0 0 0.5rem; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.inline { display: inline; margin: 0; }

/* top bar */
.topbar {
  align-items: center;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 1.5rem;
  padding: 0.6rem 1.2rem;
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { color: var(--text); font-weight: 700; letter-spacing: 0.02em; }
.brand:hover { text-decoration: none; }

.topnav { display: flex; gap: 1rem; }
.topnav a { color: var(--muted); font-size: 0.95rem; }
.topnav a:hover { color: var(--accent); }

.who { align-items: center; display: flex; gap: 0.6rem; margin-left: auto; min-width: 0; }
.who .email { color: var(--muted); font-size: 0.85rem; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  background: var(--accent-soft);
  border-radius: 999px;
  color: var(--accent-dark);
  font-size: 0.72rem;
  padding: 0.15rem 0.55rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.link-button {
  background: none;
  border: 0;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
  padding: 0;
}
.link-button:hover { text-decoration: underline; }

/* layout */
.page { margin: 0 auto; max-width: 1100px; padding: 1.5rem 1.2rem 3rem; }
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.4rem;
}
.card + .card { margin-top: 1rem; }
.card.narrow { margin: 2rem auto; max-width: 460px; }

/* forms */
.stack { display: flex; flex-direction: column; gap: 0.45rem; }
label { color: var(--muted); font-size: 0.85rem; }
input[type="email"], input[type="password"], input[type="text"], select, textarea {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font: inherit;
  padding: 0.6rem 0.75rem;
  width: 100%;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  outline: 2px solid color-mix(in srgb, var(--accent) 25%, transparent);
}
button.primary, button.secondary, button.danger, .button-link {
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
  padding: 0.55rem 1rem;
}
button.primary {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
  font-weight: 600;
}
button.primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
button.secondary, .button-link {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
}
button.secondary:hover, .button-link:hover { border-color: var(--accent); text-decoration: none; }
button.danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
button.danger:hover { background: var(--danger-soft); }
button:disabled { cursor: wait; opacity: 0.65; }

.alert { border-radius: 10px; margin: 0.4rem 0 0.8rem; padding: 0.6rem 0.8rem; }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-ok { background: var(--accent-soft); color: var(--accent-dark); }

/* auth */
.auth-body { display: flex; min-height: 100vh; }
.auth-main { align-items: center; display: flex; justify-content: center; padding: 2rem 1rem; width: 100%; }
.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  max-width: 420px;
  padding: 2rem;
  width: 100%;
}
.auth-card h1 { font-size: 1.8rem; }


/* chat */
.chat-main { display: flex; height: calc(100vh - 56px); max-width: none; padding: 0; }
.chat-shell { display: flex; flex: 1; min-height: 0; }
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  width: 280px;
}
.new-chat .wide { width: 100%; }
.conversations { display: flex; flex-direction: column; gap: 0.2rem; }
.conversation { align-items: center; border-radius: 10px; display: flex; padding: 0.1rem 0.2rem; }
.conversation:hover, .conversation.active { background: var(--accent-soft); }
.conversation-link { color: var(--text); display: flex; flex: 1; gap: 0.5rem; min-width: 0; padding: 0.45rem 0.5rem; }
.conversation-link:hover { text-decoration: none; }
.conversation-title { font-size: 0.92rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-count { color: var(--muted); font-size: 0.75rem; margin-left: auto; }
.icon-button { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1.1rem; line-height: 1; padding: 0.2rem 0.4rem; }
.icon-button:hover { color: var(--danger); }
.sidebar-empty { margin: 0.5rem; }
.thread { display: flex; flex: 1; min-height: 0; min-width: 0; }
.thread-inner {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin: 0 auto;
  max-width: 860px;
  overflow-y: auto;
  padding: 1.4rem 1.2rem 1rem;
  width: 100%;
}
.thread-head { align-items: flex-start; display: flex; gap: 1rem; justify-content: space-between; }
.thread-head h1 { font-size: 1.25rem; margin: 0; }
.messages { display: flex; flex-direction: column; gap: 0.9rem; }
.msg { display: flex; flex-direction: column; max-width: 100%; }
.msg-user { align-items: flex-end; }
.msg-user .bubble { background: var(--accent); border-radius: 14px 14px 4px 14px; color: #fff; max-width: 80%; }
.msg-assistant .bubble { background: var(--panel); border: 1px solid var(--border); border-radius: 14px 14px 14px 4px; }
.bubble { overflow-wrap: anywhere; padding: 0.7rem 0.9rem; white-space: pre-wrap; }
.msg-assistant .bubble { white-space: normal; }
.answer { white-space: pre-wrap; }
.meta { color: var(--muted); font-size: 0.75rem; margin: 0.25rem 0.2rem 0; }
.citations { border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 0.45rem; margin-top: 0.7rem; padding-top: 0.6rem; }
.citations-title { color: var(--muted); font-size: 0.75rem; letter-spacing: 0.05em; text-transform: uppercase; }
.citation { font-size: 0.9rem; }
.quote { color: var(--muted); font-size: 0.82rem; margin-top: 0.15rem; }
.empty { color: var(--muted); padding: 2rem 0; text-align: center; }
.composer {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-top: auto;
  padding: 0.8rem;
  position: sticky;
  bottom: 0;
}
.composer-label { display: block; margin-bottom: 0.3rem; }
.composer textarea { min-height: 64px; resize: vertical; }
.composer-actions { align-items: center; display: flex; gap: 1rem; justify-content: space-between; margin-top: 0.5rem; }
.hint { color: var(--muted); font-size: 0.78rem; }

@media (max-width: 760px) {
  .chat-main { height: auto; min-height: calc(100vh - 56px); }
  .chat-shell { flex-direction: column; }
  .sidebar { border-bottom: 1px solid var(--border); border-right: 0; max-height: 40vh; width: 100%; }
  .thread { min-height: 60vh; }
  .topnav { display: none; }
  .who .email { max-width: 120px; }
  .composer-actions { align-items: flex-start; flex-direction: column; }
}

/* admin */
.admin-head { align-items: center; display: flex; flex-wrap: wrap; gap: 1rem; justify-content: space-between; margin-bottom: 1rem; }
.admin-nav { display: flex; flex-wrap: wrap; gap: 0.9rem; }
.admin-nav a { color: var(--muted); }
.admin-nav a:hover { color: var(--accent); }
.stats-grid { display: grid; gap: 0.8rem; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 1rem; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.8rem; }
.facts { display: grid; gap: 0.4rem 1rem; grid-template-columns: max-content 1fr; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.table-wrap { overflow-x: auto; }
.table { border-collapse: collapse; font-size: 0.9rem; width: 100%; }
.table th, .table td { border-bottom: 1px solid var(--border); padding: 0.5rem 0.6rem; text-align: left; vertical-align: middle; }
.table th { color: var(--muted); font-size: 0.78rem; letter-spacing: 0.04em; text-transform: uppercase; }
.row-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.small-button { font-size: 0.8rem; padding: 0.3rem 0.6rem; }
.row-form { display: flex; flex-wrap: wrap; gap: 0.6rem; }
.row-form input[type="email"] { flex: 1; min-width: 220px; }
.invite-link { display: inline-block; margin-top: 0.3rem; overflow-wrap: anywhere; }
.audit-text { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; font-size: 0.82rem; max-height: 70vh; overflow: auto; padding: 0.8rem; white-space: pre-wrap; }
