/* Small tactile games sit over the existing full-screen paintings.
   Load after immersive.css: the former lens was only a short reading panel. */
.screen-card[data-activity]:not([data-activity='']) > .reader-panel {
  gap: 7px;
  padding-top: 14px;
}
.screen-card[data-activity]:not([data-activity='']) .screen-widget { width: 100%; }
.activity-panel {
  width: 100%;
  min-width: 0;
  margin: 0;
  padding: 0;
  font-family: var(--sans);
}
.activity-caption,
.activity-panel .activity-caption {
  min-height: 17px;
  margin: 0 0 8px;
  color: #f0d9ad;
  font-size: 12px;
  line-height: 1.35;
  text-align: center;
  text-wrap: balance;
}
.activity-panel button:focus-visible,
.lens-controls button:focus-visible {
  outline: 3px solid #ffe5a6;
  outline-offset: 2px;
}
.activity-feedback,
.timing-status {
  min-height: 18px;
  margin: 8px 0 0;
  color: #e2eed5;
  font-size: 12px;
  line-height: 1.35;
  text-align: center;
}
.activity-feedback:empty { display: none; }

/* Two rows keep every card within thumb reach, even in the eight-card round. */
.memory-grid {
  --memory-height: clamp(160px, 26dvh, 210px);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 8px;
  width: 100%;
  max-width: 380px;
  height: var(--memory-height);
  margin: 0 auto;
}
.memory-grid[data-pairs='2'] { max-width: 270px; }
.memory-grid[data-pairs='3'] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.memory-grid[data-pairs='4'] { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.memory-card {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  padding: 0;
  overflow: hidden;
  isolation: isolate;
  border: 1px solid #cba666;
  border-radius: 8px;
  background: #13372d;
  box-shadow: inset 0 0 0 3px #f0d59b12, 0 3px 9px #00171170;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: border-color .18s ease, box-shadow .18s ease;
}
.memory-back,
.memory-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 5px;
  overflow: hidden;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.memory-back {
  z-index: 1;
  border: 4px solid transparent;
  border-radius: 8px;
  background: radial-gradient(circle at 50% 45%, #486344, #13392d 70%);
  color: #eccb88;
  font-family: var(--serif);
  font-size: 36px;
  line-height: 1;
  opacity: 1;
  transform: scaleX(1);
}
.memory-back::after {
  content: '';
  position: absolute;
  inset: 4px;
  border: 1px solid #d3b17358;
  border-radius: 4px;
}
.memory-face {
  z-index: 2;
  background: radial-gradient(ellipse, #b89a5752, #102f27 88%);
  opacity: 0;
  transform: scaleX(.8);
}
.memory-face img {
  display: block;
  flex: 1 1 0;
  width: 100%;
  height: 100%;
  min-height: 0;
  object-fit: contain;
  object-position: center;
  border: 0;
  border-radius: 3px;
}
.memory-face > span,
.memory-face > small {
  flex: 0 0 auto;
  margin-top: 2px;
  color: #fff0d0;
  font-family: var(--sans);
  font-size: 10px;
  line-height: 1.15;
  text-align: center;
}
.memory-card.is-open .memory-back,
.memory-card.is-matched .memory-back { opacity: 0; transform: scaleX(.8); }
.memory-card:disabled { opacity: 1; filter: none; }
.memory-card.is-open .memory-face,
.memory-card.is-matched .memory-face { opacity: 1; transform: scaleX(1); }
.memory-card.is-matched {
  opacity: 1;
  border-color: #bdd997;
  box-shadow: inset 0 0 0 2px #c5df9959, 0 0 10px #b6d67c36;
}
.memory-card.is-matched::after {
  content: '✓';
  position: absolute;
  z-index: 3;
  top: 4px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 19px;
  height: 19px;
  border: 1px solid #e3edc1;
  border-radius: 50%;
  background: #24452c;
  color: #eef5d1;
  font-size: 12px;
  line-height: 1;
  pointer-events: none;
}

/* Brass pipes light progressively, rather than relying only on a success text. */
.screen-card[data-activity='pipes'] .activity-panel {
  --pipe-size: 216px;
  display: grid;
  grid-template-columns: 28px minmax(0, var(--pipe-size)) 38px;
  grid-template-rows: auto repeat(3, calc(var(--pipe-size) / 3));
  justify-content: center;
  column-gap: 4px;
}
.screen-card[data-activity='pipes'] .activity-caption { grid-column: 1 / -1; grid-row: 1; }
.pipe-board {
  grid-column: 2;
  grid-row: 2 / 5;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 4px;
  width: var(--pipe-size, 216px);
  aspect-ratio: 1;
  margin: 0 auto;
  padding: 5px;
  border: 1px solid #ba985c;
  border-radius: 9px;
  background: #071f1bd9;
  box-shadow: 0 4px 20px #00150b70, inset 0 0 0 2px #d1b27920;
}
.pipe-tile {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  padding: 0;
  border: 1px solid #b1935a77;
  border-radius: 4px;
  background: linear-gradient(135deg, #214238, #0d2c25);
  color: #d2ad6c;
  touch-action: manipulation;
  transition: background .2s ease, border-color .2s ease;
}
.pipe-tile svg { display: block; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.pipe-tile svg path { fill: none; stroke: currentColor; stroke-width: 12; stroke-linecap: round; stroke-linejoin: round; }
.pipe-tile svg circle { fill: currentColor; }
.pipe-tile.is-connected {
  border-color: #a8d9b6;
  background: radial-gradient(ellipse, #467253, #1b4032 80%);
  color: #d2f0cb;
  box-shadow: inset 0 0 12px #a5e3b435;
}
.pipe-tile.is-connected svg { filter: drop-shadow(0 0 3px #afe5bb70); }
.pipe-inlet,
.pipe-outlet {
  display: block;
  align-self: center;
  width: 100%;
  margin: 0;
  color: #f2d9a5;
  font-size: 10px;
  line-height: 1.3;
}
.pipe-inlet { grid-column: 1; grid-row: 2; text-align: right; }
.pipe-outlet { grid-column: 3; grid-row: 4; text-align: left; }

/* The real marker position is set by the game clock, not a decorative loop. */
.timing-track {
  position: relative;
  width: 100%;
  height: 74px;
  margin: 13px 0;
  overflow: hidden;
  border: 1px solid #d3b173;
  border-radius: 8px;
  background: repeating-linear-gradient(90deg, #071f1e00 0 9%, #d0bd803b 9% 9.3%, #071f1e00 9.3% 10%), linear-gradient(#18362d, #0b281f);
  box-shadow: inset 0 3px 14px #0007;
}
.timing-track::before {
  content: '';
  position: absolute;
  inset: 50% 0 auto;
  height: 1px;
  background: #e8cf9266;
}
.timing-safe {
  position: absolute;
  left: var(--safe-start, 42%);
  top: 0;
  bottom: 0;
  width: var(--safe-width, 16%);
  border-inline: 1px solid #c3e9a9;
  background: linear-gradient(#a1d88150, #a1d88185, #a1d88150);
  box-shadow: 0 0 18px #b2dc8730;
}
.timing-safe::after {
  content: '✓';
  position: absolute;
  top: 3px;
  left: 0;
  width: 100%;
  color: #eefbdc;
  font-size: 14px;
  text-align: center;
}
.timing-marker {
  position: absolute;
  z-index: 2;
  top: 7px;
  bottom: 7px;
  width: 7px;
  transform: translateX(-50%);
  border: 1px solid #fff0c2;
  border-radius: 3px;
  background: #f4d083;
  box-shadow: 0 0 13px #ffda878c, 0 1px 3px #071b10;
  pointer-events: none;
}
.timing-status { margin-block: 0 4px; }

/* Only the circular red glass reveals fragments. Outside it, the parchment
   remains noisy. Solving is handled by the separate answer window. */
.screen-card[data-activity='lens'] .screen-widget .cipher-paper {
  --lens-size: 108px;
  height: clamp(230px, 35.5dvh, 300px);
  margin: 0;
  border-color: #d3af75;
  border-radius: 7px;
  touch-action: none;
  cursor: crosshair;
}
.screen-card[data-activity='lens'] .screen-widget .cipher-paper.assisted { --lens-size: 132px; }
.screen-card[data-activity='lens'] .cipher-paper .cipher-noise {
  padding: 13px;
  font-size: 14px;
  line-height: 1.75;
  letter-spacing: .16em;
}
.screen-card[data-activity='lens'] .cipher-paper .cipher-reveal {
  visibility: hidden;
  background: radial-gradient(ellipse, #f8cbb7, #e1a895);
  clip-path: circle(calc(var(--lens-size) / 2 - 4px) at var(--lens-x, 18%) var(--lens-y, 84%));
}
.screen-card[data-activity='lens'] .cipher-paper:not(.lens-on) .cipher-reveal { display: none; }
.screen-card[data-activity='lens'] .cipher-paper.lens-on .cipher-reveal { visibility: visible; }
.screen-card[data-activity='lens'] .lens-fragment {
  position: absolute;
  transform: translate(-50%, -50%);
  width: max-content;
  max-width: 112px;
  color: #49251e;
  font-family: 'Courier New', Courier, monospace;
  font-size: 15px;
  font-weight: 700;
  line-height: 1.22;
  text-align: center;
  text-wrap: balance;
  pointer-events: none;
}
.screen-card[data-activity='lens'] .red-lens {
  background: #e9412714;
  box-shadow: inset 0 0 0 2px #f8d294, inset 0 0 18px #c43e3e22, 0 0 0 1px #533c24, 0 5px 14px #68372155;
  cursor: grab;
}
.screen-card[data-activity='lens'] .red-lens:active { cursor: grabbing; }
.screen-card[data-activity='lens'] .cipher-marker { display: none; }
.screen-card[data-activity='lens'] .cipher-progress { margin-top: 5px; }
.lens-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: 6px;
}
.lens-controls button {
  display: grid;
  place-items: center;
  flex: 0 1 52px;
  width: 52px;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  padding: 5px;
  border: 1px solid #cbae747e;
  border-radius: 5px;
  background: #153b2ee6;
  color: #f3d69d;
  font-size: 20px;
  line-height: 1;
  touch-action: manipulation;
}
.lens-controls .lens-read-button { flex: 1 1 auto; width: auto; font-size: 13px; line-height: 1.25; }

/* A compact answer window leaves the searched document visible behind it. */
.modal.screen-modal.lens-answer-sheet {
  position: fixed;
  inset: calc((var(--viewport-height, 100dvh) + env(safe-area-inset-top) - env(safe-area-inset-bottom)) / 2) auto auto 50%;
  transform: translate(-50%, -50%);
  width: calc(var(--stage-width) - 28px);
  max-width: 420px;
  height: auto;
  max-height: calc(var(--viewport-height, 100dvh) - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  margin: 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid #d2ac6e;
  border-radius: 12px;
  background: #103228;
  box-shadow: 0 16px 60px #00130dd9, inset 0 0 0 3px #cfaa6310;
}
.modal.screen-modal.lens-answer-sheet::backdrop { background: #00160f94; backdrop-filter: blur(1px); }
.screen-modal.lens-answer-sheet .modal-content {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: auto;
  min-height: 0;
  max-height: inherit;
  overflow: hidden;
  padding: 0;
}
.screen-modal.lens-answer-sheet .sheet-head {
  min-height: 0;
  padding: 17px 18px 12px;
  background: transparent;
  border-color: #c9a56336;
}
.screen-modal.lens-answer-sheet .sheet-head h2 {
  margin: 4px 0 0;
  font-size: 24px;
  line-height: 1.1;
}
.screen-modal.lens-answer-sheet .sheet-body {
  flex: 0 0 auto;
  padding: 12px 18px 6px;
  gap: 8px;
}
.screen-modal.lens-answer-sheet .sheet-message,
.screen-modal.lens-answer-sheet .sheet-body > p {
  margin: 0;
  font-size: 15px;
  line-height: 1.35;
}
.screen-modal.lens-answer-sheet .sheet-actions {
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 10px 18px 17px;
}
.screen-modal.lens-answer-sheet button {
  min-height: 44px;
  margin: 0;
  padding: 9px 10px;
  font-size: 14px;
  line-height: 1.25;
}
.screen-modal.lens-answer-sheet .lens-answer-options {
  display: grid;
  grid-template-columns: 1fr;
  gap: 7px;
}
.screen-modal.lens-answer-sheet .lens-answer-options button { width: 100%; }
.screen-modal.lens-answer-sheet .lens-answer-feedback {
  margin: 0;
  color: #eed8a8;
  font-size: 13px;
  line-height: 1.35;
}
.screen-modal.lens-answer-sheet .lens-answer-feedback:empty { display: none; }
.screen-modal.lens-answer-sheet .text-button { font-size: 13px; color: #e7dfc7; }

/* The plateau can become a search map without reopening completed places. */
.search-object {
  margin: 5px 0;
  padding: 7px 10px;
  border: 1px solid #e1c28166;
  border-radius: 6px;
  background: #17382fe8;
  color: #f7e3b9;
  font-size: 12px;
  line-height: 1.3;
  text-wrap: balance;
}
.search-object > strong { color: #ffe4a5; font-weight: 650; }
.map-space.search-candidate:not(:disabled) .space-card {
  border-color: #ffe4a8;
  box-shadow: 0 0 0 3px #efc77155, 0 0 20px #efc77145, 0 4px 12px #00160ab3;
}
.map-space.search-candidate:not(:disabled) .space-label { color: #ffecc3; border: 1px solid #eaca8077; }

@media (max-width: 360px), (max-height: 620px) {
  .screen-card[data-activity]:not([data-activity='']) > .reader-panel { gap: 6px; padding-top: 10px; }
  .screen-card[data-activity]:not([data-activity='']) .reader-panel #card-title { font-size: 21px; }
  .screen-card[data-activity]:not([data-activity='']) .screen-text { font-size: 14px; line-height: 1.3; }
  .activity-caption, .activity-panel .activity-caption { margin-bottom: 6px; font-size: 11px; }
  .memory-grid { --memory-height: 178px; gap: 6px; }
  .memory-back { font-size: 30px; }
  .screen-card[data-activity='pipes'] .activity-panel { --pipe-size: 198px; }
  .screen-card[data-activity='lens'] .screen-widget .cipher-paper { height: 230px; --lens-size: 104px; }
  .screen-card[data-activity='lens'] .screen-widget .cipher-paper.assisted { --lens-size: 126px; }
  .screen-card[data-activity='lens'] .lens-fragment { max-width: 100px; font-size: 14px; }
  .screen-modal.lens-answer-sheet .sheet-head { padding: 14px 15px 10px; }
  .screen-modal.lens-answer-sheet .sheet-head h2 { font-size: 22px; }
  .screen-modal.lens-answer-sheet .sheet-body { padding: 10px 15px 5px; }
  .screen-modal.lens-answer-sheet .sheet-actions { padding: 8px 15px 14px; gap: 6px; }
}

/* Rotate the composition, not the user: text remains on the right, and the
   activity moves to the left. The original landscape reader is only 61% wide. */
@media (orientation: landscape) and (max-height: 430px) {
  .screen-card[data-activity]:not([data-activity='']) > .reader-panel {
    display: grid;
    grid-template-columns: minmax(0, 1.05fr) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    column-gap: 18px;
    width: 100%;
    min-height: calc(var(--viewport-height, 100dvh) - 48px - env(safe-area-inset-top));
    max-height: calc(var(--viewport-height, 100dvh) - 42px - env(safe-area-inset-top));
    padding: 7px 16px calc(10px + env(safe-area-inset-bottom));
  }
  .screen-card[data-activity]:not([data-activity='']) > .reader-panel::before { inset: -45px 0 -80px; background: linear-gradient(#031b1610, #031b16c7 45px, #031b16f2); }
  .screen-card[data-activity]:not([data-activity='']) .screen-eyebrow { display: none; }
  .screen-card[data-activity]:not([data-activity='']) #card-title { grid-column: 2; grid-row: 1; }
  .screen-card[data-activity]:not([data-activity='']) .screen-text { grid-column: 2; grid-row: 2; font-size: 14px; }
  .screen-card[data-activity]:not([data-activity='']) .screen-widget { grid-column: 1; grid-row: 1 / -1; align-self: center; }
  .screen-card[data-activity]:not([data-activity='']) .screen-note { grid-column: 2; grid-row: 3; align-self: center; }
  .screen-card[data-activity]:not([data-activity='']) .screen-actions { grid-column: 2; grid-row: 4; flex-direction: column; }
  .screen-card[data-activity]:not([data-activity='']) .screen-actions > button { width: 100%; min-height: 44px; font-size: 13px; }
  .memory-grid { --memory-height: 164px; gap: 5px; }
  .screen-card[data-activity='pipes'] .activity-panel { --pipe-size: 168px; grid-template-columns: 25px minmax(0, var(--pipe-size)) 35px; column-gap: 2px; }
  .pipe-inlet, .pipe-outlet { font-size: 9px; }
  .screen-card[data-activity='lens'] .screen-widget .cipher-paper { height: clamp(135px, 48dvh, 184px); --lens-size: 90px; }
  .screen-card[data-activity='lens'] .screen-widget .cipher-paper.assisted { --lens-size: 108px; }
  .screen-card[data-activity='lens'] .lens-fragment { font-size: 12px; max-width: 87px; }
  .lens-controls { margin-top: 4px; gap: 4px; }
  .modal.screen-modal.lens-answer-sheet { width: min(92vw, 580px); max-width: 580px; }
  .screen-modal.lens-answer-sheet .modal-content { display: grid; grid-template-columns: minmax(0, .85fr) minmax(0, 1.15fr); grid-template-rows: auto minmax(0, 1fr); }
  .screen-modal.lens-answer-sheet .sheet-head { grid-column: 1; grid-row: 1; padding: 15px 14px 8px; border: 0; }
  .screen-modal.lens-answer-sheet .sheet-body { grid-column: 1; grid-row: 2; padding: 4px 14px 14px; justify-content: flex-start; }
  .screen-modal.lens-answer-sheet .sheet-actions { grid-column: 2; grid-row: 1 / -1; padding: 12px 14px; }
}

@media (prefers-reduced-motion: reduce) {
  .memory-back, .memory-face, .memory-card, .pipe-tile { transition: none; }
}
