/**
 * Gloim Popup v5.0 - Frontend Modern Native <dialog> & Accessible Carousel Stylesheet
 *
 * Built with native <dialog> top-layer rendering, CSS scroll-snap carousel,
 * @starting-style + allow-discrete transitions, WCAG AA focus/target sizing,
 * and progressive scroll containment.
 */

/* Design Tokens (easily overridable from Style Editor) */
:root {
  --gloim-bg: #ffffff;
  --gloim-surface: #f8fafc;
  --gloim-text: #0f172a;
  --gloim-muted: #475569;
  --gloim-border: #e2e8f0;
  --gloim-accent: #2563eb;
  --gloim-accent-hover: #1d4ed8;
  --gloim-btn-bg: #0f172a;
  --gloim-btn-hover: #1e293b;
  --gloim-btn-text: #ffffff;
  --gloim-radius: 14px;
  --gloim-shadow: 0 24px 56px -12px rgba(15, 23, 42, 0.28), 0 8px 20px -8px rgba(15, 23, 42, 0.14);
  --gloim-gap: 24px;
  --gloim-focus: #2563eb;
  --gloim-backdrop: rgba(15, 23, 42, 0.65);
  --gloim-max-width: 560px;
}

/* Visually hidden utility (accessible to screen readers) */
.gloim-sr-only:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  white-space: nowrap !important;
}

/* ==========================================================================
   Native <dialog> Base, Top-Layer Positioning & Transitions
   Note: Never set display unconditionally on dialog — scope to [open]/:popover-open.
   ========================================================================== */
dialog.gloim-popup-dialog {
  box-sizing: border-box;
  padding: 0;
  border: 1px solid var(--gloim-border);
  border-radius: var(--gloim-radius);
  background: var(--gloim-bg);
  color: var(--gloim-text);
  box-shadow: var(--gloim-shadow);
  width: min(calc(100vw - 32px), var(--gloim-max-width));
  max-width: calc(100vw - 32px);
  max-height: min(88dvh, 88vh);
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Segoe UI", Roboto, "Noto Sans KR", sans-serif;

  /* Exit / closed animation base state */
  opacity: 0;
  transition-property: opacity, transform, display, overlay;
  transition-duration: 0.28s;
  transition-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
  /* Separate declaration after transition so older browsers still parse transitions */
  transition-behavior: allow-discrete;
}

@media (prefers-reduced-motion: no-preference) {
  dialog.gloim-popup-dialog {
    transform: translateY(10px) scale(0.96);
  }
}

/* Open state scoped strictly to open/popover-open and not closing */
:is(dialog.gloim-popup-dialog[open], dialog.gloim-popup-dialog:popover-open, dialog.gloim-popup-dialog.\:popover-open) {
  display: flex;
  flex-direction: column;
}

:is(dialog.gloim-popup-dialog[open], dialog.gloim-popup-dialog:popover-open, dialog.gloim-popup-dialog.\:popover-open):where(:not([data-closing])) {
  opacity: 1;

  @starting-style {
    opacity: 0;
  }

  @media (prefers-reduced-motion: no-preference) {
    transform: translateY(0) scale(1);

    @starting-style {
      transform: translateY(10px) scale(0.96);
    }
  }
}

/* Prevent scroll chaining to underlying page when modal is open */
dialog.gloim-popup-dialog:modal {
  overscroll-behavior: contain;
}

/* Modal Backdrop Styling & Scroll Containment */
dialog.gloim-popup-dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background-color: transparent;
  backdrop-filter: blur(0px);
  transition-property: background-color, backdrop-filter, display, overlay;
  transition-duration: 0.28s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}

dialog.gloim-popup-dialog[open]:where(:not([data-closing]))::backdrop {
  background-color: var(--gloim-backdrop);
  backdrop-filter: blur(2px);

  @starting-style {
    background-color: transparent;
    backdrop-filter: blur(0px);
  }
}

/* Non-modal floating popups (transparent backdrop) stay non-blocking */
dialog.gloim-popup-dialog.gloim-floating {
  position: fixed;
  inset: 0;
  z-index: 999990;
}

dialog.gloim-popup-dialog.gloim-floating::backdrop {
  display: none;
  background: transparent;
  pointer-events: none;
}

/* Reduced motion preference: short fade, no scaling */
@media (prefers-reduced-motion: reduce) {
  dialog.gloim-popup-dialog,
  dialog.gloim-popup-dialog::backdrop {
    transition-duration: 0.1s;
  }
}

/* ==========================================================================
   9-Grid Desktop Positioning (uses native <dialog> margin alignment so
   transform stays free for entry/exit animations)
   ========================================================================== */
dialog.gloim-popup-dialog[data-pos="top-left"]      { margin: var(--gloim-gap) auto auto var(--gloim-gap); }
dialog.gloim-popup-dialog[data-pos="top-center"]    { margin: var(--gloim-gap) auto auto auto; }
dialog.gloim-popup-dialog[data-pos="top-right"]     { margin: var(--gloim-gap) var(--gloim-gap) auto auto; }
dialog.gloim-popup-dialog[data-pos="middle-left"]   { margin: auto auto auto var(--gloim-gap); }
dialog.gloim-popup-dialog[data-pos="center"]        { margin: auto; }
dialog.gloim-popup-dialog[data-pos="middle-right"]  { margin: auto var(--gloim-gap) auto auto; }
dialog.gloim-popup-dialog[data-pos="bottom-left"]   { margin: auto auto var(--gloim-gap) var(--gloim-gap); }
dialog.gloim-popup-dialog[data-pos="bottom-center"] { margin: auto auto var(--gloim-gap) auto; }
dialog.gloim-popup-dialog[data-pos="bottom-right"]  { margin: auto var(--gloim-gap) var(--gloim-gap) auto; }

/* ==========================================================================
   Internal Shell, Focus Ring & Floating Close Icon
   ========================================================================== */
dialog.gloim-popup-dialog *,
dialog.gloim-popup-dialog *::before,
dialog.gloim-popup-dialog *::after {
  box-sizing: border-box;
}

dialog.gloim-popup-dialog :where(button, a, input, [tabindex]):focus-visible {
  outline: 3px solid var(--gloim-focus);
  outline-offset: 2px;
}

.gloim-popup-shell {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: min(88dvh, 88vh);
  background: var(--gloim-bg);
}

.gloim-icon-close {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 4;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 34px;
  min-block-size: 34px;
  padding: 6px;
  border: 1px solid rgba(15, 23, 42, 0.12);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: #0f172a;
  cursor: pointer;
  backdrop-filter: blur(6px);
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.12);
  transition: background-color 0.18s ease, color 0.18s ease, transform 0.18s ease;
}

.gloim-icon-close:hover {
  background: #0f172a;
  color: #ffffff;
}

.gloim-icon-close:active {
  transform: scale(0.95);
}

/* ==========================================================================
   Accessible Scroll-Snap Carousel
   ========================================================================== */
.gloim-carousel {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1 1 auto;
}

.gloim-carousel-track {
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  /* Contain horizontal overscroll on the track only */
  overscroll-behavior-x: contain;
  touch-action: manipulation;
  scrollbar-width: none;
}

.gloim-carousel-track::-webkit-scrollbar {
  display: none;
}

@media (prefers-reduced-motion: no-preference) {
  .gloim-carousel-track {
    scroll-behavior: smooth;
  }
}

.gloim-slide {
  flex: 0 0 100%;
  min-width: 100%;
  scroll-snap-align: center;
  scroll-snap-stop: always;
}

.gloim-body {
  padding: 20px;
  line-height: 1.65;
  color: var(--gloim-text);
  font-size: 15px;
  max-height: min(68dvh, 68vh);
  overflow-y: auto;
  overscroll-behavior-y: contain;
  word-break: break-word;
}

.gloim-body > :first-child {
  margin-top: 0;
}

.gloim-body > :last-child {
  margin-bottom: 0;
}

.gloim-body p {
  margin: 0 0 0.75em;
}

.gloim-body p:last-child {
  margin-bottom: 0;
}

.gloim-body img,
.gloim-body video {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}

.gloim-body iframe {
  max-width: 100%;
  border: 0;
  border-radius: 6px;
}

.gloim-body a {
  color: var(--gloim-accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Thin subtle vertical scrollbar inside popup body */
.gloim-body::-webkit-scrollbar {
  width: 6px;
}

.gloim-body::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 999px;
}

.gloim-body::-webkit-scrollbar-track {
  background: transparent;
}

/* Carousel Prev/Next Overlay Controls */
.gloim-carousel-controls {
  position: absolute;
  inset-inline: 10px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 3;
  display: flex;
  justify-content: space-between;
  pointer-events: none;
}

.gloim-carousel-btn {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 36px;
  min-block-size: 36px;
  padding: 6px;
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  color: #0f172a;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.14);
  transition: background-color 0.18s ease, color 0.18s ease, transform 0.18s ease;
}

.gloim-carousel-btn:hover {
  background: var(--gloim-btn-bg);
  color: var(--gloim-btn-text);
  transform: scale(1.06);
}

.gloim-carousel-btn:active {
  transform: scale(0.96);
}

/* Carousel Bottom Marker Bar */
.gloim-carousel-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: var(--gloim-surface);
  border-top: 1px solid var(--gloim-border);
}

.gloim-carousel-markers {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px;
}

/* Marker buttons maintain >= 24x24px target size (WCAG 2.5.8) via padding + content-box */
.gloim-carousel-markers button {
  appearance: none;
  border: 0;
  background: transparent;
  min-inline-size: 24px;
  min-block-size: 24px;
  padding: 7px 4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
}

.gloim-carousel-markers button::before {
  content: "";
  display: block;
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #94a3b8;
  transition: width 0.2s ease, background-color 0.2s ease;
}

/* Non-color affordance (expanded pill width + higher contrast) for active marker (WCAG 1.4.1) */
.gloim-carousel-markers button[aria-current="true"]::before {
  width: 22px;
  background: var(--gloim-accent);
}

.gloim-indicator {
  font-size: 12px;
  font-weight: 700;
  color: var(--gloim-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ==========================================================================
   Popup Footer
   ========================================================================== */
.gloim-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--gloim-bg);
  border-top: 1px solid var(--gloim-border);
}

.gloim-dismiss-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-block-size: 32px;
}

.gloim-dismiss-control input[type="checkbox"] {
  width: 17px;
  height: 17px;
  margin: 0;
  accent-color: var(--gloim-accent);
  cursor: pointer;
  flex-shrink: 0;
}

.gloim-dismiss-control label {
  font-size: 13px;
  font-weight: 500;
  color: var(--gloim-muted);
  cursor: pointer;
  user-select: none;
  line-height: 1.3;
}

.gloim-close-btn {
  background: var(--gloim-btn-bg);
  color: var(--gloim-btn-text);
  border: 0;
  min-block-size: 34px;
  padding: 7px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  line-height: 1.2;
  white-space: nowrap;
  touch-action: manipulation;
  transition: background-color 0.18s ease, box-shadow 0.18s ease, transform 0.15s ease;
}

.gloim-close-btn:hover {
  background: var(--gloim-btn-hover);
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.18);
}

.gloim-close-btn:active {
  transform: scale(0.97);
}

/* Larger hit targets on touch / coarse pointer devices */
@media (pointer: coarse) {
  .gloim-icon-close,
  .gloim-carousel-btn {
    min-inline-size: 42px;
    min-block-size: 42px;
  }

  .gloim-close-btn {
    min-block-size: 40px;
    padding: 9px 18px;
  }
}

/* ==========================================================================
   Mobile Responsive Adaptation (<= 768px)
   ========================================================================== */
@media (max-width: 768px) {
  dialog.gloim-popup-dialog {
    margin: auto !important;
    width: calc(100vw - 24px);
    max-width: calc(100vw - 24px);
    max-height: min(86dvh, 86vh);
    border-radius: 14px;
  }

  .gloim-body {
    padding: 16px;
    max-height: min(62dvh, 62vh);
  }

  .gloim-footer {
    padding: 12px 14px;
  }

  dialog.gloim-popup-dialog.gloim-mobile-full {
    margin: 0 !important;
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    border: 0;
  }

  dialog.gloim-popup-dialog.gloim-mobile-full .gloim-popup-shell {
    height: 100%;
    max-height: 100%;
  }

  dialog.gloim-popup-dialog.gloim-mobile-full .gloim-body {
    flex: 1 1 auto;
    max-height: none;
    padding-top: max(18px, env(safe-area-inset-top));
  }

  dialog.gloim-popup-dialog.gloim-mobile-full .gloim-footer {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
}
