/* ——— Pillow Wrestling with Judaism ———
   Palette lifted from Sefaria (sefaria.org / Sefaria-Project palette.js):
   navy #212D4F · link blue #4871bf · pale blue bg #EDF4FA · highlight #CFE5F8
   off-white #FBFBFA · talmud gold #ccb479 · plus per-sage category colors. */

:root {
  --sky: #edf4fa;            /* Sefaria pale blue — page background */
  --sky-deep: #cfe5f8;       /* Sefaria highlight blue */
  --navy: #212d4f;           /* Sefaria dark navy */
  --navy-soft: #36437a;
  --ink: #1a1a1a;            /* Sefaria body text */
  --ink-soft: #5b6069;
  --link-blue: #4871bf;      /* Sefaria commentary/link blue */
  --gold: #ccb479;           /* Sefaria talmud gold */
  --card: #ffffff;
  --card-edge: rgba(33, 45, 79, 0.14);
  --card-shadow: rgba(33, 45, 79, 0.10);
  --accent: var(--link-blue); /* overridden per sage */
  --library-gradient: linear-gradient(90deg, #00505E 0% 10%, #5698B4 10% 20%,
    #CCB37C 20% 30%, #5B9370 30% 40%, #823241 40% 50%, #5A4474 50% 60%,
    #AD4F66 60% 70%, #7285A6 70% 80%, #00807E 80% 90%, #4872B3 90% 100%);
  --radius-pillow: 26px;
  --font-display: "Frank Ruhl Libre", Georgia, serif;
  --font-body: "Source Serif 4", Georgia, "Times New Roman", serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background:
    radial-gradient(1100px 480px at 85% -10%, rgba(207, 229, 248, 0.8), transparent 60%),
    radial-gradient(900px 480px at -5% 100%, rgba(72, 113, 191, 0.10), transparent 55%),
    var(--sky);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 17px;
  line-height: 1.6;
}

.page {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 max(20px, env(safe-area-inset-right)) 0 max(20px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh; /* iOS Safari: track the real viewport as the URL bar collapses */
}

/* ——— Masthead ——— */

.masthead { padding: 34px 0 10px; }

.masthead-inner {
  display: flex;
  align-items: center;
  gap: 18px;
}

.logo {
  position: relative;
  font-size: 52px;
  line-height: 1;
  filter: drop-shadow(0 6px 10px var(--card-shadow));
}

.logo-spark {
  position: absolute;
  top: -6px;
  right: -10px;
  font-size: 20px;
  color: var(--link-blue);
}

.masthead h1 {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: clamp(1.7rem, 4.5vw, 2.5rem);
  margin: 0;
  color: var(--navy);
  letter-spacing: -0.01em;
}

.masthead h1 em {
  font-style: italic;
  font-weight: 500;
  color: var(--link-blue);
}

.tagline {
  margin: 4px 0 0;
  color: var(--ink-soft);
  font-style: italic;
  font-size: 0.98rem;
}

/* ——— Sage picker ——— */

.sage-picker { padding: 18px 0 6px; }

.picker-label {
  margin: 0 0 10px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--ink-soft);
  font-family: var(--font-display);
  font-weight: 700;
}

.sage-rail {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 4px 2px 12px;
}

.sage-chip {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 16px 10px 13px;
  border: 1.5px solid var(--card-edge);
  border-left: 5px solid var(--accent);
  border-radius: 14px;
  background: var(--card);
  box-shadow: 0 3px 8px var(--card-shadow);
  cursor: pointer;
  font-family: var(--font-body);
  color: var(--ink);
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}

.sage-chip:hover { transform: translateY(-2px); border-color: var(--accent); }

.sage-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.sage-chip.active .chip-era { color: rgba(255, 255, 255, 0.85); }

/* The Beit Midrash wears the whole library */
.sage-chip.rainbow { border-left: 5px solid transparent; position: relative; }
.sage-chip.rainbow::after {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 5px;
  border-radius: 14px 0 0 14px;
  background: linear-gradient(180deg, #00505E 0% 10%, #5698B4 10% 20%,
    #CCB37C 20% 30%, #5B9370 30% 40%, #823241 40% 50%, #5A4474 50% 60%,
    #AD4F66 60% 70%, #7285A6 70% 80%, #00807E 80% 90%, #4872B3 90% 100%);
}
.sage-chip.rainbow.active {
  background: var(--navy);
  border-color: var(--navy);
}

.chip-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
}

.chip-era {
  font-size: 0.72rem;
  color: var(--ink-soft);
  white-space: nowrap;
}

.sage-blurb {
  margin: 2px 0 0;
  min-height: 1.4em;
  font-style: italic;
  color: var(--ink-soft);
  font-size: 0.95rem;
}

/* ——— Chat ——— */

.chat {
  flex: 1;
  padding: 18px 0 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.welcome {
  background: var(--card);
  border-radius: var(--radius-pillow);
  padding: 30px 32px;
  box-shadow: 0 10px 30px var(--card-shadow);
  border: 1px solid var(--card-edge);
  border-top: 5px solid transparent;
  background-clip: padding-box;
  position: relative;
  overflow: hidden;
}

.welcome::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 5px;
  background: var(--library-gradient);
}

.welcome h2 {
  font-family: var(--font-display);
  font-weight: 900;
  color: var(--navy);
  margin: 0 0 8px;
  font-size: 1.5rem;
}

.welcome p { margin: 0 0 16px; color: var(--ink-soft); }

.welcome a { color: var(--link-blue); }

.starters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.starter {
  border: 1.5px dashed rgba(72, 113, 191, 0.5);
  background: transparent;
  color: var(--navy);
  font-family: var(--font-body);
  font-size: 0.9rem;
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.starter:hover { background: var(--sky-deep); border-color: var(--link-blue); }

/* messages */

.msg {
  max-width: 92%;
  padding: 16px 22px;
  border-radius: var(--radius-pillow);
  box-shadow: 0 6px 18px var(--card-shadow);
  animation: puff 0.25s ease;
}

@keyframes puff {
  from { opacity: 0; transform: translateY(6px) scale(0.985); }
  to   { opacity: 1; transform: none; }
}

.msg.user {
  align-self: flex-end;
  background: var(--navy);
  color: #f2f6fb;
  border-bottom-right-radius: 8px;
}

.msg.sage {
  align-self: flex-start;
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-left: 5px solid var(--accent);
  border-bottom-left-radius: 8px;
}

.msg-byline {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
  font-family: var(--font-display);
}

.msg-byline .who { font-weight: 700; color: var(--accent); font-size: 0.95rem; }
.msg-byline .when { font-size: 0.75rem; color: var(--ink-soft); }

.msg-body p { margin: 0 0 0.8em; }
.msg-body p:last-child { margin-bottom: 0; }

.msg-body a {
  color: var(--link-blue);
  text-decoration-color: rgba(72, 113, 191, 0.4);
  text-underline-offset: 2px;
  font-weight: 600;
}

.msg-body blockquote {
  margin: 0.6em 0;
  padding: 8px 16px;
  border-inline-start: 3px solid var(--gold);
  background: rgba(207, 229, 248, 0.35);
  border-radius: 6px;
  font-style: italic;
  color: var(--ink-soft);
}

.msg-body ul, .msg-body ol { margin: 0.4em 0 0.8em; padding-inline-start: 1.4em; }

.msg-body strong { color: var(--navy); }

/* tool status line */

.tool-line {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-style: italic;
  color: var(--ink-soft);
  padding: 2px 10px;
  animation: puff 0.2s ease;
}

.tool-line .dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--link-blue);
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.15); }
}

/* sources panel */

.sources {
  align-self: flex-start;
  max-width: 92%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
}

.sources-label {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
}

.source-chip {
  font-size: 0.82rem;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--sky-deep);
  border: 1px solid rgba(72, 113, 191, 0.35);
  color: var(--navy);
  text-decoration: none;
  font-weight: 600;
  transition: background 0.12s ease;
}

.source-chip:hover { background: #fff; border-color: var(--link-blue); color: var(--link-blue); }

.error-line {
  align-self: center;
  color: #802f3e;
  font-style: italic;
  font-size: 0.9rem;
}

/* ——— Composer ——— */

.composer-wrap {
  position: sticky;
  bottom: 0;
  padding: 12px 0 calc(18px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--sky) 28%);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--card);
  border: 1.5px solid var(--card-edge);
  border-radius: 24px;
  padding: 10px 10px 10px 20px;
  box-shadow: 0 10px 28px var(--card-shadow);
}

.composer:focus-within { border-color: var(--link-blue); }

.composer textarea {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  font-family: var(--font-body);
  font-size: 16px; /* never below 16px or iOS Safari zooms in on focus */
  line-height: 1.5;
  color: var(--ink);
  max-height: 160px;
}

.composer button {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: var(--navy);
  color: var(--sky-deep);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.12s ease, transform 0.12s ease;
}

.composer button:hover:not(:disabled) { background: var(--link-blue); transform: scale(1.05); }
.composer button:disabled { opacity: 0.45; cursor: not-allowed; }

.fine-print {
  margin: 10px 4px 0;
  font-size: 0.74rem;
  color: var(--ink-soft);
  text-align: center;
}

.fine-print a { color: inherit; }

/* Hebrew niceties */
.msg-body, .msg-body blockquote { unicode-bidi: plaintext; }

/* ——— Mention menu ——— */

.mention-menu {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(100% - 6px);
  max-height: 280px;
  overflow-y: auto;
  background: var(--card);
  border: 1.5px solid var(--card-edge);
  border-radius: 16px;
  box-shadow: 0 -8px 28px var(--card-shadow);
  padding: 6px;
  z-index: 10;
}

.mention-item {
  display: flex;
  align-items: baseline;
  gap: 10px;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  padding: 8px 12px;
  font-family: var(--font-body);
  font-size: 0.95rem;
  color: var(--ink);
  cursor: pointer;
}

.mention-item:hover,
.mention-item.selected { background: var(--sky); }

.mention-item .mi-name {
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--navy);
}

.mention-item .mi-era {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--ink-soft);
}

@media (prefers-reduced-motion: reduce) {
  .msg, .tool-line { animation: none; }
  .tool-line .dot { animation: none; opacity: 1; }
  .sage-chip, .composer button { transition: none; }
}

/* Hover lifts only where there's a real pointer — no sticky hover on touch */
@media (hover: none) {
  .sage-chip:hover { transform: none; border-color: var(--card-edge); }
  .sage-chip.active:hover { border-color: var(--accent); }
  .composer button:hover:not(:disabled) { background: var(--navy); transform: none; }
}

@media (max-width: 560px) {
  html, body { font-size: 16px; }

  .page { padding-left: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }

  .masthead { padding: 20px 0 6px; }
  .masthead-inner { gap: 12px; }
  .logo { font-size: 38px; }
  .logo-spark { font-size: 16px; top: -4px; right: -8px; }
  .masthead h1 { font-size: 1.45rem; line-height: 1.2; }
  .tagline { font-size: 0.88rem; }

  /* One-line, edge-to-edge swipeable rail */
  .sage-picker { padding-top: 12px; }
  .sage-rail {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
    margin: 0 calc(-1 * max(14px, env(safe-area-inset-right))) 0 calc(-1 * max(14px, env(safe-area-inset-left)));
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }
  .sage-rail::-webkit-scrollbar { display: none; }
  .sage-chip { scroll-snap-align: start; }

  .welcome { padding: 22px 20px; border-radius: 20px; }
  .welcome h2 { font-size: 1.3rem; }

  .starter { font-size: 0.85rem; padding: 9px 13px; }

  .chat { padding: 14px 0 20px; gap: 14px; }
  .msg { max-width: 100%; padding: 13px 16px; border-radius: 20px; }

  .sources { max-width: 100%; }

  .composer { padding: 8px 8px 8px 16px; border-radius: 22px; }
  .composer textarea { max-height: 120px; }
  .composer button { width: 44px; height: 44px; }

  .mention-menu { max-height: 220px; }
  .mention-item { padding: 10px 12px; }

  .fine-print { font-size: 0.7rem; }
}
