/* ===========================
  BASE RESET
=========================== */
html,
body {
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  -webkit-tap-highlight-color: transparent;
}

.top-ui,
.top-ui *,
.category-strip,
.thumb,
.canvas-wrap,
.canvas-container,
button,
span,
div {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: sans-serif;
  background: #222;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  height: 100dvh;
  overflow: hidden;
}

img {
  pointer-events: none;
  -webkit-user-drag: none;
}

/* ===========================
  EDITOR FRAME
=========================== */

/* Canvas shape driven by explicit class, not orientation media query */
.editor-frame.mode-portrait .canvas-wrap {
  aspect-ratio: 5 / 7;
}
.editor-frame.mode-landscape .canvas-wrap {
  aspect-ratio: 7 / 5;
}

/* Layout reflow also driven by the class, not @media(orientation) */
.editor-frame.mode-landscape {
  flex-direction: row;
  width: auto;
  max-width: 100vw;
  height: auto;        /* was 100dvh — let content size itself instead of stretching to fill */
  max-height: 100dvh;
  padding: 80px 8px 8px;
  gap: 40px;
  align-items: flex-start;
  justify-content: center;
}

.editor-frame.mode-landscape .format-toggle {
  flex-direction: column;
}

.editor-frame.mode-landscape .canvas-container,
.editor-frame.mode-landscape .category-strip,
.editor-frame.mode-landscape .top-ui {
  width: auto;
  height: auto;        /* was 100% — let each size to its actual content */
}

.editor-frame.mode-landscape .canvas-container {
  order: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.editor-frame.mode-landscape .category-strip {
  order: 2;
  flex-direction: column;
  height: 360px;
  width: auto;
  overflow-y: auto;
  overflow-x: hidden;
  margin-top: 0; 
  margin-bottom: 0;
}
.editor-frame.mode-landscape .top-ui {
  order: 3;
  flex-direction: column;
  height: 360px;
  justify-content: space-between;
  width: 80px;
}

.editor-frame.mode-landscape .top-ui-row {
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.editor-frame.mode-landscape .top-ui button,
.editor-frame.mode-landscape .info-btn {
  width: 80px;
  height: 44px;
  padding: 0;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

.editor-frame.mode-landscape .button-group {
  width: 80px;
  height: 44px;
}

.editor-frame.mode-landscape .split-btn {
  width: 40px;
  height: 44px;
  padding: 0;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

.editor-frame.mode-landscape .split-btn svg {
  width: 20px;
  height: 20px;
  stroke-width: 2.5;
}


.editor-frame.mode-landscape .canvas-wrap {
  width: 504px;
  height: auto;
  max-width: 504px;
  max-height: 360px;
  aspect-ratio: 7 / 5;
  flex-shrink: 0;
}

.editor-frame.mode-landscape ~ #previewModal #previewCanvas,
body:has(.editor-frame.mode-landscape) #previewCanvas {
  aspect-ratio: 7 / 5;
}

.editor-frame {
  width: min(360px, 88vw);
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: -4px;
  padding-top: 12px;
  gap: 12px;
}

.top-ui,
.category-strip,
.canvas-container {
  display: block;
  width: 100%;
}

button {
  cursor: pointer;
  touch-action: manipulation;
}

.modal-close-btn {
  position: absolute;
  top: 6px;
  right: 6px;

  width: 44px;
  height: 44px;

  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.3);

  color: #fff;
  font-size: 16px;

  display: flex;
  align-items: center;
  justify-content: center;

  cursor: pointer;
  z-index: 1;
}


@media (max-height: 500px) {
  html {
    overflow-y: auto !important;
    height: auto !important;
  }

  body {
    touch-action: pan-y !important; /* only body-level background allows scroll */
    align-items: flex-start;
    overflow-y: auto;
    height: auto !important;
  }

  .editor-frame.mode-landscape {
    max-height: none;
  }

  /* Canvas and its contents keep full manual touch control — no pan-y here */
  .canvas-wrap,
  .box {
    touch-action: none !important;
  }
}

@media (orientation: portrait) {
  .editor-frame.mode-landscape {
    max-width: none;
    max-height: none;
    width: max-content; /* size to actual content width so there's something to scroll to */
  }

  body:has(.editor-frame.mode-landscape) {
    overflow-x: auto !important;
    overflow-y: hidden;
    touch-action: pan-x !important; /* horizontal scroll gesture only */
    width: 100vw;
    height: 100dvh;
    align-items: flex-start;
    justify-content: flex-start;
  }

  .editor-frame.mode-landscape .canvas-wrap,
  .editor-frame.mode-landscape .box {
    touch-action: none !important;
  }
}

/* ===========================
  TOP UI
=========================== */
.top-ui {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  width: min(360px, 88vw);
  max-width: 360px;
  margin: 0 auto;
  margin-bottom: 20px;
  padding: 8px 0;
  font-size: 14px;
  background: #222;
  gap: 8px;
}

.top-ui-row {
  display: flex;
}

/* Row 1 (ReadFirst) — full width, own line, on top — PORTRAIT ONLY */
.editor-frame.mode-portrait .row-readfirst {
  width: 100%;
  justify-content: center;
  order: 1;
}

.editor-frame.mode-portrait .row-readfirst .info-btn {
  width: 100%;
  height: 40px;
  border-radius: 10px;
  box-sizing: border-box;
}

/* Bottom row order: format, startover, undo/redo, preview — PORTRAIT ONLY */
.editor-frame.mode-portrait .row-format    { order: 2; flex: 1 1 0; min-width: 0; }
.editor-frame.mode-portrait .row-startover { order: 3; flex: 1 1 0; min-width: 0; }
.editor-frame.mode-portrait .row-undoredo  { order: 4; flex: 1 1 0; min-width: 0; }
.editor-frame.mode-portrait .row-preview   { order: 5; flex: 1 1 0; min-width: 0; }

/* Uniform button shape across the whole bottom row — PORTRAIT ONLY */
.editor-frame.mode-portrait .row-startover .info-btn,
.editor-frame.mode-portrait .row-preview .info-btn,
.editor-frame.mode-portrait .row-format .button-group,
.editor-frame.mode-portrait .row-undoredo .button-group {
  width: 100%;
  height: 40px;
  border-radius: 10px;
  box-sizing: border-box;
}

/* Make split buttons inside format/undo-redo groups fill their group evenly — PORTRAIT ONLY */
.editor-frame.mode-portrait .row-format .split-btn,
.editor-frame.mode-portrait .row-undoredo .split-btn {
  flex: 1;
  height: 100%;
}

.top-ui * {
  touch-action: manipulation;
}

.top-ui span {
  text-align: center;
  line-height: 1.4;
}

.top-ui button {
  padding: 5px 10px;
  font-size: 14px;
  border-radius: 12px;
}

.button-group {
  display: flex;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.split-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  font-size: 16px;
  background: #333;
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.split-btn.left {
  border-right: 1px solid rgba(255, 255, 255, 0.3);
}

.info-btn {
  width: auto;
  height: 30px;
  padding: 0 12px;
  border-radius: 15px;

  display: flex;
  align-items: center;
  justify-content: center;

  background: rgba(255,255,255,0.10);
  border: 1px solid rgba(255, 255, 255, 0.3);

  color: #fff;
  font-size: 14px;
  font-weight: 700;

  cursor: pointer;
}

.row-readfirst .info-btn {
  font-size: 16px;
  letter-spacing: 1px;
}


.help-popup {
  position: fixed;
  width: 260px;

  background: rgba(20, 20, 20, 0.92);
  color: white;

  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.15);

  font-size: 13px;
  line-height: 1.45;

  display: none;
  z-index: 100000;
}

.help-popup strong {
  display: block;
  margin-bottom: 6px;
}

.help-popup p {
  margin: 10px 0;
}


.help-title {
  font-weight: 700;
  font-size: 13px;
  margin-bottom: 10px;
  opacity: 0.9;
}

.help-section {
  margin-bottom: 10px;
}

.help-bold {
  font-weight: 600;
  margin-bottom: 2px;
  color: #ffffff;
}

.help-popup,
.library-modal {
    position: fixed;
}

.help-popup .modal-close-btn,
.library-modal .modal-close-btn {
    z-index: 1;
}

.format-toggle {
  display: flex;
  gap: 6px;
}

.format-toggle-btn {
  font-size: 20px;
  line-height: 1;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid #666;
  background: #333;
  color: #fff;
  cursor: pointer;
}

.format-toggle-btn.active {
  background: #555;
  color: #888;
  cursor: default;
  pointer-events: none;
}

.format-picker {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999999;
}

.format-picker-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  color: #fff;
}

.format-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  background: #333;
  border: 2px solid #666;
  border-radius: 10px;
  padding: 16px 24px;
  color: #fff;
  cursor: pointer;
}

.format-preview {
  background: #7a7a7a;
  border: 2px solid #000;
}
.portrait-preview  { width: 50px; height: 70px; }
.landscape-preview { width: 70px; height: 50px; }


.split-btn.active {
  background: #555;
  color: #888;
  cursor: default;
  pointer-events: none;
}

.split-btn svg {
  width: 24px ;
  height: 24px ;
  stroke-width: 2.5;
}

#previewBtn:disabled,
#previewBtn.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

/* ===========================
  THUMB GRID
=========================== */
.category-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 2px;
  -webkit-overflow-scrolling: touch;
  margin-top: -12px;
  margin-bottom: 20px;
}

.category-btn {
  flex-shrink: 0;
  padding: 8px 14px;
  border-radius: 10px;
  background: #333;
  color: #fff;
  border: 1px solid #666;
  white-space: nowrap;
  cursor: pointer;
  touch-action: pan-x;
}

.category-btn.active {
  background: #555;
}

.library-modal {
  position: fixed;
  width: fit-content;
  max-width: 90vw;
  max-height: 80vh;
  background: rgba(20, 20, 20, 0.92);
  color: white;
  padding: 16px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.15);
  font-size: 13px;
  line-height: 1.45;
  display: none;
  z-index: 100000;
  overflow-y: auto;
  box-sizing: border-box;
}

.library-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
}

.library-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, 70px);
  max-width: 316px; /* 4 x 70px + 3 x 12px */
  gap: 12px;
  justify-content: center;
  width: fit-content;
}

.library-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
}

.thumb {
  width: 60px;
  height: 60px;
  border: 2px solid black;
  background-color: #7a7a7a;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  touch-action: none;
}

.thumb.empty {
  background: #777;
  touch-action: manipulation;
}

/* ===========================
  CANVAS
=========================== */
.canvas-container {
  display: flex;
  margin-top: -2px;
  justify-content: center;
}

.canvas-wrap {
  width: 100%;
  max-width: 360px;
  margin: 0 auto;
  aspect-ratio: 5 / 7;
  position: relative;
  overflow: hidden;
  background: #7a7a7a;
  border: 2px solid #000;
  touch-action: none;
}

.canvas-wrap.previewing {
  transform: scale(0.95);
  opacity: 0.3;
}

/* ===========================
  BOX
=========================== */
.box {
  position: absolute;
  /* width and height removed — set per image in JS */
  opacity: 1;
  background: transparent;
  border: none;
  outline: none;
  box-shadow: none;
  touch-action: none;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  will-change: transform;
}

.image {
  position: absolute;
  inset: 0;
  display: block;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  user-select: none;
}

.svg-art {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;

  filter: drop-shadow(0 0 6px rgba(0,0,0,0.7));
}

/* ===========================
  PREVIEW MODAL
=========================== */
body.modal-open {
  overflow: hidden;
}

.preview-area {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 24px;
}

#previewCanvas {
  position: relative;
  width: min(420px, 92vw);
  aspect-ratio: 5/7;
  background: #888;
  border: 2px solid black;
  overflow: hidden;
}

#previewModal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s ease;
  align-items: flex-start; /* changed from center */
  padding: 20px 0;
  overflow-y: auto; /* allow scroll if content is taller than viewport */
  -webkit-overflow-scrolling: touch;
}

#previewModal.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

#expiredModal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(20px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10000;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

#expiredModal.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.modal-content {
  width: min(360px, 88vw);
  max-height: 85dvh;
  background: #111;
  padding: 20px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

/* ===========================
  PREVIEW MODAL BUTTONS
=========================== */
.button-row {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 40px;
  width: 100%;
  transform: translateY(-20px);
}

#purchaseBtn,
#editBtn {
  font-size: 16px;
  font-weight: 800;
  padding: 9px 14px;
  border: none;
  border-radius: 5px;
  display: inline-flex;
  justify-content: center;
  min-width: 80px;
  height: 40px;
  align-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s ease, color 0.2s ease;
}

/* GREEN — slot is open */
#purchaseBtn {
  background: #00c853;
  color: #fff;
  width: 140px;
}

/* RED — slot taken, counting down */
#purchaseBtn.locked {
  background: #e53935;
  color: #fff;
  cursor: not-allowed;
}

#editBtn {
  background: #fff;
  color: #000;
  justify-self: start;
  width: 60px;
}

.slot-caption {
  overflow: hidden;
  white-space: nowrap;
  height: 16px;
  margin-top: 4px;
  font-size: 14px;
  color: #aaa;
  display: none;
  width: 240px;
}

.slot-caption.visible {
  display: block;
}

.slot-caption span {
  display: inline-block;
  padding-right: 40px;
  animation: marquee 20s linear infinite;
}

@keyframes marquee {
  0%   { transform: translateX(240px); }
  100% { transform: translateX(-371px); }
}

/* ===========================
  SUCCESS MODAL
=========================== */
#successModal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(20px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10000;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

#successModal.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.success-content {
  width: min(360px, 88vw);
  min-height: 420px;
  max-height: 80vh;
  overflow-y: auto;
  background: #111;
  border-radius: 12px;
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  color: white;
  gap: 18px;
}

.success-content h2 {
  line-height: 1.4;
  margin: 0;
}

.success-content p {
  margin: 0;
  line-height: 1.6;
}

.success-content button {
  width: 50%;
  padding: 12px 14px;
  font-size: 16px;
  border-radius: 10px;
  margin: 0;
  min-height: 44px;
}

#notifyBtn {
  background: #ffffff;
  color: #000;
}

#backToEditBtn {
  background: transparent;
  border: 1px solid #fff;
  color: #fff;
}

/* ===========================
  MOBILE
=========================== */
