/* Reset and base styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: 'Fredoka', 'Comic Sans MS', 'Segoe UI', sans-serif;
  background: #e8edf5;
  overflow: hidden;
}

/* Game container - 16:9 aspect ratio */
.game-container {
  width: 100%;
  height: 100%;
  max-width: 100vw;
  max-height: 100vh;
  aspect-ratio: 16 / 9;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, #ffffff 0%, #f8f9ff 50%, #eef2ff 100%);
  position: relative;
  border-radius: 0;
  box-shadow: none;
  overflow: hidden;
}

/* Decorative background pattern */
.game-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    radial-gradient(circle at 10% 20%, rgba(102, 126, 234, 0.08) 0%, transparent 25%),
    radial-gradient(circle at 90% 80%, rgba(240, 147, 251, 0.08) 0%, transparent 25%),
    radial-gradient(circle at 50% 50%, rgba(118, 75, 162, 0.05) 0%, transparent 35%);
  pointer-events: none;
  z-index: 0;
}

/* Header */
.game-header {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 14px 20px;
  position: relative;
  flex-shrink: 0;
  z-index: 1;
  background: #e4ebf5;
}

.instruction {
  font-size: 1.4rem;
  font-weight: 600;
  color: #4338ca;
  text-align: center;
  letter-spacing: 0.02em;
}

/* Main game area */
.game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
  min-height: 0;
  z-index: 1;
  touch-action: manipulation;
  background: #f0f4ff;
}

/* Win message */
.win-message {
  font-size: 2rem;
  font-weight: 700;
  color: #2e7d32;
  text-align: center;
  opacity: 0;
  transform: scale(0.8);
  transition: opacity 0.3s, transform 0.3s;
  pointer-events: none;
  height: 0;
  overflow: visible;
  text-shadow: 0 2px 4px rgba(46, 125, 50, 0.2), 0 0 20px rgba(76, 175, 80, 0.3);
  letter-spacing: 0.05em;
}

.win-message.visible {
  opacity: 1;
  transform: scale(1);
  animation: winBounce 0.5s ease-out;
}

@keyframes winBounce {
  0% { transform: scale(0.5); }
  50% { transform: scale(1.1); }
  70% { transform: scale(0.95); }
  100% { transform: scale(1); }
}

/* Next prompt after solving */
.next-prompt {
  font-size: 1rem;
  color: #4f46e5;
  text-align: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s;
  pointer-events: none;
  margin-top: 8px;
  font-weight: 500;
  flex-shrink: 0;
}

.next-prompt.visible {
  opacity: 1;
  visibility: visible;
}

.equation-display {
  width: 100%;
  height: 100%;
  max-width: 950px;
  filter: drop-shadow(0 4px 8px rgba(102, 126, 234, 0.15));
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  flex: 1;
  min-height: 0;
}

/* Pencil segment styles */
.pencil-segment {
  cursor: pointer;
  transition: filter 0.2s, opacity 0.2s, transform 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.pencil-segment:hover {
  filter: brightness(1.05) drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
}

/* Pencil body (yellow) */
.pencil-body {
  fill: #FFD93D;
  stroke: #E6B800;
  stroke-width: 1;
}

/* Pencil tip (graphite) */
.pencil-tip {
  fill: #4A4A4A;
  stroke: #333333;
  stroke-width: 0.5;
}

/* Pencil eraser (subtle pink end cap) */
.pencil-eraser {
  fill: #F5B8C4;
  stroke: #D4909E;
  stroke-width: 1;
}

/* Selected state - bright blue glow */
.pencil-segment.selected .pencil-body {
  fill: #64B5F6;
  stroke: #1976D2;
}

.pencil-segment.selected .pencil-tip {
  fill: #1976D2;
  stroke: #0D47A1;
}

.pencil-segment.selected .pencil-eraser {
  fill: #90CAF9;
  stroke: #1976D2;
}

.pencil-segment.selected {
  filter: drop-shadow(0 0 8px rgba(25, 118, 210, 0.5));
}

/* Ghost/placeholder state */
.pencil-segment.ghost {
  opacity: 0.4;
  cursor: pointer;
}

.pencil-segment.ghost:hover {
  opacity: 0.65;
}

.pencil-segment.ghost .pencil-body {
  fill: #D0D0D0;
  stroke: #A0A0A0;
  stroke-dasharray: 3 2;
}

.pencil-segment.ghost .pencil-tip {
  fill: #909090;
  stroke: #707070;
}

.pencil-segment.ghost .pencil-eraser {
  fill: #D0D0D0;
  stroke: #A0A0A0;
}

/* Success state - vibrant green */
.pencil-segment.success .pencil-body {
  fill: #81C784;
  stroke: #388E3C;
}

.pencil-segment.success .pencil-tip {
  fill: #388E3C;
  stroke: #2E7D32;
}

.pencil-segment.success .pencil-eraser {
  fill: #A5D6A7;
  stroke: #388E3C;
}

.pencil-segment.success {
  filter: drop-shadow(0 0 6px rgba(76, 175, 80, 0.4));
}

/* Failure state - vibrant red */
.pencil-segment.failure .pencil-body {
  fill: #EF9A9A;
  stroke: #D32F2F;
}

.pencil-segment.failure .pencil-tip {
  fill: #D32F2F;
  stroke: #B71C1C;
}

.pencil-segment.failure .pencil-eraser {
  fill: #FFCDD2;
  stroke: #D32F2F;
}

/* Hint state */
.pencil-segment.hint {
  animation: hintPulse 0.5s ease-in-out 6;
}

@keyframes hintPulse {
  0%, 100% {
    filter: brightness(1) drop-shadow(0 0 0 transparent);
  }
  50% {
    filter: brightness(1.3) drop-shadow(0 0 12px #ffb74d);
  }
}

/* Footer - 3 equal column layout */
.game-footer {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: center;
  padding: 12px 20px;
  background: #e4ebf5;
  flex-shrink: 0;
  z-index: 1;
}

.footer-left {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-start;
}

.footer-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.footer-center {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}

.stat-label {
  font-size: 0.65rem;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 500;
}

.stat-value {
  font-size: 1.2rem;
  font-weight: 700;
  color: #334155;
}

.reset-link {
  background: none;
  border: none;
  font-size: 0.65rem;
  color: #94a3b8;
  text-decoration: underline;
  cursor: pointer;
  padding: 4px 8px;
  font-family: inherit;
  transition: color 0.15s;
}

.reset-link:hover {
  color: #4f46e5;
}

/* Difficulty toggle */
.difficulty-toggle {
  display: flex;
  background: #d8e2f0;
  border-radius: 10px;
  padding: 3px;
}

.diff-btn {
  padding: 8px 14px;
  border: none;
  background: transparent;
  border-radius: 7px;
  font-size: 0.85rem;
  font-weight: 600;
  color: #64748b;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.diff-btn.active {
  background: #fff;
  color: #4f46e5;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.diff-btn:hover:not(.active) {
  color: #4f46e5;
}

/* Tool buttons */
.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  border: none;
  background: #d8e2f0;
  border-radius: 10px;
  cursor: pointer;
  color: #64748b;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.tool-btn:hover {
  background: #cbd5e8;
  color: #4f46e5;
}

.tool-btn.primary {
  padding: 10px 24px;
  background: #4f46e5;
  color: #ffffff;
  font-weight: 600;
  font-size: 0.95rem;
}

.tool-btn.primary:hover {
  background: #4338ca;
}

.tool-btn.primary:active {
  background: #3730a3;
}

/* Responsive adjustments for medium screens */
@media (max-width: 640px) {
  .instruction {
    font-size: 1.1rem;
  }

  .footer-center {
    gap: 14px;
  }

  .stat-value {
    font-size: 1rem;
  }

  .diff-btn {
    padding: 7px 12px;
    font-size: 0.8rem;
  }

  .tool-btn.primary {
    padding: 9px 18px;
    font-size: 0.9rem;
  }

  .win-message {
    font-size: 1.6rem;
  }
}

/* Responsive adjustments for small 16:9 viewports (scaled iframe) */
@media (max-height: 450px) {
  .game-header {
    padding: 8px 12px;
  }

  .instruction {
    font-size: 0.9rem;
  }

  .game-area {
    padding: 6px;
  }

  .win-message {
    font-size: 1.1rem;
  }

  .next-prompt {
    font-size: 0.85rem;
    margin-top: 6px;
  }

  .game-footer {
    padding: 8px 12px;
  }

  .footer-center {
    gap: 10px;
  }

  .stat-label {
    font-size: 0.55rem;
  }

  .stat-value {
    font-size: 0.9rem;
  }

  .reset-link {
    font-size: 0.55rem;
  }

  .difficulty-toggle {
    padding: 2px;
    border-radius: 8px;
  }

  .diff-btn {
    padding: 5px 9px;
    font-size: 0.7rem;
    border-radius: 6px;
  }

  .tool-btn {
    padding: 7px;
    border-radius: 8px;
  }

  .tool-btn svg {
    width: 16px;
    height: 16px;
  }

  .tool-btn.primary {
    padding: 7px 14px;
    font-size: 0.8rem;
  }
}

/* Very small viewports (mobile portrait with 16:9 iframe) */
@media (max-height: 280px) {
  .game-header {
    padding: 4px 8px;
  }

  .instruction {
    font-size: 0.75rem;
  }

  .game-area {
    padding: 3px;
  }

  .win-message {
    font-size: 0.9rem;
  }

  .next-prompt {
    font-size: 0.75rem;
    margin-top: 3px;
  }

  .game-footer {
    padding: 4px 8px;
  }

  .footer-center {
    gap: 6px;
  }

  .stat-label {
    font-size: 0.45rem;
  }

  .stat-value {
    font-size: 0.7rem;
  }

  .reset-link {
    font-size: 0.45rem;
    padding: 2px 4px;
  }

  .difficulty-toggle {
    padding: 1px;
    border-radius: 5px;
  }

  .diff-btn {
    padding: 3px 6px;
    font-size: 0.6rem;
    border-radius: 4px;
  }

  .tool-btn {
    padding: 5px;
    border-radius: 6px;
  }

  .tool-btn svg {
    width: 14px;
    height: 14px;
  }

  .tool-btn.primary {
    padding: 5px 10px;
    font-size: 0.7rem;
  }
}

/* Extremely small viewports */
@media (max-height: 200px) {
  .game-header {
    padding: 2px 6px;
  }

  .instruction {
    font-size: 0.65rem;
  }

  .game-area {
    padding: 2px;
  }

  .next-prompt {
    display: none;
  }

  .game-footer {
    padding: 2px 6px;
  }

  .footer-center {
    gap: 4px;
  }

  .stat-label {
    font-size: 0.4rem;
  }

  .stat-value {
    font-size: 0.6rem;
  }

  .diff-btn {
    padding: 2px 5px;
    font-size: 0.55rem;
  }

  .tool-btn {
    padding: 3px;
  }

  .tool-btn svg {
    width: 11px;
    height: 11px;
  }

  .tool-btn.primary {
    padding: 3px 8px;
    font-size: 0.6rem;
  }
}

/* Animation for state changes */
@keyframes flashRed {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.equation-display.failure-flash {
  animation: flashRed 0.15s ease-in-out 3;
}
