/* File: assets/css/pages/puzzle.css */

/* Reset for App-Like Experience */
html, body {
  overflow: hidden; /* Prevent body scrolling on puzzle page */
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  background-color: var(--bg-color, #fff8e1);
  display: flex;
  flex-direction: column;
  max-width: 100vw;
  overflow-x: hidden;
}

/* Hide footer on small screens to maximize game area */
@media (max-width: 1024px) {
  #site-footer {
    display: none !important;
  }
}

/* Custom Header for Puzzle */
.puzzle-header {
  background: var(--primary-color) !important; /* Make it pop like a game HUD */
  border-bottom: none !important;
  padding: 10px 0 !important;
  width: 100%;
}

.puzzle-header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.puzzle-header .logo a {
  color: white !important;
  text-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.header-stats {
  display: flex;
  gap: 24px;
  background: rgba(255, 255, 255, 0.2);
  padding: 8px 16px;
  border-radius: 50px;
  backdrop-filter: blur(5px);
  color: white;
  font-weight: 900;
  font-size: 1.1rem;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.1);
}

.header-stat {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-stat i {
  font-size: 1.2rem;
  color: var(--warning-color, #ffc107);
}

@media (max-width: 768px) {
  .puzzle-header-container {
    flex-direction: column;
    justify-content: center;
  }
  .header-stats {
    gap: 16px;
    font-size: 1rem;
    padding: 6px 16px;
    width: 100%;
    justify-content: space-around;
  }
  .header-stat {
    flex-direction: row; /* Keep icon and text side-by-side */
    gap: 4px;
    font-size: 0.95rem;
  }
  .header-stat i {
    font-size: 1rem;
  }
}

/* Game Layout */
.game-layout {
  display: grid;
  flex: 1; /* Takes up all remaining height after header */
  min-height: 0; /* Prevents overflow */
  width: 100%;
  max-width: 1600px;
  margin: 0 auto;
  gap: 16px;
  padding: 16px;
  box-sizing: border-box;
}

/* --- Desktop Layout --- */
@media (min-width: 1025px) {
  .game-layout {
    grid-template-columns: 320px 1fr;
    grid-template-areas: "sidebar core";
    align-items: stretch;
  }
}

/* --- Mobile Layout --- */
@media (max-width: 1024px) {
  .game-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas: 
      "sidebar"
      "core";
    padding: 8px;
    gap: 8px;
  }
}

/* Sidebar Area */
.game-sidebar {
  grid-area: sidebar;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  padding-right: 4px;
  min-width: 0; /* Prevent grid blowout */
}

@media (max-width: 1024px) {
  .game-sidebar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    overflow-y: visible;
    gap: 8px;
  }
}

/* Core Game Area */
.game-core {
  grid-area: core;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0; /* Important for flex children to shrink */
  min-width: 0; /* Prevent grid blowout */
  position: relative;
  background: white;
  border-radius: var(--radius-lg, 20px);
  padding: 16px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border-color);
}

@media (max-width: 1024px) {
  .game-core {
    gap: 8px;
    padding: 8px;
  }
}

/* --- Components --- */

/* Toolbar / Controls */
.toolbar {
  background: rgba(255, 255, 255, 0.9);
  padding: 16px;
  border-radius: var(--radius, 14px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

@media (max-width: 1024px) {
  .toolbar {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    gap: 8px;
    padding: 10px;
    width: 100%;
  }
  
  #back-btn {
    grid-column: 1 / 2;
    grid-row: 1 / 2;
  }
  .toolbar-actions {
    grid-column: 1 / 2;
    grid-row: 2 / 3;
    margin-top: 0;
  }
  
  /* Selectors group */
  .toolbar > .control-group:nth-of-type(1) {
    grid-column: 2 / 3;
    grid-row: 1 / 2;
  }
  .toolbar > .control-group:nth-of-type(2) {
    grid-column: 2 / 3;
    grid-row: 2 / 3;
  }
}

.control-group label {
  display: block;
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 4px;
  color: var(--muted-color);
}
@media (max-width: 1024px) {
  .control-group label { display: none; /* Save space on mobile */ }
}

.select-wrap select {
  width: 100%;
  font-size: 14px;
  padding: 8px 16px;
  min-height: 38px;
}

.toolbar-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
@media (max-width: 1024px) {
  .toolbar-actions {
    margin-top: 0;
  }
}

/* Reference Card */
.reference-card {
  padding: 12px;
  text-align: center;
}
.reference-card img {
  width: 100%;
  border-radius: 8px;
  border: 2px solid var(--border-color);
}
@media (max-width: 1024px) {
  .reference-card {
    display: none; /* Hide reference image by default on mobile to save space */
  }
}

/* --- Cards General --- */
.card-title {
  font-size: 1rem;
  font-weight: 900;
  margin: 0 0 12px 0;
  color: var(--primary-color);
  text-align: center;
}
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* Board Card */
.board-card {
  flex: 1; /* Takes up remaining height */
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}
.board-scroll {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto; /* Allow panning if board is huge */
  touch-action: none; /* Prevent scroll on touch while dragging */
  height: 100%;
  min-width: 0;
}

#board {
  display: grid;
  gap: 1px;
  background-color: var(--border-color);
  border: 3px solid var(--primary-color);
  border-radius: 8px;
  box-shadow: 0 8px 16px rgba(0,0,0,0.1);
  overflow: hidden;
  margin: auto;
}

.slot {
  background-color: rgba(255, 255, 255, 0.8);
  position: relative;
}
.slot.drag-over {
  background-color: rgba(32, 201, 151, 0.2);
  outline: 3px dashed var(--primary-color);
  outline-offset: -3px;
}

/* Piece Card / Tray */
.piece-card {
  height: clamp(140px, 20vh, 200px); /* Flexible height based on screen size */
  display: flex;
  flex-direction: column;
  border-top: 1px solid rgba(0,0,0,0.05); /* Separator */
  padding-top: 12px;
}
@media (max-width: 1024px) {
  .piece-card {
    height: 120px;
    padding-top: 8px;
  }
}

.pieces {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  height: 100%;
  overflow-y: auto;
  align-content: flex-start;
}

@media (max-width: 1024px) {
  .pieces {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    align-items: center;
    padding-bottom: 8px; /* For scrollbar */
  }
}

.piece {
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.15);
  cursor: grab;
  transition: transform 0.2s, box-shadow 0.2s;
  background-color: white;
  touch-action: none; /* Critical for dragging on mobile */
  flex-shrink: 0;
  width: var(--piece-w) !important;
  height: var(--piece-h) !important;
}

.pieces .piece {
  /* Scale down pieces only while they are in the tray */
  width: calc(var(--piece-w) * 0.7) !important;
  height: calc(var(--piece-h) * 0.7) !important;
}
@media (max-width: 1024px) {
  .pieces .piece {
    width: calc(var(--piece-w) * 0.55) !important;
    height: calc(var(--piece-h) * 0.55) !important;
  }
}
.piece:active {
  cursor: grabbing;
}
.piece:hover {
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 6px 12px rgba(0,0,0,0.2);
  z-index: 2;
}
.piece.dragging {
  opacity: 0.8;
  transform: scale(1.1);
  box-shadow: 0 10px 20px rgba(0,0,0,0.3);
  z-index: 100;
  /* Snap to full size when dragging */
  width: var(--piece-w) !important;
  height: var(--piece-h) !important;
}

/* Hint Button */
#hint-btn {
  position: absolute;
  bottom: 16px;
  right: 16px;
  z-index: 50;
  transition: transform 0.2s, box-shadow 0.2s, background-color 0.2s;
}
#hint-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 8px 16px rgba(0,0,0,0.3);
}

.mobile-only-btn {
  display: none;
}

#mobile-ref-btn {
  position: absolute;
  bottom: 16px;
  left: 16px;
  z-index: 50;
  background-color: var(--secondary-color, #6c757d);
  color: white;
}
#mobile-ref-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 8px 16px rgba(0,0,0,0.3);
}

@media (max-width: 1024px) {
  #hint-btn {
    bottom: calc(120px + 24px); /* Above the pieces tray */
    right: 12px;
  }
  .mobile-only-btn {
    display: flex;
  }
  #mobile-ref-btn {
    bottom: calc(120px + 24px); /* Same height as hint-btn */
    left: 12px;
  }
}

/* Edge Styles for Slots */
.edge-top { border-top: 1px solid rgba(0,0,0,0.05); }
.edge-bottom { border-bottom: 1px solid rgba(0,0,0,0.05); }
.edge-left { border-left: 1px solid rgba(0,0,0,0.05); }
.edge-right { border-right: 1px solid rgba(0,0,0,0.05); }

/* Win Modal Adjustments */
#win-modal {
  z-index: 9999;
}

/* Hint Animations */
@keyframes hint-pulse-source {
  0% { box-shadow: 0 0 0 0 rgba(32, 201, 151, 0.7); transform: scale(1); z-index: 100; }
  50% { box-shadow: 0 0 20px 10px rgba(32, 201, 151, 0); transform: scale(1.1); z-index: 100; }
  100% { box-shadow: 0 0 0 0 rgba(32, 201, 151, 0); transform: scale(1); z-index: 100; }
}

@keyframes hint-pulse-target {
  0% { box-shadow: inset 0 0 0 0 rgba(255, 193, 7, 0.7); background-color: rgba(255, 193, 7, 0.2); }
  50% { box-shadow: inset 0 0 20px 10px rgba(255, 193, 7, 0.6); background-color: rgba(255, 193, 7, 0.6); }
  100% { box-shadow: inset 0 0 0 0 rgba(255, 193, 7, 0); background-color: transparent; }
}

.hint-source {
  animation: hint-pulse-source 1s infinite !important;
  border: 3px solid var(--primary-color) !important;
}

.hint-target {
  position: relative !important;
}

.hint-target::after {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none; /* Let clicks pass through */
  animation: hint-pulse-target 1s infinite !important;
  border: 4px dashed var(--warning-color) !important;
  z-index: 50; /* Render ON TOP of any pieces inside the slot */
  border-radius: inherit;
}
