: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;
  --accent-purple: #b392ff;
  --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-purple);
  line-height: 1;
  text-shadow: 0 0 6px #b392ff;
}

.scores-display {
  display: flex;
  align-items: center;
  gap: 16px;
}

.score-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.score-label {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

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

.score-divider {
  font-size: 18px;
  color: var(--muted);
  font-weight: 700;
}

.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;
}

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

/* Panels */
.status-box,
.controls-box,
.function-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;
  min-height: 20px;
}

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

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

.function-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: 16px;
  font-family: "Space Mono", monospace;
  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;
}

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

.function-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);
}

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

.function-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}

.fn-btn {
  padding: 10px 8px;
  border-radius: 8px;
  border: 1px solid #4a4f80;
  background: linear-gradient(180deg, #2a2d4a 0%, #1a1d33 100%);
  color: var(--accent-blue);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  font-family: "Space Mono", monospace;
}

.fn-btn:hover {
  background: linear-gradient(180deg, #38406c 0%, #2a2d4a 100%);
  border-color: var(--accent-blue);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(111, 168, 255, 0.3);
}

.fn-btn:active {
  transform: translateY(0);
  box-shadow: 0 1px 4px rgba(111, 168, 255, 0.2);
}

.fn-btn.fn-clear {
  grid-column: span 2;
  background: linear-gradient(180deg, #4a2d2d 0%, #331a1a 100%);
  border-color: #6a4f4f;
  color: var(--accent-red);
}

.fn-btn.fn-clear:hover {
  background: linear-gradient(180deg, #6a4040 0%, #4a2d2d 100%);
  border-color: var(--accent-red);
}

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

button.primary {
  width: 100%;
  background: linear-gradient(45deg, var(--accent-purple), var(--accent-blue));
  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);
}

button.primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.hidden {
  display: none !important;
}

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

.function-display {
  font-family: "Space Mono", monospace;
  font-size: 14px;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.6;
  background: rgba(111, 168, 255, 0.05);
  padding: 12px;
  border-radius: 8px;
  border: 1px solid #303560;
  word-wrap: break-word;
}

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

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

.history-list {
  margin: 0;
  padding-left: 20px;
  max-height: 150px;
  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.player {
  color: var(--accent-green);
}

.history-list li.agent {
  color: var(--accent-pink);
}

.history-list li.info {
  color: var(--accent-yellow);
}

/* 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-purple);
  text-align: center;
  margin-bottom: 20px;
  font-size: 28px;
  text-shadow: 0 0 10px rgba(179, 146, 255, 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 code {
  background: rgba(111, 168, 255, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: "Space Mono", monospace;
  font-size: 13px;
  color: var(--accent-blue);
}

.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;
}

/* Tips section styling */
.help-content .tips-section {
  background: rgba(26, 29, 51, 0.5);
  border: 1px solid #303560;
  border-radius: 12px;
  padding: 20px;
  margin: 16px 0;
}

.help-content .tips-section h4 {
  color: var(--accent-pink);
  margin: 20px 0 12px;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.help-content .tips-section h4:first-child {
  margin-top: 0;
}

.help-content .tips-section p {
  margin: 12px 0 8px;
  color: var(--accent-orange);
  font-weight: 600;
}

.help-content .tips-section ul {
  background: rgba(13, 15, 26, 0.5);
  padding: 16px 16px 16px 40px;
  border-radius: 8px;
  margin: 8px 0 16px;
  border-left: 2px solid var(--accent-blue);
}

.help-content .tips-section ol {
  background: rgba(13, 15, 26, 0.5);
  padding: 16px 16px 16px 40px;
  border-radius: 8px;
  margin: 8px 0 16px;
  border-left: 2px solid var(--accent-green);
}

.help-content .tips-section li {
  margin: 10px 0;
  line-height: 1.6;
}

.help-content .tips-section li strong {
  color: var(--accent-yellow);
  display: block;
  margin-bottom: 4px;
}

.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 code {
  background: rgba(111, 168, 255, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: "Space Mono", monospace;
  font-size: 13px;
  color: var(--accent-blue);
}

.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;
}

.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);
}

/* Result display */
.result-display {
  width: 100%;
  max-width: 600px;
  background: rgba(26, 29, 51, 0.5);
  border: 1px solid #303560;
  border-radius: 12px;
  padding: 24px;
  font-family: "Space Mono";
}

.result-item {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #303560;
  font-size: 18px;
}

.result-item:last-child {
  border-bottom: none;
}

.result-item.winner {
  color: var(--accent-green);
  font-weight: 700;
  font-size: 20px;
}

.result-item.loser {
  color: var(--accent-red);
}

.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;
  }

  .score-value {
    font-size: 24px;
  }

  .scores-display {
    gap: 10px;
  }
}
