/* Bundled locally so the page works offline */
@font-face {
  font-family: "Nunito Sans";
  font-style: normal;
  font-weight: 400 800;
  font-display: swap;
  src: url("assets/fonts/nunito-sans-latin.woff2") format("woff2");
}

/* Airport Mono is a commercial font: drop a licensed airport-mono.woff2 into
   assets/fonts and it takes over. Until then IBM Plex Mono (free, OFL) is used. */
@font-face {
  font-family: "Airport Mono";
  src: local("Airport Mono"), url("assets/fonts/airport-mono.woff2") format("woff2");
  font-display: swap;
}
@font-face {
  font-family: "IBM Plex Mono";
  font-weight: 400;
  font-display: swap;
  src: url("assets/fonts/ibm-plex-mono-400.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Mono";
  font-weight: 600;
  font-display: swap;
  src: url("assets/fonts/ibm-plex-mono-600.woff2") format("woff2");
}

/* Loughborough University palette, taken from lboro.ac.uk */
:root {
  --lboro-purple: #361163;
  --lboro-purple-light: #6f3092;
  --lboro-magenta: #b70062;
  --lboro-pink: #ed2482;
  --bg: #f6f7f7;
  --panel: #ffffff;
  --sidebar: var(--lboro-purple);
  --sidebar-hover: #4a1f7d;
  --text: #1b1b1b;
  --muted: #525e66;
  --border: #cbcece;
  --radius: 14px;
  --mono: "Airport Mono", "IBM Plex Mono", Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Nunito Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, textarea { font: inherit; }

.app { display: flex; height: 100vh; height: 100dvh; }

/* Sidebar */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--sidebar);
  color: #fff;
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  gap: 16px;
}
.brand { padding: 4px 6px; }
.logo-lboro { display: block; width: 180px; height: auto; }

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 6px 0;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
  color: #cdbfe0;
  font-size: 12px;
}
.logo-mlh { height: 26px; width: auto; }

.new-thread {
  background: var(--lboro-magenta);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 11px;
  font-weight: 700;
  cursor: pointer;
}
.new-thread:hover { filter: brightness(1.08); }

.threads { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.thread {
  display: flex;
  align-items: center;
  gap: 6px;
  border-left: 3px solid transparent;
  color: #ddd2ec;
  padding: 6px 6px 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  min-height: 40px;
}
.thread:hover { background: var(--sidebar-hover); color: #fff; }
.thread.active { background: var(--sidebar-hover); color: #fff; border-left-color: var(--lboro-pink); }
.thread-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rename {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: #cdbfe0;
  cursor: pointer;
  flex-shrink: 0;
  opacity: 0;
}
.thread:hover .rename, .thread.active .rename, .rename:focus-visible { opacity: 1; }
.rename:hover { color: #fff; background: rgba(255, 255, 255, 0.12); }
.rename-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  color: inherit;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--lboro-pink);
  border-radius: 6px;
  padding: 4px 8px;
  outline: 0;
}
.chat-header .rename-input { background: var(--bg); border-color: var(--lboro-purple); font-weight: 700; font-size: 16px; }
.chat-header h1 { cursor: text; }

/* Touch screens have no hover, so always show the rename button */
@media (hover: none) { .rename { opacity: 1; } }

/* Chat */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.chat-header h1 { font-size: 16px; margin: 0; font-weight: 700; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
}
/* MLH logo is positioned by placeLogo() in app.js; these transitions do the fly-up */
.chat { position: relative; }
.logo-slot { width: 140px; aspect-ratio: 310.59 / 130.78; }
.logo-mlh-color {
  position: absolute;
  z-index: 5;
  height: auto;
  pointer-events: none;
}
.logo-mlh-color.animate {
  transition: left 0.6s cubic-bezier(0.65, 0, 0.35, 1), top 0.6s cubic-bezier(0.65, 0, 0.35, 1), width 0.6s cubic-bezier(0.65, 0, 0.35, 1);
}
.empty h2 { color: var(--lboro-purple); margin: 18px 0 4px; font-weight: 800; }
.empty p { margin: 0; }

.msg {
  max-width: min(680px, 85%);
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg.user { align-self: flex-end; background: var(--lboro-purple); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.error { background: #fdecef; border-color: #a70531; color: #a70531; }
.msg pre {
  background: #1b1b1b;
  color: #f6f7f7;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre;
  margin: 8px 0;
}
.msg code { font-family: var(--mono); font-size: 0.9em; }
.msg :not(pre) > code { background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.msg.user :not(pre) > code { background: rgba(255, 255, 255, 0.15); }

/* Three-dot typing indicator while waiting for the first token */
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--lboro-purple-light); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; }
.typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.msg img { display: block; max-width: min(360px, 100%); max-height: 360px; border-radius: 8px; }
/* Uploaded photos: a thin purple frame around them (typed quiz answers keep normal padding) */
.msg.user:has(img) { padding: 6px; display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
/* No % max-width here: it can't resolve inside a shrink-to-fit bubble and stretches the frame */
.msg.user img { border-radius: 10px; max-width: min(360px, 70vw); }

/* Assistant output */
.msg.assistant { white-space: normal; }
.msg.assistant.msg-notes { width: min(760px, 92%); max-width: none; }
.stage { display: flex; align-items: center; gap: 10px; color: var(--muted); font-weight: 600; }
.stage.failed { color: #a70531; }

/* Pipeline panel: the chain of tool calls behind a reply */
.trace {
  margin: -2px 0 10px;
  border: 1px solid #e3dcef;
  border-radius: 10px;
  background: #faf8fd;
  font-size: 13px;
}
.trace > summary {
  cursor: pointer;
  padding: 8px 12px;
  color: var(--muted);
  font-weight: 600;
  list-style-position: inside;
}
.trace-title { color: var(--lboro-purple); font-weight: 800; margin-right: 4px; }
.trace ol { list-style: none; margin: 0; padding: 0 10px 10px; }
.step { position: relative; padding-left: 22px; }
/* vertical connector between steps */
.step::before { content: ""; position: absolute; left: 7px; top: 22px; bottom: -4px; width: 2px; background: #e3dcef; }
.step:last-child::before { display: none; }
.step > details > summary {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 8px;
  padding: 6px 4px;
  cursor: pointer;
  list-style: none;
}
.step > details > summary::-webkit-details-marker { display: none; }
.step-dot { position: absolute; left: 2px; top: 11px; width: 12px; height: 12px; border-radius: 50%; background: #008466; }
.step.running .step-dot { background: #e77e1d; animation: blink 1.2s infinite; }
.step.error .step-dot { background: #a70531; }
.step-num { color: var(--muted); font-family: var(--mono); font-size: 12px; }
.step-label { font-weight: 700; color: var(--text); }
.step-tool {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--lboro-purple);
  background: #efe9f7;
  padding: 2px 6px;
  border-radius: 4px;
  white-space: nowrap;
}
.step-time { margin-left: auto; color: var(--muted); font-family: var(--mono); font-size: 12px; white-space: nowrap; }
.step.error .step-time { color: #a70531; }
.step-io { margin: 4px 0 2px; font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.step pre { margin: 0 0 8px; max-height: 240px; overflow: auto; font-size: 11.5px; white-space: pre-wrap; word-break: break-word; }

/* Digitised notes: a clean "page" */
.notes {
  background: #fff;
  border-radius: 10px;
  padding: 24px 28px;
  margin-top: 10px;
  line-height: 1.6;
}
.stage + .notes { margin-top: 12px; }
.notes h2 { margin: 0 0 12px; font-size: 22px; color: var(--lboro-purple); }
.notes h3 { margin: 18px 0 6px; font-size: 17px; color: var(--lboro-purple); }
.notes p { margin: 0 0 10px; }
.notes ul { margin: 0 0 10px; padding-left: 22px; }
.notes li { margin: 2px 0; }
.math-inline { font-family: cmmi10, "Cambria Math", serif; font-size: 1.08em; }
.math-block, .notes .diagram { margin: 12px 0; display: grid; place-items: center; }
/* SVGs have width/height stripped: they scale from their viewBox, capped in sanitizeSvg() */
.math-block svg, .notes .diagram svg { width: 100%; height: auto; max-height: 420px; }
.notes .diagram { padding: 8px; border: 1px solid #ecebf2; border-radius: 8px; }
.notes .pending { color: var(--muted); font-style: italic; padding: 12px; }
.notes .failed { place-items: stretch; color: #a70531; font-size: 14px; }
.notes .failed p { margin: 0 0 6px; }
.notes .failed pre { max-height: 200px; overflow: auto; font-size: 12px; margin: 0; }

.notes-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.notes-actions span { color: var(--muted); font-weight: 600; margin-right: auto; }
.notes-actions button {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--lboro-purple);
  font-weight: 700;
  font-size: 14px;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
}
.notes-actions button:hover { border-color: var(--lboro-purple-light); }
.notes-actions button.primary { background: var(--lboro-magenta); border-color: var(--lboro-magenta); color: #fff; }
.notes-actions button.primary:hover { filter: brightness(1.1); }

/* Quiz answer box (replaces the upload row while quizzing) */
.quiz-row textarea {
  flex: 1;
  min-width: 0;
  height: 72px;
  resize: none;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: inherit;
  font-family: var(--mono);
  font-size: 15px;
  outline: 0;
}
.quiz-row textarea:focus { border-color: var(--lboro-purple); }
.answer-send {
  width: 96px;
  flex-shrink: 0;
  border: 0;
  border-radius: var(--radius);
  background: var(--lboro-magenta);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}
.answer-send:disabled, .quiz-row textarea:disabled { opacity: 0.55; cursor: progress; }
.quiz-hint {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: min(760px, 100%);
  margin: 6px auto 0;
  font-size: 12px;
  color: var(--muted);
}
.end-quiz { border: 0; background: none; color: var(--lboro-magenta); font-weight: 700; font-size: 12px; cursor: pointer; }
.end-quiz:hover { text-decoration: underline; }

/* Composer: image drop zone */
.composer { padding: 12px 24px 20px; }
.composer-row { display: flex; gap: 10px; width: min(760px, 100%); margin: 0 auto; }
.dropzone {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
  height: 96px;
  padding: 0 12px;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.dropzone strong { color: var(--lboro-magenta); }
.dropzone:hover, .chat.dragging .dropzone { border-color: var(--lboro-purple-light); color: var(--lboro-purple); }
.dropzone.busy { cursor: progress; border-style: solid; pointer-events: none; }
.dropzone.busy svg { animation: blink 1.2s infinite; }

/* Camera button beside the drop zone */
.camera-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: 96px;
  height: 96px;
  flex-shrink: 0;
  border: 0;
  border-radius: var(--radius);
  background: var(--lboro-magenta);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}
.camera-btn:hover { filter: brightness(1.1); }
.camera-btn:disabled { opacity: 0.5; cursor: progress; }

/* Camera preview dialog */
.camera-dialog {
  width: min(720px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: #111;
  color: #fff;
  overflow: hidden;
}
.camera-dialog::backdrop { background: rgba(27, 10, 50, 0.75); }
.camera-dialog video { display: block; width: 100%; max-height: 70vh; background: #000; object-fit: contain; }
.camera-error { margin: 0; padding: 24px; text-align: center; }
.camera-controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 14px 18px; gap: 12px; }
.camera-controls .camera-secondary:first-child { justify-self: start; }
.camera-controls .camera-secondary:last-child { justify-self: end; }
.camera-secondary {
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: none;
  color: #fff;
  font-weight: 700;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
}
.camera-secondary[hidden] { display: none !important; }
.camera-shutter {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 4px solid #fff;
  background: var(--lboro-magenta);
  box-shadow: inset 0 0 0 3px #111;
  cursor: pointer;
}
.camera-shutter:hover { filter: brightness(1.15); }

/* Shown over the whole chat area while a file is dragged over it */
.drop-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: rgba(54, 17, 99, 0.82);
  border: 3px dashed var(--lboro-pink);
  border-radius: 6px;
  color: #fff;
  font-size: 22px;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
.chat.dragging .drop-overlay { opacity: 1; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg); color: var(--lboro-purple); }
.menu { display: none; }

/* Mobile: sidebar slides over the chat */
@media (max-width: 760px) {
  .menu { display: grid; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  .sidebar.open { transform: none; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.4); }
  .messages, .composer { padding-left: 16px; padding-right: 16px; }
  .chat-header { padding: 12px 16px; }
}
