/* ==========================================================================
   The support chat, in the bottom-left corner. Loaded only on the pages that
   show it, which is every page but a lesson, a file in the viewer and a
   knowledge organiser. It takes the page's own colours, so on a subject's
   pages it wears the subject's accent like everything else.
   ========================================================================== */

.support-root {
  position: fixed; left: 1.25rem; bottom: 1.25rem; z-index: 45;
  font-family: var(--sans);
}

/* -- the button -------------------------------------------------------------- */

.support-launcher {
  position: relative;
  display: grid; place-items: center;
  width: 56px; height: 56px; padding: 0;
  border: 0; border-radius: 50%;
  background: var(--accent); color: #fff;
  box-shadow: 0 6px 18px -6px color-mix(in srgb, var(--accent) 70%, #000), var(--shadow-md);
  cursor: pointer;
  transition: transform .18s ease, box-shadow .18s ease;
}
.support-launcher:hover { transform: translateY(-2px) scale(1.03); }
.support-launcher:active { transform: scale(.97); }
.support-launcher:focus-visible { outline: 3px solid var(--accent-wash); outline-offset: 3px; }

.support-launcher svg { grid-area: 1 / 1; transition: opacity .18s ease, transform .22s ease; }
.support-icon-close { opacity: 0; transform: rotate(-90deg) scale(.6); }
.support-root.is-open .support-icon-open { opacity: 0; transform: rotate(90deg) scale(.6); }
.support-root.is-open .support-icon-close { opacity: 1; transform: none; }

.support-badge {
  position: absolute; top: -3px; right: -3px;
  min-width: 22px; height: 22px; padding: 0 6px;
  display: grid; place-items: center;
  border-radius: 999px; border: 2px solid var(--bg);
  background: var(--bad); color: #fff;
  font-size: .72rem; font-weight: 700; font-variant-numeric: tabular-nums;
}
.support-badge[hidden] { display: none; }

/* A soft ring while a reply is waiting: noticeable, never flashing. */
.support-root.has-unread .support-launcher::after {
  content: ""; position: absolute; inset: -6px; border-radius: 50%;
  border: 2px solid var(--accent); opacity: 0;
  animation: support-ring 2.4s ease-out infinite;
}
@keyframes support-ring {
  0%   { opacity: .55; transform: scale(.92); }
  100% { opacity: 0;   transform: scale(1.25); }
}

/* -- the panel --------------------------------------------------------------- */

.support-panel {
  position: absolute; left: 0; bottom: calc(100% + 14px);
  width: min(380px, calc(100vw - 2.5rem));
  height: min(600px, calc(100vh - 120px));
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line-firm);
  border-radius: var(--r-lg);
  box-shadow: 0 24px 60px -24px rgba(0, 0, 0, .45), var(--shadow-md);
  overflow: hidden;
  opacity: 0; transform: translateY(10px) scale(.98); transform-origin: bottom left;
  transition: opacity .18s ease, transform .18s ease;
}
.support-panel[hidden] { display: none; }
.support-panel.is-in { opacity: 1; transform: none; }

.support-head {
  display: flex; align-items: center; gap: .7rem;
  padding: .9rem .9rem .9rem 1rem;
  background: var(--accent); color: #fff;
}
.support-avatar {
  flex: none; display: grid; place-items: center;
  width: 38px; height: 38px; border-radius: 12px;
  background: var(--logo-bg); color: var(--logo-ink);
  font-family: var(--serif); font-weight: 700; font-size: 1.2rem;
}
.support-who { min-width: 0; flex: 1; }
.support-who h2 {
  margin: 0; color: #fff; font-size: 1rem; letter-spacing: -.01em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.support-status {
  margin: .1rem 0 0; display: flex; align-items: center; gap: .4rem;
  font-size: .78rem; color: rgba(255, 255, 255, .85);
}
.support-status i {
  flex: none; width: 8px; height: 8px; border-radius: 50%;
  background: rgba(255, 255, 255, .5);
}
.support-status.is-online i { background: #7ef0b8; box-shadow: 0 0 0 3px rgba(126, 240, 184, .25); }

.support-end, .support-close {
  flex: none; border: 0; background: rgba(255, 255, 255, .14); color: #fff;
  border-radius: 10px; cursor: pointer; font: inherit;
}
.support-end { padding: .35rem .6rem; font-size: .76rem; font-weight: 600; }
.support-end[hidden] { display: none; }
.support-close { display: grid; place-items: center; width: 34px; height: 34px; }
.support-end:hover, .support-close:hover { background: rgba(255, 255, 255, .26); }

.support-body {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 1rem .9rem .5rem;
  background:
    radial-gradient(circle at 0 0, var(--accent-wash), transparent 55%),
    var(--bg);
}

.support-messages { display: flex; flex-direction: column; gap: .5rem; }

.support-msg { display: flex; flex-direction: column; max-width: 85%; }
.support-msg[hidden] { display: none; }
.support-msg.from-visitor { align-self: flex-end; align-items: flex-end; }
.support-msg.from-admin { align-self: flex-start; align-items: flex-start; }
.support-msg.from-system { align-self: center; max-width: 92%; }

.support-bubble {
  padding: .6rem .85rem; border-radius: 16px;
  font-size: .9rem; line-height: 1.45;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.from-admin .support-bubble {
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-bottom-left-radius: 5px;
}
.from-visitor .support-bubble {
  background: var(--accent); color: #fff; border-bottom-right-radius: 5px;
}
.support-bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.from-admin .support-bubble a { color: var(--accent-ink, var(--accent)); }
.support-msg.is-pending { opacity: .6; }

.support-time, .support-seen {
  margin-top: .2rem; font-size: .68rem; color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}
.support-seen { margin-top: 0; font-weight: 600; }

.support-system {
  display: block; text-align: center;
  font-size: .76rem; color: var(--ink-faint);
  background: var(--surface-2); border-radius: 10px; padding: .4rem .7rem;
}

.support-typing {
  display: inline-flex; gap: 4px; align-items: center;
  margin-top: .5rem; padding: .65rem .8rem;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; border-bottom-left-radius: 5px;
}
.support-typing[hidden] { display: none; }
.support-typing i {
  width: 6px; height: 6px; border-radius: 50%; background: var(--ink-faint);
  animation: support-dot 1.2s ease-in-out infinite;
}
.support-typing i:nth-child(2) { animation-delay: .15s; }
.support-typing i:nth-child(3) { animation-delay: .3s; }
@keyframes support-dot {
  0%, 60%, 100% { transform: none; opacity: .5; }
  30% { transform: translateY(-3px); opacity: 1; }
}

/* -- writing ------------------------------------------------------------------ */

.support-compose {
  padding: .7rem .8rem .75rem;
  border-top: 1px solid var(--line);
  background: var(--surface);
}
.support-compose[hidden] { display: none; }

.support-name, .support-box {
  width: 100%;
  border: 1px solid var(--line-firm); border-radius: 12px;
  background: var(--bg); color: var(--ink);
  font: inherit; font-size: .9rem;
}
.support-name { padding: .5rem .75rem; margin-bottom: .5rem; }
.support-name[hidden] { display: none; }
.support-name:focus, .support-box:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-wash);
}

.support-hp { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }

.support-row { display: flex; gap: .5rem; align-items: flex-end; }
.support-box {
  flex: 1; min-height: 42px; max-height: 140px;
  padding: .6rem .75rem; resize: none; line-height: 1.4;
}
.support-send {
  flex: none; display: grid; place-items: center;
  width: 42px; height: 42px; border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; cursor: pointer;
}
.support-send:hover { filter: brightness(1.08); }
.support-send[disabled] { opacity: .5; cursor: wait; }

.support-note { margin: .5rem 0 0; font-size: .7rem; color: var(--ink-faint); line-height: 1.4; }
.support-error {
  margin: 0 0 .5rem; padding: .45rem .65rem; border-radius: 10px;
  background: var(--bad-soft); color: var(--bad); font-size: .8rem;
}
.support-error[hidden] { display: none; }

.support-ended {
  padding: 1rem; text-align: center;
  border-top: 1px solid var(--line); background: var(--surface);
}
.support-ended[hidden] { display: none; }
.support-ended p { margin: 0 0 .7rem; font-size: .88rem; color: var(--ink-soft); }
.support-again {
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
  border-radius: 999px; padding: .5rem 1.1rem; font: inherit; font-size: .86rem;
  font-weight: 600; cursor: pointer;
}

/* -- where it must not be in the way ------------------------------------------ */

/* The cookie notice spans the foot of a narrow screen; the button waits. */
@media (max-width: 900px) {
  body:has(.cookie-note:not([hidden])) .support-root { display: none; }
}

@media (max-width: 520px) {
  .support-root { left: .9rem; bottom: .9rem; }
  .support-launcher { width: 52px; height: 52px; }
  .support-root.is-open { inset: 0; left: 0; bottom: 0; }
  .support-root.is-open .support-launcher { display: none; }
  .support-panel {
    position: fixed; inset: 0; width: auto; height: auto;
    border-radius: 0; border: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .support-head { padding-top: calc(.9rem + env(safe-area-inset-top)); }
}

@media print {
  .support-root { display: none !important; }
}

@media (prefers-reduced-motion: reduce) {
  .support-launcher, .support-launcher svg, .support-panel { transition: none; }
  .support-root.has-unread .support-launcher::after, .support-typing i { animation: none; }
}
