/* AIDEV-NOTE: Crosswork game styles. Color palette: cream #FAF8F0, charcoal #2C2C2C, accent blue #4A90D9 */

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* AIDEV-NOTE: The [hidden] attribute must always win over display set by class selectors */
[hidden] {
  display: none !important;
}

:root {
  --bg-cream: #FAF8F0;
  --bg-white: #FFFFFF;
  --text-dark: #2C2C2C;
  --text-muted: #6B6B6B;
  --accent-blue: #4A90D9;
  --accent-blue-light: #6BAAEF;
  --accent-blue-dark: #3570B0;
  --success-green: #4CAF50;
  --success-green-light: #C8E6C9;
  --error-red: #D9534F;
  --error-red-light: #FFCDD2;
  --warning-amber: #F5A623;
  --border-light: #E0DCD4;
  --border-medium: #C5C0B6;
  --cell-bg: #FFFFFF;
  --blocked-bg: #2C2C2C;
  --tile-shadow: 0 2px 8px rgba(0,0,0,0.12);
  --tile-shadow-hover: 0 4px 16px rgba(0,0,0,0.18);
  --tile-shadow-drag: 0 8px 24px rgba(0,0,0,0.24);
  --grid-shadow: 0 2px 12px rgba(0,0,0,0.08);
  --modal-shadow: 0 8px 40px rgba(0,0,0,0.2);
  --transition-fast: 0.15s ease;
  --transition-med: 0.25s ease;
  --transition-slow: 0.4s ease;
  --font-display: 'Playfair Display', Georgia, serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --grid-cell-size: 48px;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-body);
  background-color: var(--bg-cream);
  color: var(--text-dark);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* ===== Header ===== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-white);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left,
.header-right {
  flex: 0 0 auto;
  min-width: 60px;
}

.header-center {
  text-align: center;
  flex: 1;
}

.game-title {
  font-family: var(--font-display);
  font-size: 1.6rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  color: var(--text-dark);
  line-height: 1;
}

.game-subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
  font-weight: 500;
}

.header-btn {
  background: none;
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 6px 8px;
  cursor: pointer;
  color: var(--text-dark);
  transition: background var(--transition-fast), border-color var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-btn:hover {
  background: var(--bg-cream);
  border-color: var(--border-medium);
}

.header-stats {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mistake-counter {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.mistake-counter svg {
  opacity: 0.6;
}

.timer {
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* ===== Loading ===== */
.loading-screen,
.error-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  gap: 16px;
  text-align: center;
}

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border-light);
  border-top-color: var(--accent-blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.error-screen h2 {
  font-family: var(--font-display);
  font-size: 1.3rem;
}

.error-screen p {
  color: var(--text-muted);
}

/* ===== Main Content ===== */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

.game-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  width: 100%;
}

/* ===== Clues Panel ===== */
.clues-panel {
  display: flex;
  gap: 24px;
  width: 100%;
  max-width: 600px;
  background: var(--bg-white);
  border-radius: 12px;
  padding: 16px 20px;
  box-shadow: var(--grid-shadow);
}

.clues-section {
  flex: 1;
  min-width: 0;
}

.clues-heading {
  font-family: var(--font-display);
  font-size: 0.9rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 2px solid var(--border-light);
}

.clues-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.clue-item {
  font-size: 0.82rem;
  line-height: 1.35;
  padding: 4px 6px;
  border-radius: 4px;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.clue-item:hover {
  background: var(--bg-cream);
}

.clue-item.clue-solved {
  color: var(--text-muted);
  text-decoration: line-through;
  opacity: 0.6;
}

.clue-item .clue-number {
  font-weight: 700;
  margin-right: 4px;
}

/* ===== Grid ===== */
.grid-wrapper {
  position: relative;
}

.grid-container {
  display: grid;
  gap: 0;
  border: 2px solid var(--text-dark);
  box-shadow: var(--grid-shadow);
  background: var(--text-dark);
  border-radius: 4px;
  overflow: hidden;
  /* grid-template set dynamically */
}

.grid-cell {
  width: var(--grid-cell-size);
  height: var(--grid-cell-size);
  background: var(--cell-bg);
  border: 0.5px solid var(--border-medium);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-body);
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-dark);
  user-select: none;
  transition: background var(--transition-fast);
}

.grid-cell.blocked {
  background: var(--blocked-bg);
  border-color: var(--blocked-bg);
}

.grid-cell.prefilled .cell-letter {
  color: var(--text-muted);
}

.grid-cell.revealed .cell-letter {
  color: var(--accent-blue);
}

.cell-number {
  position: absolute;
  top: 2px;
  left: 3px;
  font-size: 0.55rem;
  font-weight: 600;
  line-height: 1;
  color: var(--text-dark);
}

.cell-letter {
  font-size: 1.1rem;
  font-weight: 600;
  text-transform: uppercase;
  line-height: 1;
  transition: color var(--transition-fast);
}

/* Intersection feedback */
.grid-cell.match {
  background: var(--success-green-light);
}

.grid-cell.clash {
  background: var(--error-red-light);
  animation: shake 0.35s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-3px); }
  40% { transform: translateX(3px); }
  60% { transform: translateX(-2px); }
  80% { transform: translateX(2px); }
}

/* Reject shake (invalid placement) */
.grid-cell.shake-reject {
  animation: shake 0.35s ease;
  background: rgba(217, 83, 79, 0.08);
}

/* Drop target highlighting */
.grid-cell.drop-target {
  background: rgba(74, 144, 217, 0.12);
  outline: 2px dashed var(--accent-blue);
  outline-offset: -2px;
}

.grid-cell.drop-target-invalid {
  background: rgba(217, 83, 79, 0.1);
  outline: 2px dashed var(--error-red);
  outline-offset: -2px;
}

/* Slot highlight when a tile is selected */
.grid-cell.slot-highlight {
  background: rgba(74, 144, 217, 0.08);
  cursor: pointer;
}

.grid-cell.slot-highlight:hover {
  background: rgba(74, 144, 217, 0.18);
}

/* Placed word cells - clickable to remove */
.grid-cell.placed-word {
  cursor: pointer;
}

.grid-cell.placed-word:hover {
  background: rgba(74, 144, 217, 0.08);
}

.grid-cell.placed-word.match:hover {
  background: #B5DEB8;
}

/* ===== Hints Bar ===== */
.hints-bar {
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* ===== Buttons ===== */
.btn {
  font-family: var(--font-body);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all var(--transition-fast);
}

.btn-primary {
  background: var(--accent-blue);
  color: white;
  box-shadow: 0 2px 6px rgba(74, 144, 217, 0.3);
}

.btn-primary:hover {
  background: var(--accent-blue-dark);
  box-shadow: 0 3px 10px rgba(74, 144, 217, 0.4);
}

.btn-hint {
  background: var(--bg-white);
  color: var(--text-dark);
  border: 1px solid var(--border-light);
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.btn-hint:hover {
  border-color: var(--accent-blue);
  color: var(--accent-blue);
  box-shadow: var(--tile-shadow);
}

.btn-hint:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  border-color: var(--border-light);
  color: var(--text-muted);
}

/* ===== Tile Pool ===== */
.tile-pool-wrapper {
  width: 100%;
  max-width: 600px;
  text-align: center;
}

.pool-heading {
  font-family: var(--font-display);
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.tile-pool {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  min-height: 48px;
  padding: 8px;
}

/* ===== Word Tile ===== */
.word-tile {
  font-family: var(--font-body);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 8px 16px;
  background: var(--bg-white);
  border: 2px solid var(--border-medium);
  border-radius: 10px;
  cursor: grab;
  user-select: none;
  box-shadow: var(--tile-shadow);
  transition: all var(--transition-fast);
  touch-action: none;
  position: relative;
}

.word-tile:hover {
  box-shadow: var(--tile-shadow-hover);
  border-color: var(--accent-blue);
  transform: translateY(-1px);
}

.word-tile:active {
  cursor: grabbing;
}

.word-tile.selected {
  border-color: var(--accent-blue);
  background: rgba(74, 144, 217, 0.08);
  box-shadow: 0 0 0 3px rgba(74, 144, 217, 0.25), var(--tile-shadow-hover);
  transform: translateY(-2px);
}

.word-tile.dragging {
  opacity: 0.5;
  transform: scale(0.95);
}

.word-tile.tile-hidden {
  display: none;
}

/* Tile snap animation */
@keyframes tileSnap {
  0% { transform: scale(1.1); opacity: 0.6; }
  50% { transform: scale(0.95); }
  100% { transform: scale(1); opacity: 1; }
}

.grid-cell.snap-anim .cell-letter {
  animation: tileSnap 0.3s ease;
}

/* ===== Modals ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  opacity: 0;
  transition: opacity var(--transition-med);
  pointer-events: none;
}

.modal-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  background: var(--bg-white);
  border-radius: 16px;
  box-shadow: var(--modal-shadow);
  max-width: 420px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  transform: translateY(20px) scale(0.96);
  transition: transform var(--transition-med);
}

.modal-overlay.visible .modal {
  transform: translateY(0) scale(1);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 12px;
}

.modal-header h2 {
  font-family: var(--font-display);
  font-size: 1.3rem;
  font-weight: 700;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.6rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
  transition: color var(--transition-fast), background var(--transition-fast);
}

.modal-close:hover {
  color: var(--text-dark);
  background: var(--bg-cream);
}

.modal-body {
  padding: 8px 24px 24px;
}

/* How to Play */
.how-to-step {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 14px;
}

.step-num {
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  background: var(--accent-blue);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  font-weight: 700;
}

.how-to-step p {
  font-size: 0.88rem;
  line-height: 1.45;
  color: var(--text-dark);
}

.scoring-info {
  margin-top: 16px;
  padding: 12px 14px;
  background: var(--bg-cream);
  border-radius: 8px;
  font-size: 0.82rem;
  line-height: 1.6;
}

.scoring-info p:first-child {
  margin-bottom: 2px;
}

/* Win Modal */
.win-modal .modal-header {
  text-align: center;
  justify-content: center;
  position: relative;
}

.win-modal .modal-close {
  position: absolute;
  right: 16px;
  top: 16px;
}

.win-body {
  text-align: center;
}

.stars-display {
  font-size: 2.4rem;
  margin-bottom: 8px;
  letter-spacing: 4px;
}

.streak-display {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent-blue);
  margin-bottom: 16px;
  letter-spacing: 0.02em;
}

.win-stats {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin-bottom: 20px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text-dark);
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 600;
  margin-top: 2px;
}

.share-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.share-preview {
  font-family: var(--font-body);
  font-size: 0.75rem;
  line-height: 1.5;
  background: var(--bg-cream);
  padding: 12px 16px;
  border-radius: 8px;
  text-align: left;
  white-space: pre;
  max-width: 100%;
  overflow-x: auto;
}

.share-copied {
  font-size: 0.8rem;
  color: var(--success-green);
  font-weight: 600;
}

/* ===== Confetti Canvas ===== */
#confettiCanvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 999;
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  :root {
    --grid-cell-size: 40px;
  }

  .header {
    padding: 8px 12px;
  }

  .game-title {
    font-size: 1.25rem;
  }

  .clues-panel {
    flex-direction: column;
    gap: 12px;
    padding: 12px 14px;
  }

  .hints-bar {
    flex-direction: column;
    align-items: center;
  }

  .tile-pool {
    gap: 6px;
  }

  .word-tile {
    font-size: 0.82rem;
    padding: 6px 12px;
  }

  .main-content {
    padding: 10px;
  }
}

@media (max-width: 360px) {
  :root {
    --grid-cell-size: 36px;
  }

  .cell-number {
    font-size: 0.48rem;
  }

  .cell-letter {
    font-size: 0.95rem;
  }
}

@media (min-width: 768px) {
  :root {
    --grid-cell-size: 52px;
  }

  .game-title {
    font-size: 1.8rem;
  }

  .cell-letter {
    font-size: 1.25rem;
  }
}
