:root {
  --bg: #000;
  --accent: #0066cc;
  --card: #111;
  --muted: #ccc;
  --text-base: 16px;
  --piece-width: 118px;
  --column-gap: 8px;
  /* reference image sizing: min, preferred, max */
  --ref-min: 80px;
  /* preferred can grow with viewport but is capped by --ref-max */
  --ref-preferred: clamp(var(--ref-min), 9vw, var(--ref-max));
  --ref-max: 200px;
  /* central variable for piece sizing */
}

* {
  box-sizing: border-box
}

body {
  font-family: Myriad Pro, Arial, Helvetica, sans-serif;
  margin: 0;
  background: var(--bg);
  color: #fff;
  display: grid;
  grid-template-columns: 1fr;
}

/* Skip link: hidden visually but accessible to keyboard users */
.skip-link {
  position: absolute;
  left: -9999px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

.skip-link:focus {
  position: fixed;
  left: 12px;
  top: 12px;
  width: auto;
  height: auto;
  padding: 8px 12px;
  background: #ffd580;
  color: #000;
  border-radius: 4px;
  z-index: 9999;
}

.container {
  max-width: 1100px;
  margin: 24px auto;
  padding: 16px
}

header h1 {
  margin: 0 0 8px;
  font-size: 60px;
  color: orange;
  font-weight: 700;
  text-align: center;
}

.instructions {
  margin: 0 0 16px;
  color: orange;
}

/* make instructions and labels share the same base size */
.instructions {
  font-size: 1.125rem;
  text-align: center;
}

.game {
  display: flex;
  gap: 8px;
}

/* group the main interactive columns so the reference can be positioned above or to the side */
.main-columns {
  display: flex;
  gap: var(--column-gap);
  align-items: flex-start;
}

.pieces {
  /* fixed to piece width variable + 2px */
  width: calc(var(--piece-width) + 2px);
  padding: 0;
  box-sizing: border-box;
}

.strata {
  /* make the strata column about the same width as the piece images so .game is compact */
  flex: 0 0 calc(var(--piece-width) + 2px);
  /* dark surface for interactive strata */
  background: linear-gradient(180deg, #222, #111);
  padding: 8px;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  justify-content: flex-start;
  align-items: stretch;
}

.stratum {
  height: 150px;
  border: 2px dashed rgba(255, 255, 255, 1);
  border-radius: 4px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* visible focus for keyboard users */
.stratum:focus {
  outline: 3px solid rgba(0, 102, 204, 0.6);
  outline-offset: 2px;
}

/* screen-reader-only helper */
.sr-only {
  position: absolute !important;
  height: 1px;
  width: 1px;
  overflow: hidden;
  clip: rect(1px, 1px, 1px, 1px);
  white-space: nowrap;
}

.stratum.over {
  background: rgba(0, 102, 204, 0.12);
  border-color: var(--accent);
  border-style: dashed;
  box-shadow: 0 0 8px rgba(0, 102, 204, 0.18);
  animation: stratumPulse 1.2s ease-in-out infinite;
}

@keyframes stratumPulse {
  0% {
    box-shadow: 0 0 6px rgba(0, 102, 204, 0.12);
  }

  50% {
    box-shadow: 0 0 16px rgba(0, 102, 204, 0.22);
  }

  100% {
    box-shadow: 0 0 6px rgba(0, 102, 204, 0.12);
  }
}

@media (prefers-reduced-motion: reduce) {
  .stratum.over {
    animation: none;
  }

  .recent-change {
    animation: none;
  }
}

/* ghost preview for dragging */
.ghost {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.9;
  transform: translate(-50%, -50%);
  width: var(--piece-width);
}

.stratum .label {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.125rem;
  color: orange;
  text-align: center;
  max-width: calc(100% - 12px);
  word-wrap: break-word;
  white-space: normal;
  z-index: 1;
  /* keep label behind placed pieces */
  pointer-events: none;
  /* allow interactions to pass to pieces */
}

.pieces h2 {
  margin: 0 0 8px;
  color: orange;
  text-align: center;
  font-size: 1.125rem;
  font-weight: 600;
}

.strata h2 {
  margin: 0 0 8px;
  color: orange;
  text-align: center;
  font-size: 1.125rem;
  font-weight: 600;
}


.pieces-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  padding: 8px;
  background: var(--card);
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.04);
}

/* Reference strip: show three small strata column images aligned to the bottom */

#reference {
  margin-bottom: 15px;
}

.ref-caption {
  color: orange;
  margin: 0 0 6px;
  font-weight: 600;
  text-align: center;
  font-size: 1.5em
}

/* Make the paragraph(s) directly under the reference caption match the page text color */
#reference>p {
  color: inherit;
  margin: 5px 5px 5px;
  max-width: 820px;
  text-align: center;
}

/* When a reference paragraph explicitly needs the orange styling, use a
   more specific selector so it isn't overridden by the generic #reference>p rule. */
.ref-help {
  color: orange;
  font-size: 20px;
  text-align: center;
  color: orange;
}

.ref-images {
  display: grid;
  /* create responsive columns that can grow; each column at least --ref-min and may
     grow proportionally up to the container width (we cap with max-width below). */
  grid-auto-flow: column;
  /* each reference column will grow between --ref-min and --ref-preferred */
  grid-auto-columns: minmax(var(--ref-min), var(--ref-preferred));
  gap: var(--column-gap, 20px);
  align-items: end;
  padding: 6px 8px;
  justify-content: center;
  /* allow the reference to size up to the container or 3*--ref-max (whichever is smaller) */
  max-width: min(100%, calc(3 * var(--ref-max) + 2 * var(--column-gap)));
  width: 100%;
}

.ref-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
  /* fill the grid column; the column size controls the effective width */
  width: 100%;
  max-width: var(--ref-max);
}

.ref-images .ref-img {
  display: block;
  width: 100%;
  height: auto;
  object-fit: contain;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(255, 255, 255, 0.03);
}

.ref-label {
  color: orange;
  font-weight: 600;
  font-size: 20px;
  text-align: center
}

/* Slight tweak for very narrow screens: stack the reference images */
@media (max-width:420px) {
  #reference {
    flex-direction: row;
    gap: 4px;
  }

  #reference .ref-img {
    width: 33%;
  }
}

.piece {
  background: #171717;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.04);
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  touch-action: none;
  /* allow pointer events to handle touch dragging */
  position: relative;
  z-index: 2;
  /* ensure pieces render above stratum labels */
}

.piece img {
  display: block;
  width: var(--piece-width);
  max-width: 100%;
  height: auto;
  object-fit: contain;
}

.piece:focus {
  outline: 3px solid rgba(0, 102, 204, 0.4);
  outline-offset: 2px
}

/* Visual feedback for recent changes */
.recent-change {
  animation: recentFlash 1s ease-in-out;
}

@keyframes recentFlash {
  0% {
    box-shadow: 0 0 0 0 rgba(255, 165, 0, 0.0);
  }

  30% {
    box-shadow: 0 0 12px 4px rgba(255, 165, 0, 0.25);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(255, 165, 0, 0);
  }
}

.piece[aria-grabbed="true"] {
  opacity: 0.85;
  cursor: grabbing
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.controls button {
  flex: none;
  width: 100%;
  padding: 8px;
  border-radius: 6px;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

.controls button#resetBtn {
  background: #888
}

.message {
  text-align: center;
  margin-top: 20px;
  font-size: 30px;
}

.pieces-grid .placeholder {
  min-height: 56px
}

@media (max-width:800px) {

  /* Switch to a 3-column grid so the reference images and the interactive
     columns align perfectly and have equal left/right spacing. */
  .game {
    display: grid;
    grid-template-columns: repeat(3, calc(var(--piece-width) + 2px));
    justify-content: center;
    gap: var(--column-gap);
    align-items: start;
  }

  /* allow the .pieces and .strata inside .main-columns to participate in the grid */
  .main-columns {
    display: contents;
  }

  /* reference sits above and spans all three columns */
  #reference {
    order: -1;
    width: 100%;
    grid-column: 1 / -1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 8px;
  }

  /* make the reference images a 3-column grid matching piece width */
  .ref-images {
    display: grid;
    grid-template-columns: repeat(3, var(--piece-width));
    gap: var(--column-gap);
    align-items: end;
    padding: 0px 0;
  }

  /* place pieces in column 1 and strata in column 3 */
  .pieces {
    grid-column: 1;
    justify-self: center;
    width: calc(var(--piece-width) + 2px);
    padding: 0;
  }

  .strata {
    grid-column: 3;
    justify-self: center;
    width: calc(var(--piece-width) + 2px);
  }

  .pieces-grid {
    max-height: 60vh;
    overflow-y: auto;
  }

  .stratum .label {
    font-size: 1rem;
  }

}

/* Slightly increase gap on medium and larger screens for better separation */
@media (min-width:801px) {
  :root {
    --column-gap: 12px;
  }
}

/* Keyboard Navigation Button */
.keyboard-nav-btn {
  background: #ffcc4d;
  color: #000;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 0.9rem;
  cursor: pointer;
  margin-top: 12px;
  transition: background-color 0.2s ease, transform 0.1s ease;
  font-weight: 500;
}

.keyboard-nav-btn:hover {
  background: #ffd580;
  transform: translateY(-1px);
}

.keyboard-nav-btn:focus {
  outline: 2px solid #ffd580;
  outline-offset: 2px;
}

.keyboard-nav-btn:active {
  transform: translateY(0);
}

/* Modal Styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  display: none;
}

.modal[aria-hidden="false"] {
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: var(--card);
  border: 1px solid #333;
  border-radius: 8px;
  max-width: 600px;
  max-height: 80vh;
  width: 90%;
  margin: 20px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px 16px;
  border-bottom: 1px solid #333;
}

.modal-header h2 {
  margin: 0;
  color: white;
  font-size: 1.5rem;
}

.modal-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 2rem;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: color 0.2s ease, background-color 0.2s ease;
}

.modal-close:hover {
  color: white;
  background: #333;
}

.modal-close:focus {
  outline: 2px solid #ffd580;
  outline-offset: 2px;
}

.modal-body {
  padding: 20px 24px;
  overflow-y: auto;
  max-height: calc(80vh - 140px);
}

.modal-body h3 {
  color: #ffd580;
  margin: 0 0 12px 0;
  font-size: 1.2rem;
}

.modal-body h3:not(:first-child) {
  margin-top: 24px;
}

.modal-body ul {
  margin: 0 0 16px 0;
  padding-left: 20px;
}

.modal-body li {
  margin-bottom: 8px;
  line-height: 1.5;
}

.modal-body strong {
  color: #ffd580;
}

.modal-footer {
  padding: 16px 24px 20px;
  border-top: 1px solid #333;
  text-align: right;
}

.modal-close-btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.modal-close-btn:hover {
  background: #0052a3;
}

.modal-close-btn:focus {
  outline: 2px solid #ffd580;
  outline-offset: 2px;
}

/* Responsive adjustments for modal */
@media (max-width: 600px) {
  .modal-content {
    width: 95%;
    margin: 10px;
    max-height: 90vh;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding-left: 16px;
    padding-right: 16px;
  }

  .modal-body {
    max-height: calc(90vh - 120px);
  }
}