/* Single stylesheet for the whole web UI — no framework, no build step, no
   external font or CDN request (the box serves this over its own TLS and
   shouldn't depend on a third party being reachable). Light and dark both
   come from the same custom properties. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #1a1d21;
  --muted: #656d76;
  --accent: #1f5f8b;
  --accent-text: #ffffff;
  --danger: #b42318;
  --ok: #1f7a45;
  --warn: #96650a;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181d;
    --surface: #1e2127;
    --surface-2: #262a31;
    --border: #343941;
    --text: #e6e8eb;
    --muted: #9aa3ad;
    --accent: #4c9bd1;
    --accent-text: #0f1216;
    --danger: #f97066;
    --ok: #4ac47c;
    --warn: #e3b341;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

h1 { font-size: 1.45rem; margin: 0 0 .6rem; }
h2 { font-size: 1.05rem; margin: 0 0 .7rem; }
a { color: var(--accent); }

/* ---------- shared controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .5rem .9rem; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font: inherit; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.block { width: 100%; }
.btn.small { padding: .25rem .55rem; font-size: .85rem; }
.btn.danger { color: var(--danger); border-color: var(--border); }
.btn:disabled { opacity: .55; cursor: default; }

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

input[type=text], input[type=email], input[type=password], textarea, select {
  width: 100%; padding: .55rem .7rem; font: inherit;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
}
input:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

label { display: block; margin-bottom: .8rem; font-weight: 500; }
label input, label select { margin-top: .3rem; font-weight: 400; }
.hint { color: var(--muted); font-weight: 400; font-size: .85rem; }
.note { color: var(--muted); font-size: .9rem; }
.small { font-size: .85rem; }
.row { display: flex; gap: .6rem; flex-wrap: wrap; margin: 1rem 0 .4rem; }

.alert {
  padding: .6rem .8rem; border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 12%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--border));
  color: var(--text);
}
.ok-msg {
  padding: .6rem .8rem; border-radius: var(--radius);
  background: color-mix(in srgb, var(--ok) 12%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--ok) 30%, var(--border));
}

.disclaimer { color: var(--muted); font-size: .82rem; margin-top: 1.2rem; }

/* ---------- centered card pages (landing/login/register) ---------- */
body.centered {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 1.5rem;
}
.card {
  width: 100%; max-width: 26rem; padding: 1.8rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow);
}
.card.wide { max-width: 40rem; }
.lead { color: var(--muted); }
.features { color: var(--muted); padding-left: 1.1rem; margin: 1rem 0; }
.features li { margin-bottom: .3rem; }

/* ---------- chat layout ---------- */
body.app { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: 17.5rem; flex: 0 0 17.5rem; display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border);
}
.sidebar-head { padding: .8rem; border-bottom: 1px solid var(--border); }
.convo-list { flex: 1; overflow-y: auto; padding: .5rem; }
.convo {
  display: flex; align-items: center; gap: .3rem;
  padding: .45rem .55rem; border-radius: 8px; cursor: pointer;
  font-size: .9rem; color: var(--text);
}
.convo:hover { background: var(--surface-2); }
.convo.active { background: color-mix(in srgb, var(--accent) 16%, var(--surface)); }
.convo-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convo-del { opacity: 0; color: var(--muted); font-size: 1rem; }
.convo:hover .convo-del, .convo.active .convo-del { opacity: 1; }

.sidebar-foot { border-top: 1px solid var(--border); padding: .7rem .8rem; font-size: .85rem; }
.sidebar-foot summary { cursor: pointer; color: var(--muted); margin-bottom: .4rem; }
.docs-list { max-height: 12rem; overflow-y: auto; font-size: .82rem; color: var(--muted); }
.docs-list div { padding: .25rem 0; border-bottom: 1px solid var(--border); }
.docs-list div:last-child { border-bottom: 0; }
.whoami {
  display: flex; align-items: center; gap: .7rem; margin-top: .7rem;
  color: var(--muted); overflow: hidden;
}
.whoami span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.whoami form { display: contents; }

.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-head {
  display: flex; align-items: center; gap: .7rem;
  padding: .7rem 1rem; border-bottom: 1px solid var(--border); background: var(--surface);
}
.chat-head h1 {
  flex: 1; font-size: 1rem; margin: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.icon-btn {
  display: none; background: none; border: 0; color: var(--text);
  font-size: 1.2rem; cursor: pointer; padding: .2rem .4rem;
}
.mode { display: flex; align-items: center; gap: .35rem; margin: 0; font-size: .85rem; font-weight: 400; }
.mode input { width: auto; margin: 0; }

.messages { flex: 1; overflow-y: auto; padding: 1.2rem 1rem; }
.msg { max-width: 46rem; margin: 0 auto 1.1rem; }
.msg-role { font-size: .78rem; color: var(--muted); margin-bottom: .25rem; }
.msg-body {
  padding: .8rem .95rem; border-radius: 12px; white-space: pre-wrap; overflow-wrap: anywhere;
  background: var(--surface); border: 1px solid var(--border);
}
.msg.user .msg-body { background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.msg.error .msg-body { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }

.sources { margin-top: .5rem; font-size: .85rem; }
.sources-label { color: var(--muted); margin-bottom: .25rem; }
.source-link {
  display: inline-block; margin: 0 .35rem .35rem 0; padding: .2rem .5rem;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 999px; text-decoration: none; color: var(--text); font-size: .82rem;
}
.source-link:hover { border-color: var(--accent); }
.other-sources { margin-top: .3rem; }
.other-sources summary { color: var(--muted); font-size: .82rem; cursor: pointer; margin-bottom: .3rem; }
.latency { color: var(--muted); font-size: .78rem; margin-top: .3rem; }

.fragment {
  margin: 0 auto .7rem; max-width: 46rem; padding: .7rem .9rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.fragment-head { font-size: .82rem; color: var(--muted); margin-bottom: .3rem; }
.fragment-text { font-size: .88rem; white-space: pre-wrap; overflow-wrap: anywhere; }

.empty-state { max-width: 40rem; margin: 3rem auto; text-align: center; color: var(--muted); }
.examples { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; margin-top: 1.2rem; }
.chip {
  padding: .4rem .75rem; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text); font: inherit; font-size: .85rem; cursor: pointer;
}
.chip:hover { border-color: var(--accent); }

.composer {
  display: flex; gap: .6rem; align-items: flex-end;
  padding: .8rem 1rem .3rem; border-top: 1px solid var(--border); background: var(--surface);
}
.composer textarea { resize: none; max-height: 11rem; }
.chat > .disclaimer { padding: 0 1rem .7rem; margin: 0; background: var(--surface); }

.typing { color: var(--muted); font-size: .9rem; }
.typing::after {
  content: "…"; animation: dots 1.2s steps(4, end) infinite;
}
@keyframes dots { 0% { opacity: .3 } 50% { opacity: 1 } 100% { opacity: .3 } }

/* ---------- admin ---------- */
body.admin { padding: 0 0 3rem; }
.admin-head {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .9rem 1.4rem; background: var(--surface); border-bottom: 1px solid var(--border);
}
.admin-head h1 { flex: 1; font-size: 1.1rem; margin: 0; }
.admin-head-right { display: flex; align-items: center; gap: 1rem; }
.admin-head-right form { display: contents; }
.whoami-inline { color: var(--muted); font-size: .9rem; }

.tabs { display: flex; gap: .2rem; flex-wrap: wrap; padding: .6rem 1.4rem 0; border-bottom: 1px solid var(--border); background: var(--surface); }
.tab {
  padding: .5rem .8rem; border: 0; border-bottom: 2px solid transparent;
  background: none; color: var(--muted); font: inherit; cursor: pointer;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.badge {
  display: inline-block; min-width: 1.2rem; padding: 0 .3rem; margin-left: .3rem;
  background: var(--danger); color: #fff; border-radius: 999px;
  font-size: .75rem; text-align: center;
}
/* An author-level `display` beats the UA stylesheet's [hidden] rule, so the
   badge kept showing "0" pending users. Needed on every element whose display
   we set explicitly and also toggle with the hidden attribute. */
.badge[hidden] { display: none; }

.tab-panel { display: none; padding: 1.2rem 1.4rem; max-width: 78rem; }
.tab-panel.active { display: block; }

.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 1.1rem; margin-bottom: 1.1rem;
}
.panel-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1.1rem; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: .8rem; margin-bottom: 1.1rem; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: .9rem 1rem;
}
.stat-value { font-size: 1.5rem; font-weight: 600; }
.stat-label { color: var(--muted); font-size: .82rem; }

.chart { display: flex; align-items: flex-end; gap: .35rem; height: 9rem; }
.bar-wrap { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: .3rem; height: 100%; }
/* The stack's height is set inline as a share of the busiest day, so it must
   NOT also be a flex item that grows — `flex: 1` here made every stack fill the
   full 9rem and flattened all the bars to their 2px minimum. The segments
   inside it are what flex. */
.bar-stack { width: 100%; flex: 0 0 auto; display: flex; flex-direction: column; justify-content: flex-end; }
.bar { width: 100%; flex: 1 1 auto; min-height: 2px; background: var(--accent); border-radius: 3px 3px 0 0; }
.bar.nf { background: var(--warn); border-radius: 3px 3px 0 0; }
/* The "not found" segment is stacked above, so only it keeps a rounded top. */
.bar-stack > .bar:not(:first-child) { border-radius: 0; }
.bar-label { font-size: .7rem; color: var(--muted); }

.terms { display: flex; flex-wrap: wrap; gap: .4rem; }
.term { padding: .25rem .6rem; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; font-size: .85rem; }
.term b { font-weight: 600; color: var(--muted); margin-left: .3rem; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th, td { padding: .5rem .6rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 500; white-space: nowrap; }
td.nowrap, th.nowrap { white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }

.pill { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; border: 1px solid var(--border); }
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--border)); }
.pill.err { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.pill.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, var(--border)); }
.pill.muted { color: var(--muted); }

.upload { display: flex; gap: .7rem; align-items: center; flex-wrap: wrap; }
.upload input[type=file] { flex: 1; min-width: 12rem; }

.logs { display: flex; flex-direction: column; gap: .8rem; margin: .8rem 0; }
.log-entry { border: 1px solid var(--border); border-radius: 10px; padding: .8rem; }
.log-meta { display: flex; gap: .7rem; flex-wrap: wrap; color: var(--muted); font-size: .8rem; margin-bottom: .4rem; }
.log-q { font-weight: 500; margin-bottom: .4rem; }
.log-a { white-space: pre-wrap; overflow-wrap: anywhere; font-size: .88rem; color: var(--muted); }

.empty { color: var(--muted); padding: .6rem 0; }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .panel-row { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .icon-btn { display: block; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 20;
    transform: translateX(-100%); transition: transform .18s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .tab-panel { padding: 1rem; }
}
