/* ==========================================================================
   ZAKADEMY — Zako, the study assistant
   A button in the corner and the panel it opens. Everything resolves to the
   tokens in tokens.css, so it follows the theme, dark mode and the reader's
   text-size setting without any rules of its own for them.
   ========================================================================== */

:root {
  --zako-w: 400px;
  --zako-gap: var(--s4);
  --zako-user: var(--royal);          /* same pair as .btn-primary, so the same contrast */
  --zako-user-ink: #fff;
  --zako-tab: 0px;                    /* height of the phone tab bar, when there is one */
}
:root[data-theme="dark"] { --zako-user: #0F5FE8; }

/* ---------- the button ----------------------------------------------------- */
.zako-launch {
  position: fixed;
  right: var(--zako-gap);
  bottom: calc(var(--zako-gap) + var(--zako-tab));
  z-index: 89;
  display: inline-flex; align-items: center; gap: 0.5rem;
  min-height: 48px;
  padding: 0 1.1rem 0 0.9rem;
  border: 1px solid rgb(255 255 255 / 0.28);
  border-radius: var(--r-pill);
  background: var(--deep-navy);
  color: var(--on-dark);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.9375rem;
  box-shadow: var(--shadow-raise);
  cursor: pointer;
  transition: transform var(--t-state) var(--ease-out), background var(--t-state) var(--ease-out);
}
.zako-launch:hover { background: var(--mid-navy); }
.zako-launch:active { transform: translateY(1px); }
.zako-launch:focus-visible { outline: 2px solid var(--royal); outline-offset: 3px; border-radius: var(--r-pill); }
.zako-launch svg { width: 22px; height: 22px; flex: 0 0 22px; color: var(--cyan); }
.zako-launch[hidden] { display: none; }

/* the back-to-top button moves up to make room, on every page that has both */
body.has-zako .to-top { bottom: calc(var(--zako-gap) + var(--zako-tab) + 60px); }

@media (max-width: 760px) {
  :root { --zako-tab: calc(56px + env(safe-area-inset-bottom, 0px)); --zako-gap: var(--s3); }
  .zako-launch { width: 48px; padding: 0; justify-content: center; }
  .zako-launch .zako-launch-word { position: absolute; width: 1px; height: 1px; overflow: hidden;
    clip-path: inset(50%); white-space: nowrap; }
  /* a quiz in progress has the whole screen on a phone, tab bar and all */
  body:has(#zk-quiz[data-at="run"]) .zako-launch { display: none; }
}
/* never over a timed paper, the countdown box or the reader */
body:has(#zk-exam[data-at="sitting"]) .zako-launch,
body:has(.timerbox:not([hidden])) .zako-launch,
body.viewer-open .zako-launch,
body.zako-open .zako-launch { display: none; }

/* ---------- the panel ------------------------------------------------------ */
.zako {
  position: fixed;
  right: var(--zako-gap);
  bottom: var(--zako-gap);
  z-index: 185;
  display: flex; flex-direction: column;
  width: min(var(--zako-w), calc(100vw - 2 * var(--zako-gap)));
  height: min(640px, calc(100vh - var(--header-h) - 2 * var(--zako-gap)));
  height: min(640px, calc(100dvh - var(--header-h) - 2 * var(--zako-gap)));
  min-height: 320px;
  background: var(--page);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: 0 24px 60px rgb(2 8 28 / 0.28);
  overflow: hidden;
  opacity: 0; transform: translateY(8px);
  transition: opacity var(--t-state) var(--ease-out), transform var(--t-state) var(--ease-out);
}
.zako[hidden] { display: none; }
.zako[data-shown] { opacity: 1; transform: none; }

.zako-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s3);
  padding: var(--s3) var(--s3) var(--s3) var(--s4);
  background: var(--deep-navy);
  color: var(--on-dark);
}
.zako-id { display: flex; align-items: center; gap: var(--s3); min-width: 0; }
.zako-id img { flex: 0 0 34px; width: 34px; height: 34px; }
.zako-id h2 { margin: 0; font-family: var(--font-display); font-size: 1.0625rem; font-weight: 700; line-height: 1.2; color: var(--on-dark); }
.zako-id p { margin: 0; font-size: 0.8125rem; line-height: 1.3; color: var(--on-dark-soft); }
.zako-tools { display: flex; gap: var(--s1); flex: 0 0 auto; }
.zako-icon {
  display: grid; place-items: center;
  width: 44px; height: 44px;
  border-radius: var(--r-sm);
  color: var(--on-dark);
  cursor: pointer;
}
.zako-icon:hover { background: rgb(255 255 255 / 0.12); }
.zako-icon svg { width: 20px; height: 20px; }
.zako-head :focus-visible { outline: 2px solid var(--cyan); outline-offset: -2px; }

/* which page Zako takes "this" to mean, and the way to turn that off */
.zako-context {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s3);
  padding: var(--s2) var(--s4);
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  font-size: 0.8125rem; line-height: 1.35; color: var(--slate);
}
.zako-context[hidden] { display: none; }
.zako-context b { color: var(--navy); font-weight: 600; }
.zako-context span { min-width: 0; overflow-wrap: anywhere; }
.zako-link {
  flex: 0 0 auto;
  min-height: 32px; padding: 0 var(--s2);
  color: var(--royal); font-size: 0.8125rem; font-weight: 600;
  text-decoration: underline; text-underline-offset: 0.18em;
  cursor: pointer;
}
.zako-link:hover { text-decoration-thickness: 2px; }

.zako-confirm {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s3); flex-wrap: wrap;
  padding: var(--s3) var(--s4);
  background: var(--cream);
  border-bottom: 1px solid var(--border);
  font-size: 0.875rem; color: var(--ink);
}
.zako-confirm[hidden] { display: none; }
.zako-confirm div { display: flex; gap: var(--s2); }

/* ---------- the conversation ---------------------------------------------- */
.zako-log {
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto; overscroll-behavior: contain;
  padding: var(--s4);
  display: flex; flex-direction: column; gap: var(--s3);
  scroll-padding-bottom: var(--s4);
}
.zako-log:focus-visible { outline: 2px solid var(--royal); outline-offset: -2px; }

.zako-hello { display: grid; gap: var(--s3); }
.zako-hello p { margin: 0; font-size: 0.9375rem; line-height: 1.55; }
.zako-hello .zako-small { font-size: 0.8125rem; color: var(--slate); }
.zako-starters { display: grid; gap: var(--s2); margin: 0; padding: 0; list-style: none; }
.zako-starter {
  width: 100%; min-height: 44px;
  padding: 0.55rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--card);
  color: var(--navy);
  font-size: 0.875rem; font-weight: 500; line-height: 1.35; text-align: start;
  cursor: pointer;
}
.zako-starter:hover { border-color: var(--royal); color: var(--royal); }

.zako-msg { max-width: 100%; font-size: 0.9375rem; line-height: 1.55; overflow-wrap: anywhere; }
.zako-you {
  align-self: flex-end; max-width: 88%;
  padding: 0.55rem 0.85rem;
  border-radius: var(--r-md) var(--r-md) var(--r-sm) var(--r-md);
  background: var(--zako-user); color: var(--zako-user-ink);
  white-space: pre-wrap;
}
.zako-bot {
  align-self: stretch;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--border-soft);
  border-radius: var(--r-md) var(--r-md) var(--r-md) var(--r-sm);
  background: var(--card);
}
.zako-body > :first-child { margin-top: 0; }
.zako-body > :last-child { margin-bottom: 0; }
.zako-body p { margin: 0 0 0.6em; }
.zako-body ol, .zako-body ul { margin: 0 0 0.6em; padding-inline-start: 1.35em; }
.zako-body ol { list-style: decimal; }
.zako-body ul { list-style: disc; }
.zako-body li { margin: 0.2em 0; }
.zako-body li > p { margin: 0; }
.zako-body blockquote { margin: 0 0 0.6em; padding-inline-start: var(--s3); border-inline-start: 3px solid var(--border); color: var(--slate); }
.zako-body code { font-size: 0.875em; }
.zako-body pre {
  margin: 0 0 0.6em; padding: var(--s3);
  background: var(--panel); border-radius: var(--r-sm);
  overflow-x: auto; font-size: 0.8125rem; line-height: 1.5;
  white-space: pre; tab-size: 4;
}
.zako-body pre:focus-visible { outline: 2px solid var(--royal); outline-offset: 1px; }
.zako-body pre code { background: none; padding: 0; font-size: inherit; }
.zako-math-block { display: block; margin: 0 0 0.6em; overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.zako-math-block:focus-visible { outline: 2px solid var(--royal); outline-offset: 1px; }
.zako-math { font-family: var(--font-body); }
.zako-math[data-raw] { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 0.875em; }
.zako-body .katex { font-size: 1.05em; }
.zako-cite { font-size: 0.75em; vertical-align: super; line-height: 0; font-weight: 600; text-decoration: none; padding: 0 1px; }
.zako-cite:hover { text-decoration: underline; }

.zako-sources { margin: var(--s3) 0 0; padding: var(--s3) 0 0; border-top: 1px solid var(--border); font-size: 0.8125rem; line-height: 1.45; }
.zako-sources h3 { margin: 0 0 var(--s1); font-family: var(--font-display); font-size: 0.8125rem; font-weight: 600; color: var(--slate); }
.zako-sources ol { margin: 0; padding: 0; list-style: none; display: grid; gap: var(--s2); }
.zako-sources li { display: grid; grid-template-columns: 1.5em minmax(0, 1fr); gap: 0 var(--s1); }
.zako-sources .n { color: var(--slate); font-weight: 600; }
.zako-sources a { font-weight: 500; }
.zako-sources small { display: block; color: var(--slate); font-size: 0.75rem; }
.zako-more { margin: 2px 0 0; display: flex; flex-wrap: wrap; gap: 2px var(--s3); }

.zako-acts { display: flex; justify-content: flex-end; margin-top: var(--s2); }
.zako-copy {
  min-height: 32px; padding: 0 var(--s2);
  border-radius: var(--r-sm);
  color: var(--slate); font-size: 0.8125rem; font-weight: 600;
  cursor: pointer;
}
.zako-copy:hover { color: var(--royal); background: var(--panel); }

/* a notice is the service talking, not Zako answering: it never looks like a reply */
.zako-note {
  align-self: stretch;
  padding: 0.6rem 0.85rem;
  border: 1px dashed var(--border);
  border-radius: var(--r-md);
  font-size: 0.875rem; line-height: 1.5; color: var(--slate);
}
.zako-note .zako-link { padding: 0; min-height: 0; margin-inline-start: var(--s2); }

.zako-wait { display: flex; align-items: center; gap: var(--s2); min-height: 0; padding: 0 var(--s4); font-size: 0.8125rem; color: var(--slate); }
.zako-wait:empty { display: none; }
.zako-wait:not(:empty) { padding-bottom: var(--s2); }
.zako-spin { width: 14px; height: 14px; flex: 0 0 14px; border: 2px solid var(--border); border-top-color: var(--royal); border-radius: 50%; animation: zakoSpin 900ms linear infinite; }
@keyframes zakoSpin { to { transform: rotate(360deg); } }

/* ---------- the message box ------------------------------------------------ */
.zako-form {
  display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: var(--s2);
  padding: var(--s3) var(--s3) var(--s2) var(--s4);
  border-top: 1px solid var(--border);
  background: var(--page);
}
.zako-input {
  width: 100%; min-width: 0;
  min-height: 4.1rem; max-height: 8.5rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--card-solid);
  color: var(--ink);
  font: inherit; font-size: 1rem; line-height: 1.4;
  resize: none;
}
.zako-input::placeholder { color: var(--slate); opacity: 1; }
.zako-input:focus-visible { outline: 2px solid var(--royal); outline-offset: 1px; border-color: var(--royal); }
.zako-input:disabled { background: var(--panel); cursor: not-allowed; }
.zako-send {
  display: grid; place-items: center;
  width: 44px; height: 44px;
  border-radius: var(--r-md);
  background: var(--zako-user); color: var(--zako-user-ink);
  cursor: pointer;
}
.zako-send:hover { filter: brightness(0.92); }
.zako-send:disabled { background: var(--panel); color: var(--slate); cursor: not-allowed; filter: none; }
.zako-send svg { width: 20px; height: 20px; }
.zako-count { grid-column: 1 / -1; margin: 0; font-size: 0.75rem; color: var(--slate); text-align: end; }
.zako-count[hidden] { display: none; }
.zako-count[data-over] { color: var(--amber); font-weight: 600; }
.zako-foot { margin: 0; padding: 0 var(--s4) var(--s3); font-size: 0.75rem; line-height: 1.4; color: var(--slate); background: var(--page); }

/* ---------- phones: the panel is the screen -------------------------------- */
@media (max-width: 760px) {
  .zako {
    inset: 0; width: auto; height: auto; min-height: 0;
    border: 0; border-radius: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  body.zako-open { overflow: hidden; }
}
/* a short window on a desktop: use what there is */
@media (min-width: 761px) and (max-height: 520px) {
  .zako { height: calc(100vh - 2 * var(--zako-gap)); min-height: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .zako, .zako-launch { transition: none; }
  .zako { transform: none; }
  .zako-spin { animation: none; border-top-color: var(--border); }
}
:root[data-motion="off"] .zako { transform: none; }
:root[data-motion="off"] .zako-spin { animation: none; border-top-color: var(--border); }

@media print { .zako, .zako-launch { display: none !important; } }
