/* ── Design tokens ──────────────────────────────────────────────────────── */

:root {
  --bg:             #F8F7F4;
  --surface:        #FFFFFF;
  --border:         #E6E3DC;
  --border-subtle:  #EDEAE4;

  --text:           #18171A;
  --text-muted:     #6B6760;
  --text-faint:     #A09C97;

  --accent:         #2563EB;
  --accent-light:   #EFF6FF;
  --accent-fg:      #FFFFFF;

  --user-bg:        #F2F0EB;
  --user-border:    #E0DDD6;

  --danger:         #B91C1C;
  --success:        #166534;

  --r-sm:    4px;
  --r-md:    8px;
  --r-lg:    12px;
  --r-full:  999px;

  --shadow-sm: 0 1px 3px rgba(0,0,0,.07), 0 0 0 1px rgba(0,0,0,.04);
  --shadow-md: 0 4px 20px rgba(0,0,0,.10);
  --shadow-lg: 0 16px 48px rgba(0,0,0,.14);

  --navbar-h:    3.5rem;
  --inputbar-h:  5.25rem;
  --col-w:       760px;
  --col-pad:     1.5rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:             #111010;
    --surface:        #1B1A18;
    --border:         #2A2825;
    --border-subtle:  #222120;

    --text:           #F0EDE8;
    --text-muted:     #857F79;
    --text-faint:     #524E4A;

    --accent:         #60A5FA;
    --accent-light:   #1E3A5F;
    --accent-fg:      #FFFFFF;

    --user-bg:        #1F1E1B;
    --user-border:    #2E2C28;

    --shadow-sm: 0 1px 3px rgba(0,0,0,.4), 0 0 0 1px rgba(255,255,255,.04);
    --shadow-md: 0 4px 20px rgba(0,0,0,.4);
    --shadow-lg: 0 16px 48px rgba(0,0,0,.5);
  }
}


/* ── Reset / base ───────────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-feature-settings: "cv02", "cv03", "cv04", "cv11";
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

body { display: flex; flex-direction: column; overflow: hidden; }


/* ── Login ──────────────────────────────────────────────────────────────── */

.auth-page {
  min-height: 100%;
  overflow: auto;
  display: block;
}

.auth-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.auth-panel {
  width: min(100%, 360px);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-md);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  margin-bottom: 0.25rem;
}

.auth-brand h1 {
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1.2;
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  color: var(--text-muted);
  font-size: 0.8125rem;
  font-weight: 500;
}

.auth-field input {
  width: 100%;
  height: 2.4rem;
  padding: 0 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  outline: none;
}

.auth-field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

.auth-error {
  padding: 0.65rem 0.75rem;
  border: 1px solid color-mix(in srgb, var(--danger) 30%, var(--border));
  border-radius: var(--r-md);
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 8%, transparent);
  font-size: 0.85rem;
}


/* ── Navbar ─────────────────────────────────────────────────────────────── */

.navbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--navbar-h);
  padding: 0 var(--col-pad);
  background: rgba(248, 247, 244, 0.9);
  backdrop-filter: blur(20px) saturate(1.3);
  -webkit-backdrop-filter: blur(20px) saturate(1.3);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

@media (prefers-color-scheme: dark) {
  .navbar { background: rgba(17, 16, 16, 0.9); }
}

.navbar__brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.navbar__logo {
  width: 26px;
  height: 26px;
  color: var(--accent);
  flex-shrink: 0;
}

.navbar__title {
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: var(--text);
}

.navbar__actions { display: flex; align-items: center; gap: 0.4rem; }


/* ── Buttons ────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.42rem 0.8rem;
  border: none;
  border-radius: var(--r-md);
  font-family: inherit;
  font-size: 0.8125rem;
  font-weight: 500;
  letter-spacing: 0.005em;
  cursor: pointer;
  transition: background 0.12s, opacity 0.12s, box-shadow 0.12s, transform 0.08s;
  white-space: nowrap;
  line-height: 1;
  text-decoration: none;
}

.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.35; cursor: not-allowed; pointer-events: none; }

.btn--primary {
  background: var(--accent);
  color: var(--accent-fg);
}
.btn--primary:not(:disabled):hover { opacity: 0.88; }

.btn--ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.btn--ghost:not(:disabled):hover {
  background: var(--border-subtle);
  color: var(--text);
}

.btn--send {
  flex-shrink: 0;
  width: 2.1rem;
  height: 2.1rem;
  padding: 0;
  border-radius: var(--r-full);
  background: var(--accent);
  color: var(--accent-fg);
}
.btn--send:not(:disabled):hover { opacity: 0.85; }
.btn--send:disabled {
  background: var(--border);
  opacity: 1;
  pointer-events: none;
}

.btn--full { width: 100%; justify-content: center; }


/* ── Chat window ────────────────────────────────────────────────────────── */

.chat-window {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 2.25rem var(--col-pad) calc(var(--inputbar-h) + 1.5rem);
  display: flex;
  flex-direction: column;
  max-width: var(--col-w);
  width: 100%;
  margin-inline: auto;
}

.chat-window::-webkit-scrollbar { width: 4px; }
.chat-window::-webkit-scrollbar-track { background: transparent; }
.chat-window::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: var(--r-full);
}


/* ── Message rows ───────────────────────────────────────────────────────── */

.msg {
  display: flex;
  flex-direction: column;
  width: 100%;
  animation: msgIn 0.18s cubic-bezier(0.22, 1, 0.36, 1) both;
}

/* Separate messages by a thin rule */
.msg + .msg {
  margin-top: 0;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border-subtle);
}

/* System notices don't need the full separator treatment */
.msg--system + .msg,
.msg + .msg--system { border-top: none; }

@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.msg--user   { align-items: flex-start; padding-left: 2.5rem; }
.msg--bot    { align-items: flex-start; }
.msg--system { align-items: center; padding-block: 0.75rem; }


/* ── Content blocks ─────────────────────────────────────────────────────── */

.bubble {
  width: 100%;
  padding: 0.85rem 1.1rem;
  word-break: break-word;
  font-size: 0.9375rem;
  line-height: 1.7;
  border-radius: var(--r-md);
}

/* User: warm tinted block */
.msg--user .bubble {
  background: var(--user-bg);
  border: 1px solid var(--user-border);
  border-right: 3px solid var(--text-muted);
  color: var(--text);
  white-space: pre-wrap;
}

/* Bot: white with a left accent bar */
.msg--bot .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* System: small centred notice */
.msg--system .bubble {
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0.2rem 0.75rem;
  font-size: 0.8rem;
  color: var(--text-faint);
  text-align: center;
  font-style: italic;
  white-space: pre-wrap;
  width: auto;
}


/* ── Markdown inside bot bubbles ────────────────────────────────────────── */

.bubble--md { white-space: normal; }

.bubble--md p               { margin: 0 0 0.7em; }
.bubble--md p:last-child    { margin-bottom: 0; }

.bubble--md h1,
.bubble--md h2,
.bubble--md h3,
.bubble--md h4              { font-weight: 600; margin: 1.1em 0 0.4em; line-height: 1.3; letter-spacing: -0.01em; }
.bubble--md h1              { font-size: 1.1em; }
.bubble--md h2              { font-size: 1.02em; }
.bubble--md h3,
.bubble--md h4              { font-size: 0.95em; }

.bubble--md ul,
.bubble--md ol              { margin: 0.4em 0 0.7em 1.4em; padding: 0; }
.bubble--md li              { margin-bottom: 0.3em; }

.bubble--md strong          { font-weight: 600; }
.bubble--md em              { font-style: italic; }

.bubble--md code            {
  font-family: "JetBrains Mono", "Fira Code", ui-monospace, monospace;
  font-size: 0.84em;
  background: rgba(0,0,0,.05);
  padding: 0.15em 0.4em;
  border-radius: 3px;
}
@media (prefers-color-scheme: dark) {
  .bubble--md code { background: rgba(255,255,255,.09); }
}

.bubble--md pre {
  background: rgba(0,0,0,.04);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 0.9em 1.1em;
  overflow-x: auto;
  margin: 0.7em 0;
}
@media (prefers-color-scheme: dark) {
  .bubble--md pre { background: rgba(0,0,0,.35); }
}
.bubble--md pre code { background: none; padding: 0; }

.bubble--md blockquote {
  border-left: 3px solid var(--accent);
  margin: 0.65em 0;
  padding: 0.3em 1em;
  color: var(--text-muted);
  font-style: italic;
}

.bubble--md table           { border-collapse: collapse; width: 100%; margin: 0.7em 0; font-size: 0.9em; }
.bubble--md th,
.bubble--md td              { border: 1px solid var(--border); padding: 0.45em 0.75em; text-align: left; }
.bubble--md th              { background: rgba(0,0,0,.04); font-weight: 600; }
@media (prefers-color-scheme: dark) {
  .bubble--md th            { background: rgba(255,255,255,.05); }
}

.bubble--md hr              { border: none; border-top: 1px solid var(--border); margin: 1em 0; }
.bubble--md a               { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }


/* ── Typing cursor ──────────────────────────────────────────────────────── */

.cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  border-radius: 1px;
  animation: blink 1s step-end infinite;
}

@keyframes blink { 50% { opacity: 0; } }


/* ── Sources section ────────────────────────────────────────────────────── */

.sources {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  margin-top: 0.75rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border-subtle);
}

.sources__label {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-right: 0.15rem;
  flex-shrink: 0;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.55rem;
  background: var(--accent-light);
  color: var(--accent);
  border-radius: var(--r-full);
  font-size: 0.7rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  white-space: nowrap;
  max-width: 26ch;
  overflow: hidden;
  text-overflow: ellipsis;
  border: 1px solid rgba(37,99,235,.15);
  text-decoration: none;
  transition: background 0.12s, border-color 0.12s;
}

a.pill:hover {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
}

@media (prefers-color-scheme: dark) {
  .pill { border-color: rgba(37,99,235,.2); }
}


/* ── Input bar ──────────────────────────────────────────────────────────── */

.input-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 0.9rem var(--col-pad) 1.1rem;
  background: linear-gradient(to top, var(--bg) 55%, transparent);
  z-index: 90;
  flex-shrink: 0;
}

.input-bar__form {
  max-width: var(--col-w);
  margin: 0 auto;
}

.input-bar__inner {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 0.55rem 0.55rem 0.55rem 1rem;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.input-bar__inner:focus-within {
  border-color: var(--accent);
  box-shadow: var(--shadow-sm), 0 0 0 3px rgba(37,99,235,.1);
}

.input-bar__textarea {
  flex: 1;
  resize: none;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.9375rem;
  line-height: 1.5;
  max-height: 9rem;
  overflow-y: auto;
  outline: none;
  padding: 0.2rem 0;
}

.input-bar__textarea::placeholder { color: var(--text-faint); }

@supports (-webkit-touch-callout: none) {
  input,
  textarea,
  select {
    font-size: 16px;
  }
}


/* ── Modal ──────────────────────────────────────────────────────────────── */

.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal[hidden] { display: none; }

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal__box {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  width: min(460px, 100%);
  display: flex;
  flex-direction: column;
  animation: modalIn 0.2s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.modal__box--wide {
  width: min(720px, 100%);
  max-height: min(80vh, 720px);
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.97) translateY(6px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-subtle);
}

.modal__header-left {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--accent);
}

.modal__title {
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}

.modal__close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.9rem;
  height: 1.9rem;
  background: none;
  border: none;
  border-radius: var(--r-sm);
  cursor: pointer;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}
.modal__close:hover { background: var(--border-subtle); color: var(--text); }

.modal__body {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.modal__footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border-subtle);
}

.prompt-list {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  max-height: 58vh;
  overflow-y: auto;
  padding-right: 0.25rem;
}

.prompt-row {
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-md);
  background: var(--bg);
}

.prompt-row__meta {
  margin-bottom: 0.35rem;
  color: var(--text-faint);
  font-size: 0.72rem;
  font-weight: 600;
}

.prompt-row__text {
  color: var(--text);
  font-size: 0.875rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.user-form {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
}

.user-form__grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 0.75rem;
  flex: 1;
}

.user-form__field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
}

.user-form__field input {
  min-height: 2.45rem;
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-sm);
  padding: 0 0.75rem;
  color: var(--text);
  background: var(--bg);
  font: inherit;
  font-size: 16px;
  outline: none;
}

.user-form__field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.user-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-md);
  background: var(--bg);
}

.user-row__name {
  color: var(--text);
  font-size: 0.875rem;
  font-weight: 600;
  word-break: break-word;
}

.user-row__actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

.btn--danger {
  color: var(--danger);
}


/* ── File drop zone ─────────────────────────────────────────────────────── */

.file-drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
  padding: 2.25rem 1.5rem;
  border: 1.5px dashed var(--border);
  border-radius: var(--r-md);
  cursor: pointer;
  text-align: center;
  transition: border-color 0.15s, background 0.15s;
}

.file-drop:hover,
.file-drop.drag-over {
  border-color: var(--accent);
  background: var(--accent-light);
}

.file-drop__icon-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: var(--r-md);
  background: var(--accent-light);
  color: var(--accent);
}

.file-drop__text  { font-size: 0.875rem; color: var(--text-muted); }
.file-drop__link  { color: var(--accent); font-weight: 500; }
.file-drop__name  { font-size: 0.8125rem; font-weight: 500; color: var(--accent); min-height: 1.1em; }


/* ── Upload status ──────────────────────────────────────────────────────── */

.upload-status {
  font-size: 0.8125rem;
  padding: 0.65rem 0.9rem;
  border-radius: var(--r-md);
  text-align: center;
  font-weight: 500;
}

.upload-status.success {
  background: #F0FDF4;
  color: var(--success);
  border: 1px solid #BBF7D0;
}

.upload-status.error {
  background: #FEF2F2;
  color: var(--danger);
  border: 1px solid #FECACA;
}

@media (prefers-color-scheme: dark) {
  .upload-status.success { background: #052E16; border-color: #166534; }
  .upload-status.error   { background: #450A0A; border-color: #991B1B; }
}


/* ── Mobile ─────────────────────────────────────────────────────────────── */

@media (max-width: 600px) {
  :root {
    --col-pad: 1rem;
    --navbar-h: 3.25rem;
  }

  .btn { padding: 0.38rem 0.65rem; font-size: 0.75rem; }
  .chat-window { padding-top: 1.5rem; }
  .input-bar { padding-bottom: max(1rem, env(safe-area-inset-bottom)); }
  .input-bar__inner { align-items: center; padding: 0.55rem; }
  .input-bar__textarea { font-size: 16px; line-height: 1.45; padding: 0.3rem 0.25rem; }
  .btn--send { width: 2.75rem; height: 2.75rem; min-width: 2.75rem; }
  .user-form { align-items: stretch; flex-direction: column; }
  .user-form__grid { grid-template-columns: 1fr; }
  .user-row { align-items: flex-start; flex-direction: column; }
  .user-row__actions { width: 100%; justify-content: flex-end; }
}
