:root {
  --bg: radial-gradient(
      1200px 800px at 20% 10%,
      #0e1226 0%,
      #050710 40%,
      #02030a 100%
    ),
    linear-gradient(135deg, #0b0f21, #0a0d1b);
  --panel: rgba(255, 255, 255, 0.06);
  --panel-strong: rgba(255, 255, 255, 0.12);
  --border: rgba(255, 255, 255, 0.18);
  --text: #e9ecff;
  --muted: #a8b0d3;
  --primary: #7c4dff;
  --primary-2: #26c6da;
  --accent: #ffca28;
  --danger: #ff5252;
  --success: #00e676;
  --ring: #26c6da;
}

* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
}
body {
  margin: 0;
  font-family: "Poppins", system-ui, -apple-system, Segoe UI, Roboto, Arial,
    sans-serif;
  color: var(--text);
  background: var(--bg);
  background-attachment: fixed;
}

.app-header {
  position: sticky;
  top: 0;
  padding: 16px 24px;
  backdrop-filter: blur(10px);
  background: linear-gradient(180deg, rgba(5, 7, 16, 0.8), rgba(5, 7, 16, 0.3));
  border-bottom: 1px solid var(--border);
  z-index: 100;
}

.brand {
  display: flex;
  gap: 16px;
  align-items: center;
}
.brand-mark {
  padding: 10px 16px;
  border-radius: 12px;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.08),
    rgba(255, 255, 255, 0.02)
  );
  border: 1px solid var(--border);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3) inset,
    0 4px 18px rgba(39, 69, 255, 0.2);
  font-weight: 700;
}
.k-to-inf {
  font-family: "Orbitron", monospace;
  font-size: 28px;
  color: var(--primary-2);
}
.detonator {
  font-size: 24px;
  color: var(--accent);
  letter-spacing: 1px;
}

.app-main {
  display: grid;
  grid-template-columns: 300px 1fr 280px;
  gap: 16px;
  padding: 16px;
}

/* When game starts, expand graph to use setup space */
.app-main.game-only {
  /* Large center area + small right rail */
  grid-template-columns: 1fr 260px;
}

.left-panel,
.right-panel,
.center-panel {
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  backdrop-filter: blur(8px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
}

.left-panel h2,
.right-panel h2 {
  margin: 0 0 12px 0;
}
.hidden {
  display: none !important; /* Ensure hidden class doesn't collapse layout */
}
.form-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
  color: var(--muted);
}
input[type="number"],
input[type="text"],
input[type="range"] {
  width: 100%;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text);
}

.names-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
.topics {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 8px 12px;
  margin: 12px 0;
}
.topics label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-right: 12px;
}

.primary {
  background: linear-gradient(135deg, var(--primary), #5c6cff);
  border: none;
  color: white;
  padding: 10px 14px;
  border-radius: 12px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 6px 14px rgba(92, 108, 255, 0.35);
}
.primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.round-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  color: var(--muted);
}
.current-player {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.question-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 12px 0;
}

/* Timer Ring */
.timer {
  position: relative;
  width: 72px;
  height: 72px;
}
.timer-ring {
  width: 72px;
  height: 72px;
  transform: rotate(-90deg);
}
.ring-bg {
  fill: none;
  stroke: rgba(255, 255, 255, 0.15);
  stroke-width: 8;
}
.ring {
  fill: none;
  stroke: var(--ring);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 339.292;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.2s linear;
}
.timer-text {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-weight: 700;
}

.answer-row {
  display: flex;
  gap: 8px;
}
.math-entry {
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.math-key {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  line-height: 1.1;
}
.feedback {
  margin-top: 8px;
  min-height: 24px;
  color: var(--muted);
}

.center-panel {
  position: relative;
  min-height: 90vh;
  display: grid;
  place-items: center;
}

.hud-overlay {
  position: absolute;
  top: 12px;
  left: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  backdrop-filter: blur(8px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  max-width: min(380px, 42vw);
  z-index: 50;
}
.canvas-2d {
  width: 100%;
  height: 86vh;
  border-radius: 12px;
  background: radial-gradient(
    800px 500px at 30% 20%,
    rgba(255, 255, 255, 0.06),
    rgba(255, 255, 255, 0.03)
  );
  border: 1px solid var(--border);
}
.three-container {
  width: 100%;
  height: 86vh;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  position: relative;
}

.three-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.label {
  position: absolute;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  transform: translate(-50%, -50%);
}

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

.scoreboard,
.leaderboard {
  padding-left: 18px;
}
.scoreboard li,
.leaderboard li {
  margin-bottom: 6px;
}

.shape-info {
  margin-top: 10px;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  min-height: 120px;
  color: var(--muted);
}

/* Shape preview canvas inside Shape Info */
.shape-preview {
  display: block;
  width: 100%;
  max-width: 240px;
  height: 160px;
  margin: 8px auto 0;
  background: radial-gradient(
    400px 260px at 30% 20%,
    rgba(255, 255, 255, 0.06),
    rgba(255, 255, 255, 0.03)
  );
  border: 1px solid var(--border);
  border-radius: 10px;
}

/* Shape Info Tabs */
.shape-tabs {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.shape-tablist {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.shape-tab {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 8px; /* smaller chip-like */
  border-radius: 10px;
  cursor: pointer;
  font-size: 12px;
  line-height: 1.1;
}
.shape-tab.op-add {
  border-color: rgba(0, 230, 118, 0.7);
}
.shape-tab.op-sub {
  border-color: rgba(255, 82, 82, 0.6);
}
.shape-tab.active {
  background: linear-gradient(
    135deg,
    rgba(124, 77, 255, 0.25),
    rgba(38, 198, 218, 0.15)
  );
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2) inset;
}
.shape-tabpanel {
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.03);
}
.shape-detail-row {
  margin: 4px 0;
}
.shape-detail-title {
  font-weight: 700;
  color: var(--text);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
  z-index: 200;
}
.modal-content {
  width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.modal-content.wide {
  width: min(860px, 95vw);
  max-height: 85vh;
  overflow: auto;
}
.instructions-body h4 {
  margin: 10px 0 6px;
}
.instructions-body ul {
  margin: 0 0 8px 18px;
}
.level-display {
  text-align: center;
  margin: 8px 0 12px;
  font-weight: 700;
  color: var(--accent);
}
.icon {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  cursor: pointer;
}

/* Breakdown */
.breakdown {
  position: fixed;
  right: 0;
  top: 0;
  width: min(520px, 95vw);
  height: 100vh;
  background: var(--panel-strong);
  border-left: 1px solid var(--border);
  backdrop-filter: blur(10px);
  box-shadow: -12px 0 24px rgba(0, 0, 0, 0.25);
  z-index: 150;
  transform: translateX(0);
}
.breakdown.hidden {
  display: none;
}
.breakdown-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.breakdown-body {
  padding: 10px 0;
  max-height: calc(100vh - 120px);
  overflow: auto;
}
.formula {
  font-family: "Orbitron", monospace;
  color: var(--primary-2);
}
.piece {
  margin-bottom: 10px;
  padding: 8px;
  border: 1px dashed var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
}
.piece.add {
  border-color: rgba(0, 230, 118, 0.7);
}
.piece.sub {
  border-color: rgba(255, 82, 82, 0.6);
}

.katex {
  font-size: 1.05em;
}

@media (max-width: 1100px) {
  .app-main {
    grid-template-columns: 1fr;
  }
  .right-panel {
    order: 3;
  }
  .center-panel {
    order: 2;
  }
  .left-panel {
    order: 1;
  }
  .names-grid {
    grid-template-columns: 1fr;
  }
}
