/* ── Tile base ────────────────────────────────────────────────────────────── */
.tile {
  position: relative;
  width: 2.2rem;
  height: 4.3rem;
  background: #1B1B1B;
  border-radius: 2px;
  overflow: hidden;
  perspective: 400px;
  flex-shrink: 0;
  /* Inset shadow gives the tile a recessed-into-the-board depth */
  box-shadow: inset 0 0 4px rgba(0, 0, 0, 0.7), inset 0 1px 2px rgba(0, 0, 0, 0.5);
}

/* ── Top and bottom structural halves ────────────────────────────────────── */
/*
 * Each half clips its children to its own 50% of the tile.
 * overflow:hidden is critical — it cuts the character exactly at the seam.
 */
.tile-top,
.tile-bottom {
  position: absolute;
  left: 0;
  width: 100%;
  height: 50%;
  overflow: hidden;
}

.tile-top {
  top: 0;
  border-bottom: 2px solid #000; /* center flap seam — splits top and bottom flap */
  box-shadow: 0 2px 0 rgba(255, 255, 255, 0.06); /* faint light edge below seam for depth */
  z-index: 1;
}

.tile-bottom { bottom: 0; }

/*
 * Surface texture overlay — sits above all face panels (z-index: 4 > max panel z-index 3).
 * Simulates the physical flap material: scanline grain + angled light reflection.
 * Characters appear printed INTO the tile rather than floating on top.
 */
.tile-top::before,
.tile-bottom::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
  background:
    /* fine horizontal scanlines — matte plastic grain */
    repeating-linear-gradient(
      0deg,
      rgba(0, 0, 0, 0)    0px,
      rgba(0, 0, 0, 0)    2px,
      rgba(0, 0, 0, 0.04) 2px,
      rgba(0, 0, 0, 0.04) 3px
    ),
    /* angled light catch — ambient surface sheen */
    linear-gradient(
      148deg,
      rgba(255, 255, 255, 0.06) 0%,
      rgba(255, 255, 255, 0.00) 42%,
      rgba(0,   0,   0,   0.10) 100%
    );
}

/* ── Face panels ─────────────────────────────────────────────────────────── */
/*
 * Four panels, three in the top half and one static in the bottom half:
 *
 *  .top-half-static      (z:1) — static background, current char top.
 *                                Updated to new char after the flip settles.
 *  .bottom-flap-animating (z:2) — next char top, pre-rotated edge-on (-90°).
 *                                 Unfolds to 0° during the flip.
 *  .top-flap-animating   (z:3) — current char top, flat at rest.
 *                                 Folds down to 90° (falls away).
 *  .bottom-half-static          — next char bottom, snapped immediately.
 *                                 Never animates.
 */
.top-half-static,
.top-flap-animating,
.bottom-flap-animating,
.bottom-half-static {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1B1B1B;
}


.top-half-static       { z-index: 1; }
.bottom-flap-animating {
  z-index: 2;
  backface-visibility: hidden;
  transform-origin: bottom center;
  transform: rotateX(-90deg);
}
.top-flap-animating    {
  z-index: 3;
  backface-visibility: hidden;
  transform-origin: bottom center;
  transform: rotateX(0deg);
}

/* ── Fold-score lines ────────────────────────────────────────────────────── */
/*
 * Three horizontal crease marks in the lower portion of the bottom half,
 * mimicking the physical score lines on real Solari split-flap cards.
 * Percentage-based so they scale with any tile size.
 * Each line: faint light edge (crease ridge) + dark shadow below it.
 */
.tile-bottom::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
  background: linear-gradient(
    to bottom,
    transparent 59%,
    rgba(255, 255, 255, 0.09) 59%,   rgba(255, 255, 255, 0.09) 60%,
    rgba(0,   0,   0,   0.55) 60%,   rgba(0,   0,   0,   0.55) calc(60% + 1.5px),
    transparent calc(60% + 1.5px)    73.5%,
    rgba(255, 255, 255, 0.07) 73.5%, rgba(255, 255, 255, 0.07) 74.5%,
    rgba(0,   0,   0,   0.48) 74.5%, rgba(0,   0,   0,   0.48) calc(74.5% + 1.5px),
    transparent calc(74.5% + 1.5px)  88%,
    rgba(255, 255, 255, 0.05) 88%,   rgba(255, 255, 255, 0.05) 89%,
    rgba(0,   0,   0,   0.40) 89%,   rgba(0,   0,   0,   0.40) calc(89% + 1.5px),
    transparent calc(89% + 1.5px)
  );
}

/* ── Characters ───────────────────────────────────────────────────────────── */
/*
 * Panel height = 1.5rem (50% of 3rem tile).
 * Doto renders as direct amber text on dark panels — no stencil trick needed.
 * translateY offsets remain panel_height/2 = 0.75rem for seam alignment.
 */
.tile-char {
  font-family: "Doto", 'Courier New', monospace;
  font-size: 1.5rem;
  font-weight: 700;
  font-style: normal;
  font-optical-sizing: auto;
  font-variation-settings: "ROND" 0;
  color: #E8D5A3;
  background: transparent;
  line-height: 1;
  user-select: none;
  pointer-events: none;
  display: block;
}

.tile-top    .tile-char { transform: translateY( 0.75rem); }
.tile-bottom .tile-char { transform: translateY(-0.75rem); }

/* ── Flip animation ───────────────────────────────────────────────────────── */
/*
 * Only the top flap moves. The bottom half is already showing the next char.
 *
 * .top-flap-animating  folds DOWN  (0° → 90°)  — current char falls away.
 * .bottom-flap-animating unfolds   (-90° → 0°) — next char top is revealed.
 */
.tile.flipping .top-flap-animating {
  animation: flapFall var(--flip-ms, 60ms) linear forwards;
}
.tile.flipping .bottom-flap-animating {
  animation: flapRise var(--flip-ms, 60ms) linear forwards;
}

@keyframes flapFall {
  from { transform: rotateX(  0deg); }
  to   { transform: rotateX( 90deg); }
}
@keyframes flapRise {
  from { transform: rotateX(-90deg); }
  to   { transform: rotateX(  0deg); }
}


/* ── Color tile overrides ─────────────────────────────────────────────────── */
.tile.color-tile .tile-top,
.tile.color-tile .tile-bottom {
  background: var(--tile-color);
  border-color: rgba(0, 0, 0, 0.55);
}
.tile.color-tile .top-half-static,
.tile.color-tile .top-flap-animating,
.tile.color-tile .bottom-flap-animating,
.tile.color-tile .bottom-half-static {
  background: var(--tile-color);
}
.tile.color-tile .tile-char {
  display: none;
}
