/* Reset and Base Styles */
html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height for mobile */
  width: 100%;
  width: 100vw; /* Ensure full viewport width */
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  position: fixed;
  overscroll-behavior: none;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

* {
  box-sizing: border-box;
}

:root {
  --bg: #f8f9fa;
  --bg-gradient: linear-gradient(180deg, #ffffff 0%, #f8f9fa 100%);
  --text: #1a1a1a;
  --tile-empty: #ffffff;
  --tile-border: #d3d6da;
  --tile-border-filled: #565758;
  --correct: #6aaa64;
  --present: #c9b458;
  --absent: #787c7e;
  --key-bg: linear-gradient(180deg, #e9ecef 0%, #d3d6da 100%);
  --key-text: #1a1a1a;
  --key-hover: #c5c8cb;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
  --header-border: #e9ecef;
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
}

[data-theme="dark"] {
  --bg: #0f0f10;
  --bg-gradient: linear-gradient(180deg, #1a1a1b 0%, #0f0f10 100%);
  --text: #ffffff;
  --tile-empty: #1a1a1b;
  --tile-border: #3a3a3c;
  --tile-border-filled: #565758;
  --correct: #538d4e;
  --present: #b59f3b;
  --absent: #3a3a3c;
  --key-bg: linear-gradient(180deg, #565758 0%, #3a3a3c 100%);
  --key-text: #ffffff;
  --key-hover: #2a2a2c;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  --header-border: #2a2a2c;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Helvetica Neue', sans-serif;
  background: var(--bg-gradient);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  height: 100vh;
  height: 100dvh;
  padding: 0;
  transition: background 0.3s ease, color 0.2s ease;
}

/* Header Styles */
header {
  width: 100%;
  max-width: 540px;
  padding: 0.75rem 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--header-border);
  flex-shrink: 0;
}

header h2 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  background: linear-gradient(90deg, #ff0080 0%, #ff8c00 20%, #ffd700 40%, #00ff00 60%, #0080ff 80%, #8000ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

[data-theme="dark"] header h2 {
  background: linear-gradient(90deg, #ff0080 0%, #ff8c00 20%, #ffd700 40%, #00ff00 60%, #0080ff 80%, #8000ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

#stats-btn {
  font-size: 1.25rem;
  padding: 0.4rem;
}

#timer {
  font-size: 0.95rem;
  font-weight: 600;
  min-width: 3rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
  opacity: 0.9;
}

.toggle-btn {
  background-color: transparent;
  border: none;
  cursor: pointer;
  font-size: 1.25rem;
  color: var(--text);
  padding: 0.4rem;
  border-radius: var(--radius-md);
  transition: all 0.15s ease;
  opacity: 0.8;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-btn svg {
  display: block;
}

.toggle-btn:hover {
  background-color: var(--tile-border);
  opacity: 1;
  transform: scale(1.05);
}

.toggle-btn:active {
  transform: scale(0.95);
}

/* Game Container */
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 0.3rem;
  padding: 0.5rem;
  margin: auto 0;
  touch-action: none;
  flex-shrink: 1;
  min-height: 0;
}

.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.3rem;
}

.tile {
  width: min(14vw, 80px);
  height: min(14vw, 80px);
  perspective: 1000px;
  position: relative;
}

.face {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: clamp(1.5rem, 5vw, 2rem);
  font-weight: 700;
  text-transform: uppercase;
  border: 2px solid var(--tile-border);
  background-color: var(--tile-empty);
  color: var(--text);
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1),
              background-color 0.6s ease,
              border-color 0.6s ease;
  border-radius: var(--radius-sm);
  backface-visibility: hidden;
  box-shadow: var(--shadow-md);
}

.face:not(:empty) {
  border-color: var(--tile-border-filled);
  animation: pop 0.1s ease-in-out;
}

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

.front {
  transform: rotateX(0deg);
}

.back {
  transform: rotateX(180deg);
}

.tile.flip .front {
  transform: rotateX(180deg);
}

.tile.flip .back {
  transform: rotateX(0deg);
}

.back[data-result="correct"] {
  background-color: var(--correct) !important;
  border-color: var(--correct) !important;
  color: white !important;
}

.back[data-result="present"] {
  background-color: var(--present) !important;
  border-color: var(--present) !important;
  color: white !important;
}

.back[data-result="absent"] {
  background-color: var(--absent) !important;
  border-color: var(--absent) !important;
  color: white !important;
}

/* Message Area */
.message-area {
  min-height: 1.5rem;
  font-size: 0.875rem;
  font-weight: 600;
  text-align: center;
  padding: 0.25rem 0.5rem;
  color: var(--text);
  flex-shrink: 0;
}

/* Keyboard Styles */
.keyboard {
  width: 100%;
  max-width: 100%;
  padding: 0.5rem clamp(0.5rem, 2vw, 1.5rem);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
  flex-shrink: 0;
}

.key-row {
  display: flex;
  justify-content: center;
  gap: clamp(0.2rem, 0.5vw, 0.4rem);
  width: 100%;
  max-width: 100%;
}

.key {
  flex: 1;
  min-width: 22px;
  max-width: 46px;
  height: 74px;
  background: var(--key-bg);
  color: var(--key-text);
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: clamp(0.7rem, 1.8vw, 0.9rem);
  text-transform: uppercase;
  cursor: pointer;
  user-select: none;
  transition: all 0.12s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  touch-action: manipulation;
  box-shadow: var(--shadow-sm);
  position: relative;
}

.key:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.key:active {
  transform: translateY(0);
  box-shadow: var(--shadow-sm);
}

/* Make Enter key wider to fit text */
.key-row:nth-child(3) .key:first-child {
  flex: 1.5;
  max-width: 64px;
  font-size: clamp(0.65rem, 1.5vw, 0.8rem);
}

/* Make Delete key slightly wider */
.key-row:nth-child(3) .key:last-child {
  flex: 1.2;
  max-width: 52px;
  font-size: clamp(0.9rem, 2vw, 1.1rem);
}

.key.correct {
  background: var(--correct) !important;
  color: white !important;
  border: 1px solid var(--correct);
}

.key.present {
  background: var(--present) !important;
  color: white !important;
  border: 1px solid var(--present);
}

.key.absent {
  background: var(--absent) !important;
  color: #ffffff !important;
  border: 1px solid var(--absent);
  opacity: 0.7;
}

/* Modal Styles */
.modal,
.post-game-modal {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.2s ease;
  justify-content: center;
  align-items: center;
}

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

.modal-content,
.post-game-content {
  background-color: var(--bg);
  color: var(--text);
  margin: 0;
  padding: 2rem;
  border-radius: 16px;
  width: 90%;
  max-width: 400px;
  text-align: center;
  border: 1px solid var(--tile-border);
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: slideUp 0.3s ease;
}

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

.close {
  color: var(--text);
  float: right;
  font-size: 1.75rem;
  font-weight: 600;
  cursor: pointer;
  line-height: 1;
  opacity: 0.6;
  transition: opacity 0.2s ease;
}

.close:hover {
  opacity: 1;
}

.stats-grid,
.performance-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.25rem;
  margin: 1.5rem 0;
}

.stat-item,
.perf-item {
  padding: 1rem;
  background-color: var(--tile-empty);
  border-radius: 12px;
  border: 1px solid var(--tile-border);
  box-shadow: 0 2px 8px var(--shadow);
}

.stat-number,
.perf-number {
  font-size: 2rem;
  font-weight: 700;
  color: var(--correct);
  margin-bottom: 0.25rem;
}

.stat-label,
.perf-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.8;
  font-weight: 600;
}

.avg-time-section {
  margin-top: 1.5rem;
  padding: 1rem;
  background-color: var(--tile-empty);
  border-radius: 12px;
  border: 1px solid var(--tile-border);
  box-shadow: 0 2px 8px var(--shadow);
}

.game-result-title {
  font-size: 1.75rem;
  font-weight: 700;
  margin-bottom: 1.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.game-result-title svg {
  flex-shrink: 0;
}

.win-title {
  color: var(--correct);
}

.win-title svg {
  stroke: var(--correct);
  animation: sparkle 1.5s ease-in-out infinite;
}

@keyframes sparkle {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(1.1); }
}

.lose-title {
  color: var(--text);
}

.achievement-section {
  margin: 1.5rem 0;
}

.achievement-badge {
  background: linear-gradient(135deg, #ffd700, #ffed4e);
  color: #000;
  padding: 0.875rem 1.5rem;
  border-radius: 30px;
  font-size: 0.95rem;
  font-weight: 700;
  margin: 0.5rem 0;
  display: inline-block;
  box-shadow: 0 6px 20px rgba(255, 215, 0, 0.4);
  animation: bounceIn 0.5s ease;
}

@keyframes bounceIn {
  0% {
    transform: scale(0.3);
    opacity: 0;
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.play-again-btn {
  background-color: var(--correct);
  color: white;
  border: none;
  padding: 1rem 2rem;
  border-radius: 12px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 700;
  margin-top: 1.5rem;
  transition: all 0.2s ease;
  width: 100%;
  box-shadow: 0 4px 12px rgba(106, 170, 100, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
}

.play-again-btn svg {
  flex-shrink: 0;
}

.play-again-btn:hover {
  background-color: var(--present);
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(106, 170, 100, 0.4);
}

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

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

  header h2 {
    font-size: 1.25rem;
  }

  .header-controls {
    gap: 0.5rem;
  }

  #timer {
    font-size: 0.875rem;
    min-width: 2.75rem;
  }

  .toggle-btn {
    font-size: 1.1rem;
    padding: 0.3rem;
  }

  .board {
    gap: 0.25rem;
    padding: 0.4rem;
  }

  .row {
    gap: 0.25rem;
  }

  .tile {
    width: min(15vw, 68px);
    height: min(15vw, 68px);
  }

  .keyboard {
    gap: 0.3rem;
    padding: 0.4rem 0.5rem;
  }

  .key-row {
    gap: clamp(0.15rem, 0.4vw, 0.3rem);
  }

  .key {
    height: 70px;
    min-width: 19px;
    max-width: 38px;
    font-size: clamp(0.65rem, 1.6vw, 0.8rem);
  }

  .key-row:nth-child(3) .key:first-child {
    flex: 1.5;
    max-width: 54px;
    font-size: clamp(0.6rem, 1.4vw, 0.75rem);
  }

  .key-row:nth-child(3) .key:last-child {
    flex: 1.2;
    max-width: 44px;
  }

  .message-area {
    font-size: 0.8rem;
    min-height: 1.25rem;
  }

  .modal-content,
  .post-game-content {
    padding: 1.5rem;
    margin: 0;
  }

  .stats-grid,
  .performance-grid {
    gap: 0.875rem;
  }

  .stat-number,
  .perf-number {
    font-size: 1.5rem;
  }
}

@media (min-width: 481px) and (max-width: 768px) {
  .tile {
    width: min(12vw, 70px);
    height: min(12vw, 70px);
  }

  .key {
    height: 72px;
    min-width: 21px;
    max-width: 42px;
  }

  .key-row:nth-child(3) .key:first-child {
    flex: 1.5;
    max-width: 59px;
  }

  .key-row:nth-child(3) .key:last-child {
    flex: 1.2;
    max-width: 48px;
  }
}

@media (min-height: 800px) {
  .board {
    padding: 0.75rem;
  }

  .tile {
    width: min(11vw, 78px);
    height: min(11vw, 78px);
  }

  .key {
    height: 80px;
    min-width: 24px;
    max-width: 50px;
  }

  .key-row:nth-child(3) .key:first-child {
    flex: 1.5;
    max-width: 69px;
  }

  .key-row:nth-child(3) .key:last-child {
    flex: 1.2;
    max-width: 56px;
  }
}

/* Additional Enhancements */
[data-theme="light"] body {
  background: var(--bg-gradient);
}

[data-theme="dark"] body {
  background: var(--bg-gradient);
}

/* Scrollbar Styling */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--tile-border);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--tile-border-filled);
}
