:root { --entual-primary:#AEEC3F; --entual-secondary:#9ED638; --bg:#0f1115; --panel:#0f131b; --text:#e5e7eb; --muted:#1b2130; --header-height:64px; }
body { font-family: 'Montserrat', ui-sans-serif, system-ui, Arial, sans-serif; margin: 0; background: var(--bg); color: var(--text); min-height: 100vh; }
[data-theme="free"], body.free-mode { }
body.free-mode .toolbar #levelPicker { display: none !important; }
[hidden] { display: none !important; }
header { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 12px; align-items: center; padding: 10px 14px; background: #0b0d12; color: #f3f3f3; border-bottom: 1px solid #1f2430; position: sticky; top: 0; z-index: 80; min-height: var(--header-height); }
header .brand { display:flex; align-items:center; gap:10px; flex: 1 1 auto; min-width: 200px; }
header .brand .brand-logo { height:28px; }
header .brand .subtitle { font-size: 16px; margin: 0; font-weight: 600; letter-spacing: .2px; color: #cbd5e1; }
.toolbar { display: flex; flex: 1 1 auto; gap: 10px; align-items: center; justify-content: flex-end; flex-wrap: wrap; }
.toolbar select, .toolbar button { background: #111623; color: var(--text); border: 1px solid var(--entual-secondary); border-radius: 6px; padding: 6px 10px; }
.toolbar button { background: #142229; border-color: var(--entual-secondary); cursor: pointer; }
.toolbar button:hover { background: #19303a; box-shadow: 0 0 0 1px var(--entual-primary) inset; }
.toolbar-toggle { display: none; }
main { display: grid; grid-template-columns: minmax(220px, 260px) minmax(0, 1fr); gap: 12px; padding: 12px 16px 24px; align-items: start; max-width: 1380px; margin: 0 auto; width: 100%; box-sizing: border-box; }
.palette { border-left: 1px solid var(--muted); padding-left: 12px; position: sticky; top: calc(var(--header-height) + 16px); align-self: start; max-height: calc(100vh - var(--header-height) - 32px); overflow-y: auto; }
.palette h3 { margin-top: 0; color: #9fb3c8; }
.palette-groups { display: flex; flex-direction: column; gap: 16px; margin-bottom: 14px; }
.palette-group h4 { margin: 6px 0; font-size: 13px; letter-spacing: 0.5px; text-transform: uppercase; color: #7b8ca1; }
.palette-group .items { margin-top: 4px; }
.palette-close { display: none; margin-bottom: 12px; background: #142229; color: var(--text); border: 1px solid var(--entual-secondary); border-radius: 6px; padding: 6px 10px; cursor: pointer; width: 100%; text-align: center; }
.palette-close:hover { background: #19303a; box-shadow: 0 0 0 1px var(--entual-primary) inset; }
.items button { display: inline-block; margin: 6px 6px 0 0; background: #0e1a21; color: #dbe5f1; border: 1px solid var(--entual-secondary); border-radius: 6px; padding: 6px 8px; cursor: pointer; }
.items button:hover { background: #13232b; box-shadow: 0 0 0 1px var(--entual-primary) inset; }
.workspace { display: grid; grid-template-rows: auto auto auto auto; gap: 12px; align-content: start; }
.canvas-wrap { border: 1px solid var(--muted); background: var(--panel); overflow: auto; position: relative; border-radius: 12px; min-height: 360px; height: clamp(360px, 68vh, 720px); touch-action: none; }
#canvas { background: var(--panel); display: block; width: 100%; height: 100%; }
svg defs + rect.bg-grid { fill: url(#gridPattern); }
svg .node { cursor: move; }
svg .node rect { fill: #15212a; stroke: var(--entual-secondary); stroke-width: 1.5; rx: 8; ry: 8; }
svg .node.selected rect { stroke: #e0af68; stroke-width: 2; }
svg .node text { fill: #cbd5e1; font-weight: 600; pointer-events: none; user-select: none; }
svg .wire { stroke: var(--entual-primary); stroke-width: 3; fill: none; }
svg .wire.active { stroke: var(--entual-secondary); stroke-dasharray: 4 4; }
svg .pin { fill: var(--entual-primary); cursor: crosshair; }
svg .pin.target { fill: #a3be8c; }
svg .pin.source { fill: #7aa2f7; }
details.json textarea#json { width: 100%; box-sizing: border-box; background:#0b0f16; color:#d1d5db; border:1px solid #1f2937; border-radius: 6px; padding: 6px; margin-top: 6px; }
.hint { font-size: 12px; color: #94a3b8; }

/* Tests panel */
.tests { margin-bottom: 4px; }
.testcase { border: 1px solid var(--muted); border-radius: 6px; padding: 8px; margin-bottom: 6px; background: var(--panel); }
.testcase .head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.testcase .inputs, .testcase .expected, .testcase .actual { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: #cbd5e1; }
.badge { padding: 2px 6px; border-radius: 10px; font-size: 11px; }
.badge.pass { background: #1f3d2b; color: #a7f3d0; border: 1px solid #2f6b49; }
.badge.fail { background: #3d1f1f; color: #fecaca; border: 1px solid #6b2f2f; }

/* Compact matrix view for inputs/outputs (TC-like) */
.tests-matrix { border: 1px solid var(--muted); border-radius: 8px; background: var(--panel); padding: 8px; }
.tests-matrix .row { display: flex; align-items: center; gap: 12px; margin: 6px 0; }
.tests-matrix .label { width: 180px; color: #d6b36b; font-weight: 700; }
.tests-matrix .cells { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 4px; }
.tests-matrix .dot { width: 14px; height: 14px; border-radius: 50%; box-shadow: inset 0 0 0 1px #263041; display: inline-block; flex: 0 0 14px; }
.tests-matrix .dot.on { background: #2f9e44; }
.tests-matrix .dot.off { background: #a11d2f; }
.tests-matrix .dot.na { background: #2a3344; box-shadow: inset 0 0 0 1px #3a455a; }
.tests-matrix .dot.mismatch { outline: 2px solid #e11d48; }
.tests-matrix .value {
  min-width: 48px;
  padding: 4px 8px;
  border-radius: 6px;
  background: #111923;
  border: 1px solid #1f2937;
  color: #e5e7eb;
  text-align: center;
  font-family: 'Roboto Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 0 8px rgba(15, 23, 42, 0.35);
}
.tests-matrix .value.mismatch { border-color: #e11d48; color: #fda4af; }
.tests-matrix .value.na { opacity: 0.55; font-style: italic; }
.tests-legend { opacity: 0.8; font-size: 12px; margin-top: 6px; }
.binary-racer .br-info { margin: 0 0 10px; font-size: 14px; color: #cbd5e1; }

/* Modal overlay for narrative */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: flex; align-items: center; justify-content: center; z-index: 1000; }
.modal { width: min(680px, 92vw); background: #0e141c; border: 1px solid #2a354a; border-radius: 10px; box-shadow: 0 10px 28px rgba(0,0,0,0.4); }
.modal-head { padding: 10px 14px; border-bottom: 1px solid #1e2a3c; }
.modal-title { font-weight: 700; color: #b8c7db; }
.modal-body { padding: 12px 14px; color: #d5dde8; white-space: pre-wrap; line-height: 1.45; }
.narrative-content { display: flex; gap: 14px; align-items: flex-start; }
.narrative-content img { width: 74px; height: 74px; border-radius: 50%; object-fit: cover; background: #111923; border: 1px solid #2a354a; flex-shrink: 0; box-shadow: 0 4px 12px rgba(0,0,0,0.25); }
.narrative-text { flex: 1; white-space: pre-wrap; color: #d5dde8; line-height: 1.45; }
.narrative-text strong { color: #facc15; }
@media (max-width: 520px) {
  .narrative-content { flex-direction: column; align-items: center; text-align: center; }
  .narrative-content img { width: 68px; height: 68px; }
  .narrative-text { text-align: center; }
}
.modal-actions { padding: 10px 14px; display: flex; justify-content: flex-end; border-top: 1px solid #1e2a3c; }
.modal-actions button { background:#142229; color:var(--text); border:1px solid var(--entual-secondary); border-radius:6px; padding:6px 10px; cursor:pointer; }
.modal-actions button:hover { background:#19303a; box-shadow: 0 0 0 1px var(--entual-primary) inset; }

/* Task panel */
.task { border: 1px solid var(--muted); border-radius: 8px; padding: 8px; background: var(--panel); }
.task h3 { margin: 0 0 6px; color: #9fb3c8; }
.task .content { white-space: pre-wrap; color: #cbd5e1; font-size: 14px; line-height: 1.4; }
.task .hintline { margin-top: 6px; }
.task .hintline button { background:#142229; color:var(--text); border:1px solid var(--entual-secondary); border-radius:6px; padding:4px 8px; cursor:pointer; }
.task .hintline button:hover { background:#19303a; box-shadow: 0 0 0 1px var(--entual-primary) inset; }
.hint-content { margin-top: 6px; border-left: 2px solid #2f3f5c; padding-left: 8px; color:#cbd5e1; white-space: pre-wrap; }

/* Intro modal removed */

/* --- TC-like visual overrides --- */
svg defs + rect.bg-grid { fill: url(#wavePattern) !important; }
svg .node rect { fill: #15212a; stroke: var(--entual-secondary); stroke-width: 1.5; rx: 8; ry: 8; }
svg .wire { stroke: var(--entual-primary); stroke-width: 4.5; stroke-linecap: round; fill: none; }
svg .wire.active { stroke: var(--entual-secondary); stroke-dasharray: 6 6; }
svg .endpoint { fill: var(--panel); stroke: var(--entual-primary); stroke-width: 2; pointer-events: none; }
svg .pin { fill: var(--entual-primary); cursor: crosshair; }
.snap-indicator { fill: none; stroke: #a3be8c; stroke-width: 2; stroke-dasharray: 3 3; }
.items { display: flex; flex-direction: column; gap: 6px; }
.items button { text-align: left; display: block; padding: 8px 10px; }
.items button:disabled { opacity: 0.4; cursor: not-allowed; }

.palette-backdrop { position: fixed; inset: 0; background: rgba(9,12,18,0.62); backdrop-filter: blur(2px); z-index: 85; display: none; }

body.palette-open { overflow: hidden; }
body.palette-open .palette-backdrop { display: block; }

@media (max-width: 1220px) {
  main { padding: 12px; grid-template-columns: minmax(220px, 240px) minmax(0, 1fr); gap: 10px; }
  .canvas-wrap { height: clamp(340px, 64vh, 680px); }
}

@media (max-width: 1024px) {
  header { position: sticky; }
  .toolbar { justify-content: flex-start; }
  .toolbar-toggle { display: inline-flex; align-items: center; gap: 6px; }
  main { grid-template-columns: minmax(0, 1fr); padding: 12px 14px 24px; }
  .palette {
    position: fixed;
    top: var(--header-height);
    bottom: 0;
    left: 0;
    width: min(320px, 82vw);
    max-height: none;
    transform: translateX(-110%);
    transition: transform .2s ease-in-out;
    padding: 20px 18px;
    border-left: none;
    border-right: 1px solid var(--muted);
    box-shadow: 0 14px 32px rgba(0,0,0,0.45);
    z-index: 120;
    background: var(--panel);
    overflow-y: auto;
  }
  .palette-close { display: block; }
  body.palette-open .palette { transform: translateX(0); }
  .workspace { grid-template-rows: auto auto auto auto; gap: 14px; }
  .canvas-wrap { min-height: 320px; height: clamp(320px, 60vh, 600px); }
}

@media (max-width: 680px) {
  header { gap: 10px; }
  header .brand { justify-content: space-between; }
  .toolbar { gap: 8px; }
  #levelPicker { width: 100%; }
  .toolbar button, .toolbar select { flex: 1 1 auto; min-width: 140px; }
  .canvas-wrap { height: clamp(300px, 58vh, 520px); }
  .tests, .task, details.json { border-radius: 10px; }
}

@media (max-width: 480px) {
  header { padding: 10px; }
  .toolbar button, .toolbar select { min-width: 0; width: 100%; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar-toggle { width: 100%; justify-content: center; }
  .canvas-wrap { height: 420px; }
  .tests-matrix .label { width: 140px; }
}
