:root {
  --bg-dark: #0d0f1a;
  --bg-panel: #1a1d33;
  --text: #e0e8ff;
  --muted: #a0a8c8;
  --accent-blue: #6fa8ff;
  --accent-pink: #ff7ac2;
  --accent-green: #6fe3a2;
  --accent-yellow: #ffd86b;
  --accent-red: #ff6f6f;
  --accent-orange: #ff9b4a;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: auto;
}

body {
  background: radial-gradient(circle at top, #1e2245, var(--bg-dark) 70%);
  color: var(--text);
  font-family: "Lexend", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

/* Header */
.app-header {
  padding: 16px 24px;
  border-bottom: 1px solid #303560;
}

.title-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.help-btn {
  margin-left: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid #4a4f80;
  background: #2a2d4a;
  color: #fff;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.help-btn:hover {
  background: #38406c;
  transform: translateY(-1px);
}

.help-btn:active {
  transform: translateY(1px);
}

.limit-badge,
.game-title {
  font-size: 32px;
  font-weight: 900;
  color: var(--accent-orange);
  line-height: 1;
  text-shadow: 0 0 6px #ff6600;
}

.timer-display {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.timer-label {
  font-size: 12px;
  color: var(--muted);
}

.timer-value {
  font-family: "Space Mono", monospace;
  font-size: 36px;
  font-weight: 700;
  color: var(--accent-yellow);
  text-shadow: 0 0 10px var(--accent-yellow);
}

.subtitle {
  color: var(--muted);
  font-size: 13px;
  margin-top: 2px;
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 1fr 420px;
  gap: 16px;
  padding: 16px;
  height: calc(100vh - 100px);
}

.game-canvas {
  background: #000;
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1px solid #303560;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

#gameCanvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
}

.help-signs {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.help-sign {
  position: absolute;
  font-size: 11px;
  font-weight: 700;
  font-family: "Lexend", sans-serif;
  color: #ff3333;
  background: white;
  padding: 4px 8px;
  border-radius: 8px;
  border: 2px solid #ff3333;
  box-shadow: 0 4px 12px rgba(255, 51, 51, 0.4);
  animation: helpPulse 1.5s ease-in-out infinite;
  white-space: nowrap;
}

.help-sign::after {
  content: "";
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid white;
}

.help-sign::before {
  content: "";
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 7px solid #ff3333;
}

@keyframes helpPulse {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-5px) scale(1.05);
  }
}

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

.right-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

/* Panels */
.status-box,
.controls-box,
.equations-box,
.history-box {
  background: var(--bg-panel);
  border: 1px solid #303560;
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.status-box {
  display: flex;
  justify-content: space-around;
  text-align: center;
}

.status-item .label {
  font-size: 14px;
  color: var(--muted);
}

.status-item .value {
  display: block;
  font-size: 24px;
  font-weight: 700;
  font-family: "Space Mono";
  margin-top: 4px;
  color: var(--accent-green);
}

/* Controls */
.controls-box {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.question {
  text-align: center;
  font-weight: 700;
  color: var(--accent-blue);
  font-size: 18px;
}

.control-prompt {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.coordinate-entry label {
  display: block;
  margin-bottom: 8px;
  color: var(--text);
  font-size: 14px;
}

.coord-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.coord-form input {
  height: 48px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid #4a4f80;
  background: linear-gradient(180deg, #141938 0%, #0d112b 100%);
  color: var(--text);
  font-size: 18px;
  font-family: "Space Mono", monospace;
  text-align: center;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.5);
  outline: none;
  transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

.coord-form input::placeholder {
  color: var(--muted);
  opacity: 0.85;
}

.coord-form input:focus {
  border-color: var(--accent-blue);
  box-shadow: 0 0 0 3px rgba(111, 168, 255, 0.2),
    inset 0 2px 6px rgba(0, 0, 0, 0.55);
}

.coord-form .primary {
  width: 100%;
  height: 48px;
  padding: 0 20px;
}

.feedback {
  min-height: 22px;
  text-align: center;
  color: var(--accent-pink);
  font-family: "Space Mono";
  font-size: 13px;
  margin-top: 4px;
}

.hint {
  text-align: center;
  color: var(--accent-yellow);
  font-size: 12px;
  margin-top: 8px;
  font-family: "Space Mono";
}

button.primary {
  width: 100%;
  background: linear-gradient(45deg, var(--accent-orange), var(--accent-red));
  border: none;
  color: #fff;
  padding: 12px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  text-shadow: 0 1px 3px #0005;
  transition: transform 0.2s;
}

button.primary:hover {
  transform: scale(1.02);
}

.hidden {
  display: none !important;
}

/* Equations */
.equations-box h3 {
  margin: 0 0 12px;
  text-align: center;
  color: var(--accent-blue);
}

.equations-list {
  font-family: "Space Mono", monospace;
  font-size: 13px;
  max-height: 200px;
  overflow-y: auto;
  line-height: 1.8;
}

.equations-placeholder {
  color: var(--muted);
  text-align: center;
  padding: 20px;
}

.equation-group {
  margin-bottom: 16px;
  padding: 10px;
  background: rgba(111, 168, 255, 0.05);
  border-radius: 8px;
  border-left: 3px solid var(--accent-blue);
}

.equation-group.house {
  border-left-color: var(--accent-red);
  background: rgba(255, 111, 111, 0.05);
}

.equation-group.puddle {
  border-left-color: var(--accent-blue);
  background: rgba(111, 168, 255, 0.05);
}

.equation-group-title {
  color: var(--accent-yellow);
  font-weight: 700;
  margin-bottom: 6px;
}

.equation-item {
  color: var(--text);
  margin: 4px 0;
}

.equation-item.solved {
  text-decoration: line-through;
  opacity: 0.4;
}

/* History */
.history-box h3 {
  margin: 0 0 12px;
  text-align: center;
  color: var(--accent-blue);
}

.history-list {
  margin: 0;
  padding-left: 20px;
  height: 120px;
  max-height: 120px;
  overflow-y: auto;
  overflow-x: hidden;
  font-family: "Space Mono";
  font-size: 12px;
  line-height: 1.6;
}

.history-list li {
  margin: 4px 0;
  padding: 4px 0;
}

.history-list li.good {
  color: var(--accent-green);
}

.history-list li.bad {
  color: var(--accent-red);
}

/* Level tabs */
.level-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
}

.level-tab {
  flex: 1;
  padding: 6px 8px;
  border: 1px solid #4a4f80;
  background: #2a2d4a;
  color: var(--muted);
  font-size: 11px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.level-tab:hover {
  background: #38406c;
}

.level-tab.active {
  background: var(--accent-blue);
  color: #fff;
  border-color: var(--accent-blue);
}

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  background: #0d0f1acc;
  backdrop-filter: blur(8px);
  z-index: 100;
}

.overlay.visible {
  display: grid;
}

.overlay .card {
  width: min(900px, 96vw);
  min-height: 40vh;
  max-height: 90vh;
  background: linear-gradient(135deg, #1a1d33 0%, #0d0f1a 100%);
  border: 2px solid #303560;
  border-radius: 20px;
  padding: 32px;
  color: #fff;
  box-shadow: var(--shadow);
  overflow-y: auto;
}

.overlay .card h2 {
  color: var(--accent-orange);
  text-align: center;
  margin-bottom: 20px;
  font-size: 28px;
  text-shadow: 0 0 10px rgba(255, 155, 74, 0.3);
}

.overlay .card.instructions {
  min-height: 60vh;
}

.overlay .card.gameover {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
}

.overlay .card.help {
  width: min(1200px, 98vw);
  min-height: 70vh;
  max-height: 90vh;
}

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

.help-header h2 {
  margin: 0;
}

.close-btn {
  background: #2a2d4a;
  border: 1px solid #4a4f80;
  color: #fff;
  border-radius: 8px;
  font-size: 24px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  transition: all 0.2s;
  line-height: 1;
  padding: 0;
}

.close-btn:hover {
  background: #38406c;
  transform: scale(1.05);
}

.help-content {
  line-height: 1.7;
  font-size: 15px;
}

.help-content h3 {
  color: var(--accent-yellow);
  margin: 24px 0 12px;
  font-size: 18px;
}

.help-content ul,
.help-content ol {
  padding-left: 24px;
  margin: 12px 0;
}

.help-content li {
  margin: 8px 0;
}

.help-content strong {
  color: var(--accent-green);
}

.help-content .tip {
  background: rgba(111, 168, 255, 0.1);
  padding: 12px;
  border-radius: 8px;
  border-left: 3px solid var(--accent-blue);
  margin-top: 20px;
}

.difficulty-table,
.scoring-table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-family: "Space Mono", monospace;
  font-size: 13px;
}

.difficulty-table td,
.scoring-table td,
.scoring-table th {
  padding: 10px 12px;
  border-bottom: 1px solid #303560;
  text-align: left;
}

.scoring-table th {
  color: var(--accent-blue);
  font-weight: 700;
  border-bottom: 2px solid #4a4f80;
}

.difficulty-table tr:hover,
.scoring-table tr:hover {
  background: rgba(111, 168, 255, 0.05);
}

.pts-good {
  color: var(--accent-green);
  font-weight: 700;
}
.pts-ok {
  color: var(--accent-yellow);
  font-weight: 700;
}
.pts-warn {
  color: var(--accent-orange);
  font-weight: 700;
}
.pts-bad {
  color: var(--accent-red);
  font-weight: 700;
}
.pts-fail {
  color: #999;
  font-weight: 700;
}

.instructions-text {
  line-height: 1.7;
  font-size: 15px;
}

.instructions-text h3 {
  color: var(--accent-yellow);
  margin: 20px 0 10px;
  font-size: 18px;
}

.instructions-text ul,
.instructions-text ol {
  padding-left: 24px;
}

.instructions-text li {
  margin: 8px 0;
}

.instructions-text strong {
  color: var(--accent-green);
}

.instructions-text .tip {
  background: rgba(111, 168, 255, 0.1);
  padding: 12px;
  border-radius: 8px;
  border-left: 3px solid var(--accent-blue);
  margin-top: 16px;
}

.score-table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-family: "Space Mono", monospace;
  font-size: 13px;
}

.score-table td {
  padding: 8px 12px;
  border-bottom: 1px solid #303560;
}

.score-table tr:hover {
  background: rgba(111, 168, 255, 0.05);
}

.countdown {
  text-align: center;
  font-family: "Space Mono";
  color: var(--accent-yellow);
  font-size: 18px;
  margin-top: 20px;
}

.countdown span {
  font-weight: 700;
  font-size: 24px;
}

/* Setup form */
.setup label {
  display: block;
  margin: 16px 0;
  font-size: 15px;
}

.setup input,
.setup select {
  width: 100%;
  margin-top: 8px;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid #4a4f80;
  background: linear-gradient(180deg, #141938 0%, #0d112b 100%);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  outline: none;
}

.setup input:focus,
.setup select:focus {
  border-color: var(--accent-blue);
  box-shadow: 0 0 0 3px rgba(111, 168, 255, 0.2);
}

/* Score table in game over */
.score-table-container {
  width: 100%;
  max-width: 700px;
  background: rgba(26, 29, 51, 0.5);
  border: 1px solid #303560;
  border-radius: 12px;
  padding: 20px;
}

.score-table-container table {
  width: 100%;
  border-collapse: collapse;
}

.score-table-container th {
  color: var(--accent-blue);
  font-weight: 700;
  padding: 12px;
  border-bottom: 2px solid #4a4f80;
  text-align: left;
}

.score-table-container td {
  padding: 10px 12px;
  border-bottom: 1px solid #303560;
}

.score-table-container tr.total-row {
  font-weight: 700;
  font-size: 18px;
  color: var(--accent-yellow);
  border-top: 2px solid var(--accent-yellow);
}

.score-table-container tr.total-row td {
  padding-top: 16px;
}

.gameover-actions {
  display: flex;
  gap: 12px;
  margin-top: 20px;
}

.gameover-actions button {
  min-width: 180px;
}

@media (max-width: 1200px) {
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .game-canvas {
    min-height: 500px;
  }
}

@media (max-width: 600px) {
  .limit-badge,
  .game-title {
    font-size: 24px;
  }

  .timer-value {
    font-size: 28px;
  }
}
