:root {
  /* Backgrounds */
  --color-bg: #FFFFFF;
  --color-bg-modal: rgba(0, 0, 0, 0.5);
  
  /* Text */
  --color-text-primary: #1A1A1A;
  --color-text-secondary: #565758;
  --color-text-light: #878A8C;
  
  /* Feedback Colors */
  --color-correct: #6AAA64;    /* Green */
  --color-present: #C9B458;    /* Yellow */
  --color-absent: #787C7E;     /* Gray */
  
  /* Tiles */
  --color-tile-empty-border: #D3D6DA;
  --color-tile-empty-bg: #FFFFFF;
  --color-tile-filled-border: #878A8C;
  
  /* Keyboard */
  --color-key-bg-default: #D3D6DA;
  --color-key-text: #1A1A1A;
  --color-key-border: transparent;
  
  /* Other */
  --color-divider: #D3D6DA;
  --color-shadow: rgba(0, 0, 0, 0.1);

  /* Animation constants */
  --tile-flip-duration: 0.6s;
  --retro-reveal-duration: 0.8s;
}

[data-theme='dark'] {
  /* Backgrounds */
  --color-bg: #121213;
  --color-bg-modal: rgba(0, 0, 0, 0.75);
  
  /* Text */
  --color-text-primary: #FFFFFF;
  --color-text-secondary: #C1C2C1;
  --color-text-light: #9B9B9B;
  
  /* Feedback Colors */
  --color-correct: #538D4E;
  --color-present: #B59F3B;
  --color-absent: #3A3A3C;
  
  /* Tiles */
  --color-tile-empty-border: #3A3A3C;
  --color-tile-empty-bg: #121213;
  --color-tile-filled-border: #565758;
  
  /* Keyboard */
  --color-key-bg-default: #818384;
  --color-key-text: #FFFFFF;
  
  /* Other */
  --color-divider: #3A3A3C;
  --color-shadow: rgba(0, 0, 0, 0.3);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
  background-color: var(--color-bg);
  color: var(--color-text-primary);
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: background-color 0.3s ease, color 0.3s ease;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  height: 65px;
  border-bottom: 1px solid var(--color-divider);
  flex-shrink: 0;
}

header h1 {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-align: center;
  flex-grow: 1;
  margin: 0;
  line-height: 1;
  white-space: nowrap;
  overflow: visible;
}

@media (max-width: 400px) {
  header h1 {
    font-size: 16px;
    letter-spacing: 0.05em;
  }
}

.header-left, .header-right {
  display: flex;
  width: 80px;
  gap: 4px;
  align-items: center;
}

.header-right {
  justify-content: flex-end;
}

header button, header .header-link {
  background: none;
  border: none;
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  border-radius: 4px;
  text-decoration: none;
  font-size: 1.2rem;
}

header button:hover, header .header-link:hover {
    background: var(--color-key-bg-default);
    color: var(--color-text-primary);
    transform: scale(1.1);
}

#help-btn {
    font-weight: bold;
    font-size: 1.1rem;
    border: 2px solid var(--color-border) !important;
    border-radius: 50% !important;
    width: 28px;
    height: 28px;
    padding: 0 !important;
}

#game-container {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 3vh, 30px);
  padding: 10px;
  max-width: 650px;
  margin: 0 auto;
  width: 100%;
  height: calc(100% - 65px);
}

/* Grid Styles */
.grid {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  grid-gap: 5px;
  padding: 5px;
  width: clamp(260px, 95vw, 380px);
  height: auto;
  aspect-ratio: 5 / 6;
  flex-grow: 0;
  max-height: 55vh;
}

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

.tile {
  width: 100%;
  height: 100%;
  border: 2px solid var(--color-tile-empty-border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 35px;
  font-weight: 700;
  text-transform: uppercase;
  user-select: none;
  transition: transform 0.1s ease;
}

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

.tile.correct {
  background-color: var(--color-correct);
  border-color: var(--color-correct);
  color: #FFFFFF;
}

.tile.present {
  background-color: var(--color-present);
  border-color: var(--color-present);
  color: #FFFFFF;
}

.tile.absent {
  background-color: var(--color-absent);
  border-color: var(--color-absent);
  color: #FFFFFF;
}

/* Keyboard Styles */
.keyboard {
  width: 100%;
  max-width: 600px;
  padding: 0 8px;
  flex-shrink: 0;
  margin-bottom: clamp(5px, 2vh, 20px);
}

.keyboard-row {
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
  touch-action: manipulation;
}

.key {
  font-family: inherit;
  font-weight: 700;
  border: 0;
  padding: 0;
  margin: 0 3px;
  height: clamp(45px, 8vh, 62px);
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  background-color: var(--color-key-bg-default);
  color: var(--color-key-text);
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  text-transform: uppercase;
  -webkit-tap-highlight-color: rgba(0,0,0,0.3);
  transition: all 0.1s ease;
}

.key.big {
  flex: 1.5;
  font-size: 12px;
}

.key.correct {
  background-color: var(--color-correct);
  color: #FFFFFF;
}

.key.present {
  background-color: var(--color-present);
  color: #FFFFFF;
}

.key.absent {
  background-color: var(--color-absent);
  color: #FFFFFF;
}

.spacer.half {
  flex: 0.5;
}

/* Animations */
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

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

.tile-row.shake {
  animation: shake 0.5s ease-in-out;
}

@keyframes tileFlip {
  0% { transform: rotateX(0deg); }
  50% { transform: rotateX(90deg); }
  100% { transform: rotateX(0deg); }
}

.tile.flip {
  animation: tileFlip var(--tile-flip-duration) cubic-bezier(0.45, 0.05, 0.55, 0.95) forwards;
}

@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-30px); }
  50% { transform: translateY(5px); }
  60% { transform: translateY(-15px); }
  80% { transform: translateY(2px); }
  100% { transform: translateY(0); }
}

.tile.bounce {
  animation: bounce 1s ease;
}

@keyframes retroReveal {
  0% { transform: rotateY(0deg) scale(1); filter: brightness(1); }
  50% { transform: rotateY(90deg) scale(1.1); filter: brightness(1.5); }
  100% { transform: rotateY(0deg) scale(1); filter: brightness(1); }
}

.tile.retro-reveal {
  animation: retroReveal var(--retro-reveal-duration) cubic-bezier(0.45, 0.05, 0.55, 0.95) forwards;
}

/* Modals */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: var(--color-bg-modal);
  display: flex;
  align-items: center;
  padding: 16px;
  justify-content: center;
  z-index: 1000;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.modal-overlay.hidden {
  display: none !important;
}

.hidden {
  display: none !important;
}

.modal-content {
  background: var(--color-bg);
  padding: 16px 20px;
  border-radius: 12px;
  width: 100%;
  max-width: 440px;
  position: relative;
  box-shadow: 0 4px 24px var(--color-bg-modal);
  max-height: 90vh;
  overflow-y: auto;
  text-align: center;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 4px;
}

.close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  font-size: 24px;
  color: var(--color-text-secondary);
  cursor: pointer;
}

.modal-title {
  text-align: center;
  margin-bottom: 16px;
  font-size: 0.85rem;
  letter-spacing: 0.3em;
  font-weight: 900;
  color: var(--color-text-secondary);
  text-transform: uppercase;
}

.help-content {
    max-width: 800px !important;
    width: 92% !important;
    padding: 20px !important;
}

.help-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    text-align: left;
}

.help-column h3 {
    font-size: 0.8rem;
    letter-spacing: 0.1em;
    margin-bottom: 12px;
    color: var(--color-correct);
    text-transform: uppercase;
}

.example .tile-row {
    display: flex;
    gap: 5px;
    margin-bottom: 8px;
}

.example .tile {
    width: 42px;
    height: 42px;
    font-size: 1.5rem;
    flex-shrink: 0;
}

.no-cap-badge {
    position: absolute;
    left: calc(100% + 15px);
    top: 50%;
    bottom: auto;
    transform: translateY(-50%) scale(0.8);
    font-size: 0.85rem;
    color: var(--color-correct);
    border: 2px solid var(--color-correct);
    padding: 6px 12px;
    border-radius: 6px;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    background: var(--color-bg);
    white-space: nowrap;
    opacity: 0;
    z-index: 100;
    transition: all 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    pointer-events: none;
}

.no-cap-badge.visible {
    opacity: 1;
    transform: translateY(-50%) scale(1);
}

@media (max-width: 800px) {
    .help-grid {
        grid-template-columns: 1fr;
        gap: 16px;
    }
    .help-column h3 {
        margin: 8px 0;
    }
    .example {
        margin-bottom: 12px;
    }
}

.input-group {
  margin: 20px 0;
}

#username {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--color-divider);
  border-radius: 4px;
  background-color: var(--color-bg);
  color: var(--color-text-primary);
  font-size: 16px;
  text-align: center;
}

.stats-container {
  display: flex;
  justify-content: space-around;
  margin-bottom: 24px;
}

.stat-item {
  flex: 1;
}

.stat-value {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--color-text-primary);
  line-height: 1;
  font-family: 'Outfit', sans-serif;
}

.stat-label {
  font-size: 0.65rem;
  color: var(--color-text-light);
  text-transform: uppercase;
  margin-top: 4px;
  letter-spacing: 0.08em;
  font-weight: 600;
}

.stats-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  justify-content: center;
}

.modal-button {
  background: linear-gradient(135deg, var(--color-correct) 0%, #538d4e 100%);
  color: white;
  border: none;
  padding: 14px 28px;
  border-radius: 8px;
  font-size: 1.1rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  cursor: pointer;
  width: 100%;
  margin-top: 12px;
  transition: all 0.2s ease;
  box-shadow: 0 4px 15px rgba(106, 170, 100, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.modal-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(106, 170, 100, 0.4);
  filter: brightness(1.1);
}

.modal-button:active {
  transform: translateY(0);
}

.secondary-button {
  background-color: var(--color-key-bg-default);
  color: var(--color-key-text);
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
}

/* Toggle Switch */
.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid var(--color-divider);
}

.setting-text {
  text-align: left;
}

.setting-label {
  font-size: 18px;
  font-weight: 600;
}

.setting-desc {
  font-size: 12px;
  color: var(--color-text-secondary);
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 4px;
  bottom: 3px;
  background-color: white;
  transition: .4s;
}

input:checked + .slider {
  background-color: var(--color-correct);
}

input:checked + .slider:before {
  transform: translateX(24px);
}

.slider.round {
  border-radius: 24px;
}

.slider.round:before {
  border-radius: 50%;
}

/* Help Content */
.help-content {
  text-align: left;
  width: 500px;
  max-height: 90vh;
  overflow-y: auto;
}

.help-content h3 {
  margin: 16px 0 8px;
}

.example {
  margin-bottom: 16px;
}

.example .tile-row {
  grid-template-columns: repeat(5, 40px);
  grid-gap: 4px;
  margin-bottom: 8px;
}

.example .tile {
  width: 40px;
  height: 40px;
  font-size: 24px;
}

hr {
  border: 0;
  border-top: 1px solid var(--color-divider);
  margin: 16px 0;
}

/* Toasts */
#toast-container {
  position: fixed;
  top: 10%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.toast {
  background-color: var(--color-text-primary);
  color: var(--color-bg);
  padding: 12px 16px;
  border-radius: 4px;
  font-weight: 700;
  margin-bottom: 8px;
  opacity: 1;
  transition: opacity 0.3s ease;
}

/* Responsive */
@media (max-width: 500px) {
  header h1 {
    font-size: 24px;
  }
  
  .grid {
    width: 300px;
    height: 360px;
  }
  
  .key {
    height: 50px;
    margin: 0 2px;
  }
  
  .tile {
    font-size: 28px;
  }
}

.hidden {
  display: none !important;
}
@keyframes stat-pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); color: var(--color-correct); text-shadow: 0 0 10px var(--color-correct); }
    100% { transform: scale(1); }
}

@keyframes pulse-glow {
    0% { box-shadow: 0 0 0 0 rgba(106, 170, 100, 0.4); }
    70% { box-shadow: 0 0 0 10px rgba(106, 170, 100, 0); }
    100% { box-shadow: 0 0 0 0 rgba(106, 170, 100, 0); }
}

.stat-animate {
    animation: stat-pop 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.stat-glow {
    animation: pulse-glow 2s infinite;
    border-radius: 4px;
}

/* Deception Audit Grid */
.audit-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 10px;
    justify-content: center;
}

.audit-card {
    background: #1a1a1b;
    border: 2px solid #3a3a3c;
    border-radius: 12px;
    padding: 8px;
    min-width: 80px;
    height: 90px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    box-shadow: 0 4px 15px rgba(0,0,0,0.4);
    transition: all 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.audit-card:hover {
    transform: translateY(-5px) scale(1.05);
    border-color: var(--color-correct);
    box-shadow: 0 8px 25px rgba(0,0,0,0.5);
}

.audit-turn {
    font-size: 9px;
    color: var(--color-text-light);
    text-transform: uppercase;
    font-weight: 700;
    margin-bottom: 4px;
    letter-spacing: 0.05em;
}

.audit-letter {
    font-size: 1.1rem;
    font-weight: 800;
    margin-bottom: 6px;
    color: var(--color-text-primary);
}

.audit-shift {
    display: flex;
    align-items: center;
    gap: 4px;
    background: var(--color-key-bg-default);
    padding: 3px 6px;
    border-radius: 20px;
    font-size: 9px;
}

.audit-status {
    position: absolute;
    top: -10px;
    right: -10px;
    font-size: 18px;
    background: var(--color-bg);
    border-radius: 50%;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 8px rgba(0,0,0,0.5);
    border: 2px solid #3a3a3c;
    z-index: 10;
}

.color-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.color-dot.absent { background: var(--color-absent); }
.color-dot.present { background: var(--color-present); }
.color-dot.correct { background: var(--color-correct); }

/* Final fail-safe for extremely short phones */
@media (max-height: 600px) {
  header { height: 45px; }
  header h1 { font-size: 18px; }
  .grid { max-height: 40vh; width: 80vw; }
  .key { height: 42px; font-size: 11px; }
  #game-container { gap: 5px; }
}

/* Global Mobile Fixes (Width-based) */
@media (max-width: 600px) {
  .no-cap-badge {
    left: auto !important;
    right: -10px !important;
    transform: translateY(-50%) scale(0.7) !important;
    box-shadow: 0 4px 12px rgba(0,0,0,0.6);
  }
  .no-cap-badge.visible {
    transform: translateY(-50%) scale(0.85) !important;
  }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 16px; }
  .stat-value { font-size: 1.2rem; }
  .stat-label { font-size: 0.55rem; }
  .modal-content { padding: 12px 14px; width: 94%; }
  .audit-card { min-width: 70px; height: 80px; padding: 5px; }
  .audit-letter { font-size: 0.9rem; }
  .audit-grid { gap: 6px; padding: 5px 0; }
  .modal-button { padding: 10px; font-size: 0.85rem; }
  .stats-actions { flex-direction: row; flex-wrap: wrap; }
  .stats-actions .modal-button { flex: 1 1 45%; }
}

/* Tablet and Desktop Enhancements */
@media (min-width: 600px) and (min-height: 800px) {
  .grid { max-width: 400px; max-height: 500px; }
  .tile { font-size: 38px; }
  header h1 { font-size: 32px; }
  .key { font-size: 14px; }
}
