/* Reset and Base Styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary-color: #2563eb;
  --primary-hover: #1d4ed8;
  --secondary-color: #64748b;
  --secondary-hover: #475569;
  --background: #ffffff;
  --surface: #f8fafc;
  --border: #e2e8f0;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --cell-selected: #bfdbfe;
  --cell-highlighted: #93c5fd;
  --cell-black: #1e293b;
  --success: #10b981;
  --error: #ef4444;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--surface);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Loading Screen */
.loading {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  gap: 1rem;
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--primary-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

/* Error Message */
.error {
  display: none;
  padding: 1rem 2rem;
  margin: 2rem;
  background: #fee2e2;
  color: var(--error);
  border-radius: 8px;
  text-align: center;
}

/* App Container */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 1400px;
  margin: 0 auto;
  position: relative;
}

/* On-Screen Keyboard */
.on-screen-keyboard {
  background: var(--surface);
  padding: 0.5rem 0.75rem 0.75rem 0.75rem;
  border-top: 2px solid var(--border);
  display: none; /* Hidden by default on desktop */
  flex-direction: column;
  gap: 0.5rem;
  align-items: center;
  width: 100%;
}

.keyboard-row {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  width: 100%;
  max-width: 700px;
}

.key-btn {
  flex: 1;
  min-width: 32px;
  max-width: 50px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--background);
  color: var(--text-primary);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  touch-action: manipulation;
  user-select: none;
}

.key-btn:hover {
  background: var(--cell-highlighted);
  transform: translateY(-1px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.key-btn:active {
  transform: translateY(0);
  background: var(--cell-selected);
}

.key-space {
  flex: 2;
  max-width: 200px;
  font-size: 0.85rem;
}

.key-backspace {
  flex: 1.5;
  max-width: 80px;
  font-size: 1.25rem;
}

/* Footer */
.footer {
  background: var(--background);
  border-top: 2px solid var(--border);
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.1);
  width: fit-content;
  margin: 0 auto;
}

/* Header */
.header {
  background: var(--background);
  padding: 0.25rem 2rem 0.5rem 2rem;
  text-align: center;
}

.header-content h1 {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.author {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* Action Buttons */
.actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  padding: 0.5rem 1rem 0.75rem 1rem;
  background: var(--background);
}

.actions-row {
  display: flex;
  justify-content: center;
  gap: 0.4rem;
  width: auto;
}

.btn {
  padding: 0.625rem 1rem;
  border: none;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 44px;
  min-width: 44px;
  touch-action: manipulation;
  flex: 1;
}

.btn-primary {
  background: var(--primary-color);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
}

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

.btn-secondary {
  background: var(--secondary-color);
  color: white;
}

.btn-secondary:hover {
  background: var(--secondary-hover);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(100, 116, 139, 0.3);
}

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

.btn-info {
  background: var(--surface);
  color: var(--primary-color);
  border: 2px solid var(--primary-color);
  padding: 0.5rem;
  min-width: 44px;
  max-width: 50px;
  font-size: 1.2rem;
  font-weight: 700;
  flex: 0 0 50px;
}

.btn-info:hover {
  background: var(--primary-color);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
}

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

/* Main Content */
.main-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1 1 auto;
  padding: 0;
  padding-top: 70px; /* Account for fixed current-clue-display */
  padding-bottom: 0;
  gap: 0;
  overflow: auto;
}

@media (min-width: 769px) {
  .main-content {
    flex: 0 0 auto; /* Let content determine height so footer hugs grid */
  }
}

/* Grid Container */
.grid-container {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  width: 100%;
  padding: 0;
  margin-bottom: 0;
}

.grid {
  display: grid;
  gap: 1px;
  background: var(--border);
  border: 2px solid var(--border);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: fit-content;
  max-width: 100%;
  aspect-ratio: 1;
}

/* Grid Cells */
.cell {
  position: relative;
  background: var(--background);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.15s ease;
  user-select: none;
  min-width: 28px;
  min-height: 28px;
  width: 100%;
  padding-bottom: 100%;
}

.cell > * {
  position: absolute;
}

.cell.black {
  background: var(--cell-black);
  cursor: default;
}

.cell.selected {
  background: var(--cell-selected);
  outline: 3px solid var(--primary-color);
  outline-offset: -3px;
  z-index: 2;
}

.cell.highlighted {
  background: var(--cell-highlighted);
  z-index: 1;
}

.cell:not(.black):hover:not(.selected) {
  background: #e0f2fe;
}

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

.cell-letter {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  text-transform: uppercase;
}

.cell-incorrect-mark {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 3;
}

.cell-incorrect-mark::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 70%;
  height: 3px;
  background-color: var(--error);
  transform-origin: center;
  transform: translate(-50%, -50%) rotate(45deg);
}

/* Clues Container */
.clues-container {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}

.clues-section {
  background: var(--background);
  padding: 1.25rem;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.clues-section h3 {
  font-size: 1.25rem;
  margin-bottom: 1rem;
  color: var(--text-primary);
  font-weight: 600;
}

.current-clue-display {
  background: var(--background);
  padding: 0.75rem 1rem;
  border-bottom: 2px solid var(--border);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
  text-align: center;
  width: fit-content;
  max-width: 90%;
  position: fixed;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  min-height: 45px;
}

.current-clue-display h3 {
  font-size: 1.05rem;
  color: var(--primary-color);
  margin: 0;
  font-weight: 600;
  flex-shrink: 0;
}

.current-clue-display p {
  font-size: 1.05rem;
  line-height: 1.4;
  color: var(--text-primary);
  margin: 0;
  text-align: left;
}

.clue {
  padding: 0.75rem;
  margin-bottom: 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  line-height: 1.5;
  font-size: 0.95rem;
  min-height: 44px;
  display: flex;
  align-items: center;
}

.clue:hover {
  background: var(--cell-highlighted);
}

.clue.active {
  background: var(--cell-selected);
  border-left: 4px solid var(--primary-color);
  padding-left: calc(0.75rem - 4px);
}

.clue strong {
  color: var(--primary-color);
  margin-right: 0.5rem;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  justify-content: center;
  align-items: center;
  z-index: 1000;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: var(--background);
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 20px 25px rgba(0, 0, 0, 0.15);
  max-width: 400px;
  width: 90%;
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal-content p {
  font-size: 1rem;
  margin-bottom: 1.2rem;
  color: var(--text-primary);
  text-align: center;
}

.modal-buttons {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
}

.modal-buttons .btn {
  flex: 1;
}

/* Responsive Design */
@media (max-width: 768px) {
  .header {
    padding: 0.75rem 1rem;
  }

  .header-content h1 {
    font-size: 1.25rem;
  }
  
  .author {
    font-size: 0.85rem;
  }

  .footer {
    width: 100%;
  }

  .actions {
    padding: 0.25rem 0.5rem 0.5rem 0.5rem;
    gap: 0.25rem;
  }

  .actions-row {
    gap: 0.3rem;
    width: 100%;
  }

  .btn {
    padding: 0.2rem 0.2rem;
    font-size: 0.8rem;
    flex: 1;
    min-width: 0;
  }

  .btn-info {
    padding: 0.4rem;
    min-width: 40px;
    max-width: 45px;
    font-size: 1.1rem;
    flex: 0 0 45px;
  }

  .main-content {
    padding: 0;
    padding-top: 65px; /* Account for fixed current-clue-display on mobile */
    padding-bottom: 0;
    gap: 0;
  }
  
  .grid-container {
    padding: 0;
    margin-bottom: 0;
    width: 100%;
    max-width: 100%;
    justify-content: center;
  }

  .grid {
    width: 100%;
    max-width: 100vw;
    margin: 0;
  }

  .cell {
    min-width: 24px;
    min-height: 24px;
  }

  .cell-number {
    font-size: 0.55rem;
    top: 1px;
    left: 2px;
  }

  .cell-letter {
    font-size: 1rem;
    transform: translate(-50%, -50%);
  }

  .cell-incorrect-mark::before {
    height: 1px;
  }

  .clues-container {
    max-width: 100%;
  }
  
  .current-clue-display {
    padding: 0.65rem 0.75rem;
    min-height: 55px;
    width: 100%;
    max-width: 100%;
    left: 0;
    transform: none;
  }
  
  .current-clue-display h3 {
    font-size: 0.9rem;
    margin: 0;
  }
  
  .current-clue-display p {
    font-size: 0.9rem;
    line-height: 1.3;
    margin: 0;
    text-align: left;
  }

  .clues-section {
    padding: 1rem;
  }

  .clues-section h3 {
    font-size: 1.1rem;
  }

  .clue {
    padding: 0.625rem;
    font-size: 0.9rem;
  }

  .modal-content {
    padding: 1.5rem;
  }

  .on-screen-keyboard {
    display: flex; /* Show on mobile */
    padding: 0 0.5rem 0.5rem 0.5rem;
    gap: 0.4rem;
  }

  .keyboard-row {
    gap: 0.3rem;
  }

  .key-btn {
    min-width: 28px;
    max-width: 40px;
    height: 40px;
    font-size: 0.9rem;
  }

  .key-space {
    font-size: 0.75rem;
  }

  .key-backspace {
    font-size: 1.1rem;
  }
}

@media (max-width: 480px) {
  .header-content h1 {
    font-size: 1.25rem;
  }

  .btn {
    padding: 0.5rem 0.3rem;
    font-size: 0.75rem;
  }

  .btn-info {
    max-width: 40px;
    flex: 0 0 40px;
    font-size: 1rem;
  }

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

  .cell-letter {
    font-size: 0.875rem;
    transform: translate(-50%, -50%);
  }
}

/* Larger screens - optimize grid size */
@media (min-width: 1200px) {
  .grid {
    max-width: 600px;
  }

  .cell {
    min-width: 40px;
    min-height: 40px;
  }

  .cell-number {
    font-size: 0.7rem;
    top: 3px;
    left: 4px;
  }

  .cell-letter {
    font-size: 1.5rem;
    transform: translate(-50%, -50%);
  }
}

/* Hidden input for mobile keyboard */
#hidden-input {
  position: absolute;
  left: -9999px;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* Confetti Animation */
.confetti {
  position: fixed;
  width: 10px;
  height: 10px;
  top: -10px;
  z-index: 9999;
  pointer-events: none;
  animation: confetti-fall 3s linear forwards;
}

@keyframes confetti-fall {
  to {
    transform: translateY(100vh) rotate(360deg);
    opacity: 0;
  }
}

.celebration-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9998;
  pointer-events: none;
}

.celebration-message {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  background: white;
  padding: 3rem;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  z-index: 10000;
  text-align: center;
  animation: celebrate-popup 0.5s ease forwards;
}

@keyframes celebrate-popup {
  0% {
    transform: translate(-50%, -50%) scale(0);
    opacity: 0;
  }
  50% {
    transform: translate(-50%, -50%) scale(1.1);
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
}

.celebration-message h2 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  color: var(--primary-color);
}

.celebration-message p {
  font-size: 1.25rem;
  color: var(--text-secondary);
}

@media (max-width: 768px) {
  .celebration-message {
    padding: 2rem;
    max-width: 90%;
  }
  
  .celebration-message h2 {
    font-size: 2rem;
  }
  
  .celebration-message p {
    font-size: 1.1rem;
  }
}

/* Print Styles */
@media print {
  .actions,
  .modal {
    display: none !important;
  }

  .main-content {
    flex-direction: row;
  }

  .grid {
    border: 2px solid black;
  }

  .cell {
    border: 1px solid black;
  }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Focus visible for keyboard navigation */
.cell:focus-visible {
  outline: 3px solid var(--primary-color);
  outline-offset: -3px;
}

.btn:focus-visible {
  outline: 2px solid var(--primary-color);
  outline-offset: 2px;
}

.clue:focus-visible {
  outline: 2px solid var(--primary-color);
  outline-offset: -2px;
}

