/* ==========================================================================
   Word Grid - Main Stylesheet
   ========================================================================== */

/* --------------------------------------------------------------------------
   CSS Custom Properties (Theme)
   -------------------------------------------------------------------------- */
:root {
  /* Colors */
  --color-bg: #f8f9fa;
  --color-surface: #ffffff;
  --color-text: #1a1a2e;
  --color-text-muted: #6c757d;
  --color-primary: #4361ee;
  --color-success: #06d6a0;
  --color-cell-bg: #ffffff;
  --color-cell-border: #dee2e6;
  --color-cell-active: #4361ee;
  --color-cell-path: #e8ecff;

  /* Spacing */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
  --spacing-xl: 2rem;

  /* Grid */
  --grid-size: min(90vw, 400px);
  --cell-gap: 6px;

  /* Typography */
  --font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-size-sm: 0.875rem;
  --font-size-base: 1rem;
  --font-size-lg: 1.25rem;
  --font-size-xl: 1.5rem;
  --font-size-cell: clamp(1.5rem, 5vw, 2rem);

  --color-reject: #e63946;
  --color-duplicate: #f4a261;
  --color-used-bg: rgba(6, 214, 160, 0.15);
  --color-used-border: rgba(6, 214, 160, 0.4);

  /* Borders & Shadows */
  --border-radius: 8px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
}

/* Dark mode */
[data-theme="dark"] {
  --color-bg: #1a1a2e;
  --color-surface: #16213e;
  --color-text: #edf2f4;
  --color-text-muted: #8d99ae;
  --color-primary: #4cc9f0;
  --color-success: #06d6a0;
  --color-cell-bg: #0f3460;
  --color-cell-border: #1a1a2e;
  --color-cell-active: #4cc9f0;
  --color-cell-path: #1a3a5c;
  --color-reject: #e63946;
  --color-duplicate: #f4a261;
  --color-used-bg: rgba(6, 214, 160, 0.2);
  --color-used-border: rgba(6, 214, 160, 0.5);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
}

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

html {
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-family);
  background-color: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

/* --------------------------------------------------------------------------
   Layout
   -------------------------------------------------------------------------- */
.game-container {
  position: relative;
  max-width: 500px;
  margin: 0 auto;
  padding: var(--spacing-md);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--spacing-lg);
}

.game-header {
  text-align: center;
}

.game-header h1 {
  font-size: var(--font-size-xl);
  font-weight: 700;
  margin-bottom: var(--spacing-xs);
}

.game-date {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}

/* --------------------------------------------------------------------------
   Menu
   -------------------------------------------------------------------------- */
.menu-button {
  position: absolute;
  top: var(--spacing-md);
  right: var(--spacing-md);
  background: var(--color-surface);
  border: 1px solid var(--color-cell-border);
  border-radius: var(--border-radius);
  padding: var(--spacing-sm);
  cursor: pointer;
  font-size: var(--font-size-lg);
  line-height: 1;
  box-shadow: var(--shadow-sm);
  transition: background-color 0.2s ease;
  z-index: 31;
}

.menu-button:hover {
  background: var(--color-cell-path);
}

.menu-panel {
  display: none;
  position: absolute;
  top: calc(var(--spacing-md) + 2.5rem);
  right: var(--spacing-md);
  background: var(--color-surface);
  border: 1px solid var(--color-cell-border);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow-md);
  min-width: 160px;
  z-index: 30;
  overflow: hidden;
}

.menu-panel.menu-open {
  display: block;
}

.menu-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  background: none;
  border: none;
  color: var(--color-text);
  font-size: var(--font-size-sm);
  font-family: var(--font-family);
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.menu-item:hover {
  background: var(--color-cell-path);
}

.menu-item-value {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}

.menu-divider {
  height: 1px;
  background: var(--color-cell-border);
}

/* --------------------------------------------------------------------------
   About Modal
   -------------------------------------------------------------------------- */
.about-modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.6);
  align-items: center;
  justify-content: center;
  padding: var(--spacing-lg);
}

.about-modal.about-open {
  display: flex;
}

.about-content {
  position: relative;
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: var(--border-radius);
  padding: var(--spacing-xl);
  max-width: 400px;
  width: 100%;
  box-shadow: var(--shadow-md);
}

.about-content h2 {
  font-size: var(--font-size-xl);
  font-weight: 700;
  margin-bottom: var(--spacing-md);
}

.about-content p {
  font-size: var(--font-size-sm);
  line-height: 1.6;
  margin-bottom: var(--spacing-sm);
}

.about-attribution {
  color: var(--color-text-muted);
  margin-top: var(--spacing-md);
}

.about-attribution a {
  color: var(--color-primary);
  text-decoration: none;
}

.about-attribution a:hover {
  text-decoration: underline;
}

.about-close {
  position: absolute;
  top: var(--spacing-sm);
  right: var(--spacing-sm);
  background: none;
  border: none;
  font-size: var(--font-size-xl);
  color: var(--color-text-muted);
  cursor: pointer;
  line-height: 1;
  padding: var(--spacing-xs);
}

.about-close:hover {
  color: var(--color-text);
}

/* --------------------------------------------------------------------------
   Letter Grid
   -------------------------------------------------------------------------- */
.grid-section {
  width: 100%;
  display: flex;
  justify-content: center;
  touch-action: none; /* Prevent scroll while dragging */
}

.grid-outer {
  position: relative;
  width: var(--grid-size);
}

.game-complete-overlay {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.6);
  border-radius: var(--border-radius);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--spacing-lg);
}

.game-complete .game-complete-overlay {
  display: flex;
}

.game-complete .grid-wrapper,
.game-complete .path-line {
  pointer-events: none;
}

.complete-title {
  font-size: var(--font-size-xl);
  font-weight: 700;
  color: var(--color-success);
}

.complete-subtitle {
  font-size: var(--font-size-base);
  color: #edf2f4;
  margin-top: var(--spacing-sm);
}

.grid-wrapper {
  width: 100%;
  transition: transform 0.3s ease;
}

.grid-wrapper .cell-letter,
.grid-wrapper .word-count {
  transition: transform 0.3s ease;
}

.path-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
  overflow: visible;
}

.path-line polyline {
  fill: none;
  stroke: var(--color-cell-active);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-opacity: 0.5;
}

.letter-grid {
  display: flex;
  flex-direction: column;
  gap: var(--cell-gap);
  width: 100%;
  padding: var(--spacing-sm);
  background: var(--color-surface);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow-md);
  user-select: none;
  -webkit-user-select: none;
}

.grid-row {
  display: flex;
  gap: var(--cell-gap);
}

.grid-cell {
  position: relative;
  flex: 1;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-cell-bg);
  border: 2px solid var(--color-cell-border);
  border-radius: var(--border-radius);
  cursor: pointer;
  transition: transform 0.1s ease, background-color 0.1s ease, border-color 0.1s ease;
}

.grid-cell:hover {
  transform: scale(1.02);
}

.grid-cell.in-path {
  background: var(--color-cell-path);
  border-color: var(--color-cell-active);
  transform: scale(1.05);
}

.grid-cell.current {
  background: var(--color-cell-active);
  border-color: var(--color-cell-active);
}

.grid-cell.current .cell-letter {
  color: white;
}

.cell-letter {
  font-size: var(--font-size-cell);
  font-weight: 700;
  text-transform: uppercase;
  pointer-events: none;
}

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

.grid-cell.exhausted {
  opacity: 0.35;
}

.word-count {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--color-text-muted);
  pointer-events: none;
  line-height: 1;
}

/* Accept animation for valid words */
.grid-cell.accept {
  background: var(--color-success);
  border-color: var(--color-success);
  animation: pop 0.3s ease;
}

.grid-cell.accept .cell-letter {
  color: white;
}

.path-line polyline.accept {
  stroke: var(--color-success);
}

@keyframes pop {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

/* Reject animation for invalid words (after .used so it overrides) */
.grid-cell.reject {
  background: var(--color-reject);
  border-color: var(--color-reject);
  animation: shake 0.3s ease;
}

.grid-cell.reject .cell-letter {
  color: white;
}

.path-line polyline.reject {
  stroke: var(--color-reject);
  animation: shake 0.3s ease;
}

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

/* Duplicate word feedback */
.grid-cell.duplicate {
  background: var(--color-duplicate);
  border-color: var(--color-duplicate);
  animation: shake 0.3s ease;
}

.grid-cell.duplicate .cell-letter {
  color: white;
}

.path-line polyline.duplicate {
  stroke: var(--color-duplicate);
  animation: shake 0.3s ease;
}

/* --------------------------------------------------------------------------
   Current Word Display
   -------------------------------------------------------------------------- */
.current-word {
  min-height: 3rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

#current-word-display {
  font-size: var(--font-size-xl);
  font-weight: 600;
  letter-spacing: 0.1em;
  color: var(--color-primary);
}

/* --------------------------------------------------------------------------
   Hint Section
   -------------------------------------------------------------------------- */
.hint-section {
  display: flex;
  justify-content: center;
  gap: var(--spacing-sm);
}

.hint-button {
  background: var(--color-surface);
  border: 2px solid var(--color-cell-border);
  border-radius: var(--border-radius);
  padding: var(--spacing-sm) var(--spacing-lg);
  font-size: var(--font-size-base);
  font-weight: 600;
  color: var(--color-text);
  cursor: pointer;
  transition: all 0.2s ease;
}

.hint-button:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.hint-button:active {
  transform: scale(0.98);
}

/* Hint highlighting on grid cells */
.grid-cell.hint-start {
  background: #ffd700;
  border-color: #ffa500;
  animation: hint-pulse 0.5s ease infinite alternate;
  position: relative;
}

@keyframes hint-pulse {
  from { transform: scale(1); }
  to { transform: scale(1.08); }
}

.hint-arrow {
  position: absolute;
  top: 50%;
  left: 50%;
  font-size: 2.5rem;
  font-weight: bold;
  color: #ff6600;
  text-shadow: 0 0 4px white, 0 0 8px white;
  pointer-events: none;
  z-index: 20;
}

/* --------------------------------------------------------------------------
   Stats Section
   -------------------------------------------------------------------------- */
.stats-section {
  width: 100%;
  display: flex;
  justify-content: space-around;
  background: var(--color-surface);
  border-radius: var(--border-radius);
  padding: var(--spacing-md);
  box-shadow: var(--shadow-sm);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--spacing-xs);
}

.stat-label {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  font-weight: 500;
}

.stat-value {
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--color-text);
}

.stat-value.achieved {
  color: var(--color-success);
}

/* Blackout celebration */
.clear-achieved .letter-grid {
  animation: clear-pulse 0.5s ease;
}

@keyframes clear-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.02); }
}

.clear-achieved .grid-cell.used {
  animation: cell-glow 0.3s ease forwards;
}

@keyframes cell-glow {
  to { box-shadow: 0 0 12px var(--color-success); }
}

/* --------------------------------------------------------------------------
   Words Section
   -------------------------------------------------------------------------- */
.words-section {
  width: 100%;
  background: var(--color-surface);
  border-radius: var(--border-radius);
  padding: var(--spacing-md);
  box-shadow: var(--shadow-sm);
}

.words-section:has(#found-words-list:empty) {
  display: none;
}

.sort-controls {
  display: flex;
  gap: 2px;
  margin-bottom: var(--spacing-sm);
  background: var(--color-cell-border);
  border-radius: var(--border-radius);
  overflow: hidden;
}

.sort-button {
  flex: 1;
  padding: var(--spacing-xs) var(--spacing-sm);
  border: none;
  background: var(--color-bg);
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.sort-button:hover {
  color: var(--color-text);
}

.sort-button.active {
  background: var(--color-primary);
  color: white;
}

#found-words-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-sm);
}

#found-words-list li {
  background: var(--color-success);
  color: white;
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--border-radius);
  font-size: var(--font-size-sm);
  font-weight: 500;
}

#found-words-list li.bonus {
  background: var(--color-primary);
}

/* --------------------------------------------------------------------------
   Utility Classes
   -------------------------------------------------------------------------- */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
