/* SchoolsGPT web UI — modern, mobile-first, keyboard-aware. */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --brand:       #0f2a44;
  --brand-2:     #16406a;
  --brand-lite:  #e8f0f8;
  --accent:      #2563eb;
  --accent-soft: #dbeafe;
  --surface:     #ffffff;
  --bg:          #f4f6fa;
  --border:      #e3e8ef;
  --text:        #1d2733;
  --muted:       #64748b;
  --radius:      16px;
  --shadow:      0 1px 3px rgba(15, 42, 68, .08);
  --sidebar-w:   280px;
}

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  display: flex;
  /* dvh tracks the visual viewport when the mobile keyboard opens */
  height: 100dvh;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--brand);
  color: #dbe6f2;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform .25s ease;
}
.side-head { padding: 14px; }
#new-chat {
  width: 100%;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  background: rgba(255,255,255,.1);
  border: 1px solid rgba(255,255,255,.22);
  color: #fff;
  border-radius: 10px;
  padding: 10px;
  font-size: .9rem; font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
#new-chat:hover { background: rgba(255,255,255,.18); }

#conv-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.conv-item {
  display: block; width: 100%; text-align: left;
  background: none; border: none; color: #c3d3e3;
  padding: 10px 12px; border-radius: 8px;
  font-size: .85rem; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-bottom: 2px;
}
.conv-item:hover { background: rgba(255,255,255,.08); color: #fff; }
.conv-item.active { background: rgba(255,255,255,.14); color: #fff; }

.side-foot {
  padding: 12px 14px;
  font-size: .7rem; color: #8fa6bc;
  border-top: 1px solid rgba(255,255,255,.1);
}

#overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 40;
}

/* ── Main column ─────────────────────────────────────────────── */
#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  display: flex; align-items: center; gap: 10px;
  flex-shrink: 0;
}
#menu-btn, #mobile-new {
  display: none;
  background: none; border: none; color: var(--brand);
  cursor: pointer; padding: 6px; border-radius: 8px;
}
#menu-btn:hover, #mobile-new:hover { background: var(--brand-lite); }

.brand { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.logo {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, var(--brand), var(--accent));
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 19px; flex-shrink: 0;
  color: #fff;
}
.brand-text { line-height: 1.2; }
.brand-text strong { font-size: 1rem; color: var(--brand); letter-spacing: .01em; }
.brand-text span { font-size: .72rem; color: var(--muted); display: block; }

/* ── Chat area ───────────────────────────────────────────────── */
#chat {
  flex: 1;
  overflow-y: auto;
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}
#chat > * { max-width: 820px; width: 100%; margin-left: auto; margin-right: auto; }

/* Empty (hero) state — ChatGPT-style: greeting, centred input, chips below.
 * body.empty pushes #chat + footer together into the vertical centre. */
#empty-state {
  text-align: center;
  padding: 12px;
}
.hero-logo { font-size: 44px; margin-bottom: 12px; }
#empty-state h1 { font-size: 1.35rem; color: var(--brand); margin-bottom: 8px; }
#empty-state p { color: var(--muted); font-size: .9rem; max-width: 520px; margin: 0 auto; line-height: 1.55; }

body.empty #chat {
  flex: 0 0 auto;
  margin-top: auto;
  overflow: visible;
}
body.empty footer {
  margin-bottom: auto;
  border-top: none;
  background: transparent;
}
body.empty .input-wrap {
  max-width: 680px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 26px;
  padding: 6px;
  box-shadow: 0 4px 20px rgba(15, 42, 68, .1);
}
body.empty .input-wrap:focus-within { border-color: var(--accent); }
body.empty #input { border: none; background: transparent; padding: 12px 16px; }
body.empty #input:focus { background: transparent; }
body.empty .disclaimer { display: none; }

.chips {
  display: none;
  flex-wrap: wrap; gap: 8px; justify-content: center;
  max-width: 680px; margin: 16px auto 0;
}
body.empty .chips { display: flex; }
.chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: .82rem;
  color: var(--accent);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background .15s, border-color .15s;
}
.chip:hover { background: var(--accent-soft); border-color: var(--accent); }

/* ── Message rows ────────────────────────────────────────────── */
.row { display: flex; gap: 9px; align-items: flex-end; }
.row.user { flex-direction: row-reverse; }
.row.bot  { align-items: flex-start; }
.row.bot .bubble-wrap { flex: 1; min-width: 0; }

.avatar {
  width: 30px; height: 30px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; flex-shrink: 0;
}
.avatar.bot  { background: linear-gradient(135deg, var(--brand), var(--accent)); color: #fff; }
.avatar.user { background: #cbd5e1; color: var(--brand); }

.bubble {
  padding: 11px 15px;
  border-radius: var(--radius);
  line-height: 1.6;
  word-wrap: break-word;
  overflow-x: auto;
}
.bubble.user {
  max-width: min(80%, 500px);
  background: linear-gradient(135deg, var(--brand-2), var(--brand));
  color: #fff;
  border-bottom-right-radius: 5px;
}
.bubble.bot {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow);
}
.bubble.thinking {
  width: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  border-bottom-left-radius: 5px;
}
.thinking .tdots { display: inline-flex; gap: 4px; align-items: center; height: 1em; }
.tdots i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent); opacity: .35;
  animation: pulse 1.2s infinite;
}
.tdots i:nth-child(2) { animation-delay: .2s; }
.tdots i:nth-child(3) { animation-delay: .4s; }
@keyframes pulse { 0%,100% { opacity:.25; transform: translateY(0);} 40% { opacity:1; transform: translateY(-2px);} }

/* Message meta row: elapsed + feedback */
.meta {
  display: flex; align-items: center; gap: 8px;
  margin-top: 5px; padding: 0 4px;
  font-size: .7rem; color: var(--muted);
}
.fb-btn {
  background: none; border: none; cursor: pointer;
  color: #9aa8b8; font-size: .85rem;
  padding: 2px 4px; border-radius: 6px; line-height: 1;
}
.fb-btn:hover { background: var(--brand-lite); color: var(--accent); }
.fb-btn.chosen { color: var(--accent); }

/* ── Markdown inside bot bubble ──────────────────────────────── */
.bubble.bot h1, .bubble.bot h2 {
  font-size: 1rem; font-weight: 700;
  margin: 14px 0 6px;
  color: var(--brand);
  padding-bottom: 4px;
  border-bottom: 2px solid var(--brand-lite);
}
.bubble.bot h1:first-child, .bubble.bot h2:first-child { margin-top: 0; }
.bubble.bot h3 { font-size: .92rem; font-weight: 600; margin: 10px 0 4px; }
.bubble.bot p  { margin: 6px 0; }
.bubble.bot ul, .bubble.bot ol { padding-left: 20px; margin: 6px 0; }
.bubble.bot li { margin: 3px 0; }
.bubble.bot strong { color: var(--brand); }
.bubble.bot em { color: var(--muted); }
.bubble.bot code {
  background: var(--bg);
  padding: 1px 5px; border-radius: 4px;
  font-size: .88em; font-family: "SF Mono", Menlo, Consolas, monospace;
}
.bubble.bot blockquote {
  border-left: 3px solid #f59e0b;
  padding: 6px 12px; margin: 8px 0;
  background: #fffbeb; border-radius: 0 6px 6px 0;
  color: #92400e;
}
.bubble.bot hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.bubble.bot a { color: var(--accent); }

.bubble.bot table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: .9em; }
.bubble.bot th {
  background: var(--brand); color: #fff;
  padding: 7px 10px; text-align: left; font-weight: 600;
}
.bubble.bot td { padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.bubble.bot tr:nth-child(even) td { background: var(--bg); }
.bubble.bot tr:last-child td { border-bottom: none; }
.bubble.bot thead th:empty { display: none; }

/* ── Footer / input ──────────────────────────────────────────── */
footer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 14px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  flex-shrink: 0;
}
.input-wrap {
  max-width: 820px; margin: 0 auto;
  display: flex; gap: 9px; align-items: flex-end;
}
#input {
  flex: 1;
  border: 1.5px solid var(--border);
  border-radius: 22px;
  padding: 11px 18px;
  font-size: 16px; /* ≥16px prevents iOS Safari zoom-on-focus */
  outline: none;
  resize: none;
  max-height: 130px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  transition: border-color .15s, background .15s;
}
#input:focus { border-color: var(--accent); background: var(--surface); }
#send {
  background: linear-gradient(135deg, var(--brand-2), var(--accent));
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 44px; height: 44px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  transition: opacity .15s, transform .1s;
}
#send:hover:not(:disabled) { transform: scale(1.05); }
#send:disabled { opacity: .45; cursor: default; transform: none; }
.disclaimer {
  max-width: 820px; margin: 6px auto 0;
  text-align: center;
  font-size: .68rem; color: #9aa8b8;
}

/* ── Scrollbars ──────────────────────────────────────────────── */
#chat::-webkit-scrollbar, #conv-list::-webkit-scrollbar { width: 5px; }
#chat::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
#conv-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,.25); border-radius: 4px; }

/* No double-tap zoom / tap delay on controls */
button, textarea, .chip, .conv-item { touch-action: manipulation; }

/* ── Mobile ──────────────────────────────────────────────────── */
@media (max-width: 800px) {
  #sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    box-shadow: 4px 0 24px rgba(0,0,0,.25);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }
  body.sidebar-open #overlay { display: block; }
  #menu-btn, #mobile-new { display: flex; }
  .brand { justify-content: center; }
  .brand-text span { display: none; }
  #chat { padding: 14px 10px; }
  .bubble.user { max-width: 86%; }

  /* Maximise visible chat area: compact header + input row, and drop the
     disclaimer line once a conversation is underway. */
  header { padding-top: calc(6px + env(safe-area-inset-top)); padding-bottom: 6px; }
  footer { padding-top: 8px; }
  body:not(.empty) .disclaimer { display: none; }
}
