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

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: #fff;
}

.game-container {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 20px;
  padding: 18px 30px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.game-title {
  text-align: center;
  margin-bottom: 10px;
}

.game-title h1 {
  font-size: 1.7rem;
  font-weight: 700;
  background: linear-gradient(90deg, #3498db, #e74c3c, #f1c40f);
  background-size: 200% auto;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: gradient-shift 3s ease-in-out infinite;
}

@keyframes gradient-shift {
  0%, 100% { background-position: 0% center; }
  50% { background-position: 200% center; }
}

.mode-selector {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-bottom: 8px;
}

.mode-btn {
  padding: 6px 24px;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.6);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.mode-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.4);
  color: #fff;
}

.mode-btn.active {
  border-color: #f1c40f;
  background: rgba(241, 196, 15, 0.15);
  color: #f1c40f;
  box-shadow: 0 0 20px rgba(241, 196, 15, 0.2);
}

.grade-selector {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.grade-label {
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.6);
  margin-right: 4px;
}

.grade-btn {
  padding: 4px 12px;
  border-radius: 6px;
  border: 1.5px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.grade-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.4);
  color: #fff;
}

.grade-btn.active {
  border-color: #2ecc71;
  background: rgba(46, 204, 113, 0.15);
  color: #2ecc71;
  box-shadow: 0 0 15px rgba(46, 204, 113, 0.2);
}

.rank-btn {
  padding: 4px 14px;
  border-radius: 6px;
  border: 1.5px solid rgba(241, 196, 15, 0.4);
  background: rgba(241, 196, 15, 0.1);
  color: #f1c40f;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-left: 8px;
}

.rank-btn:hover {
  background: rgba(241, 196, 15, 0.2);
  border-color: #f1c40f;
  box-shadow: 0 0 12px rgba(241, 196, 15, 0.3);
}

.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.modal-content {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  padding: 25px 30px;
  max-height: 80vh;
  overflow-y: auto;
  animation: modal-in 0.25s ease;
}

@keyframes modal-in {
  from { transform: scale(0.9); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.ranking-modal {
  min-width: 420px;
  max-width: 520px;
}

.name-modal {
  min-width: 360px;
  max-width: 440px;
  text-align: center;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}

.modal-header h2 {
  font-size: 1.4rem;
  color: #f1c40f;
}

.modal-close {
  background: none;
  border: none;
  color: rgba(255,255,255,0.6);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0 4px;
  transition: color 0.2s;
}

.modal-close:hover {
  color: #e74c3c;
}

.ranking-grade-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 15px;
  flex-wrap: wrap;
}

.rank-tab {
  padding: 4px 12px;
  border-radius: 6px;
  border: 1.5px solid rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.05);
  color: rgba(255,255,255,0.6);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s;
}

.rank-tab:hover {
  color: #fff;
  border-color: rgba(255,255,255,0.4);
}

.rank-tab.active {
  border-color: #2ecc71;
  background: rgba(46, 204, 113, 0.15);
  color: #2ecc71;
}

.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.rank-row {
  display: grid;
  grid-template-columns: 40px 1fr 100px 90px;
  gap: 10px;
  align-items: center;
  padding: 8px 12px;
  border-radius: 8px;
  background: rgba(255,255,255,0.04);
  font-size: 0.9rem;
  transition: background 0.2s;
}

.rank-row:hover {
  background: rgba(255,255,255,0.08);
}

.rank-row.highlight {
  background: rgba(241, 196, 15, 0.12);
  border: 1px solid rgba(241, 196, 15, 0.3);
}

.rank-num {
  font-weight: 700;
  text-align: center;
}

.rank-num.gold { color: #f1c40f; font-size: 1.05rem; }
.rank-num.silver { color: #bdc3c7; }
.rank-num.bronze { color: #cd7f32; }

.rank-name {
  color: #ecf0f1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-time {
  color: #f1c40f;
  font-family: 'Courier New', monospace;
  font-weight: 600;
  text-align: right;
}

.rank-date {
  color: rgba(255,255,255,0.4);
  font-size: 0.8rem;
  text-align: right;
}

.rank-empty {
  text-align: center;
  color: rgba(255,255,255,0.4);
  padding: 40px 0;
  font-size: 0.95rem;
}

.name-prompt {
  color: #ecf0f1;
  font-size: 1rem;
  margin-bottom: 15px;
}

.name-input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 8px;
  border: 2px solid rgba(46, 204, 113, 0.3);
  background: rgba(255,255,255,0.05);
  color: #fff;
  font-size: 1rem;
  margin-bottom: 18px;
  outline: none;
  transition: border-color 0.2s;
}

.name-input:focus {
  border-color: #2ecc71;
}

.name-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.name-btn {
  padding: 8px 28px;
  border-radius: 8px;
  border: none;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.name-skip {
  background: rgba(255,255,255,0.1);
  color: rgba(255,255,255,0.7);
}

.name-skip:hover {
  background: rgba(255,255,255,0.15);
}

.name-submit {
  background: rgba(46, 204, 113, 0.2);
  color: #2ecc71;
  border: 1px solid #2ecc71;
}

.name-submit:hover {
  background: rgba(46, 204, 113, 0.3);
  box-shadow: 0 0 12px rgba(46, 204, 113, 0.3);
}

.timer-bar {
  text-align: center;
  margin-bottom: 6px;
  padding: 4px;
  background: rgba(241, 196, 15, 0.08);
  border-radius: 8px;
  border: 1px solid rgba(241, 196, 15, 0.2);
}

.timer-icon {
  font-size: 1.1rem;
  margin-right: 8px;
}

.timer-text {
  font-size: 1.3rem;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  color: #f1c40f;
}

.status-bar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  gap: 20px;
}

.player-status {
  flex: 1;
  padding: 6px 16px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.player-status.player1 {
  border-color: #3498db;
  box-shadow: 0 0 15px rgba(52, 152, 219, 0.3);
}

.player-status.player2 {
  border-color: #e74c3c;
  box-shadow: 0 0 15px rgba(231, 76, 60, 0.3);
}

.player-label {
  font-size: 0.95rem;
  font-weight: 600;
}

.player1 .player-label {
  color: #3498db;
}

.player2 .player-label {
  color: #e74c3c;
}

.length-display {
  font-size: 0.9rem;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.8);
}

.length-display span {
  font-weight: 700;
  font-size: 1.2rem;
  color: #f1c40f;
}

.numbers-container {
  display: flex;
  gap: 30px;
  justify-content: center;
}

.equation-display {
  font-size: 1.6rem;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  color: #fff;
  text-shadow: 0 2px 10px rgba(241, 196, 15, 0.7);
}

.player1 .equation-display {
  color: #5dade2;
}

.player2 .equation-display {
  color: #ec7063;
}

.game-canvas-container {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
}

#gameCanvas {
  display: block;
  border-radius: 12px;
  border: 3px solid #2c3e50;
}

.controls-section {
  margin-top: 25px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.controls-title {
  font-size: 1.1rem;
  margin-bottom: 15px;
  color: #f1c40f;
  text-align: center;
}

.controls-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
}

.control-group {
  text-align: center;
}

.control-group h4 {
  font-size: 1rem;
  margin-bottom: 10px;
}

.control-group.player1 h4 {
  color: #3498db;
}

.control-group.player2 h4 {
  color: #e74c3c;
}

.key-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
  color: #bdc3c7;
}

.key-badge {
  display: inline-block;
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 10px;
  border-radius: 6px;
  font-family: 'Courier New', monospace;
  font-weight: 600;
  margin: 0 3px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  transition: all 0.2s ease;
}

.key-badge:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.rule-section {
  margin-top: 20px;
  padding: 15px;
  background: linear-gradient(135deg, rgba(241, 196, 15, 0.1), rgba(243, 156, 18, 0.05));
  border-radius: 10px;
  border-left: 4px solid #f1c40f;
}

.rule-title {
  font-size: 1rem;
  color: #f1c40f;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.rule-text {
  font-size: 0.9rem;
  color: #ecf0f1;
  line-height: 1.6;
}

.formula {
  font-family: 'Courier New', monospace;
  background: rgba(0, 0, 0, 0.3);
  padding: 6px 12px;
  border-radius: 6px;
  margin: 8px 0;
  text-align: center;
  font-size: 1rem;
  color: #f1c40f;
}

footer {
  margin-top: 30px;
  text-align: center;
  color: #7f8c8d;
  font-size: 0.85rem;
}

@media (max-width: 700px) {
  .game-container {
    padding: 15px;
  }

  .game-title h1 {
    font-size: 1.8rem;
  }

  .status-bar {
    flex-direction: column;
    gap: 10px;
  }

  .controls-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  #gameCanvas {
    width: 100%;
    height: auto;
  }
}
