@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');

:root{
  --bg-primary: #0a1208;
  --bg-secondary: #0f1a0c;
  --bg-tertiary: #141f11;
  --surface-primary: rgba(20,35,18,0.95);
  --surface-secondary: rgba(25,40,23,0.8);
  --surface-elevated: rgba(30,45,28,0.9);
  
  --text-primary: #f8fdf7;
  --text-secondary: #d4e8d0;
  --text-muted: #9bb596;
  --text-accent: #b7ff4a;
  
  --accent-primary: #b7ff4a;
  --accent-secondary: #9be94a;
  --accent-tertiary: rgba(183, 255, 74, 0.1);
  
  --success: #4bff9a;
  --error: #ff6b6b;
  --warning: #ffa726;
  
  --shadow-sm: 0 2px 8px rgba(0,0,0,0.15);
  --shadow-md: 0 8px 25px rgba(0,0,0,0.25);
  --shadow-lg: 0 20px 40px rgba(0,0,0,0.4);
  --shadow-glow: 0 0 30px rgba(183, 255, 74, 0.3);
  
  --border-radius-sm: 8px;
  --border-radius-md: 12px;
  --border-radius-lg: 18px;
  --border-radius-xl: 24px;
  
  --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-medium: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-bounce: 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  background: radial-gradient(ellipse at top, var(--bg-secondary) 0%, var(--bg-primary) 70%);
  color: var(--text-primary);
  overflow-x: hidden;
  font-feature-settings: 'cv02', 'cv03', 'cv04', 'cv11';
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100svh; /* ensure fit when browser UI is visible */
  /* Use dynamic viewport height on iOS to prevent overflow behind browser chrome */
  min-height: 100dvh;
  position: relative;
}

/* Animated background particles */
body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 25% 25%, rgba(183, 255, 74, 0.03) 0%, transparent 50%),
    radial-gradient(circle at 75% 75%, rgba(75, 255, 154, 0.02) 0%, transparent 50%);
  animation: backgroundPulse 8s ease-in-out infinite alternate;
  pointer-events: none;
  z-index: -1;
}

@keyframes backgroundPulse {
  0% { opacity: 0.3; transform: scale(1); }
  100% { opacity: 0.6; transform: scale(1.1); }
}

.wrap {
  width: 95vw;
  max-width: 1000px;
  background: linear-gradient(145deg, var(--surface-primary), var(--surface-secondary));
  border-radius: var(--border-radius-xl);
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255,255,255,0.03);
  padding: 32px;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255,255,255,0.05);
  position: relative;
  overflow: hidden;
  animation: slideInUp 0.6s var(--transition-medium) backwards;
}

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

.wrap::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(183, 255, 74, 0.3), transparent);
}

/* Header Section */
.header {
  text-align: center;
  margin-bottom: 24px;
  position: relative;
}

.logo {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.level {
  font-size: 13px;
  color: var(--text-muted);
  letter-spacing: 1.2px;
  font-weight: 500;
  text-transform: uppercase;
  margin-bottom: 12px;
  opacity: 0.8;
}

.target {
  font-size: 64px;
  font-weight: 800;
  letter-spacing: 8px;
  margin: 16px 0;
  color: var(--text-primary);
  font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
  background: linear-gradient(135deg, var(--surface-elevated), var(--surface-secondary));
  border-radius: var(--border-radius-lg);
  padding: 20px 32px;
  border: 2px solid rgba(183, 255, 74, 0.2);
  box-shadow: var(--shadow-md), inset 0 1px 0 rgba(255,255,255,0.05);
  position: relative;
  transition: all var(--transition-medium);
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.target.reveal {
  transform: scale(1.05);
  border-color: rgba(183, 255, 74, 0.5);
  box-shadow: var(--shadow-glow), var(--shadow-md);
  background: linear-gradient(135deg, var(--accent-tertiary), rgba(30,45,28,0.3));
}

.target.scrambling {
  animation: targetScramble 0.1s ease-in-out;
}

@keyframes targetScramble {
  0%, 100% { transform: scale(1) rotate(0deg); }
  50% { transform: scale(0.98) rotate(0.5deg); }
}

.subtitle {
  font-size: 14px;
  color: var(--text-muted);
  font-weight: 400;
  margin-bottom: 16px;
  opacity: 0.9;
}

/* Grid Section */
.grid-container {
  background: linear-gradient(145deg, rgba(0,0,0,0.1), rgba(0,0,0,0.05));
  padding: 24px;
  border-radius: var(--border-radius-lg);
  display: flex;
  justify-content: center;
  position: relative;
  margin: 24px 0;
}

.grid {
  width: 100%;
  max-width: 720px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 16px;
  padding: 20px;
  border-radius: var(--border-radius-md);
  background: linear-gradient(145deg, var(--surface-secondary), var(--bg-tertiary));
  box-shadow: inset var(--shadow-sm), var(--shadow-sm);
  border: 1px solid rgba(255,255,255,0.03);
  position: relative;
}

.tile {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--border-radius-sm);
  background: linear-gradient(145deg, var(--surface-elevated), var(--surface-secondary));
  color: var(--text-primary);
  font-weight: 600;
  font-size: 16px;
  font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
  letter-spacing: 1px;
  cursor: pointer;
  user-select: none;
  border: 2px solid transparent;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition-fast);
  position: relative;
  overflow: hidden;
  appearance: none;
  -webkit-appearance: none;
}

.tile::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.08), transparent);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.tile:hover::before {
  opacity: 1;
}

.tile:hover {
  transform: translateY(-2px);
  border-color: rgba(183, 255, 74, 0.4);
  box-shadow: var(--shadow-md), 0 0 20px rgba(183, 255, 74, 0.15);
}

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

/* Prevent persistent focus/hover highlight on touch while preserving keyboard focus */
.tile:focus { outline: none; }
.tile:focus-visible { outline: 2px solid rgba(183, 255, 74, 0.5); outline-offset: 2px; }

@media (hover: none) and (pointer: coarse) {
  .tile:hover::before { opacity: 0; }
  .tile:hover { transform: none; border-color: transparent; box-shadow: var(--shadow-sm); }
}

.tile:nth-child(odd) {
  background: linear-gradient(145deg, 
    rgba(255,255,255,0.02), 
    var(--surface-secondary)
  );
}

.tile.correct {
  animation: correctPulse 0.6s var(--transition-bounce);
  background: linear-gradient(145deg, rgba(75, 255, 154, 0.2), rgba(75, 255, 154, 0.1));
  border-color: var(--success);
}

@keyframes correctPulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); box-shadow: 0 0 30px rgba(75, 255, 154, 0.5); }
  100% { transform: scale(1); }
}

.grid.scrambling {
  animation: gridScramble 0.3s ease-out;
}

@keyframes gridScramble {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(0.98); opacity: 0.7; }
  100% { transform: scale(1); opacity: 1; }
}

/* Controls Section */
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 24px 0 16px 0;
  gap: 16px;
  flex-wrap: wrap;
}

.streak-display {
  display: flex;
  align-items: center;
  gap: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.streak-number {
  color: var(--accent-primary);
  font-size: 18px;
  font-weight: 700;
}

.game-info {
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  opacity: 0.8;
}

/* Progress Section */
.progress-section {
  margin-top: 20px;
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.progress-section.hidden {
  opacity: 0;
  transform: translateY(-10px);
  pointer-events: none;
}

.progress-bar {
  height: 8px;
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  overflow: hidden;
  position: relative;
  box-shadow: inset var(--shadow-sm);
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
  width: 100%;
  border-radius: inherit;
  transition: width 0.1s linear, opacity 0.3s ease;
  position: relative;
  box-shadow: 0 0 10px rgba(183, 255, 74, 0.3);
}

.progress-fill.hidden {
  opacity: 0;
  transform: scaleX(0);
  transform-origin: left;
}

.progress-fill::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  animation: progressShine 2s ease-in-out infinite;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.progress-fill.active::after {
  opacity: 1;
}

@keyframes progressShine {
  0% { 
    transform: translateX(-100%); 
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% { 
    transform: translateX(100%); 
    opacity: 0;
  }
}

.time-display {
  margin-top: 8px;
  text-align: right;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

/* Overlay System */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(12px);
  z-index: 1000;
  animation: overlayFadeIn 0.3s ease-out;
}

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

.modal {
  min-width: 400px;
  max-width: 90vw;
  background: linear-gradient(145deg, var(--surface-primary), var(--surface-secondary));
  border-radius: var(--border-radius-lg);
  padding: 32px;
  text-align: center;
  box-shadow: var(--shadow-lg);
  border: 1px solid rgba(255,255,255,0.1);
  position: relative;
  animation: modalSlideIn 0.4s var(--transition-bounce) backwards;
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.modal-title {
  margin: 0 0 12px 0;
  font-size: 32px;
  font-weight: 800;
  color: var(--text-primary);
}

.modal-body {
  margin: 0 0 24px 0;
  color: var(--text-secondary);
  font-size: 16px;
  line-height: 1.5;
}

.modal.success {
  border-left: 4px solid var(--success);
}

.modal.error {
  border-left: 4px solid var(--error);
}

/* Difficulty Selection */
.difficulty-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin: 24px 0;
}

.difficulty-card {
  background: linear-gradient(145deg, var(--surface-elevated), var(--surface-secondary));
  border: 2px solid rgba(255,255,255,0.1);
  border-radius: var(--border-radius-md);
  padding: 24px;
  cursor: pointer;
  transition: all var(--transition-medium);
  text-align: center;
  position: relative;
  overflow: hidden;
}

.difficulty-card::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.05), transparent);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.difficulty-card:hover::before {
  opacity: 1;
}

.difficulty-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-md);
}

.difficulty-card.easy {
  border-color: rgba(75, 255, 154, 0.3);
}

.difficulty-card.easy:hover {
  border-color: rgba(75, 255, 154, 0.6);
  box-shadow: 0 8px 25px rgba(75, 255, 154, 0.2);
}

.difficulty-card.hard {
  border-color: rgba(255, 107, 107, 0.3);
}

.difficulty-card.hard:hover {
  border-color: rgba(255, 107, 107, 0.6);
  box-shadow: 0 8px 25px rgba(255, 107, 107, 0.2);
}

.difficulty-card.zen {
  border-color: rgba(183, 255, 74, 0.3);
}

.difficulty-card.zen:hover {
  border-color: rgba(183, 255, 74, 0.6);
  box-shadow: var(--shadow-glow);
}

.difficulty-icon {
  font-size: 40px;
  margin-bottom: 12px;
  display: block;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

.difficulty-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text-primary);
}

.difficulty-subtitle {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 12px;
  font-weight: 500;
}

.difficulty-details {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
  opacity: 0.8;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  border-radius: var(--border-radius-sm);
  background: linear-gradient(135deg, rgba(255,255,255,0.1), rgba(255,255,255,0.05));
  color: var(--text-primary);
  text-decoration: none;
  cursor: pointer;
  border: 1px solid rgba(255,255,255,0.2);
  font-weight: 600;
  font-size: 14px;
  transition: all var(--transition-fast);
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.1), transparent);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.btn:hover::before {
  opacity: 1;
}

.btn:hover {
  transform: translateY(-1px);
  border-color: rgba(183, 255, 74, 0.4);
  box-shadow: var(--shadow-sm), 0 0 15px rgba(183, 255, 74, 0.2);
}

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

/* Responsive Design */
@media (max-width: 768px) {
  .wrap {
    padding: 24px;
    margin: 16px;
    width: auto;
  }

  .target {
    font-size: 48px;
    letter-spacing: 4px;
    padding: 16px 24px;
  }

  .grid {
    gap: 12px;
    padding: 16px;
  }

  .tile {
    height: 56px;
    font-size: 15px;
  }

  .controls {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .difficulty-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .modal {
    min-width: 320px;
    margin: 20px;
  }
}

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

@media (prefers-contrast: high) {
  :root {
    --text-primary: #ffffff;
    --text-secondary: #e0e0e0;
    --accent-primary: #00ff00;
  }

  .tile {
    border: 2px solid rgba(255,255,255,0.3);
  }
}

/* Loading animation for initial state */
.loading {
  opacity: 0;
  animation: fadeIn 0.6s ease-out forwards;
}

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

/* Mobile and touch improvements */
.tile, .btn {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Narrow phones and small devices */
@media (max-width: 600px) {
  /* Account for iOS notches and dynamic toolbars */
  body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    align-items: stretch;
    justify-content: flex-start;
    overflow: hidden; /* prevent overscroll */
    height: 100dvh;   /* ensure full viewport height */
  }

  .wrap {
    padding: 12px;
    padding-top: calc(12px + env(safe-area-inset-top));
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    margin: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
    width: 100vw;
    display: grid;
    grid-template-rows: auto 1fr auto auto;
    height: 100dvh;
    max-height: 100dvh;
    box-sizing: border-box;
  }

  .header { margin-bottom: 12px; }
  .logo { font-size: 22px; }
  .level { font-size: 12px; }
  .subtitle { display: none; }

  .target {
    font-size: clamp(24px, 8.5vw, 36px);
    letter-spacing: clamp(1px, 0.9vw, 4px);
    padding: 8px 12px;
    margin: 10px 0;
  }

  .grid-container {
    padding: 6px;
    margin: 8px 0;
    min-height: 0; /* allow to shrink within grid layout */
    overflow: hidden; /* avoid inner scroll */
  }

  /* Keep 8 columns to minimize vertical rows on phones */
  .grid {
    grid-template-columns: repeat(8, minmax(0, 1fr));
    gap: clamp(3px, 1.6vw, 7px);
    padding: clamp(6px, 2vw, 10px);
    min-height: 0; /* allow to shrink */
    height: 100%;
    grid-auto-rows: minmax(0, 1fr);
    align-items: stretch;
  }

  /* Shorter tiles to ensure full layout fits on screen */
  .tile {
    height: 100%;
    font-size: clamp(11px, 3.2vw, 15px);
    letter-spacing: 0.5px;
  }

  .controls {
    gap: 8px;
  }

  .streak-display { font-size: 12px; }
  .streak-number { font-size: 14px; }
  .game-info { display: none; }

  .btn {
    padding: 14px 20px;
    font-size: 16px;
  }

  .progress-section { margin-top: 8px; }
  .time-display { font-size: 11px; }

  .progress-bar { height: 6px; }

  .modal {
    min-width: 0;
    width: calc(100vw - 32px);
    max-width: 520px;
    margin: 16px;
    max-height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow: auto;
  }
}

/* Extra-small phones */
@media (max-width: 380px) {
  .wrap {
    padding: 10px;
    padding-top: calc(10px + env(safe-area-inset-top));
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    margin: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
  }

  .target {
    font-size: clamp(24px, 9vw, 38px);
    letter-spacing: clamp(1px, 0.8vw, 4px);
  }

  .grid {
    grid-template-columns: repeat(8, 1fr);
    gap: 6px;
    padding: 8px;
  }

  .tile {
    height: clamp(24px, 5.4vw, 34px);
    font-size: clamp(11px, 3.6vw, 15px);
  }

  .modal {
    width: calc(100vw - 20px);
    margin: 10px;
    padding: 20px;
    max-height: calc(100dvh - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow: auto;
  }
}

/* Very short portrait screens: tighten layout further */
@media (max-width: 600px) and (orientation: portrait) and (max-height: 750px) {
  .logo { display: none; }
  .header { margin-bottom: 8px; }
  .level { font-size: 11px; }
  .target { font-size: clamp(22px, 7.5vw, 32px); padding: 6px 10px; margin: 8px 0; }
  .grid { gap: clamp(2px, 1.2vw, 5px); padding: clamp(4px, 1.8vw, 8px); }
  .tile { font-size: clamp(10px, 2.9vw, 14px); letter-spacing: 0.4px; }
  .progress-bar { height: 4px; }
  .progress-section { margin-top: 6px; }
}
