.gallery-commentary {
  position: fixed;
  z-index: 21;
  inset-inline-start: max(12px, env(safe-area-inset-left, 0px));
  inset-block-end: max(16px, env(safe-area-inset-bottom, 0px));
  max-inline-size: var(--commentary-max-width, calc(100vw - 24px));
  color: var(--text);
  font-size: .9rem;
  line-height: 1.45;
  cursor: grab;
  touch-action: pinch-zoom;
  -webkit-user-select: none;
  user-select: none;
}

.gallery-commentary[data-positioned] {
  inset: var(--commentary-top) auto auto var(--commentary-left);
  inline-size: max-content;
}

.gallery-commentary[hidden],
.gallery-commentary [hidden],
.gallery-commentary-restore[hidden],
body:has(dialog[open]) .gallery-commentary {
  display: none;
}

@media (hover: hover) and (pointer: fine) and (min-width: 601px) {
  body:has(.gallery-navigation:not([hidden])) .gallery-commentary:not([data-positioned]) {
    inset-inline-start: max(clamp(46px, 3.2vw, 64px), env(safe-area-inset-left, 0px));
  }
}

.gallery-commentary button,
.gallery-commentary-restore {
  color: inherit;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

.gallery-commentary__trigger,
.gallery-commentary__launcher,
.gallery-commentary__panel {
  border: 1px solid #625b64;
  border-radius: .5rem;
  background: #1b191e;
  box-shadow: 0 6px 24px #0008;
}

.gallery-commentary__launcher {
  display: inline-flex;
  align-items: stretch;
}

.gallery-commentary[data-dragging],
.gallery-commentary[data-dragging] :is(button, summary) { cursor: grabbing; }

.gallery-commentary__trigger {
  display: flex;
  align-items: center;
  gap: .55rem;
  min-block-size: 44px;
  padding: .5rem .85rem;
  font-weight: 600;
}

.gallery-commentary__launcher .gallery-commentary__trigger {
  min-inline-size: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.gallery-commentary__trigger:not(:has(.gallery-commentary__robot))::before {
  content: '';
  inline-size: .44rem;
  block-size: .44rem;
  border-radius: 50%;
  background: var(--accent);
}

.gallery-commentary__trigger:hover { border-color: var(--accent); }

.gallery-commentary__robot {
  position: relative;
  inline-size: 20px;
  block-size: 22px;
  flex: none;
  pointer-events: none;
}

.gallery-commentary__robot-head {
  position: absolute;
  z-index: 1;
  inset: 3px 1px auto;
  block-size: 12px;
  border: 1px solid #6f89ec;
  border-radius: 4px;
  background: linear-gradient(#9fb4ff, #526ccd);
  box-shadow: inset 0 1px #b6c6ff8c;
}

.gallery-commentary__robot-head::before {
  content: '';
  position: absolute;
  inset: -4px 2px auto auto;
  inline-size: 2px;
  block-size: 4px;
  border-radius: 1px;
  background: var(--accent, #e18842);
}

.gallery-commentary__robot-screen {
  position: absolute;
  inset: 2px 2px 1px;
  border-radius: 2px;
  background: #192344;
  color: #a4efff;
}

.gallery-commentary__robot-screen::before,
.gallery-commentary__robot-screen::after {
  content: '';
  position: absolute;
}

.gallery-commentary__robot-screen::before {
  inset: 2px auto auto 2px;
  inline-size: 3px;
  block-size: 3px;
  border: solid currentColor;
  border-width: 1px 1px 0 0;
  transform: rotate(45deg);
}

.gallery-commentary__robot-screen::after {
  inset: auto 1px 1px auto;
  inline-size: 3px;
  block-size: 1px;
  background: currentColor;
}

.gallery-commentary__robot-body {
  position: absolute;
  inset: 14px 5px auto;
  block-size: 5px;
  border-radius: 2px;
  background: linear-gradient(#7596fa, #475eb4);
  box-shadow: -3px 1px 0 -1px #5974ca, 3px 1px 0 -1px #5974ca;
}

.gallery-commentary__robot-body::before {
  content: '';
  position: absolute;
  inset: 4px auto auto 1px;
  inline-size: 3px;
  block-size: 3px;
  border-radius: 1px;
  background: #5f7cde;
  box-shadow: 5px 0 #5f7cde;
}

.gallery-commentary__robot-body::after {
  content: '';
  position: absolute;
  inset: 2px 4px auto;
  block-size: 1px;
  background: #c1e3ff;
}

.gallery-commentary__panel {
  display: flex;
  flex-direction: column;
  inline-size: 22rem;
  max-inline-size: min(calc(100vw - 80px), var(--commentary-max-width, 100vw));
  max-block-size: min(65vh, 30rem, var(--commentary-max-height, 100vh));
  max-block-size: min(65dvh, 30rem, var(--commentary-max-height, 100vh));
  overflow: hidden;
}

.gallery-commentary__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding-inline-start: .9rem;
  border-block-end: 1px solid #3c3740;
  flex: none;
}

.gallery-commentary__header:has(.gallery-commentary__toggle) {
  padding-inline-start: 0;
}

.gallery-commentary__header h2 {
  display: flex;
  align-items: center;
  gap: .45rem;
  margin: 0;
  font: inherit;
  font-weight: 700;
  flex: 1 1 auto;
}

.gallery-commentary__toggle {
  display: flex;
  align-items: center;
  gap: .45rem;
  inline-size: 100%;
  min-block-size: 44px;
  padding: .35rem .85rem;
  border: 0;
  background: transparent;
  text-align: start;
}

.gallery-commentary__toggle:hover { background: #e1884214; }
.gallery-commentary__toggle:focus-visible { outline-offset: -3px; }

.gallery-commentary .gallery-commentary__close {
  border: 0;
  background: transparent;
  inline-size: 44px;
  block-size: 44px;
  font-size: 1.5rem;
  flex: 0 0 44px;
}

.gallery-commentary__launcher .gallery-commentary__close {
  border-inline-start: 1px solid #3c3740;
  color: var(--muted);
}

.gallery-commentary__close:hover { color: var(--text); }
.gallery-commentary__close:focus-visible { outline-offset: -4px; }

.gallery-commentary__body {
  min-block-size: 0;
  padding: .65rem .9rem .35rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  overflow-wrap: anywhere;
  touch-action: pinch-zoom;
}

.gallery-commentary__subject {
  margin: 0 0 .25rem;
  color: var(--accent);
  font-size: .8rem;
}

.gallery-commentary__note { margin: 0 0 .55rem; }

.gallery-commentary__questions {
  margin-block-start: .75rem;
  border-block-start: 1px solid #3c3740;
}

.gallery-commentary__questions details { border-block-end: 1px solid #3c3740; }
.gallery-commentary__questions summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  min-block-size: 44px;
  padding-block: .4rem;
  list-style: none;
  cursor: pointer;
}

.gallery-commentary__questions summary::-webkit-details-marker { display: none; }
.gallery-commentary__questions summary::after {
  content: '';
  flex: none;
  inline-size: .45rem;
  block-size: .45rem;
  margin-inline-end: .2rem;
  border: solid var(--accent);
  border-width: 0 1.5px 1.5px 0;
  transform: rotate(45deg);
}

.gallery-commentary__questions details[open] summary::after { transform: rotate(225deg); }
.gallery-commentary__questions summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.gallery-commentary__questions p { margin: 0 0 .7rem; color: var(--muted); }

.gallery-commentary__questions > .gallery-commentary__choice {
  display: grid;
  grid-template-columns: .6rem 1fr;
  align-items: center;
  gap: .55rem;
  inline-size: 100%;
  min-block-size: 44px;
  padding: .35rem .25rem;
  border: 0;
  border-block-end: 1px solid #3c3740;
  background: transparent;
  text-align: start;
}

.gallery-commentary__choice::before {
  content: '';
  inline-size: .35rem;
  block-size: .35rem;
  border: solid var(--accent);
  border-width: 1.5px 1.5px 0 0;
  transform: rotate(45deg);
}

.gallery-commentary__choice:is(:hover, :focus-visible) {
  background: #e1884214;
  outline-offset: -2px;
}

.gallery-commentary__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 .8rem;
}

.gallery-commentary__actions button,
.gallery-commentary-restore {
  min-block-size: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  text-decoration: underline;
  text-underline-offset: .2em;
}

.gallery-commentary .gallery-commentary__dismiss,
.gallery-commentary .gallery-commentary__back,
.gallery-commentary .gallery-commentary__restart,
.gallery-commentary-restore {
  color: var(--muted);
  font-size: .82rem;
}

.gallery-commentary-restore { margin-inline-start: 1rem; }

@media (hover: none), (pointer: coarse), (max-width: 600px) {
  .gallery-commentary {
    font-size: .82rem;
    line-height: 1.35;
  }

  .gallery-commentary__launcher .gallery-commentary__trigger {
    gap: .27rem;
    min-block-size: 36px;
    padding: .2rem .4rem;
    font-size: .76rem;
  }

  .gallery-commentary .gallery-commentary__close {
    flex-basis: 32px;
    inline-size: 32px;
    block-size: 36px;
    font-size: 1.2rem;
  }

  .gallery-commentary__header { gap: .3rem; }

  .gallery-commentary__toggle {
    gap: .35rem;
    min-block-size: 36px;
    padding: .2rem .65rem;
  }

  .gallery-commentary__panel {
    inline-size: min(20rem, 86vw);
    max-inline-size: var(--commentary-max-width, calc(100vw - 24px));
    max-block-size: min(49vh, 25rem, var(--commentary-max-height, 100vh));
    max-block-size: min(49dvh, 25rem, var(--commentary-max-height, 100vh));
  }

  .gallery-commentary__body { padding: .45rem .65rem .25rem; }
  .gallery-commentary__subject { margin-block-end: .15rem; font-size: .73rem; }
  .gallery-commentary__note { margin-block-end: .35rem; }
  .gallery-commentary__questions { margin-block-start: .4rem; }
  .gallery-commentary__questions p { margin-block-end: .45rem; }
  .gallery-commentary__questions summary,
  .gallery-commentary__questions > .gallery-commentary__choice,
  .gallery-commentary__actions button {
    min-block-size: 34px;
    padding-block: .2rem;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .gallery-commentary__robot { animation: gallery-commentary-bob 1.55s ease-in-out infinite; }
  .gallery-commentary__robot-screen::after { animation: gallery-commentary-cursor 1.55s steps(1, end) infinite; }
  .gallery-commentary__panel:not([hidden]) { animation: gallery-commentary-in 160ms ease-out; }
  @keyframes gallery-commentary-bob {
    0%, 74%, 100% { transform: translateY(0); }
    35% { transform: translateY(-2px); }
  }

  @keyframes gallery-commentary-cursor {
    0%, 78% { opacity: 1; }
    58%, 100% { opacity: .25; }
  }

  @keyframes gallery-commentary-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
  }
}
