:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --bg-modal: #1e293be6;
  
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  
  --accent-sky: #38bdf8;
  --accent-sky-dark: #0284c7;
  --accent-green: #22c55e;
  --accent-green-glow: rgba(34, 197, 94, 0.5);
  --accent-red: #ef4444;
  --accent-red-glow: rgba(239, 68, 68, 0.5);
  --accent-gold: #fbbf24;
  
  --border-subtle: #334155;
  --border-focus: #38bdf8;
  
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  
  --font-stack: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-stack);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: contain;
}

/* App Container Layout */
.app-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  height: 100dvh;
  padding-top: var(--sat);
  padding-bottom: var(--sab);
  padding-left: var(--sal);
  padding-right: var(--sar);
  position: relative;
  overflow: hidden;
}

/* Discrete Header / Status Bar */
.statusbar {
  width: 100%;
  background-color: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 10;
  border-bottom: 1px solid rgba(51, 65, 85, 0.5);
  display: flex;
  flex-direction: column;
}

.statusbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  height: 48px;
}

.status-left {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.unit-title {
  color: var(--text-primary);
  letter-spacing: 0.02em;
}

.status-divider {
  color: var(--text-muted);
}

.progress-text {
  color: var(--accent-sky);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.status-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.status-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.icon-btn:hover {
  color: var(--text-primary);
  background-color: var(--bg-secondary);
}

.icon-btn:active {
  transform: scale(0.92);
}

.audio-replay-btn {
  color: var(--accent-sky);
  background-color: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.25);
  padding: 6px 12px;
  border-radius: 20px;
}

.audio-replay-btn:hover {
  background-color: rgba(56, 189, 248, 0.2);
  color: #fff;
}

/* Progress bar track */
.progress-track {
  width: 100%;
  height: 3px;
  background-color: rgba(51, 65, 85, 0.3);
  position: relative;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-sky), var(--accent-green));
  transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Main Immersion Viewport */
.main-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  position: relative;
  max-height: 100%;
}

/* 2x2 Grid */
.card-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 12px;
  width: 100%;
  max-width: 480px;
  aspect-ratio: 1 / 1;
  max-height: calc(100dvh - 100px - var(--sat) - var(--sab));
  margin: auto;
}

@media (min-width: 640px) {
  .card-grid {
    gap: 16px;
    max-width: 520px;
  }
}

/* Interactive Card Tile (Faceted Low-Poly Monolit) */
.card-tile {
  position: relative;
  aspect-ratio: 1 / 1;
  min-width: 0;
  min-height: 0;
  background-color: var(--bg-secondary);
  border: 2px solid var(--border-subtle);
  border-radius: 6px;
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 4px 12px rgba(0, 0, 0, 0.4);
  outline: none;
}

.card-tile:active {
  transform: scale(0.97);
}

.card-tile:focus-visible {
  border-color: var(--border-focus);
}

.tile-content {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #0f172a;
}

.card-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  display: block;
}

.tile-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-color: transparent;
  transition: background-color 0.15s ease;
}

/* Feedback Highlights */
.card-tile.correct-highlight {
  border-color: var(--accent-green) !important;
  box-shadow: 0 0 24px var(--accent-green-glow), inset 0 0 16px rgba(34, 197, 94, 0.25) !important;
  animation: pulse-correct 0.35s ease;
  z-index: 2;
}

.card-tile.correct-highlight .tile-overlay {
  background-color: rgba(34, 197, 94, 0.12);
}

.card-tile.wrong-highlight {
  border-color: var(--accent-red) !important;
  box-shadow: 0 0 24px var(--accent-red-glow), inset 0 0 16px rgba(239, 68, 68, 0.25) !important;
  animation: shake-wrong 0.4s ease;
  z-index: 2;
}

.card-tile.wrong-highlight .tile-overlay {
  background-color: rgba(239, 68, 68, 0.15);
}

/* Dynamic Immersion Hint Ladder */
.card-tile.hint-dimmed {
  opacity: 0.25;
  pointer-events: none;
  transform: scale(0.96);
  transition: opacity 0.35s ease, transform 0.35s ease;
  filter: grayscale(40%);
}

/* Modal Overlay Base */
.modal-overlay {
  position: absolute;
  inset: 0;
  background-color: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background-color: var(--bg-secondary);
  border: 1px solid rgba(56, 189, 248, 0.25);
  border-radius: 8px;
  padding: 28px 24px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 20px 40px rgba(0, 0, 0, 0.6);
  animation: modal-pop 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal-icon-wrapper {
  margin-bottom: 16px;
  display: flex;
  justify-content: center;
}

.app-logo {
  border-radius: 8px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
}

.modal-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.modal-subtitle {
  font-size: 0.95rem;
  color: var(--text-secondary);
  margin-bottom: 20px;
}

.modal-features {
  background-color: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(51, 65, 85, 0.6);
  border-radius: 6px;
  padding: 12px 16px;
  margin-bottom: 24px;
  text-align: start;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.feature-bullet {
  color: var(--accent-sky);
  font-size: 0.65rem;
}

/* Buttons (Faceted Low-Poly Monolit) */
.primary-btn {
  width: 100%;
  min-height: 48px;
  padding: 12px 18px;
  background: linear-gradient(135deg, var(--accent-sky), var(--accent-sky-dark));
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(56, 189, 248, 0.35);
  transition: all 0.15s ease;
  white-space: normal;
  word-break: break-word;
  line-height: 1.3;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.primary-btn:active {
  transform: scale(0.97);
  box-shadow: 0 2px 6px rgba(56, 189, 248, 0.2);
}

.pulse-glow {
  animation: button-pulse 2s infinite;
}

.secondary-btn, .danger-btn {
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.15s ease;
}

.secondary-btn {
  background-color: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.secondary-btn:hover {
  background-color: #475569;
}

.secondary-btn:active {
  transform: scale(0.98);
}

.danger-btn {
  background-color: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.danger-btn:hover {
  background-color: rgba(239, 68, 68, 0.25);
}

/* Audio Resume Overlay (iOS Zombie State Wakeup) */
#audio-resume-overlay {
  z-index: 60;
  padding: calc(20px + var(--sat)) calc(20px + var(--sar)) calc(20px + var(--sab)) calc(20px + var(--sal));
  height: 100dvh;
  min-height: 100dvh;
}

.resume-card {
  max-width: 360px;
  border-color: rgba(56, 189, 248, 0.4);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.7), 0 0 24px rgba(56, 189, 248, 0.25);
}

.resume-icon-circle {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: rgba(56, 189, 248, 0.15);
  border: 2px solid rgba(56, 189, 248, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-sky);
  margin: 0 auto;
  animation: button-pulse 2s infinite;
}

#audio-resume-btn {
  font-size: 1.05rem;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
}

/* Completion Modal Specific */
.completion-badge {
  display: inline-block;
  background: linear-gradient(135deg, var(--accent-gold), #d97706);
  color: #0f172a;
  font-weight: 800;
  font-size: 0.9rem;
  padding: 4px 14px;
  border-radius: 20px;
  margin-bottom: 12px;
  box-shadow: 0 0 16px rgba(251, 191, 36, 0.4);
}

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

.stat-box {
  background-color: rgba(15, 23, 42, 0.5);
  border: 1px solid rgba(51, 65, 85, 0.4);
  border-radius: 12px;
  padding: 10px 6px;
}

.stat-number {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent-sky);
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.completion-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}

/* Menu Modal Specific */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.close-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

.close-btn:hover {
  color: var(--text-primary);
  background-color: var(--bg-tertiary);
}

.menu-desc {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 16px;
  text-align: start;
}

.menu-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
  text-align: start;
}

.menu-section-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.language-select {
  width: 100%;
  padding: 10px 14px;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  outline: none;
  transition: border-color 0.15s ease;
}

.language-select:focus {
  border-color: var(--accent-sky);
}

.menu-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Glosario Modal & 3D Flip Cards */
.glosario-card-container {
  max-width: 520px;
  max-height: 85vh;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
}

.glosario-desc {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
  text-align: start;
}

.glosario-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  overflow-y: auto;
  padding: 4px;
  max-height: calc(85dvh - 120px);
  -webkit-overflow-scrolling: touch;
}

@media (min-width: 480px) {
  .glosario-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
  }
}

.glosario-card {
  perspective: 1000px;
  -webkit-perspective: 1000px;
  aspect-ratio: 1 / 1;
  cursor: pointer;
  background: transparent;
  border: none;
  padding: 0;
  position: relative;
  border-radius: 12px;
}

.glosario-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.6s;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
}

.glosario-card.flipped .glosario-card-inner {
  transform: rotateY(180deg);
  -webkit-transform: rotateY(180deg);
}

.glosario-front, .glosario-back {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 12px;
  border: 2px solid var(--border-subtle);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.glosario-front {
  background-color: #ffffff;
}

.glosario-front img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.glosario-audio-btn {
  position: absolute;
  bottom: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  background-color: rgba(15, 23, 42, 0.75);
  color: var(--accent-sky);
  border: 1px solid rgba(56, 189, 248, 0.3);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
  z-index: 5;
}

.glosario-audio-btn:hover {
  background-color: var(--accent-sky);
  color: #0f172a;
  transform: scale(1.1);
}

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

.glosario-back {
  background-color: var(--bg-secondary);
  transform: rotateY(180deg);
  -webkit-transform: rotateY(180deg);
  padding: 10px 8px;
  color: var(--text-primary);
  justify-content: center;
}

.glosario-back-text {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--accent-sky);
  margin-bottom: 6px;
  line-height: 1.3;
  word-break: break-word;
}

[dir="rtl"] .glosario-back-text {
  font-size: 1.15rem;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* Toast */
.toast {
  position: absolute;
  bottom: calc(20px + var(--sab));
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background-color: rgba(30, 41, 59, 0.95);
  color: var(--text-primary);
  border: 1px solid var(--accent-sky);
  padding: 10px 18px;
  border-radius: 24px;
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275), opacity 0.25s ease;
  z-index: 100;
  white-space: nowrap;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* Keyframe Animations */
@keyframes pulse-correct {
  0% { transform: scale(1); }
  50% { transform: scale(1.03); }
  100% { transform: scale(1); }
}

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

@keyframes modal-pop {
  0% { transform: scale(0.9); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes button-pulse {
  0%, 100% {
    box-shadow: 0 4px 14px rgba(56, 189, 248, 0.35);
  }
  50% {
    box-shadow: 0 4px 24px rgba(56, 189, 248, 0.65), 0 0 10px rgba(56, 189, 248, 0.4);
  }
}
