.image-viewer {
  isolation: isolate;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  justify-items: center;
  align-items: center;
  justify-content: stretch;
  gap: 12px;
  top: var(--web-visual-top, 0px);
  bottom: auto;
  height: var(--form-active-height, var(--visible-height, var(--browser-viewport-height, 100dvh)));
  padding: max(64px, var(--tg-controls-top, 0px), env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.image-viewer[hidden] { display: none; }
.image-viewer .image-viewer-backdrop { z-index: -1; }
.image-viewer > img, .image-viewer .viewer-original-image {
  position: relative;
  z-index: 1;
  grid-row: 1;
  width: auto;
  height: auto;
  min-width: 0;
  min-height: 0;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.image-viewer:has(.image-viewer-edit:not([hidden])) img { max-height: 100%; }
.image-viewer-file { position: relative; z-index: 1; grid-row: 1; min-width: 0; box-sizing: border-box; overflow: auto; max-height: 100%; }
.image-viewer-actions { grid-row: 2; max-width: 100%; gap: 8px; }
.image-viewer-actions button, .image-viewer-actions a, .image-viewer-edit button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 44px;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background .18s ease, border-color .18s ease, opacity .18s ease;
}
.image-viewer-actions a { padding: 10px 16px; border: 1px solid #ffffff55; border-radius: 999px; color: #fff; background: #ffffff18; }
.image-viewer-actions button[hidden], .image-viewer-actions a[hidden] { display: none; }
.image-viewer .ui-icon { width: 18px; height: 18px; flex: 0 0 18px; }
.image-viewer .ui-download { --icon: var(--ic-download, url('icons/download.svg')); }
.image-viewer .ui-share { --icon: var(--ic-share, url('icons/share.svg')); }
.image-viewer .ui-external-link { --icon: var(--ic-globe, url('icons/globe.svg')); }
@media (hover: hover) and (pointer: fine) { .image-viewer-actions a:hover { background: #ffffff18; } }
@media (hover: hover) and (pointer: fine) { .image-viewer-actions button:hover { background: #ffffff18; } }
.image-viewer button:active, .image-viewer-actions a:active { background: #ffffff38; }
.image-viewer button:focus-visible, .image-viewer a:focus-visible, .image-viewer textarea:focus-visible { outline: 2px solid #74b0ff; outline-offset: 3px; }
.viewer-edit-reveal {
  position: relative;
  z-index: 2;
  grid-row: 3;
  width: min(620px, 100%);
  min-height: 0;
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows .28s cubic-bezier(.22,1,.36,1), opacity .2s ease;
  opacity: 0;
  pointer-events: none;
}
.viewer-edit-reveal.is-open { grid-template-rows: 1fr; opacity: 1; pointer-events: auto; }
.image-viewer .image-viewer-edit, .image-viewer .image-viewer-edit[hidden] {
  display: flex;
  position: relative;
  width: 100%;
  min-height: 0;
  overflow: hidden;
  gap: 8px;
}
.image-viewer-edit textarea { box-sizing: border-box; width: 100%; min-height: 64px; height: 80px; max-height: 140px; flex-shrink: 0; resize: none; }
.image-viewer-edit textarea:focus-visible { outline: none; border-color: #74b0ff; box-shadow: inset 0 0 0 1px #74b0ff; }
.image-viewer .image-viewer-edit[hidden] { visibility: hidden; }
.image-viewer-edit button { flex-shrink: 0; padding: 10px 16px; }
@media (hover: hover) and (pointer: fine) {
  .image-viewer-actions button:hover, .image-viewer-actions a:hover { background: #ffffff30; border-color: #ffffff88; }
  .image-viewer-edit button:hover { background: #dceaff; }
  .image-viewer #closeImageViewer:hover { background: #505050; }
}
@media (max-width: 760px) {
  .image-viewer { gap: 8px; }
  .image-viewer-actions button, .image-viewer-actions a { font-size: 13px; padding: 8px 11px; }
  .image-viewer-edit textarea { font-size: 16px; }
  :root.form-keyboard-overlay .image-viewer, :root.web-keyboard-open .image-viewer { padding-bottom: 8px; }
}
@media (prefers-reduced-motion: reduce) { .viewer-edit-reveal, .image-viewer button, .image-viewer a { transition: none; } }
