html,
body {
  height: 100%;
  background: #0c1a33;
  overflow: hidden;
}

body {
  margin: 0;
}

#optica-root {
  height: 100%;
  background: #0c1a33;
}

:root {
  --optica-bg:#0c1a33;
  --optica-panel:#172a45;
  --optica-build:#1f3561;
  --optica-ink:#eaf1ff;
  --optica-ink2:#a0b4d3;
  --optica-grid:#405f9e;
  --optica-accent:#6c8ed4;
  --optica-ok:#2da44e;
  --optica-warn:#c18f00;
  --optica-bad:#d9534f;
  --violet:#a38ce4;
  --blue:#5fa8ff;
  --green:#62c980;
  --yellow:#f2d86b;
  --orange:#ff9b47;
  --red:#f25c5c;
  --ink2: var(--optica-ink2);
}

.optica-shell {
  --bg: var(--optica-bg);
  --panel: var(--optica-panel);
  --build: var(--optica-build);
  --ink: var(--optica-ink);
  --ink2: var(--optica-ink2);
  --grid: var(--optica-grid);
  --accent: var(--optica-accent);
  --ok: var(--optica-ok);
  --warn: var(--optica-warn);
  --bad: var(--optica-bad);
  color: var(--ink);
  font-family: system-ui, "Segoe UI", sans-serif;
  background: var(--panel);
  border-radius: 1rem;
  border: 1px solid #233a61;
  overflow: visible;
  height: 100%;
  display: flex;
  flex-direction: column;
}

#optica-root {
  height: 100%;
}

.optica-shell .optica-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  padding: 0.35rem 0.6rem;
  border-bottom: 1px solid #233a61;
  background: #162743;
  position: relative;
  z-index: 2;
  flex-shrink: 0;
}

.optica-shell .optica-group {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: center;
}

/* Compact cursor toggle (re-used in play + editor controls) */
.optica-shell .optica-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 2px 6px;
  border-radius: 999px;
  border: 1px solid #2f4d7d;
  background: rgba(34, 60, 104, 0.65);
}

.optica-shell .optica-toggle-label {
  font-size: 0.82rem;
  color: var(--ink2);
  font-weight: 700;
}

.optica-shell .optica-switch {
  position: relative;
  display: inline-block;
  width: 34px;
  height: 18px;
}

.optica-shell .optica-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.optica-shell .optica-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(160, 180, 211, 0.25);
  transition: 0.2s ease;
  border-radius: 999px;
  border: 1px solid #2f4d7d;
}

.optica-shell .optica-slider:before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 2px;
  top: 1px;
  background: var(--ink2);
  transition: 0.2s ease;
  border-radius: 999px;
}

.optica-shell .optica-switch input:checked + .optica-slider {
  background: rgba(108, 142, 212, 0.28);
  border-color: var(--accent);
}

.optica-shell .optica-switch input:checked + .optica-slider:before {
  transform: translateX(16px);
  background: var(--accent);
}

.optica-shell .optica-group label {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.optica-shell .optica-menu {
  position: relative;
}

.optica-shell .optica-menu-trigger {
  list-style: none;
  cursor: pointer;
  border: 1px solid #2f4d7d;
  border-radius: 8px;
  padding: 6px 12px;
  background: #223c68;
  color: var(--ink);
  font-weight: 700;
}

.optica-shell .optica-menu-trigger::-webkit-details-marker {
  display: none;
}

.optica-shell .optica-menu[open] .optica-menu-trigger {
  outline: 2px solid var(--accent);
}

.optica-shell .optica-menu-panel {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  min-width: 220px;
  background: #152746;
  border: 1px solid #2f4d7d;
  border-radius: 12px;
  padding: 10px;
  z-index: 10;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
}

.optica-shell .optica-menu-title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink2);
  margin-bottom: 8px;
}

.optica-shell .optica-menu-list {
  display: grid;
  gap: 6px;
}

.optica-shell .optica-menu-list button {
  text-align: left;
  width: 100%;
}

.optica-shell button,
.optica-shell select,
.optica-shell input[type="file"],
.optica-shell input[type="number"],
.optica-shell input[type="text"],
.optica-shell input[type="range"] {
  border: 1px solid #2f4d7d;
  border-radius: 6px;
  padding: 4px 8px;
  background: #223c68;
  color: var(--ink);
  font-weight: 600;
  font-size: 0.82rem;
}

.optica-shell button.active {
  outline: 2px solid var(--accent);
}

.optica-shell .budget {
  padding: 4px 10px;
  border-radius: 999px;
  background: #223c68;
  font-weight: 700;
  font-size: 0.82rem;
  color: var(--ink);
}

.optica-shell #budget.low { background:#4a5700; }
.optica-shell #budget.zero { background:#5e0f0f; }

.optica-shell #wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 6px;
  padding: 8px;
  background: var(--bg);
  flex: 1;
  min-height: 0;
}

.optica-shell #left {
  display: flex;
  justify-content: center;
  align-items: stretch;
  /* Allow content (like the palette select) to extend beyond the canvas without being clipped */
  overflow: visible;
  min-height: 0;
}

.optica-shell #canvasWrap {
  background: var(--build);
  border-radius: 10px;
  padding: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
  width: 100%;
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.optica-shell .optica-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
  color: var(--ink2);
  background: rgba(12, 26, 51, 0.88);
  border-radius: 12px;
  z-index: 5;
}


.optica-shell canvas {
  display: block;
  border-radius: 8px;
  box-shadow: inset 0 0 0 1px #2f4d7d;
  background: linear-gradient(transparent 23px, var(--grid) 24px),
              linear-gradient(90deg, transparent 23px, var(--grid) 24px);
  background-size: 24px 24px;
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  min-height: 240px;

}

.optica-shell #right {
  background: var(--panel);
  padding: 12px;
  border-left: 1px solid #294471;
  /* Scroll the content of the inspector independently to avoid growing the entire engine height. */
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.optica-shell #right::-webkit-scrollbar {
  width: 0;
  height: 0;
}


.optica-shell h3 { margin: 0.6rem 0; }

/* Detector / checkpoint status list */
.optica-shell .detlist {
  display: grid;
  gap: 6px;
}

.optica-shell .detlist .det-item {
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(34, 60, 104, 0.45);
  border: 1px solid #294471;
}

.optica-shell .detlist .det-title {
  font-weight: 800;
}

.optica-shell .detlist .det-req {
  margin-top: 2px;
  font-size: 0.8rem;
  color: var(--ink2);
  font-weight: 600;
  line-height: 1.2;
}

.optica-shell .detlist .on {
  filter: drop-shadow(0 0 4px rgba(255,255,255,.25));
}

/* Backwards-compatible styling if any legacy spans remain */
.optica-shell .detlist span { display: block; margin: 2px 0; font-weight: 700; }
.optica-shell .legend { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: .9rem; }
.optica-shell .chip { width: 14px; height: 14px; border-radius: 4px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
.optica-shell .pnl { border: 1px solid #294471; border-radius: 10px; padding: 10px; margin-top: 10px; background: #1c3254; }
.optica-shell .row { display: grid; grid-template-columns: 110px 1fr; gap: 8px; align-items: center; margin: 6px 0; }
.optica-shell input[type="range"] { width: 100%; }
.optica-shell .palette {
  /* Switch from flex to block layout so the palette fills the canvas width. */
  display: block;
  width: 100%;
  margin-top: 6px;
}
.optica-shell .palette button { font-size: .9rem; background: #223c68; color: var(--ink); border: 1px solid #2f4d7d; }
.optica-shell .palette button.locked { background: #142549; color: #566b8e; border-style: dashed; }
.optica-shell .palette button.cost::after { content: attr(data-cost); font-weight: 700; margin-left: .4rem; color: var(--ink2); }

/* Palette group styling: wrap groups into collapsible details elements */
.optica-shell .palette-group {
  width: 100%;
  margin-top: 0.5rem;
}

.optica-shell .palette-group summary {
  cursor: pointer;
  /* Use primary ink colour for better contrast and add background for visibility */
  color: var(--ink);
  font-weight: 700;
  padding: 4px 6px;
  background: #223c68;
  border-radius: 4px;
  margin-bottom: 0.25rem;
  list-style: none;
}

.optica-shell .palette-group summary::-webkit-details-marker {
  display: none;
}

.optica-shell .palette-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 0.25rem;
}

/* Reduce button size for palette items */
.optica-shell .palette button {
  font-size: 0.75rem;
  padding: 4px 8px;
  line-height: 1.2;
}

/* Highlight active palette button */
.optica-shell .palette-buttons button.active {
  outline: 2px solid var(--accent);
}

/* Styling for allow-list groups (used for Level Rules toggles). This mirrors
   the palette grouping but uses more compact buttons and typography. */
.optica-shell .allow-group {
  width: 100%;
  margin-top: 0.5rem;
}
.optica-shell .allow-group summary {
  cursor: pointer;
  background: #223c68;
  color: var(--ink);
  font-weight: 700;
  padding: 4px 6px;
  border-radius: 4px;
  margin-bottom: 0.25rem;
  list-style: none;
}
.optica-shell .allow-group summary::-webkit-details-marker {
  display: none;
}
.optica-shell .allow-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 0.25rem;
}
/* Base styling for allowed-list buttons */
.optica-shell #allowList button {
  font-size: 0.7rem;
  padding: 4px 8px;
  line-height: 1.2;
  background: #223c68;
  color: var(--ink);
  border: 1px solid #2f4d7d;
  border-radius: 6px;
  cursor: pointer;
}
/* Style for locked (disallowed) items */
.optica-shell #allowList button.locked {
  background: #142549;
  color: #566b8e;
  border-style: dashed;
}

/* Styling for collapsible inspector sections on the right panel */
.optica-shell #right details {
  border: 1px solid #294471;
  border-radius: 10px;
  margin-bottom: 0.5rem;
  overflow: hidden;
}

.optica-shell #right details summary {
  cursor: pointer;
  background: #223c68;
  color: var(--ink);
  padding: 6px 10px;
  font-weight: 700;
  list-style: none;
}

.optica-shell #right details summary::-webkit-details-marker {
  display: none;
}

.optica-shell #right details > div {
  /* Give interior panel content a bit of padding */
  padding: 8px 10px;
}

/* Style for the compact palette select used in place of the full palette */
.optica-shell .palette-select {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  padding: 6px 8px;
  border: 1px solid #2f4d7d;
  border-radius: 8px;
  background: #223c68;
  color: var(--ink);
  font-size: 0.85rem;
  margin-bottom: 6px;
}

.optica-shell .palette-select optgroup {
  color: var(--ink2);
  font-weight: 700;
}

.optica-shell .palette-select option {
  color: var(--ink);
}
.optica-shell .note { font-size: .85rem; color: var(--ink2); }
.optica-shell #statusMsg { font-weight: 700; margin-top: 6px; padding: 4px; border-radius: 6px; text-align: center; color: var(--ink); }
.optica-shell #statusMsg.win { color: var(--ok); }
.optica-shell #statusMsg.fail { color: var(--bad); }

.optica-shell .submit-solution-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  border: 1px solid var(--ok);
  background: var(--ok);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.optica-shell .submit-solution-btn:hover {
  background: #249a42;
  border-color: #249a42;
}

/* Stats bar (timer + moves) */
.optica-shell .optica-stats-bar {
  display: flex;
  gap: 0.6rem;
  align-items: center;
}
.optica-shell .optica-stat {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(34, 60, 104, 0.65);
  border: 1px solid #2f4d7d;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.optica-shell .optica-stat-icon {
  font-size: 0.8rem;
  opacity: 0.7;
}

/* Level completion overlay */
.optica-complete-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(12, 26, 51, 0.92);
  border-radius: 12px;
  z-index: 20;
  animation: overlayFadeIn 0.3s ease;
}
@keyframes overlayFadeIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}
.completion-card {
  background: linear-gradient(135deg, #1a3460, #0f2340);
  border: 1px solid #2f5aa0;
  border-radius: 1.25rem;
  padding: 2rem 2.5rem;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  max-width: 680px;
  width: 90%;
}
.completion-stars {
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
  letter-spacing: 0.3rem;
}
.completion-star.earned {
  color: #f2d86b;
  text-shadow: 0 0 12px rgba(242, 216, 107, 0.5);
}
.completion-star.empty {
  color: #3a5070;
}
.completion-title {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--optica-ok);
  margin-bottom: 0.25rem;
}
.completion-level-name {
  font-size: 1rem;
  color: var(--optica-ink2);
  margin-bottom: 1.25rem;
}
.completion-stats {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.completion-stat {
  background: rgba(34, 60, 104, 0.5);
  border: 1px solid #294471;
  border-radius: 0.75rem;
  padding: 0.6rem 1rem;
  min-width: 65px;
}
.completion-stat-value {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--optica-ink);
}
.completion-stat-label {
  font-size: 0.7rem;
  color: var(--optica-ink2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 0.15rem;
}
.completion-best {
  font-size: 0.8rem;
  color: var(--optica-ink2);
  margin-bottom: 1rem;
}
.completion-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
}
.completion-btn {
  padding: 0.6rem 1.4rem;
  border-radius: 0.75rem;
  border: 1px solid #2f5aa0;
  background: #223c68;
  color: var(--optica-ink);
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  transition: 0.2s ease;
}
.completion-btn:hover {
  border-color: var(--optica-accent);
  background: #2a4a80;
}
.completion-btn-primary {
  background: var(--optica-accent);
  color: #0c1a33;
  border-color: var(--optica-accent);
}
.completion-btn-primary:hover {
  background: #7fa0e0;
}

/* Solution comparison columns in submit overlay */
.compare-columns {
  display: flex;
  gap: 1rem;
  margin: 1rem 0;
  text-align: left;
}
.compare-col {
  flex: 1;
  background: rgba(34, 60, 104, 0.4);
  border: 1px solid #294471;
  border-radius: 0.75rem;
  padding: 0.75rem;
  min-width: 0;
}
.compare-col-title {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--optica-accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.5rem;
  text-align: center;
}
.compare-item {
  display: flex;
  justify-content: space-between;
  padding: 2px 0;
  font-size: 0.85rem;
}
.compare-type {
  color: var(--optica-ink);
}
.compare-count {
  color: var(--optica-ink2);
  font-weight: 700;
}
.compare-details {
  margin-top: 0.5rem;
  border-top: 1px solid #294471;
  padding-top: 0.4rem;
  max-height: 120px;
  overflow-y: auto;
}
.compare-detail {
  font-size: 0.72rem;
  color: var(--optica-ink2);
  padding: 1px 0;
  font-family: monospace;
}
.compare-empty {
  font-size: 0.85rem;
  color: var(--optica-ink2);
  font-style: italic;
  text-align: center;
  padding: 0.5rem 0;
}

.optica-shell .optica-hidden { display: none !important; }

@media (max-width: 1100px) {
  .optica-shell #wrap {
    grid-template-columns: 1fr;
  }

  .optica-shell #right {
    border-left: none;
    border-top: 1px solid #294471;
  }
}

/* Hide inspector scrollbar without disabling scrolling */
.optica-shell #right { scrollbar-width: none; }
.optica-shell #right::-webkit-scrollbar { width: 0; height: 0; }
