/* All rules are confined to addon elements or explicitly enhanced images. */
.aimg-upload {
  --aimg-bg: #f8fafc;
  --aimg-border: #cbd5e1;
  --aimg-text: #334155;
  --aimg-muted: #475569;
  --aimg-accent: #1d4ed8;
  --aimg-error: #b91c1c;
  box-sizing: border-box;
  margin: .65rem 0 1rem;
  padding: .75rem;
  border: 1px solid var(--aimg-border);
  border-radius: .65rem;
  background: var(--aimg-bg);
  color: var(--aimg-text);
  min-width: 0;
  max-width: 100%;
  font: inherit;
  text-align: left;
}
.aimg-upload *, .aimg-viewer * { box-sizing: border-box; }
.aimg-upload [hidden], .aimg-viewer [hidden], .aimg-caption [hidden] { display: none !important; }
.aimg-upload-actions { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.aimg-button {
  appearance: none;
  box-sizing: border-box;
  border: 1px solid var(--aimg-border, #475569);
  border-radius: .5rem;
  padding: .5rem .75rem;
  min-height: 40px;
  min-width: 40px;
  margin: 0;
  color: inherit;
  background: transparent;
  font: inherit;
  font-size: .875rem;
  font-weight: 500;
  line-height: 1.4;
  white-space: nowrap;
  cursor: pointer;
}
.aimg-upload-button { color: #fff; background: var(--aimg-accent); border-color: var(--aimg-accent); font-weight: 600; }
.aimg-button:disabled { cursor: wait; opacity: .55; }
/* Upload-only appearance; native disabled remains owned by host validation. */
[data-aimg-submit-lock] { cursor: wait !important; opacity: .55 !important; }
.aimg-button:focus-visible, .aimg-thumbnail:focus-visible, img[data-aimg-zoom]:focus-visible, .aimg-viewport:focus-visible { outline: 3px solid #60a5fa; outline-offset: 3px; }
.aimg-help, .aimg-status { margin: .5rem 0 0; overflow-wrap: anywhere; font-size: .8rem; line-height: 1.65; }
.aimg-help { color: var(--aimg-muted); }
.aimg-status:empty { display: none; }
.aimg-status.aimg-error { color: var(--aimg-error); }
.aimg-progress { display: block; width: 100%; height: .5rem; margin-top: .65rem; accent-color: var(--aimg-accent); }
.aimg-preview { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .65rem; }
.aimg-thumbnail { appearance: none; padding: 0; margin: 0; border: 1px solid var(--aimg-border); background: transparent; border-radius: .4rem; overflow: hidden; width: 72px; height: 72px; cursor: zoom-in; }
.aimg-thumbnail img { display: block; width: 100%; height: 100%; object-fit: contain; }
.markdown-body img[data-aimg-zoom] {
  display: block; width: auto !important; max-width: 100% !important; height: auto !important;
  max-height: min(48vh, 420px) !important; object-fit: contain; cursor: zoom-in;
  margin: .75rem auto .25rem; border-radius: .65rem;
}
.markdown-body img[data-aimg-state="loading"], .markdown-body img[data-aimg-state="error"] { min-height: 80px; min-width: min(240px, 100%); background: rgb(148 163 184 / .12); }
.markdown-body .aimg-caption {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: .4rem .6rem;
  box-sizing: border-box; max-width: 100%; margin: .25rem 0 1.1rem; padding: .45rem .6rem;
  border-radius: .65rem; background: rgb(148 163 184 / .09); color: inherit;
  font: 12px/1.5 system-ui, sans-serif;
}
.aimg-caption-label { min-width: 0; overflow-wrap: anywhere; opacity: .8; }
.markdown-body .aimg-caption .aimg-button { padding: .35rem .65rem; min-height: 36px; font-size: 12px; border-color: rgb(100 116 139 / .35); color: inherit; }
.markdown-body .aimg-caption .aimg-open { font-weight: 600; background: rgb(59 130 246 / .1); }
.aimg-caption .aimg-button:focus-visible { outline: 3px solid #60a5fa; outline-offset: 2px; }
.aimg-viewer {
  --aimg-border: #64748b;
  box-sizing: border-box;
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100vh;
  max-height: 100dvh;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: #0b1220;
  color: #f1f5f9;
  overflow: hidden;
  font: 14px/1.5 system-ui, sans-serif;
  overscroll-behavior: contain;
  color-scheme: dark;
}
.aimg-viewer[open] { display: flex; flex-direction: column; }
.aimg-viewer::backdrop { background: rgb(2 6 23 / .9); }
.aimg-viewer-toolbar { display: flex; flex-wrap: wrap; gap: .375rem; align-items: center; padding: .6rem max(.6rem, env(safe-area-inset-right)) .6rem max(.6rem, env(safe-area-inset-left)); padding-top: max(.6rem, env(safe-area-inset-top)); flex: none; border-bottom: 1px solid #334155; }
.aimg-viewer-title { flex: 1 1 120px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.aimg-viewer-tools { display: flex; flex-wrap: wrap; align-items: center; gap: .375rem; }
.aimg-viewer [data-aimg-close] { order: 1; background: #243249; }
.aimg-viewer-navigation { display: flex; align-items: center; justify-content: center; gap: .65rem; }
.aimg-viewer-navigation .aimg-button:disabled { cursor: default; }
.aimg-count { min-width: 3rem; text-align: center; font-variant-numeric: tabular-nums; }
.aimg-viewer-footer { flex: none; display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: .25rem .75rem; padding: .35rem .75rem max(.5rem, env(safe-area-inset-bottom)); border-top: 1px solid #334155; }
.aimg-scale { font-size: .75rem; font-variant-numeric: tabular-nums; min-width: 3rem; text-align: center; }
.aimg-viewport { position: relative; flex: 1 1 auto; min-height: 0; min-width: 0; overflow: auto; overscroll-behavior: contain; touch-action: pan-x pan-y pinch-zoom; cursor: grab; scrollbar-color: #64748b #0b1220; }
.aimg-viewport:active { cursor: grabbing; }
.aimg-canvas { display: flex; align-items: center; justify-content: center; width: max-content; height: max-content; min-width: 100%; min-height: 100%; padding: 16px; }
.aimg-full-image { display: block; flex: none; max-width: none !important; max-height: none !important; object-fit: contain; user-select: none; -webkit-user-drag: none; }
.aimg-full-image:not([src]) { visibility: hidden; }
.aimg-viewer-message { margin: 0; padding: .25rem 0; font-size: .75rem; color: #cbd5e1; text-align: center; overflow-wrap: anywhere; }
.aimg-viewport[aria-busy="true"]::after { content: ''; position: absolute; top: calc(50% - 14px); left: calc(50% - 14px); width: 28px; height: 28px; border: 3px solid #334155; border-top-color: #93c5fd; border-radius: 50%; animation: aimg-spin .8s linear infinite; }
@keyframes aimg-spin { to { transform: rotate(360deg); } }
:is(.dark, [data-theme="dark"]) .aimg-upload {
  --aimg-bg: #162033; --aimg-border: #475569; --aimg-text: #e2e8f0; --aimg-muted: #cbd5e1; --aimg-accent: #2563eb; --aimg-error: #fca5a5;
}
@media (prefers-color-scheme: dark) {
  html:not(.light):not([data-theme="light"]) .aimg-upload { --aimg-bg: #162033; --aimg-border: #475569; --aimg-text: #e2e8f0; --aimg-muted: #cbd5e1; --aimg-accent: #2563eb; --aimg-error: #fca5a5; }
}
@media (max-width: 480px) {
  .aimg-upload { padding: .6rem; }
  .aimg-viewer-title { flex: 1 1 0; }
  .aimg-viewer-tools { flex-basis: 100%; order: 2; gap: .2rem; }
  .aimg-viewer [data-aimg-close] { order: 0; }
  .aimg-viewer-toolbar { gap: .25rem; }
  .aimg-viewer-toolbar .aimg-button { padding: .4rem .5rem; min-width: 36px; min-height: 44px; font-size: .8rem; }
  .aimg-scale { min-width: 2.5rem; }
  .aimg-viewer-message { font-size: .7rem; }
}
@media (max-height: 480px) and (min-width: 481px) {
  .aimg-viewer-toolbar { padding-top: .25rem; padding-bottom: .25rem; }
  .aimg-viewer-toolbar .aimg-button { min-height: 36px; padding: .35rem .5rem; }
  .aimg-viewer-footer { padding-top: .2rem; padding-bottom: .2rem; }
}
@media (prefers-reduced-motion: reduce) {
  .aimg-upload *, .aimg-viewer *, img[data-aimg-zoom] { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
  .aimg-viewport::after { animation: none !important; }
}
