/*
 * Shared dialogs + toasts (js/ah-dialog.js). Plain CSS, not Tailwind, so the same
 * file works on headless pages, standalone pages (chat, upload iframe) and
 * Bootstrap 3 admin screens. Colours read the headless tokens (css/headless.css
 * :root) with fallbacks for pages that do not load it. Everything is namespaced
 * .ah-dlg / .ah-btn / .ah-field / .ah-toast so Bootstrap's .btn/.modal never collide.
 * The dialog box is selected as dialog.ah-dlg[open] to outrank headless.css's
 * global dialog[open] rule (used by the page-specific dialogs).
 */
.ah-dlg-open { overflow: hidden; }

dialog.ah-dlg, dialog.ah-dlg[open] {
  --ah-bg: var(--card, #fff);
  --ah-fg: var(--foreground, #1b2133);
  --ah-muted: var(--muted-foreground, #6b7280);
  --ah-border: var(--border, #e5e7eb);
  --ah-ring: var(--ring, #3b82f6);
  --ah-accent: var(--ink, #1b2133);
  --ah-accent-fg: var(--ink-foreground, #fff);
  --ah-danger: var(--destructive, #dc2626);
  --ah-danger-fg: var(--destructive-foreground, #fff);
  --ah-success: var(--success, #16a34a);
  --ah-warning: #d97706;
  --ah-info: var(--primary, #3b82f6);
  --ah-secondary: var(--secondary, #f3f4f6);

  position: fixed;
  inset: 0;
  z-index: auto;
  box-sizing: border-box;
  width: calc(100% - 32px);
  max-width: 28rem;
  max-height: calc(100% - 32px);
  margin: auto;
  padding: 0;
  border: 1px solid var(--ah-border);
  border-radius: 1rem;
  background: var(--ah-bg);
  color: var(--ah-fg);
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
  font: inherit;
  font-size: 14px;
  line-height: 1.5;
  overflow: auto;
}
dialog.ah-dlg[open]:has(.ah-dlg__form) { max-width: 32rem; }
dialog.ah-dlg::backdrop { background: rgb(15 23 42 / 0.45); }
.ah-dlg *, .ah-dlg *::before, .ah-dlg *::after { box-sizing: border-box; }
dialog.ah-dlg[open] { animation: ah-dlg-in 140ms ease-out; }
@keyframes ah-dlg-in { from { opacity: 0; transform: translateY(6px) scale(0.98); } }

.ah-dlg__panel { padding: 24px; }
.ah-dlg__head { display: flex; align-items: flex-start; gap: 12px; }
.ah-dlg__icon {
  display: inline-flex; flex: none; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 9999px;
  background: color-mix(in oklab, var(--ah-tone, var(--ah-info)) 12%, transparent);
  color: var(--ah-tone, var(--ah-info));
}
.ah-dlg__icon svg { width: 20px; height: 20px; }
.ah-dlg--success { --ah-tone: var(--ah-success); }
.ah-dlg--warning { --ah-tone: var(--ah-warning); }
.ah-dlg--error, .ah-dlg--destructive { --ah-tone: var(--ah-danger); }

.ah-dlg__title {
  flex: 1; margin: 0; align-self: center;
  font-size: 18px; font-weight: 600; line-height: 1.35; letter-spacing: -0.01em; color: var(--ah-fg);
}
.ah-dlg__x {
  display: inline-flex; flex: none; align-items: center; justify-content: center;
  width: 32px; height: 32px; margin: -4px -8px 0 0; padding: 0;
  border: 0; border-radius: 9999px; background: transparent; color: var(--ah-muted); cursor: pointer;
}
.ah-dlg__x:hover { background: var(--ah-secondary); color: var(--ah-fg); }
.ah-dlg__x-icon svg { width: 18px; height: 18px; }
.ah-dlg__msg { margin: 8px 0 0; color: var(--ah-muted); white-space: pre-line; }
/* Indent text under the icon on wide screens. */
@media (min-width: 640px) {
  .ah-dlg__head:has(.ah-dlg__icon) ~ .ah-dlg__msg,
  .ah-dlg__head:has(.ah-dlg__icon) ~ .ah-dlg__error { margin-left: 52px; }
}
.ah-dlg__body { margin-top: 16px; }
.ah-dlg__error {
  margin: 16px 0 0; padding: 8px 12px; border-radius: 8px;
  border: 1px solid color-mix(in oklab, var(--ah-danger) 30%, transparent);
  background: color-mix(in oklab, var(--ah-danger) 10%, transparent);
  color: var(--ah-fg);
}
.ah-dlg__error[hidden] { display: none; }

.ah-dlg__actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 24px; }
@media (max-width: 479px) {
  .ah-dlg__actions { flex-direction: column-reverse; }
  .ah-dlg__actions .ah-btn { width: 100%; }
}

/* Buttons */
.ah-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 0 20px; margin: 0;
  border: 1px solid transparent; border-radius: 9999px;
  font: inherit; font-size: 14px; font-weight: 500; line-height: 1;
  cursor: pointer; transition: opacity 120ms, background-color 120ms;
}
.ah-btn:focus-visible, .ah-dlg__x:focus-visible, .ah-toast__x:focus-visible {
  outline: 2px solid var(--ah-ring, #3b82f6); outline-offset: 2px;
}
.ah-btn:disabled { cursor: not-allowed; opacity: 0.6; }
.ah-btn--primary { background: var(--ah-accent); color: var(--ah-accent-fg); }
.ah-btn--destructive { background: var(--ah-danger); color: var(--ah-danger-fg); }
.ah-btn--primary:hover:not(:disabled), .ah-btn--destructive:hover:not(:disabled) { opacity: 0.9; }
.ah-btn--secondary { background: transparent; border-color: var(--ah-border); color: var(--ah-fg); }
.ah-btn--secondary:hover:not(:disabled) { background: var(--ah-secondary); }
.ah-btn.is-loading::before {
  content: ""; width: 14px; height: 14px; border-radius: 9999px;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: ah-spin 700ms linear infinite;
}
@keyframes ah-spin { to { transform: rotate(360deg); } }

/* Form fields */
.ah-dlg__form { display: grid; gap: 16px; margin: 0; }
.ah-field { display: grid; gap: 6px; }
.ah-field__label { margin: 0; font-size: 14px; font-weight: 500; color: var(--ah-fg); max-width: none; }
.ah-field__input {
  width: 100%; min-height: 40px; padding: 8px 12px; margin: 0;
  border: 1px solid var(--ah-border); border-radius: 10px;
  background: var(--background, #fff); color: var(--ah-fg);
  font: inherit; font-size: 14px; box-shadow: none;
}
textarea.ah-field__input { resize: vertical; min-height: 88px; }
.ah-field__input:focus { outline: 2px solid var(--ah-ring); outline-offset: 0; border-color: transparent; }
.ah-field__hint { margin: 0; font-size: 12px; color: var(--ah-muted); }

/* Toasts */
.ah-toasts {
  position: fixed; z-index: 2147483000; right: 16px; bottom: 16px; left: 16px;
  display: flex; flex-direction: column; align-items: flex-end; gap: 8px;
  pointer-events: none;
}
@media (min-width: 640px) { .ah-toasts { left: auto; width: 24rem; } }
.ah-toast {
  --ah-tone: var(--primary, #3b82f6);
  box-sizing: border-box; pointer-events: auto;
  display: flex; align-items: flex-start; gap: 10px; width: 100%;
  padding: 12px 12px 12px 14px;
  border: 1px solid var(--border, #e5e7eb); border-left: 4px solid var(--ah-tone);
  border-radius: 12px; background: var(--card, #fff); color: var(--foreground, #1b2133);
  box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  font-size: 14px; line-height: 1.45;
  animation: ah-toast-in 160ms ease-out;
}
@keyframes ah-toast-in { from { opacity: 0; transform: translateY(8px); } }
.ah-toast--success { --ah-tone: var(--success, #16a34a); }
.ah-toast--warning { --ah-tone: #d97706; }
.ah-toast--error { --ah-tone: var(--destructive, #dc2626); }
.ah-toast__icon { flex: none; color: var(--ah-tone); margin-top: 1px; }
.ah-toast__icon svg { display: block; width: 18px; height: 18px; }
.ah-toast__text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.ah-toast__title { margin: 0; font-weight: 600; }
.ah-toast__msg { margin: 0; }
.ah-toast__x {
  display: inline-flex; flex: none; align-items: center; justify-content: center;
  width: 24px; height: 24px; padding: 0; border: 0; border-radius: 9999px;
  background: transparent; color: var(--muted-foreground, #6b7280); cursor: pointer;
}
.ah-toast__x:hover { color: var(--foreground, #1b2133); }
.ah-toast__x-icon svg { display: block; width: 16px; height: 16px; }

@media (prefers-reduced-motion: reduce) {
  dialog.ah-dlg[open], .ah-toast { animation: none; }
  .ah-btn.is-loading::before { animation-duration: 2s; }
}
