/* Base Styles and Variables */
:root {
  --primary: #4361ee;
  --primary-hover: #3a56d4;
  --success: #20c997;
  --danger: #dc3545;
  --warning: #fd7e14;
  --light-bg: #f8f9fa;
  --dark-text: #212529;
  --gray-text: #6c757d;
  --card-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  --transition-speed: 0.3s;
  --border-radius: 12px;
  --square-active: #4361ee;
  --square-correct: #20c997;
  --square-incorrect: #dc3545;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  color: var(--dark-text);
  background: #f0f2f5;
  line-height: 1.6;
  height: 100vh;
  width: 100vw;
  overflow-x: hidden;
}

/* Full-height container */
.main-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 100vw;
  padding: 0;
}

/* Typography */
.title {
  color: var(--primary);
  font-size: 1.8rem;
  font-weight: 700;
  margin-bottom: 0;
}

.subtitle {
  color: var(--gray-text);
  font-size: 0.9rem;
  margin-bottom: 0;
}

/* Main Layout */
.app-layout {
  display: grid;
  grid-template-columns: 280px 1fr 280px;
  gap: 15px;
  flex: 1;
  padding: 0 15px 15px;
  width: 100%;
  max-width: 1800px;
  margin: 0 auto;
}

/* Panel Layouts */
.panel {
  background-color: white;
  border-radius: var(--border-radius);
  box-shadow: var(--card-shadow);
  padding: 15px;
  transition: all var(--transition-speed) ease;
  display: flex;
  flex-direction: column;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.panel-title {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--primary);
  margin: 0;
}

/* Settings Panel */
.settings-panel {
  height: 100%;
}

/* Stats Panel */
.stats-panel {
  height: 100%;
}

/* Game Grid (Center) */
.game-area {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.game-panel {
  width: 100%;
  height: auto;
  display: flex;
  flex-direction: column;
}

/* Instructions */
.instructions {
  background-color: var(--light-bg);
  border-radius: 8px;
  padding: 10px 15px;
  margin-bottom: 15px;
  transform-origin: top;
  transition: transform var(--transition-speed) ease, 
              opacity var(--transition-speed) ease,
              max-height var(--transition-speed) ease;
  max-height: 1000px;
  opacity: 1;
}

.instructions.hidden {
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  padding-top: 0;
  padding-bottom: 0;
  margin-bottom: 0;
  transform: scaleY(0.8);
}

.instructions h3 {
  font-size: 1rem;
  margin-bottom: 8px;
}

.instructions ul {
  padding-left: 15px;
  margin-bottom: 0;
}

.instructions li {
  margin-bottom: 4px;
  font-size: 0.85rem;
}

/* Controls */
.controls {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 20px;
}

.control-group {
  display: flex;
  flex-direction: column;
}

.control-group label {
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--gray-text);
  font-size: 0.9rem;
}

.slider-container {
  display: flex;
  align-items: center;
  gap: 10px;
}

.slider-container input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  height: 6px;
  border-radius: 4px;
  background: #e9ecef;
  outline: none;
}

.slider-container input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  transition: background var(--transition-speed) ease, transform var(--transition-speed) ease;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.slider-container input[type="range"]::-webkit-slider-thumb:hover {
  background: var(--primary-hover);
  transform: scale(1.1);
}

.slider-value {
  min-width: 50px;
  text-align: center;
  font-weight: 600;
  color: var(--primary);
  padding: 2px 8px;
  background-color: rgba(67, 97, 238, 0.1);
  border-radius: 20px;
  font-size: 0.85rem;
}

/* Action Bar */
.action-bar {
  margin-top: auto;
  display: flex;
  justify-content: center;
  padding-top: 15px;
}

/* Buttons */
.btn-primary, .btn-danger {
  color: white;
  border: none;
  padding: 8px 20px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all var(--transition-speed) ease;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

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

.btn-primary:hover {
  background-color: var(--primary-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(67, 97, 238, 0.25);
}

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

.btn-danger:hover {
  background-color: #c82333;
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(220, 53, 69, 0.25);
}

.btn-icon {
  background: none;
  border: none;
  font-size: 1.2rem;
  color: var(--primary);
  cursor: pointer;
  transition: transform var(--transition-speed) ease;
}

.btn-icon:hover {
  transform: scale(1.1);
}

/* Game Container */
.game-header {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  margin-bottom: 10px;
  min-height: 30px;
}

.instruction {
  font-size: 0.9rem;
  color: var(--gray-text);
  text-align: center;
}

#n-display {
  color: var(--primary);
  font-weight: 700;
}

.round-badge {
  position: absolute;
  right: 0;
  top: 0;
  font-size: 0.8rem;
  color: var(--gray-text);
  background: var(--light-bg);
  padding: 3px 10px;
  border-radius: 20px;
  font-weight: 500;
}

/* Feedback Area */
.feedback-container {
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  pointer-events: none; /* Prevents feedback from interfering with clicks */
}

.feedback {
  font-weight: 600;
  font-size: 0.9rem;
  padding: 6px 14px;
  border-radius: 20px;
  opacity: 0;
  transform: translateY(-10px);
  transition: opacity 0.15s ease-in, transform 0.15s ease-out;
  min-width: 90px; /* Ensure consistent width */
  text-align: center; /* Center the text */
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.feedback.visible {
  opacity: 1;
  transform: translateY(0);
}

.feedback.correct {
  background-color: rgba(32, 201, 151, 0.9);
  color: white;
}

.feedback.incorrect {
  background-color: rgba(220, 53, 69, 0.9);
  color: white;
}

.feedback.missed {
  background-color: rgba(253, 126, 20, 0.9);
  color: white;
}

.feedback.game-over {
  background-color: rgba(52, 58, 64, 0.9);
  color: white;
  font-size: 1rem;
  min-width: 200px;
}

/* Game Over Styles */
.square.game-over {
  background-color: rgba(52, 58, 64, 0.5);
  box-shadow: none;
  animation: fadeSquare 0.5s ease-in-out forwards;
}

@keyframes fadeSquare {
  from { background-color: rgba(52, 58, 64, 0.5); }
  to { background-color: rgba(52, 58, 64, 0.2); }
}

.game-over-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 8px;
  animation: fadeIn 0.3s ease-in-out forwards;
  z-index: 10;
}

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

.game-over-stats {
  background-color: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
  text-align: center;
  max-width: 80%;
}

.game-over-stats h3 {
  color: var(--danger);
  margin-bottom: 10px;
  font-size: 1.3rem;
}

.game-over-stats p {
  color: var(--dark-text);
  margin-bottom: 15px;
  font-weight: 500;
}

.final-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 15px;
}

.final-stats div {
  padding: 5px;
  background-color: var(--light-bg);
  border-radius: 5px;
  font-size: 0.85rem;
  font-weight: 600;
}

.restart-btn {
  background-color: var(--primary);
  color: white;
  border: none;
  padding: 8px 20px;
  border-radius: 20px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.restart-btn:hover {
  background-color: var(--primary-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(67, 97, 238, 0.25);
}

.feedback.correct {
  background-color: rgba(32, 201, 151, 0.15);
  color: var(--success);
}

.feedback.incorrect {
  background-color: rgba(220, 53, 69, 0.15);
  color: var(--danger);
}

.feedback.missed {
  background-color: rgba(253, 126, 20, 0.15);
  color: var(--warning);
}

/* Grid Container with responsive sizing */
.grid-container {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: var(--light-bg);
  border-radius: 8px;
  padding: 10px;
}

.grid {
  display: grid;
  gap: 5px;
  width: 100%;
  height: 100%;
}

/* Squares */
.square {
  position: relative;
  aspect-ratio: 1;
  background-color: white;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.2s ease, box-shadow 0.2s ease, border 0.2s ease;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: center;
  align-items: center;
}

.square:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
  background-color: rgba(67, 97, 238, 0.05);
}

.square.active {
  background-color: var(--square-active);
  box-shadow: 0 0 12px rgba(67, 97, 238, 0.4);
}

/* Glow effect for correct/incorrect without changing the background */
.square.correct-click {
  background-color: transparent; /* Transparent background */
  box-shadow: 0 0 8px 2px rgba(32, 201, 151, 0.7),
              0 0 20px 5px rgba(32, 201, 151, 0.4); /* Outer glow only */
  border: 2px solid rgba(32, 201, 151, 0.9); /* Brighter border */
}

.square.incorrect-click {
  background-color: transparent; /* Transparent background */
  box-shadow: 0 0 8px 2px rgba(220, 53, 69, 0.7),
              0 0 20px 5px rgba(220, 53, 69, 0.4); /* Outer glow only */
  border: 2px solid rgba(220, 53, 69, 0.9); /* Brighter border */
}

/* Combined styles for active + feedback */
.square.active.correct-click {
  background-color: var(--square-active); /* Keep the blue background */
  box-shadow: 0 0 12px rgba(67, 97, 238, 0.4), /* Blue glow */
              0 0 8px 2px rgba(32, 201, 151, 0.7), /* Green glow */
              0 0 20px 5px rgba(32, 201, 151, 0.4);
  border: 2px solid rgba(32, 201, 151, 0.9);
}

.square.active.incorrect-click {
  background-color: var(--square-active); /* Keep the blue background */
  box-shadow: 0 0 12px rgba(67, 97, 238, 0.4), /* Blue glow */
              0 0 8px 2px rgba(220, 53, 69, 0.7), /* Red glow */
              0 0 20px 5px rgba(220, 53, 69, 0.4);
  border: 2px solid rgba(220, 53, 69, 0.9);
}

/* Missed square indicator */
.square.missed-square {
  background-color: transparent;
  box-shadow: 0 0 8px 2px rgba(253, 126, 20, 0.7),
              0 0 20px 5px rgba(253, 126, 20, 0.4);
  border: 2px solid rgba(253, 126, 20, 0.9);
  animation: pulseMissed 0.5s ease-in-out infinite alternate;
}

@keyframes pulseMissed {
  from { box-shadow: 0 0 8px 2px rgba(253, 126, 20, 0.7),
                    0 0 20px 5px rgba(253, 126, 20, 0.4); }
  to { box-shadow: 0 0 12px 3px rgba(253, 126, 20, 0.8),
                  0 0 25px 7px rgba(253, 126, 20, 0.5); }
}

/* Score Cards */
.score-container {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  gap: 10px;
  margin-bottom: 15px;
}

.score-card {
  padding: 10px;
  border-radius: 10px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.score-card.correct {
  background-color: rgba(32, 201, 151, 0.1);
}

.score-card.incorrect {
  background-color: rgba(220, 53, 69, 0.1);
}

.score-card.missed {
  background-color: rgba(253, 126, 20, 0.1);
}

.score-label {
  font-size: 0.8rem;
  font-weight: 500;
  margin-bottom: 3px;
}

.score-value {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
}

.score-card.correct .score-label, 
.score-card.correct .score-value {
  color: var(--success);
}

.score-card.incorrect .score-label, 
.score-card.incorrect .score-value {
  color: var(--danger);
}

.score-card.missed .score-label, 
.score-card.missed .score-value {
  color: var(--warning);
}

/* Stats Display */
.stats-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-item {
  background-color: var(--light-bg);
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 0.8rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  position: relative;
}

.tooltip-container {
  cursor: help;
}

.tooltip-text {
  visibility: hidden;
  width: 200px;
  background-color: rgba(0, 0, 0, 0.8);
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 6px 10px;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -100px;
  opacity: 0;
  transition: opacity 0.3s;
  font-size: 0.75rem;
  pointer-events: none;
}

.tooltip-container:hover .tooltip-text {
  visibility: visible;
  opacity: 1;
}

.tooltip-text::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: rgba(0, 0, 0, 0.8) transparent transparent transparent;
}

.stat-value {
  font-weight: 600;
  color: var(--primary);
}

/* Utility Classes */
.hidden {
  display: none;
}

/* ===== RESPONSIVE DESIGN ===== */
/* Organized media queries from largest to smallest screens */

/* Ultra-widescreen layout (21:9 and larger) */
@media (min-width: 2000px) or (min-aspect-ratio: 21/9) {
  .app-layout {
    grid-template-columns: 350px 1fr 350px;
    max-width: 2200px;
  }
  
  .grid-container {
    max-width: 85vh;
    max-height: 85vh;
  }
  
  .title {
    font-size: 2.2rem;
  }
  
  .subtitle {
    font-size: 1.1rem;
  }
  
  .panel-title {
    font-size: 1.4rem;
  }
  
  .score-value {
    font-size: 1.8rem;
  }
}

/* Widescreen layout (16:9) */
@media (min-width: 1600px) and (max-width: 1999px), 
       (min-aspect-ratio: 16/9) and (max-aspect-ratio: 21/9) {
  .app-layout {
    grid-template-columns: 320px 1fr 320px;
    gap: 20px;
  }
  
  .grid-container {
    max-width: 75vh;
    max-height: 75vh;
  }
}

/* Standard desktop layout */
@media (min-width: 1200px) and (max-width: 1599px) {
  .app-layout {
    grid-template-columns: 280px 1fr 280px;
  }
  
  .grid-container {
    max-width: 70vh;
    max-height: 70vh;
  }
}

/* Smaller desktop and large tablet */
@media (min-width: 992px) and (max-width: 1199px) {
  .app-layout {
    grid-template-columns: 250px 1fr 250px;
    gap: 12px;
  }
  
  .panel {
    padding: 12px;
  }
  
  .grid-container {
    max-width: 65vh;
    max-height: 65vh;
  }
}

/* Tablet layout */
@media (min-width: 768px) and (max-width: 991px) {
  .app-layout {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr auto;
  }
  
  .game-area {
    grid-column: 1 / 3;
    grid-row: 1 / 2;
    margin-bottom: 15px;
  }
  
  .settings-panel {
    grid-column: 1 / 2;
    grid-row: 2 / 3;
    max-height: 40vh;
    overflow-y: auto;
  }
  
  .stats-panel {
    grid-column: 2 / 3;
    grid-row: 2 / 3;
    max-height: 40vh;
    overflow-y: auto;
  }
  
  .grid-container {
    max-width: 60vh;
    max-height: 60vh;
  }
  
  .score-container {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* Mobile landscape layout */
@media (max-width: 767px) and (orientation: landscape) {
  .app-layout {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto;
    gap: 10px;
  }
  
  .main-container {
    height: auto;
    overflow-y: auto;
  }
  
  .game-area {
    grid-column: 1 / 3;
    grid-row: 1 / 2;
    margin-bottom: 10px;
  }
  
  .settings-panel {
    grid-column: 1 / 2;
    grid-row: 2 / 3;
    height: auto;
  }
  
  .stats-panel {
    grid-column: 2 / 3;
    grid-row: 2 / 3;
    height: auto;
  }
  
  .grid-container {
    max-height: 60vh;
  }
  
  .score-container {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
  
  .score-value {
    font-size: 1.3rem;
  }
  
  body {
    height: auto;
    overflow-y: auto;
  }
  
  .title {
    font-size: 1.5rem;
  }
  
  header {
    padding: 8px 0 !important;
  }
  
  .panel-title {
    font-size: 1.1rem;
  }
  
  .controls {
    gap: 8px;
  }
}

/* Mobile portrait layout */
@media (max-width: 767px) and (orientation: portrait) {
  .app-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    gap: 10px;
  }
  
  .main-container {
    height: auto;
    overflow-y: auto;
  }
  
  body {
    height: auto;
    overflow-y: auto;
  }
  
  .game-area {
    grid-column: 1 / 2;
    grid-row: 1 / 2;
    margin-bottom: 10px;
  }
  
  .settings-panel {
    grid-column: 1 / 2;
    grid-row: 2 / 3;
    height: auto;
  }
  
  .stats-panel {
    grid-column: 1 / 2;
    grid-row: 3 / 4;
    height: auto;
  }
  
  .panel {
    padding: 12px;
  }
  
  .panel-header {
    margin-bottom: 10px;
  }
  
  .controls {
    gap: 10px;
    margin-bottom: 10px;
  }
  
  .score-container {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
  
  .stats-container {
    flex-direction: row;
    flex-wrap: wrap;
  }
  
  .stat-item {
    flex: 1;
    min-width: 120px;
  }
  
  .action-bar {
    padding-top: 10px;
  }
  
  .title {
    font-size: 1.5rem;
  }
  
  .subtitle {
    font-size: 0.8rem;
  }
  
  header {
    padding: 8px 0 !important;
  }
}

/* Small phones (< 480px) */
@media (max-width: 479px) {
  .app-layout {
    padding: 0 10px 10px;
  }
  
  .panel {
    padding: 10px;
    border-radius: 10px;
  }
  
  .panel-header {
    margin-bottom: 8px;
  }
  
  .controls {
    gap: 8px;
  }
  
  .control-group label {
    margin-bottom: 4px;
    font-size: 0.8rem;
  }
  
  .slider-value {
    min-width: 45px;
    font-size: 0.75rem;
  }
  
  .feedback {
    font-size: 0.8rem;
    padding: 3px 10px;
  }
  
  .score-value {
    font-size: 1.2rem;
  }
  
  .score-label {
    font-size: 0.7rem;
  }
  
  .round-badge {
    font-size: 0.7rem;
    padding: 2px 8px;
  }
  
  .instruction {
    font-size: 0.8rem;
  }
  
  .stat-item {
    padding: 4px 8px;
    font-size: 0.7rem;
  }
  
  .tooltip-text {
    width: 150px;
    margin-left: -75px;
  }
  
  .btn-primary, .btn-danger {
    padding: 6px 15px;
    font-size: 0.8rem;
  }
  
  .title {
    font-size: 1.3rem;
  }
  
  .subtitle {
    font-size: 0.7rem;
  }
}

/* Height-specific adjustments */
@media (max-height: 700px) {
  .title {
    font-size: 1.3rem;
    margin-bottom: 0;
  }
  
  .subtitle {
    font-size: 0.7rem;
    margin-bottom: 0;
  }
  
  header {
    padding: 5px 0 !important;
  }
  
  .panel-title {
    font-size: 1rem;
  }
  
  .controls {
    gap: 8px;
    margin-bottom: 8px;
  }
  
  .action-bar {
    padding-top: 8px;
  }
  
  .panel {
    padding: 10px;
  }
}

/* Tall screens */
@media (min-height: 900px) {
  .grid-container {
    max-height: 75vh;
    max-width: 75vh;
  }
}

/* Ensure square grid cells for very small devices */
@media (max-width: 320px) {
  .grid {
    gap: 3px;
  }
  
  .square {
    border-radius: 5px;
  }
  
  .panel {
    padding: 8px;
  }
}

/* Fix for devices with notches */
@supports (padding-top: env(safe-area-inset-top)) {
  .main-container {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
}
