/**
 * Darsgah AI — the study teacher panel.
 * Fully branded (maroon/cream/gold); nothing here ever hints at which
 * model is behind it. Everything is scoped to .dg-ai-* so it cannot touch
 * the rest of the site; with the panel closed the only thing on the page
 * is the launcher icon.
 *
 * Design intent: a teacher sitting beside the student, not a chat product.
 * The panel reads as a notebook margin — cream paper, a maroon header bar,
 * bubbles that look like written exchange. On phones it becomes a bottom
 * sheet that grows to fill the visible screen when the keyboard opens.
 */

.dg-ai-widget {
  position: fixed;
  /* Clear the notch/rounded corner in landscape; --dg-ai-lift / --dg-ai-shift
     are set by ai-widget.js only when another fixed element (a chat bubble,
     a WhatsApp button, a cookie bar) would otherwise sit under the launcher. */
  right: calc(var(--dg-space-5) + env(safe-area-inset-right, 0px) + var(--dg-ai-shift, 0px));
  bottom: calc(var(--dg-space-5) + env(safe-area-inset-bottom, 0px) + var(--dg-ai-lift, 0px));
  z-index: var(--dg-z-ai-widget);
}

/* ---------- Launcher: the Darsgah AI icon ---------- */

.dg-ai-fab {
  position: relative;
  display: block;
  padding: 0;
  margin: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  border-radius: 20px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.dg-ai-fab__icon {
  display: block;
  width: 64px;
  height: auto;
  max-width: none;
  filter: drop-shadow(0 6px 12px rgba(32, 27, 22, 0.28));
  transition: transform var(--dg-transition), filter var(--dg-transition);
}
/* Only if the icon image cannot load. */
.dg-ai-fab.is-fallback .dg-ai-fab__icon { display: none; }
.dg-ai-fab.is-fallback {
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--dg-maroon-deep);
  box-shadow: 0 6px 12px rgba(32, 27, 22, 0.28);
}
.dg-ai-fab.is-fallback::after {
  content: "AI";
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%;
  color: var(--dg-surface);
  font-weight: 700; font-size: 16px; letter-spacing: 0.02em;
}
.dg-ai-fab:focus-visible {
  outline: 3px solid var(--dg-focus);
  outline-offset: 4px;
}

/* "Ask Darsgah AI" — hover/focus tooltip, only on devices that can hover.
   Touch screens keep the icon alone (the button carries an aria-label). */
.dg-ai-fab__label {
  display: none;
}
@media (hover: hover) and (pointer: fine) {
  .dg-ai-fab__label {
    display: block;
    position: absolute;
    right: calc(100% + var(--dg-space-3));
    top: 50%;
    transform: translate(6px, -50%);
    padding: 7px var(--dg-space-3);
    border-radius: var(--dg-radius-pill);
    background: var(--dg-maroon-deep);
    color: var(--dg-surface);
    font-family: var(--dg-font-body);
    font-size: var(--dg-text-xs);
    font-weight: 600;
    letter-spacing: 0.01em;
    white-space: nowrap;
    box-shadow: var(--dg-shadow-md);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dg-transition), transform var(--dg-transition);
  }
  .dg-ai-fab:hover .dg-ai-fab__label,
  .dg-ai-fab:focus-visible .dg-ai-fab__label {
    opacity: 1;
    transform: translate(0, -50%);
  }
  .dg-ai-fab:hover .dg-ai-fab__icon {
    transform: translateY(-2px) scale(1.04);
    filter: drop-shadow(0 10px 16px rgba(32, 27, 22, 0.32));
  }
}
@media (prefers-reduced-motion: reduce) {
  .dg-ai-fab__icon, .dg-ai-fab__label { transition: none; }
  .dg-ai-fab:hover .dg-ai-fab__icon { transform: none; }
}

/* Once the panel is open the launcher has nothing left to say, and it
   would sit under the panel that replaced it. */
.dg-ai-fab[aria-expanded="true"] { visibility: hidden; }

@media (max-width: 640px) {
  .dg-ai-widget {
    right: calc(var(--dg-space-4) + env(safe-area-inset-right, 0px) + var(--dg-ai-shift, 0px));
    bottom: calc(var(--dg-space-4) + env(safe-area-inset-bottom, 0px) + var(--dg-ai-lift, 0px));
  }
  .dg-ai-fab__icon { width: 54px; }
}

/* ---------- Panel ---------- */

.dg-ai-panel {
  position: absolute;
  right: 0;
  bottom: 0;                       /* takes the launcher's corner while open */
  width: 400px;
  max-width: calc(100vw - 2 * var(--dg-space-5));
  height: 600px;
  max-height: min(78vh, 680px);
  background: var(--dg-surface);
  border: 1px solid var(--dg-line);
  border-radius: var(--dg-radius-lg);
  box-shadow: var(--dg-shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: dg-ai-in var(--dg-transition-slow) both;
}
.dg-ai-panel[hidden] { display: none; }
@keyframes dg-ai-in {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) { .dg-ai-panel { animation: none; } }

.dg-ai-panel__header {
  display: flex;
  align-items: center;
  gap: var(--dg-space-3);
  padding: var(--dg-space-4);
  background: var(--dg-maroon-deep);
  color: var(--dg-surface);
  flex: none;
}
.dg-ai-panel__avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 34px; height: 34px;
  border-radius: 50%;
  background: rgba(255,255,255,0.14);
  color: var(--dg-surface);
}
.dg-ai-panel__avatar svg {
  width: 18px; height: 18px;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.dg-ai-panel__identity { display: flex; flex-direction: column; min-width: 0; gap: 1px; flex: 1; }
.dg-ai-panel__title {
  font-family: var(--dg-font-display);
  font-weight: 700;
  font-size: var(--dg-text-base);
  line-height: 1.2;
}
.dg-ai-panel__role { font-size: var(--dg-text-2xs); color: rgba(255,255,255,0.72); }

.dg-ai-panel__close {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px; height: 44px;       /* full-size touch target */
  margin: -6px -8px -6px 0;
  border-radius: 50%;
  background: transparent;
  border: 0;
  color: var(--dg-surface);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  transition: background var(--dg-transition);
}
.dg-ai-panel__close:hover { background: rgba(255,255,255,0.16); }
.dg-ai-panel__close:focus-visible,
.dg-ai-selection__clear:focus-visible,
.dg-ai-starter:focus-visible,
.dg-ai-retry:focus-visible,
.dg-ai-panel__form button:focus-visible {
  outline: 2px solid var(--dg-focus);
  outline-offset: 2px;
}
.dg-ai-panel__close:focus-visible { outline-color: var(--dg-surface); }

/* "Currently studying" — what the teacher understands this page to be. */
.dg-ai-context {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px var(--dg-space-2);
  padding: var(--dg-space-2) var(--dg-space-4);
  background: var(--dg-maroon-tint-06);
  border-bottom: 1px solid var(--dg-line);
  font-size: var(--dg-text-2xs);
  line-height: 1.4;
}
.dg-ai-context[hidden] { display: none; }
.dg-ai-context__label { color: var(--dg-ink-muted); font-weight: 600; }
.dg-ai-context__value { color: var(--dg-maroon-deep); font-weight: 650; overflow-wrap: anywhere; min-width: 0; }

/* ---------- Messages ---------- */

.dg-ai-panel__messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: var(--dg-space-4);
  display: flex;
  flex-direction: column;
  gap: var(--dg-space-3);
  background: var(--dg-surface-alt);
}
.dg-ai-message {
  max-width: 92%;
  min-width: 0;
  padding: var(--dg-space-3) var(--dg-space-4);
  border-radius: var(--dg-radius);
  font-size: var(--dg-text-sm);
  line-height: var(--dg-leading-normal);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.dg-ai-message--assistant {
  background: var(--dg-surface);
  border: 1px solid var(--dg-line);
  border-bottom-left-radius: var(--dg-radius-xs);
  align-self: flex-start;
  color: var(--dg-ink);
  white-space: normal;             /* rendered blocks manage their own spacing */
}
.dg-ai-message--user {
  background: var(--dg-maroon);
  border-bottom-right-radius: var(--dg-radius-xs);
  align-self: flex-end;
  color: var(--dg-surface);
}
.dg-ai-message--error {
  background: var(--dg-danger-tint);
  border: 1px solid rgba(166,54,44,0.3);
  color: var(--dg-danger);
  align-self: flex-start;
  white-space: normal;
}
.dg-ai-retry {
  display: inline-block;
  margin-top: var(--dg-space-2);
  padding: 6px var(--dg-space-3);
  border: 1px solid currentColor;
  border-radius: var(--dg-radius-pill);
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: var(--dg-text-xs);
  font-weight: 650;
  cursor: pointer;
}
.dg-ai-retry:hover { background: rgba(166,54,44,0.08); }

/* Rendered answer content (markdown → DOM, see ai-widget.js). */
.dg-ai-message--assistant > :first-child { margin-top: 0; }
.dg-ai-message--assistant > :last-child { margin-bottom: 0; }
.dg-ai-message--assistant p { margin: 0 0 0.65em; }
.dg-ai-message--assistant h3,
.dg-ai-message--assistant h4 {
  margin: 0.9em 0 0.35em;
  font-family: var(--dg-font-display);
  font-size: var(--dg-text-sm);
  line-height: 1.3;
  color: var(--dg-maroon-deep);
}
.dg-ai-message--assistant h3 { font-size: var(--dg-text-base); }
.dg-ai-message--assistant ul,
.dg-ai-message--assistant ol { margin: 0 0 0.65em; padding-left: 1.3em; }
.dg-ai-message--assistant li { margin: 0 0 0.25em; padding-left: 0.1em; }
.dg-ai-message--assistant li > ul,
.dg-ai-message--assistant li > ol { margin: 0.25em 0 0; }
.dg-ai-message--assistant strong { font-weight: 700; }
.dg-ai-message--assistant hr { border: 0; border-top: 1px solid var(--dg-line); margin: 0.8em 0; }
.dg-ai-message--assistant code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--dg-maroon-tint-06);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}
.dg-ai-message--assistant pre {
  margin: 0 0 0.65em;
  padding: var(--dg-space-3);
  border-radius: var(--dg-radius-sm);
  background: var(--dg-maroon-tint-06);
  border: 1px solid var(--dg-line);
  overflow-x: auto;                /* scrolls inside the bubble, never the page */
  white-space: pre;
  max-width: 100%;
}
.dg-ai-message--assistant pre code { padding: 0; background: none; font-size: var(--dg-text-xs); }
.dg-ai-table {
  margin: 0 0 0.65em;
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid var(--dg-line);
  border-radius: var(--dg-radius-sm);
}
.dg-ai-table table { border-collapse: collapse; width: 100%; font-size: var(--dg-text-xs); }
.dg-ai-table th,
.dg-ai-table td { padding: 6px 10px; border-bottom: 1px solid var(--dg-line-soft); text-align: left; vertical-align: top; }
.dg-ai-table th { background: var(--dg-maroon-tint-06); font-weight: 650; white-space: nowrap; }
.dg-ai-table tr:last-child td { border-bottom: 0; }

/* Loading: a stated activity plus three quiet dots. */
.dg-ai-message--pending {
  display: inline-flex;
  align-items: center;
  gap: var(--dg-space-3);
  color: var(--dg-ink-muted);
  font-size: var(--dg-text-xs);
  user-select: none;
}
.dg-ai-dots {
  display: inline-block;
  position: relative;
  width: 6px; height: 6px;
  margin-right: 24px;
  border-radius: 50%;
  background: var(--dg-ink-faint);
  box-shadow: 12px 0 0 var(--dg-ink-faint), 24px 0 0 var(--dg-ink-faint);
  animation: dg-ai-typing 1.1s ease-in-out infinite;
}
@keyframes dg-ai-typing {
  0%, 100% { opacity: 0.35; }
  50% { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .dg-ai-dots { animation: none; opacity: 0.6; }
}

/* ---------- Quick actions ---------- */

.dg-ai-starters {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  gap: var(--dg-space-2);
  padding: 0 var(--dg-space-4) var(--dg-space-3);
  background: var(--dg-surface-alt);
}
.dg-ai-starters[hidden] { display: none; }
.dg-ai-starters__label {
  width: 100%;
  margin: 0 0 var(--dg-space-1);
  font-size: var(--dg-text-2xs);
  font-weight: 600;
  color: var(--dg-ink-muted);
}
.dg-ai-starter {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px var(--dg-space-3);
  border: 1px solid var(--dg-line-strong);
  border-radius: var(--dg-radius-pill);
  background: var(--dg-surface);
  color: var(--dg-ink);
  font-family: var(--dg-font-body);
  font-size: var(--dg-text-xs);
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: border-color var(--dg-transition), color var(--dg-transition), background var(--dg-transition);
}
.dg-ai-starter:hover { border-color: var(--dg-maroon); color: var(--dg-maroon); }
.dg-ai-starter svg {
  width: 14px; height: 14px; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
  color: var(--dg-gold-deep);
}

/* ---------- Highlighted text ---------- */

.dg-ai-selection {
  flex: none;
  display: flex;
  align-items: center;
  gap: var(--dg-space-2);
  padding: var(--dg-space-2) var(--dg-space-3) var(--dg-space-2) var(--dg-space-4);
  background: var(--dg-surface);
  border-top: 1px solid var(--dg-line);
  font-size: var(--dg-text-2xs);
}
.dg-ai-selection[hidden] { display: none; }
.dg-ai-selection__label { flex: none; color: var(--dg-ink-muted); font-weight: 600; }
.dg-ai-selection__text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--dg-maroon-deep);
  font-style: italic;
}
.dg-ai-selection__clear {
  flex: none;
  width: 32px; height: 32px;
  border: 0; border-radius: 50%;
  background: transparent;
  color: var(--dg-ink-muted);
  font-size: 20px; line-height: 1;
  cursor: pointer;
}
.dg-ai-selection__clear:hover { background: var(--dg-maroon-tint-06); color: var(--dg-maroon); }

/* ---------- Composer ---------- */

.dg-ai-panel__form {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: var(--dg-space-2);
  padding: var(--dg-space-3);
  border-top: 1px solid var(--dg-line);
  background: var(--dg-surface);
}
.dg-ai-panel__form textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 1px solid var(--dg-line-strong);
  border-radius: var(--dg-radius);
  padding: var(--dg-space-3) var(--dg-space-4);
  font-family: var(--dg-font-body);
  font-size: var(--dg-text-sm);
  line-height: var(--dg-leading-normal);
  color: var(--dg-ink);
  background: var(--dg-surface);
  max-height: 100px;
  transition: border-color var(--dg-transition), box-shadow var(--dg-transition);
}
.dg-ai-panel__form textarea:focus {
  outline: none;
  border-color: var(--dg-maroon);
  box-shadow: var(--dg-focus-ring);
}
.dg-ai-panel__form button {
  flex-shrink: 0;
  width: 44px; height: 44px;
  border: 0; border-radius: 50%;
  background: var(--dg-maroon); color: var(--dg-surface);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background var(--dg-transition);
}
.dg-ai-panel__form button:hover:not(:disabled) { background: var(--dg-maroon-deep); }
.dg-ai-panel__form button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.dg-ai-panel__form button:disabled { opacity: 0.45; cursor: default; }

.dg-ai-panel__disclaimer {
  flex: none;
  padding: 0 var(--dg-space-4) var(--dg-space-3);
  margin: 0;
  background: var(--dg-surface);
  font-size: 0.6875rem;
  color: var(--dg-ink-faint);
  text-align: center;
}

/* Focus lands on the panel itself when it opens on a phone; it is a
   container, not a control, so it gets no ring. */
.dg-ai-panel:focus { outline: none; }

/* Keyboard up on a phone: give the conversation every spare pixel. */
.dg-ai-panel.is-compact .dg-ai-starters,
.dg-ai-panel.is-compact .dg-ai-panel__disclaimer,
.dg-ai-panel.is-compact .dg-ai-context,
.dg-ai-panel.is-compact .dg-ai-panel__role,
.dg-ai-panel.is-compact .dg-ai-panel__header::before { display: none; }
.dg-ai-panel.is-compact .dg-ai-panel__header { padding-top: var(--dg-space-2); padding-bottom: var(--dg-space-2); }
.dg-ai-panel.is-compact .dg-ai-panel__close { margin-top: -2px; margin-bottom: -2px; }

/* ---------- Phones: bottom sheet ---------- */

.dg-ai-backdrop { display: none; }

@media (max-width: 600px) {
  .dg-ai-backdrop {
    position: fixed;
    inset: 0;
    z-index: -1;                   /* under the sheet, inside the widget's stacking context */
    background: rgba(32, 27, 22, 0.45);
  }
  .dg-ai-backdrop[hidden] { display: none; }
  .dg-ai-backdrop:not([hidden]) { display: block; }

  /* Fixed to the screen, not to the launcher. ai-widget.js sets top/height
     inline from window.visualViewport so the sheet fills what is actually
     visible when the on-screen keyboard is up; these are the no-JS/older
     browser defaults (dvh where supported, vh otherwise). */
  .dg-ai-panel {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    width: 100%; max-width: 100%;
    height: 88vh;
    height: 88dvh;
    max-height: 100%;
    border: 0;
    border-radius: var(--dg-radius-lg) var(--dg-radius-lg) 0 0;
    animation: dg-ai-sheet var(--dg-transition-slow) both;
  }
  .dg-ai-panel__header {
    padding-top: var(--dg-space-4);
    position: relative;
  }
  /* A grabber that reads as a sheet; purely decorative. */
  .dg-ai-panel__header::before {
    content: "";
    position: absolute;
    top: 6px; left: 50%;
    width: 36px; height: 4px;
    margin-left: -18px;
    border-radius: 2px;
    background: rgba(255,255,255,0.35);
  }
  .dg-ai-panel__form { padding-bottom: max(var(--dg-space-3), env(safe-area-inset-bottom, 0px)); }
  .dg-ai-panel__disclaimer { padding-bottom: max(var(--dg-space-3), env(safe-area-inset-bottom, 0px)); }
  /* iOS zooms the page when a focused field is under 16px. */
  .dg-ai-panel__form textarea { font-size: 16px; }
  .dg-ai-message { max-width: 94%; }
}
@keyframes dg-ai-sheet {
  from { transform: translateY(24px); opacity: 0.6; }
  to { transform: none; opacity: 1; }
}
@media (prefers-reduced-motion: reduce) { .dg-ai-panel { animation: none; } }

/* While the sheet is open on a phone the page behind it must not scroll
   (nor slide sideways). Applied by ai-widget.js only in that state. */
html.dg-ai-lock, html.dg-ai-lock body { overflow: hidden; }

/* Technical reason, shown to logged-in site administrators only. */
.dg-ai-admin-note {
  display: block;
  margin-top: var(--dg-space-2);
  font-size: 12px;
  line-height: 1.4;
  word-break: break-word;
  opacity: 0.85;
}
