/* ============================================================
   Professor Perseid — vintage celestial-atlas chatbot
   Direction: observatory at night (dark) / antique star chart (light)
   Fraunces (Professor's voice, display) + Hanken Grotesk (UI)
   ============================================================ */

:root {
  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --font-ui: "Hanken Grotesk", system-ui, sans-serif;

  --sidebar-w: 288px;
  --radius: 16px;
  --radius-sm: 11px;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

/* ---------- THEMES ---------- */
html[data-theme="dark"] {
  --bg:          #070b1a;
  --bg-2:        #0c1226;
  --void:        #05070f;
  --surface:     rgba(20, 27, 52, 0.72);
  --surface-2:   rgba(28, 37, 68, 0.62);
  --surface-solid:#0e1430;
  --line:        rgba(210, 200, 170, 0.13);
  --line-strong: rgba(214, 178, 100, 0.34);

  --ink:         #f3ecd9;
  --ink-soft:    #c3c6d6;
  --ink-mute:    #838aa3;

  --brass:       #e8bd5e;
  --brass-deep:  #c8963a;
  --brass-glow:  rgba(232, 189, 94, 0.5);

  --user-bg:     linear-gradient(180deg, rgba(232,189,94,0.16), rgba(232,189,94,0.10));
  --user-line:   rgba(232, 189, 94, 0.30);
  --prof-bg:     linear-gradient(180deg, rgba(22,30,58,0.66), rgba(16,22,44,0.66));

  --star:        #f5efe0;
  --shadow:      0 18px 50px -22px rgba(0, 0, 0, 0.85);
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg:          #f3e9d2;
  --bg-2:        #efe2c4;
  --void:        #ece0c1;
  --surface:     rgba(255, 250, 238, 0.82);
  --surface-2:   rgba(249, 240, 220, 0.9);
  --surface-solid:#faf3e2;
  --line:        rgba(74, 55, 28, 0.16);
  --line-strong: rgba(150, 106, 40, 0.4);

  --ink:         #241d10;
  --ink-soft:    #4c4230;
  --ink-mute:    #857a5f;

  --brass:       #9a6a1c;
  --brass-deep:  #7e5410;
  --brass-glow:  rgba(154, 106, 28, 0.28);

  --user-bg:     linear-gradient(180deg, rgba(154,106,28,0.15), rgba(154,106,28,0.09));
  --user-line:   rgba(154, 106, 28, 0.34);
  --prof-bg:     linear-gradient(180deg, rgba(255,251,240,0.9), rgba(247,238,216,0.9));

  --star:        #7e5410;
  --shadow:      0 16px 40px -24px rgba(80, 55, 15, 0.5);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
}

#starfield {
  position: fixed;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  display: block;
  background:
    radial-gradient(120% 80% at 78% -10%, var(--bg-2) 0%, var(--bg) 46%, var(--void) 100%);
}

button { font-family: inherit; cursor: pointer; }

/* focus */
:focus-visible {
  outline: 2px solid var(--brass);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- APP SHELL ---------- */
.app {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100dvh;
}

/* ---------- SIDEBAR ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 18px 14px 14px;
  gap: 14px;
  background: linear-gradient(180deg, rgba(9,13,30,0.55), rgba(6,9,20,0.4));
  border-right: 1px solid var(--line);
  backdrop-filter: blur(10px);
}
html[data-theme="light"] .sidebar {
  background: linear-gradient(180deg, rgba(240,229,203,0.7), rgba(236,224,196,0.55));
}

.sidebar__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  text-decoration: none;
  color: var(--ink);
}
.brand__seal {
  display: grid;
  place-items: center;
  width: 44px; height: 44px;
  color: var(--brass);
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: radial-gradient(circle at 40% 30%, rgba(232,189,94,0.16), transparent 70%);
  box-shadow: inset 0 0 14px -4px var(--brass-glow);
  flex-shrink: 0;
}
.brand__text { display: flex; flex-direction: column; line-height: 1.1; }
.brand__name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.06rem;
  letter-spacing: 0.01em;
}
.brand__role {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--brass);
  margin-top: 3px;
}

.new-chat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 12px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--void);
  background: linear-gradient(180deg, var(--brass), var(--brass-deep));
  border: none;
  border-radius: var(--radius-sm);
  box-shadow: 0 6px 18px -8px var(--brass-glow);
  transition: transform 0.15s var(--ease), box-shadow 0.2s var(--ease), filter 0.2s var(--ease);
}
html[data-theme="light"] .new-chat { color: #fdf6e6; }
.new-chat svg { stroke: currentColor; stroke-width: 2.4; fill: none; stroke-linecap: round; }
.new-chat:hover { transform: translateY(-1px); box-shadow: 0 10px 24px -8px var(--brass-glow); filter: brightness(1.05); }
.new-chat:active { transform: translateY(0); }

.sessions {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin: 0 -4px;
  padding: 2px 4px;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}
.sessions::-webkit-scrollbar { width: 7px; }
.sessions::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 20px; }

.sessions__label {
  font-size: 0.66rem;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--ink-mute);
  padding: 8px 8px 4px;
}

.session {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 8px 9px 11px;
  border-radius: var(--radius-sm);
  color: var(--ink-soft);
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s var(--ease), border-color 0.15s var(--ease), color 0.15s var(--ease);
}
.session:hover { background: var(--surface-2); color: var(--ink); }
.session.is-active {
  background: var(--surface);
  border-color: var(--line-strong);
  color: var(--ink);
}
.session__dot { color: var(--brass); flex-shrink: 0; display: grid; place-items: center; }
.session__dot svg { width: 13px; height: 13px; }
.session__title {
  flex: 1;
  min-width: 0;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session__title-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 0.875rem;
  color: var(--ink);
  background: var(--void);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 3px 6px;
}
.session__actions {
  display: flex;
  gap: 1px;
  opacity: 0;
  transition: opacity 0.15s var(--ease);
}
.session:hover .session__actions,
.session.is-active .session__actions { opacity: 1; }
.session__act {
  display: grid;
  place-items: center;
  width: 26px; height: 26px;
  border: none;
  background: transparent;
  color: var(--ink-mute);
  border-radius: 7px;
  transition: color 0.15s, background 0.15s;
}
.session__act svg { width: 15px; height: 15px; stroke: currentColor; stroke-width: 1.9; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.session__act:hover { color: var(--ink); background: var(--surface-2); }
.session__act.danger:hover { color: #e8795e; }

.sessions__empty {
  padding: 16px 10px;
  font-size: 0.82rem;
  color: var(--ink-mute);
  line-height: 1.5;
}

.sidebar__foot {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* theme toggle */
.theme-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 6px;
  background: transparent;
  border: none;
  color: var(--ink-soft);
}
.theme-toggle__track {
  position: relative;
  width: 46px; height: 25px;
  border-radius: 20px;
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  flex-shrink: 0;
  transition: background 0.2s;
}
.theme-toggle__thumb {
  position: absolute;
  top: 2px; left: 2px;
  width: 19px; height: 19px;
  border-radius: 50%;
  background: linear-gradient(180deg, var(--brass), var(--brass-deep));
  box-shadow: 0 2px 6px -1px var(--brass-glow);
  transition: transform 0.28s var(--ease);
}
html[data-theme="light"] .theme-toggle__thumb { transform: translateX(21px); }
.theme-toggle__labels { position: relative; font-size: 0.8rem; font-weight: 500; }
.theme-toggle__labels .tl-sun { display: none; }
html[data-theme="light"] .theme-toggle__labels .tl-moon { display: none; }
html[data-theme="light"] .theme-toggle__labels .tl-sun { display: inline; }

.colophon {
  margin: 0;
  font-size: 0.72rem;
  line-height: 1.5;
  color: var(--ink-mute);
  padding: 0 6px;
}

/* ---------- MAIN ---------- */
.main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(7,11,26,0.7), rgba(7,11,26,0.3));
  backdrop-filter: blur(8px);
}
html[data-theme="light"] .topbar { background: linear-gradient(180deg, rgba(243,233,210,0.8), rgba(243,233,210,0.4)); }
.topbar__title {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.05rem;
}
.topbar__seal { color: var(--brass); display: grid; place-items: center; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px; height: 40px;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  border-radius: 10px;
  transition: background 0.15s, color 0.15s;
}
.icon-btn svg { stroke: currentColor; stroke-width: 2; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

/* ---------- THREAD ---------- */
.thread-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}
.thread-wrap::-webkit-scrollbar { width: 9px; }
.thread-wrap::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 20px; }

.thread {
  max-width: 760px;
  margin: 0 auto;
  padding: 30px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 26px;
}

/* messages */
.msg { display: flex; flex-direction: column; animation: rise 0.4s var(--ease) both; }
@keyframes rise { from { opacity: 0; transform: translateY(9px); } to { opacity: 1; transform: none; } }

.msg--user { align-items: flex-end; }
.msg--user .bubble {
  max-width: 84%;
  padding: 12px 16px;
  background: var(--user-bg);
  border: 1px solid var(--user-line);
  border-radius: 18px 18px 5px 18px;
  color: var(--ink);
  font-size: 1rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg--prof .prof-head {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 9px;
}
.prof-head__seal {
  display: grid; place-items: center;
  width: 30px; height: 30px;
  color: var(--brass);
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: radial-gradient(circle at 40% 30%, rgba(232,189,94,0.18), transparent 70%);
  flex-shrink: 0;
}
.prof-head__name {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.94rem;
  letter-spacing: 0.01em;
}
.prof-head__name em { color: var(--brass); font-style: normal; }

.msg--prof .bubble {
  padding: 14px 18px 15px;
  background: var(--prof-bg);
  border: 1px solid var(--line);
  border-left: 2.5px solid var(--brass);
  border-radius: 6px 16px 16px 16px;
  font-family: var(--font-display);
  font-size: 1.075rem;
  line-height: 1.66;
  color: var(--ink);
  box-shadow: var(--shadow);
}

/* markdown inside professor bubble */
.bubble p { margin: 0 0 0.75em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble strong { color: var(--brass); font-weight: 600; }
.bubble em { font-style: italic; }
.bubble ul, .bubble ol { margin: 0.4em 0 0.75em; padding-left: 1.35em; }
.bubble li { margin: 0.28em 0; }
.bubble li::marker { color: var(--brass); }
.bubble h1, .bubble h2, .bubble h3 {
  font-weight: 600;
  line-height: 1.25;
  margin: 0.6em 0 0.4em;
}
.bubble h1 { font-size: 1.3em; }
.bubble h2 { font-size: 1.18em; }
.bubble h3 { font-size: 1.06em; }
.bubble a { color: var(--brass); text-decoration: underline; text-underline-offset: 2px; }
.bubble a:hover { color: var(--brass-deep); }
.bubble code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.86em;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
}
.bubble pre {
  background: var(--void);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0.5em 0;
}
.bubble pre code { background: none; border: none; padding: 0; font-size: 0.85em; }
.bubble blockquote {
  margin: 0.5em 0;
  padding: 0.2em 0 0.2em 0.9em;
  border-left: 2px solid var(--line-strong);
  color: var(--ink-soft);
  font-style: italic;
}
.bubble hr { border: none; border-top: 1px solid var(--line); margin: 0.9em 0; }

/* streaming caret */
.bubble.is-streaming > *:last-child::after {
  content: "";
  display: inline-block;
  width: 0.5em; height: 1.05em;
  margin-left: 2px;
  vertical-align: -0.15em;
  background: var(--brass);
  border-radius: 1px;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* message actions */
.msg__actions {
  display: flex;
  gap: 2px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity 0.18s var(--ease);
}
.msg--prof:hover .msg__actions,
.msg__actions.is-pinned { opacity: 1; }
.msg-act {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 9px;
  font-size: 0.76rem;
  font-weight: 500;
  color: var(--ink-mute);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
}
.msg-act svg { width: 14px; height: 14px; stroke: currentColor; stroke-width: 1.8; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.msg-act:hover { color: var(--ink); background: var(--surface-2); border-color: var(--line); }

/* typing dots (before first token) */
.typing { display: inline-flex; gap: 5px; padding: 4px 2px; }
.typing span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--brass);
  animation: bob 1.2s var(--ease) infinite;
}
.typing span:nth-child(2) { animation-delay: 0.18s; }
.typing span:nth-child(3) { animation-delay: 0.36s; }
@keyframes bob { 0%, 60%, 100% { transform: translateY(0); opacity: 0.45; } 30% { transform: translateY(-5px); opacity: 1; } }

/* in-thread notice */
.notice {
  align-self: center;
  max-width: 90%;
  padding: 11px 16px;
  font-size: 0.86rem;
  color: var(--ink-soft);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  text-align: center;
  line-height: 1.5;
}

/* ---------- EMPTY STATE ---------- */
.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: min(9vh, 70px) 8px 24px;
  animation: rise 0.5s var(--ease) both;
}
.hero__seal {
  width: 86px; height: 86px;
  display: grid; place-items: center;
  color: var(--brass);
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: radial-gradient(circle at 40% 32%, rgba(232,189,94,0.2), transparent 68%);
  box-shadow: inset 0 0 26px -8px var(--brass-glow), 0 0 40px -14px var(--brass-glow);
  margin-bottom: 22px;
}
.hero__seal svg { width: 56px; height: 56px; }
.hero__eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  color: var(--brass);
  margin-bottom: 12px;
}
.hero__title {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: clamp(1.9rem, 5vw, 2.9rem);
  line-height: 1.08;
  letter-spacing: -0.01em;
  margin: 0 0 14px;
  max-width: 16ch;
}
.hero__lede {
  font-family: var(--font-display);
  font-size: 1.08rem;
  font-style: italic;
  line-height: 1.6;
  color: var(--ink-soft);
  max-width: 46ch;
  margin: 0 0 30px;
}
.prompts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 11px;
  width: 100%;
  max-width: 560px;
}
.prompt-chip {
  text-align: left;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  transition: transform 0.15s var(--ease), border-color 0.18s var(--ease), background 0.18s var(--ease);
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.prompt-chip:hover {
  transform: translateY(-2px);
  border-color: var(--line-strong);
  background: var(--surface-2);
}
.prompt-chip__k {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.13em;
  color: var(--brass);
}
.prompt-chip__t { font-size: 0.94rem; line-height: 1.4; }

/* ---------- JUMP TO LATEST ---------- */
.jump-latest {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 14px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink);
  background: var(--surface-solid);
  border: 1px solid var(--line-strong);
  border-radius: 20px;
  box-shadow: var(--shadow);
  z-index: 5;
}
.jump-latest[hidden] { display: none; }
.jump-latest svg { stroke: var(--brass); stroke-width: 2.2; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.jump-latest:hover { border-color: var(--brass); }

/* ---------- COMPOSER ---------- */
.composer-wrap {
  padding: 6px 18px 16px;
  background: linear-gradient(180deg, transparent, var(--bg) 42%);
}
.composer {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 8px;
  background: var(--surface-solid);
  border: 1px solid var(--line-strong);
  border-radius: 20px;
  box-shadow: var(--shadow);
  transition: border-color 0.2s var(--ease);
}
.composer:focus-within { border-color: var(--brass); }
.composer__input {
  flex: 1;
  min-height: 26px;
  max-height: 190px;
  padding: 8px 6px 8px 12px;
  font-family: var(--font-ui);
  font-size: 1rem;
  line-height: 1.5;
  color: var(--ink);
  background: transparent;
  border: none;
  resize: none;
  outline: none;
}
.composer__input::placeholder { color: var(--ink-mute); }

.send-btn {
  display: grid;
  place-items: center;
  width: 44px; height: 44px;
  flex-shrink: 0;
  border: none;
  border-radius: 13px;
  color: var(--void);
  background: linear-gradient(180deg, var(--brass), var(--brass-deep));
  box-shadow: 0 5px 14px -6px var(--brass-glow);
  transition: transform 0.14s var(--ease), filter 0.18s var(--ease), opacity 0.18s;
}
html[data-theme="light"] .send-btn { color: #fdf6e6; }
.send-btn .ic-send { fill: none; stroke: currentColor; stroke-width: 2.3; stroke-linecap: round; stroke-linejoin: round; }
.send-btn .ic-stop { display: none; fill: currentColor; }
.send-btn:not(:disabled):hover { transform: translateY(-1px); filter: brightness(1.06); }
.send-btn:disabled { opacity: 0.4; cursor: default; }
body.is-streaming .send-btn { opacity: 1; cursor: pointer; }
body.is-streaming .send-btn .ic-send { display: none; }
body.is-streaming .send-btn .ic-stop { display: block; fill: currentColor; }

.composer__hint {
  max-width: 760px;
  margin: 8px auto 0;
  text-align: center;
  font-size: 0.74rem;
  color: var(--ink-mute);
  line-height: 1.4;
}

/* ---------- SCRIM (mobile) ---------- */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(3, 5, 12, 0.6);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity 0.25s var(--ease);
}
.scrim.is-open { opacity: 1; }

.sidebar__close { display: none; }

/* ---------- RESPONSIVE ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .topbar { display: flex; }
  .sidebar__close { display: grid; }

  .sidebar {
    position: fixed;
    z-index: 50;
    top: 0; left: 0; bottom: 0;
    width: min(86vw, 320px);
    transform: translateX(-102%);
    transition: transform 0.3s var(--ease);
    backdrop-filter: blur(18px);
    background: linear-gradient(180deg, rgba(9,13,30,0.96), rgba(6,9,20,0.96));
  }
  html[data-theme="light"] .sidebar {
    background: linear-gradient(180deg, rgba(243,233,210,0.98), rgba(236,224,196,0.98));
  }
  .sidebar.is-open { transform: none; box-shadow: 0 0 60px -10px rgba(0,0,0,0.7); }

  .thread { padding: 22px 16px 16px; gap: 22px; }
  .composer-wrap { padding: 4px 12px 12px; }
  .prompts { grid-template-columns: 1fr; }
  .msg--user .bubble { max-width: 90%; }
  .msg--prof .bubble { font-size: 1.02rem; }
  .hero { padding-top: 6vh; }
}

@media (min-width: 821px) {
  .scrim { display: none !important; }
}

/* ---------- MOTION ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
