/*
 * Reveal Spread component.
 * Positions items on a 2D layout and reveals them through animation.
 * Displayed inline in conversation like a table surface.
 * Supports pinch-zoom/pan via panzoom_controller.
 */

/* --- Container (the "table") --- */

.reveal-spread {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 0.75rem;
  overflow: hidden;
  background: radial-gradient(
    ellipse at center,
    var(--spread-bg, #7c3aed) 0%,
    color-mix(in srgb, var(--spread-bg, #7c3aed), black 50%) 70%,
    color-mix(in srgb, var(--spread-bg, #7c3aed), black 70%) 100%
  );
  box-shadow:
    inset 0 2px 20px rgba(0, 0, 0, 0.5),
    0 4px 12px rgba(0, 0, 0, 0.3);
  outline: none;
  transition: box-shadow 0.2s;
  cursor: pointer;
}

.reveal-spread--active {
  cursor: default;
}

.reveal-spread::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  border-radius: inherit;
  pointer-events: none;
  transition:
    opacity 0.3s,
    background 0.3s;
  z-index: 2;
}

.reveal-spread:hover::after {
  background: rgba(0, 0, 0, 0.15);
}

.reveal-spread--active::after {
  opacity: 0;
}

.reveal-spread__canvas {
  position: absolute;
  inset: 4%;
}

/* --- Items --- */

.reveal-spread__item {
  position: absolute;
  width: var(--item-width, 12%);
  transform: translate(-50%, -50%) rotate(var(--item-rotation, 0deg));
  transition: all 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 1;
  cursor: pointer;
  border-radius: 4%;
}

.reveal-spread__item--hidden {
  opacity: 0;
  pointer-events: none;
}

/* --- Flipper (front/back reveal) --- */

.reveal-spread__flipper {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.6s ease-in-out;
}

.reveal-spread__flipper--concealed {
  transform: rotateY(180deg);
}

.reveal-spread__flipper--revealing {
  transform: rotateY(0deg);
  animation: none;
}

.reveal-spread__item:has(.reveal-spread__flipper--concealed) {
  animation: reveal-spread-pulse 2s ease-in-out infinite;
}

@keyframes reveal-spread-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 transparent;
  }
  50% {
    box-shadow: 0 0 12px 3px rgba(200, 180, 255, 0.4);
  }
}

.reveal-spread__face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 4%;
  overflow: hidden;
  box-shadow:
    0 2px 8px rgba(0, 0, 0, 0.4),
    0 0 1px rgba(255, 255, 255, 0.1);
}

.reveal-spread__face img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.reveal-spread__face--back {
  transform: rotateY(180deg);
}

/* --- Animation Phases --- */

.reveal-spread__item--gathered {
  left: 50% !important;
  top: 50% !important;
  opacity: 1;
  transform: translate(-50%, -50%) rotate(0deg);
}

.reveal-spread__item--spreading {
  opacity: 1;
}

.reveal-spread__item--fading-in {
  opacity: 1;
  transition: opacity 0.6s ease-out;
}

.reveal-spread__item--scaling-in {
  opacity: 1;
  animation: reveal-spread-scale-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1)
    forwards;
}

@keyframes reveal-spread-scale-in {
  from {
    transform: translate(-50%, -50%) rotate(var(--item-rotation, 0deg)) scale(0);
  }
  to {
    transform: translate(-50%, -50%) rotate(var(--item-rotation, 0deg)) scale(1);
  }
}

.reveal-spread__item--dropping {
  opacity: 1;
  animation: reveal-spread-drop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes reveal-spread-drop {
  from {
    transform: translate(-50%, -150%) rotate(var(--item-rotation, 0deg));
    opacity: 0;
  }
  to {
    transform: translate(-50%, -50%) rotate(var(--item-rotation, 0deg));
    opacity: 1;
  }
}

/* --- Action Button (View Reading) --- */

.reveal-spread__action {
  display: flex;
  justify-content: flex-end;
  margin-top: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--color-base-100);
  border-radius: 0.75rem;
  filter: drop-shadow(0 0 1px var(--color-neutral));
}

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

.reveal-spread__controls {
  position: absolute;
  bottom: 0.5rem;
  left: 0.5rem;
  display: flex;
  gap: 0.25rem;
  z-index: 10;
  opacity: 0.4;
  transition: opacity 0.2s;
}

.reveal-spread:hover .reveal-spread__controls {
  opacity: 0.8;
}

.reveal-spread__control-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.5rem;
  height: 1.5rem;
  border: none;
  border-radius: 0.375rem;
  background: rgba(0, 0, 0, 0.5);
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  transition:
    background 0.15s,
    color 0.15s;
}

.reveal-spread__control-btn:hover {
  background: rgba(0, 0, 0, 0.7);
  color: white;
}

/* --- Fullscreen --- */

.reveal-spread--fullscreen {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: auto;
  border-radius: 0;
}

.reveal-spread--fullscreen::after {
  display: none;
}

.reveal-spread--fullscreen .reveal-spread__canvas {
  position: relative;
  width: min(85vh, 85vw);
  height: min(85vh, 85vw);
  inset: auto;
}

.reveal-spread__close-btn {
  display: none;
}

.reveal-spread--fullscreen .reveal-spread__close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 1rem;
  right: 1rem;
  z-index: 10;
  width: 2.5rem;
  height: 2.5rem;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  transition: background 0.15s;
}

.reveal-spread--fullscreen .reveal-spread__close-btn:hover {
  background: rgba(0, 0, 0, 0.7);
  color: white;
}

.reveal-spread--fullscreen .reveal-spread__controls {
  bottom: 1.5rem;
  left: 1.5rem;
  opacity: 0.6;
  gap: 0.5rem;
}

.reveal-spread--fullscreen .reveal-spread__control-btn {
  width: 2.5rem;
  height: 2.5rem;
}

.reveal-spread--fullscreen .reveal-spread__control-btn svg {
  width: 1.25rem;
  height: 1.25rem;
}

/* Native fullscreen: browser handles positioning, undo fixed */
.reveal-spread:fullscreen {
  position: relative;
  inset: auto;
  z-index: auto;
  border-radius: 0;
}

/* --- Thumbnail (in chat) --- */

.reveal-spread-thumbnail {
  max-height: 180px;
  position: relative;
}

.reveal-spread-thumbnail .reveal-spread {
  pointer-events: none;
}

.reveal-spread-thumbnail::after {
  content: 'タップして表示';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.875rem;
  font-weight: 500;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 0.75rem;
}

/* --- Fullscreen Modal --- */

.reveal-spread-modal {
  position: fixed;
  inset: 0;
  z-index: 9999;
}

.reveal-spread-modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.95);
}

.reveal-spread-modal__content {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.reveal-spread-modal__canvas {
  width: 90vw;
  max-width: 900px;
  max-height: 80vh;
  touch-action: none;
}

.reveal-spread-modal__canvas .reveal-spread__item {
  width: var(--item-width, 15%);
}

.reveal-spread-modal__close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  z-index: 10000;
  background: none;
  border: none;
  color: white;
  font-size: 2rem;
  cursor: pointer;
  width: 3rem;
  height: 3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.6;
  transition: opacity 0.2s;
}

.reveal-spread-modal__close:hover {
  opacity: 1;
}
