:root {
  --bg-color: #1a1a2e;
  --panel-bg: #16213e;
  --panel-bg-light: #1f2e52;
  --accent-blue: #4ecca3;
  --accent-red: #e94560;
  --text-primary: #ffffff;
  --text-secondary: #a0a0a0;
  --card-hidden: #2a3b55;
  
  /* Rank Colors */
  --rank-top: #FFD700;
  --rank-high: #C0C0C0;
  --rank-mid: #CD7F32;
  --rank-low: #8B4513;
  --rank-bot: #555555;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  user-select: none;
}

body {
  background-color: var(--bg-color);
  color: var(--text-primary);
  min-height: 100vh;
  padding: 20px;
  display: flex;
  justify-content: center;
}

.app-container {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 30px;
  width: 100%;
  max-width: 1000px;
  justify-content: center;
  flex-wrap: wrap;
}

/* Game Main Column */
.game-wrapper {
  flex: 1;
  min-width: 350px;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

/* Rules Side Panel */
.rules-panel {
  flex: 0 0 300px;
  background: var(--panel-bg);
  padding: 20px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.05);
  align-self: flex-start;
}

.rules-panel h2 {
  color: var(--accent-blue);
  font-size: 1.2rem;
  margin-bottom: 15px;
  text-transform: uppercase;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  padding-bottom: 10px;
}

.rules-panel ul {
  list-style: none;
}

.rules-panel li {
  margin-bottom: 15px;
  font-size: 0.9rem;
  color: #d1d5db;
  line-height: 1.5;
}

.rules-panel li strong {
  color: #fff;
  display: block;
  margin-bottom: 4px;
}

.rules-panel .cycle-preview {
  background: rgba(0,0,0,0.3);
  padding: 5px 8px;
  border-radius: 4px;
  margin-top: 4px;
  font-family: monospace;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.rules-panel .important-rule {
  background: rgba(233, 69, 96, 0.1);
  padding: 10px;
  border-radius: 6px;
  border-left: 3px solid var(--accent-red);
}

.highlight-red {
  color: var(--accent-red);
  font-weight: bold;
}

/* Responsive Handling for Rules */
@media (max-width: 850px) {
  .app-container {
    flex-direction: column;
    align-items: center;
  }
  .rules-panel {
    width: 100%;
    max-width: 500px;
    flex: none;
    order: 2; /* Put rules below game on mobile */
  }
  .game-wrapper {
    order: 1;
    width: 100%;
  }
}

header {
  text-align: center;
}

h1 {
  font-size: 1.2rem;
  margin-bottom: 10px;
  color: var(--accent-blue);
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* Score Board */
.score-board {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel-bg);
  padding: 10px 20px;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}

.score-card { text-align: center; width: 70px; }
.score-card.player .score { color: var(--accent-blue); }
.score-card.cpu .score { color: var(--accent-red); }

.score { font-size: 1.8rem; font-weight: bold; }
.label { font-size: 0.75rem; color: var(--text-secondary); text-transform: uppercase; }

.current-round { font-size: 1.1rem; font-weight: bold; margin-top: 2px; }
.round-info { text-align: center; }

/* Lineup Sections */
.lineup-section {
  background: var(--panel-bg);
  padding: 12px;
  border-radius: 10px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}

.section-header h3 {
  font-size: 0.95rem;
  color: var(--text-primary);
}

.section-hint {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.cards-container {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

/* Card Styles */
.horse-card {
  background: var(--panel-bg-light);
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 8px 2px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 80px;
  position: relative;
  transition: all 0.2s;
}

.horse-card .horse-icon-wrapper {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  font-size: 1.8rem;
  line-height: 1;
}

.horse-card .rank-name {
  font-size: 0.7rem;
  font-weight: bold;
  text-transform: uppercase;
  text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}

/* CPU Specifics */
.cpu-section .horse-card {
  background: var(--card-hidden);
}

.cpu-section .horse-card.hidden {
  color: var(--text-secondary);
}

.cpu-section .horse-card.hidden .horse-icon-wrapper {
  opacity: 0.3;
  filter: grayscale(100%);
}

.cpu-section .horse-card.revealed {
  background: var(--panel-bg-light);
  border-color: var(--accent-red);
}

/* Player Specifics */
.player-section .horse-card {
  cursor: pointer;
  border-color: rgba(78, 204, 163, 0.3);
}

.player-section .horse-card:hover:not(.disabled) {
  transform: translateY(-2px);
  border-color: var(--accent-blue);
  background: rgba(78, 204, 163, 0.1);
}

.player-section .horse-card.disabled {
  opacity: 0.3;
  cursor: not-allowed;
  background: #0f1623;
  border-color: transparent;
}

.player-section .horse-card.selected {
  background: var(--accent-blue);
  color: #000;
  border-color: var(--accent-blue);
}

/* Track Area */
.track-container {
  position: relative;
  background: var(--panel-bg);
  border-radius: 12px;
  padding: 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  min-height: 200px;
}

canvas {
  max-width: 100%;
  height: auto;
}

.match-status {
  position: absolute;
  bottom: 10px;
  width: 90%;
  background: rgba(0,0,0,0.6);
  padding: 8px;
  border-radius: 8px;
  text-align: center;
  backdrop-filter: blur(2px);
}

.match-status h3 { font-size: 1rem; margin-bottom: 2px; }
.match-status p { font-size: 0.8rem; color: #ccc; }

/* Advisor */
.advisor-panel {
  margin-top: 10px;
  background: rgba(78, 204, 163, 0.05);
  border-left: 3px solid var(--accent-blue);
  padding: 10px;
  border-radius: 4px;
  display: flex;
  gap: 10px;
  align-items: start;
}

.advisor-icon { font-size: 1.2rem; }
.advisor-text { font-size: 0.85rem; line-height: 1.4; color: #d1d5db; }

/* History */
.history-panel {
  margin-top: 10px;
  background: var(--panel-bg);
  padding: 12px;
  border-radius: 10px;
  max-height: 150px;
  overflow-y: auto;
}

.history-panel h3 { font-size: 0.9rem; margin-bottom: 8px; }

.history-item {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  font-size: 0.8rem;
}
.history-item.win { color: var(--accent-blue); }
.history-item.loss { color: var(--accent-red); }

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  opacity: 1;
  transition: opacity 0.3s;
}
.modal-overlay.hidden { opacity: 0; pointer-events: none; }

.modal-content {
  background: var(--panel-bg);
  padding: 30px;
  border-radius: 15px;
  text-align: center;
  max-width: 90%;
  width: 350px;
  border: 1px solid var(--text-secondary);
}

#modal-title { font-size: 1.8rem; margin-bottom: 10px; }
#modal-message { margin-bottom: 20px; line-height: 1.5; font-size: 0.95rem; }

.btn-restart {
  background: var(--accent-blue);
  color: #000;
  border: none;
  padding: 10px 25px;
  font-size: 1rem;
  font-weight: bold;
  border-radius: 20px;
  cursor: pointer;
  transition: transform 0.1s;
}
.btn-restart:hover { transform: scale(1.05); }
