/* The dialogs are self-contained: they carry their own parchment palette and button style
   instead of borrowing the host page's theme variables (the GM panel is a dark theme, where
   the site's --ink and .btn would render pale-on-pale). */
.jye-uploader-backdrop {
  --jye-ink: #2c1810;
  --jye-ink-light: #5c4030;
  --jye-ink-faint: #7a6555;
  --jye-parchment: #f4e4c1;
  --jye-gold: #c9a227;
  --jye-gold-dark: #9a7b1a;
  --jye-gold-shine: #f4d976;
  --jye-accent: #b01212;
  --jye-rule: rgba(92, 64, 48, 0.25);
}

.jye-uploader-backdrop button.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--jye-gold-dark);
  border-radius: 8px;
  background: rgba(255, 252, 244, 0.92);
  color: var(--jye-ink);
  font-family: Cinzel, Georgia, serif;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  line-height: 1.2;
  text-transform: uppercase;
  white-space: nowrap;
  text-shadow: none;
  box-shadow: none;
  transform: none;
  overflow: visible;
  cursor: pointer;
}

.jye-uploader-backdrop button.btn::before {
  content: none;
  display: none;
}

.jye-uploader-backdrop button.btn:hover:not(:disabled) {
  background: var(--jye-gold-shine);
  color: var(--jye-ink);
  border-color: var(--jye-gold-dark);
  transform: none;
}

.jye-uploader-backdrop button.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.jye-uploader-backdrop button.btn.jye-uploader-save {
  background: linear-gradient(180deg, rgba(244, 217, 118, 0.85), rgba(201, 162, 39, 0.6));
  font-weight: 700;
}

.jye-uploader-backdrop button.btn.jye-uploader-save:hover:not(:disabled) {
  background: var(--jye-gold-shine);
}

.jye-uploader-backdrop button.btn.jye-lib-delete,
.jye-uploader-backdrop button.btn.jye-lib-confirm-yes {
  color: var(--jye-accent);
}

/* Jye Image Uploader — the shared crop dialog and drop-zone affordances.
   Loaded by any page that hosts an uploader surface. Styling follows the
   scribe's-ledger system: Cinzel for the heading and buttons, ruled edges,
   parchment ground, gold for the affirmative action. */

/* --- Crop dialog ------------------------------------------------------- */

.jye-uploader-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1300; /* above the doodle studio (900) and the wandering overlays */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(28, 14, 6, 0.62);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.jye-uploader-backdrop[hidden] {
  display: none;
}

/* The dialog can be dragged inside, so the page behind it must not scroll
   along with the gesture on touch. */
body.jye-uploader-open {
  overflow: hidden;
  overscroll-behavior: none;
}

.jye-uploader-dialog {
  box-sizing: border-box;
  width: min(440px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 18px 20px 16px;
  border: 2px solid var(--jye-gold-dark);
  border-top-width: 4px;
  border-radius: 14px;
  background: var(--jye-parchment);
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.4),
    0 18px 44px rgba(28, 14, 6, 0.42);
  color: var(--jye-ink);
}

.jye-uploader-title {
  margin: 0 0 12px;
  font-family: Cinzel, Georgia, serif;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-align: center;
  text-transform: uppercase;
  color: var(--jye-ink);
}

/* --- The crop frame ---------------------------------------------------- */

.jye-uploader-viewport {
  --jye-uploader-aspect: 1;
  position: relative;
  /* Only the width is capped, so the aspect ratio always sets the height and the frame
     keeps its exact proportions on a short screen. */
  width: min(100%, calc(52vh * var(--jye-uploader-aspect)));
  margin: 0 auto;
  aspect-ratio: var(--jye-uploader-aspect);
  overflow: hidden;
  border: 2px solid var(--jye-gold-dark);
  border-radius: 12px;
  background: #2a1a0e;
  cursor: grab;
  /* The drag gesture is ours; the browser must not pan or zoom the page. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.jye-uploader-viewport.is-dragging {
  cursor: grabbing;
}

.jye-uploader-image {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  /* Transformed from the top-left so the pan/zoom math is plain viewport
     pixels — no half-size offsets to reason about when exporting the crop. */
  transform-origin: 0 0;
  max-width: none;
  will-change: transform;
  -webkit-user-drag: none;
  user-select: none;
  pointer-events: none;
}

/* A faint inner rule reads as "this is the frame you are filling" without
   masking any of the image the way a dimmed overlay would. */
.jye-uploader-viewport::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 10px;
  box-shadow: inset 0 0 0 1px rgba(255, 246, 204, 0.35);
  pointer-events: none;
}

/* --- Controls ---------------------------------------------------------- */

.jye-uploader-zoom {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  font-family: Cinzel, Georgia, serif;
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--jye-ink-light);
}

.jye-uploader-zoom-input {
  flex: 1 1 auto;
  min-width: 0;
  accent-color: var(--jye-gold-dark);
}

.jye-uploader-hint {
  margin: 8px 0 0;
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.82rem;
  font-style: italic;
  text-align: center;
  color: var(--jye-ink-faint);
}

.jye-uploader-status {
  margin: 6px 0 0;
  min-height: 1.2em;
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.85rem;
  text-align: center;
  color: var(--jye-accent);
}

.jye-uploader-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--jye-rule));
}

.jye-uploader-save {
  border-color: var(--jye-gold-dark);
  background: linear-gradient(180deg, rgba(244, 217, 118, 0.55), rgba(201, 162, 39, 0.35));
  font-weight: 600;
}

.jye-uploader-save:disabled {
  opacity: 0.55;
  cursor: progress;
}

/* The picker input exists only to be clicked programmatically. Kept in the
   layout but visually gone, rather than display:none, because Safari has
   historically refused .click() on a fully hidden input. */
.jye-uploader-file-input {
  position: fixed;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
  left: -9999px;
}

/* --- Drop-zone affordance (applies to any attached element) ------------- */

.jye-uploader-dropping {
  outline: 2px dashed var(--jye-gold);
  outline-offset: 3px;
}

/* --- Portrait frame: the camera button and its photo ------------------- */

/* The frame itself is a <button> that opens Doodle II, so the photo control
   is a sibling positioned over it — a button inside a button is invalid and
   would not receive clicks reliably. */
.character-portrait-cell {
  position: relative;
}

.character-portrait-photo-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--gold-dark, #9a7b1a);
  border-radius: 9px;
  background: rgba(244, 228, 193, 0.92);
  color: var(--ink, #2c1810);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  opacity: 0.78;
  transform: none;
  transition: opacity 140ms ease, transform 140ms ease, background 140ms ease;
  box-shadow: 0 2px 6px rgba(54, 30, 12, 0.28);
}

.character-portrait-cell:hover .character-portrait-photo-btn,
.character-portrait-cell:focus-within .character-portrait-photo-btn,
.character-portrait-photo-btn:focus-visible {
  opacity: 1;
  transform: translateY(0);
}

.character-portrait-photo-btn:hover {
  background: var(--gold-shine, #f4d976);
}

.character-portrait-photo-btn:focus-visible {
  outline: 2px solid var(--gold, #c9a227);
  outline-offset: 2px;
}

/* The remove control sits below the camera and only exists while a photo is
   actually set — the frame carries that state as .has-photo. */
.character-portrait-remove-btn {
  position: absolute;
  top: 52px;
  right: 10px;
  z-index: 3;
  display: none;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--gold-dark, #9a7b1a);
  border-radius: 9px;
  background: rgba(244, 228, 193, 0.92);
  color: var(--accent, #c61515);
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transform: translateY(-3px);
  transition: opacity 140ms ease, transform 140ms ease, background 140ms ease;
  box-shadow: 0 2px 6px rgba(54, 30, 12, 0.28);
}

.character-portrait-cell:has(.character-portrait-frame.has-photo) .character-portrait-remove-btn {
  display: flex;
}

.character-portrait-cell:has(.character-portrait-frame.has-photo):hover .character-portrait-remove-btn,
.character-portrait-cell:has(.character-portrait-frame.has-photo):focus-within .character-portrait-remove-btn {
  opacity: 1;
  transform: translateY(0);
}

.character-portrait-remove-btn:hover {
  background: var(--accent, #c61515);
  color: var(--parchment, #f4e4c1);
}

.character-portrait-remove-btn:focus-visible {
  outline: 2px solid var(--gold, #c9a227);
  outline-offset: 2px;
}

/* Touch has no hover, so the controls have to be permanently visible there or
   they are simply unreachable. */
@media (pointer: coarse) {
  .character-portrait-photo-btn {
    opacity: 1;
    transform: none;
    width: 44px;
    height: 44px;
  }

  .character-portrait-cell:has(.character-portrait-frame.has-photo) .character-portrait-remove-btn {
    opacity: 1;
    transform: none;
    top: 62px;
    width: 44px;
    height: 44px;
  }
}

/* An uploaded photo fills the frame edge to edge; a doodle portrait keeps
   the existing letterboxed look because it is already square. */
.character-portrait-frame.has-photo .character-portrait-image {
  object-fit: cover;
}

.character-portrait-cell.is-uploading .character-portrait-frame {
  opacity: 0.65;
  transition: opacity 120ms ease;
}


/* --- Editor: rotate / zoom / reset row, fit mode, busy + error states ------ */

.jye-uploader-dialog.is-fit {
  width: min(620px, 100%);
}

.jye-uploader-viewport:focus-visible {
  outline: 3px solid var(--jye-gold);
  outline-offset: 2px;
}

.jye-uploader-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

.jye-uploader-tools .btn {
  flex: 0 0 auto;
  min-width: 40px;
  min-height: 40px;
  padding: 4px 10px;
  line-height: 1;
}

.jye-uploader-rotl,
.jye-uploader-rotr {
  font-size: 1.25rem;
}

/* The zoom slider takes the remaining width; fit mode has nothing to zoom. */
.jye-uploader-tools .jye-uploader-zoom {
  flex: 1 1 auto;
  margin-top: 0;
  min-width: 0;
}

.jye-uploader-dialog.is-fit .jye-uploader-zoom {
  visibility: hidden;
}

.jye-uploader-dialog.is-busy .jye-uploader-viewport {
  cursor: progress;
  opacity: 0.75;
}

.jye-uploader-dialog.is-busy .jye-uploader-status {
  color: var(--jye-ink-light);
}

.jye-uploader-dialog.is-busy .jye-uploader-status::before,
.jye-lib-panel-loading::before {
  content: "";
  display: inline-block;
  width: 0.85em;
  height: 0.85em;
  margin-right: 0.5em;
  vertical-align: -0.1em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: jye-uploader-spin 0.8s linear infinite;
}

@keyframes jye-uploader-spin {
  to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .jye-uploader-dialog.is-busy .jye-uploader-status::before {
    animation: none;
  }
}

.jye-uploader-dialog.has-error .jye-uploader-status {
  padding: 6px 10px;
  border: 1px solid var(--jye-accent);
  border-radius: 8px;
  background: rgba(198, 21, 21, 0.08);
}

.jye-uploader-tools .btn:focus-visible,
.jye-uploader-actions .btn:focus-visible {
  outline: 2px solid var(--jye-gold);
  outline-offset: 2px;
}

/* --- Library ----------------------------------------------------------- */

.jye-lib {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  width: min(720px, 100%);
  max-height: 100%;
  padding: 16px 18px 14px;
  border: 2px solid var(--jye-gold-dark);
  border-top-width: 4px;
  border-radius: 14px;
  background: var(--jye-parchment);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.4), 0 18px 44px rgba(28, 14, 6, 0.42);
  color: var(--jye-ink);
  overflow: hidden;
}

.jye-lib.is-dropping {
  outline: 3px dashed var(--jye-gold);
  outline-offset: -8px;
}

.jye-lib-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.jye-lib-head .jye-uploader-title {
  margin: 0;
  text-align: left;
}

.jye-lib-close {
  width: 40px;
  height: 40px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--jye-ink-light);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.jye-lib-close:hover {
  background: rgba(92, 64, 48, 0.12);
}

.jye-lib-drop {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 2px dashed var(--jye-gold-dark);
  border-radius: 12px;
  background: rgba(255, 250, 235, 0.55);
  cursor: pointer;
}

.jye-lib-drop:hover,
.jye-lib-drop:focus-visible {
  background: rgba(244, 217, 118, 0.3);
  outline: none;
  border-style: solid;
}

.jye-lib.is-offline .jye-lib-drop {
  opacity: 0.5;
  pointer-events: none;
}

.jye-lib-drop-icon {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(180deg, rgba(244, 217, 118, 0.7), rgba(201, 162, 39, 0.5));
  font-size: 1.5rem;
  line-height: 1;
}

.jye-lib-drop-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.jye-lib-drop-text strong {
  font-family: Cinzel, Georgia, serif;
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.jye-lib-drop-text span {
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.9rem;
  color: var(--jye-ink-light);
}

.jye-lib-meter {
  margin: 10px 0 6px;
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.85rem;
  color: var(--jye-ink-faint);
}

.jye-lib-meter.is-full {
  color: var(--jye-accent);
  font-weight: 600;
}

.jye-lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 10px;
  flex: 1 1 auto;
  min-height: 0;
  max-height: 40vh;
  overflow-y: auto;
  padding: 4px;
  overscroll-behavior: contain;
}

.jye-lib-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 5px;
  border: 2px solid transparent;
  border-radius: 10px;
  background: rgba(255, 250, 235, 0.6);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.jye-lib-tile:hover {
  border-color: rgba(154, 123, 26, 0.5);
}

.jye-lib-tile:focus-visible {
  outline: 3px solid var(--jye-gold);
  outline-offset: 1px;
}

.jye-lib-tile.is-selected {
  border-color: var(--jye-gold-dark);
  background: rgba(244, 217, 118, 0.4);
  box-shadow: 0 0 0 2px rgba(201, 162, 39, 0.35);
}

.jye-lib-thumb {
  display: block;
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 6px;
  background:
    repeating-conic-gradient(rgba(92, 64, 48, 0.1) 0% 25%, transparent 0% 50%) 50% / 14px 14px,
    #e9d9b5;
}

.jye-lib-thumb img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0;
  transition: opacity 160ms ease;
}

.jye-lib-tile.has-image .jye-lib-thumb img {
  opacity: 1;
}

.jye-lib-tile.is-missing .jye-lib-thumb::after {
  content: "missing";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  color: var(--jye-ink-faint);
}

.jye-lib-caption {
  overflow: hidden;
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.85rem;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.jye-lib-badge {
  align-self: flex-start;
  max-width: 100%;
  overflow: hidden;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(92, 64, 48, 0.14);
  font-family: Cinzel, Georgia, serif;
  font-size: 0.6rem;
  letter-spacing: 0.05em;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}

.jye-lib-badge.is-current {
  background: var(--jye-gold-dark);
  color: #fff;
}

.jye-lib-empty {
  margin: 14px 0;
  text-align: center;
  font-family: "Crimson Pro", Georgia, serif;
  font-style: italic;
  color: var(--jye-ink-faint);
}

.jye-lib-detail {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--jye-rule));
}

.jye-lib-label {
  flex: 1 1 180px;
  min-width: 0;
  min-height: 38px;
  padding: 4px 10px;
  border: 1px solid var(--jye-gold-dark);
  border-radius: 8px;
  background: rgba(255, 252, 244, 0.9);
  color: inherit;
  font: inherit;
}

.jye-lib-meta {
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.85rem;
  color: var(--jye-ink-light);
}

.jye-lib-confirm {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.9rem;
  color: var(--jye-accent);
}

.jye-lib-confirm[hidden] { display: none; }

.jye-lib-confirm-yes,
.jye-lib-delete {
  color: var(--jye-accent);
}

.jye-lib-delete[hidden] { display: none; }

.jye-lib-status {
  min-height: 1.3em;
  margin: 8px 0 0;
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 0.9rem;
  text-align: center;
  color: var(--jye-ink-light);
}

.jye-lib-status.is-error {
  color: var(--jye-accent);
}

.jye-lib.is-loading .jye-lib-status::before {
  content: "";
  display: inline-block;
  width: 0.85em;
  height: 0.85em;
  margin-right: 0.5em;
  vertical-align: -0.1em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: jye-uploader-spin 0.8s linear infinite;
}

.jye-lib .jye-uploader-actions {
  margin-top: 10px;
}

@media (max-width: 520px) {
  .jye-uploader-backdrop {
    padding: 8px;
    align-items: flex-end;
  }

  .jye-lib-grid {
    grid-template-columns: repeat(3, 1fr);
    max-height: 34vh;
  }

  .jye-lib-tile,
  .jye-uploader-actions .btn {
    min-height: 44px;
  }
}

/* While the editor is open on top of the library, the library steps out of sight rather than
   showing through a second blurred backdrop. */
.jye-lib-backdrop.is-covered {
  visibility: hidden;
}
