/* Melvin demo styles.  Light/dark themes via CSS custom properties on
   html[data-theme]; layout is a fixed navbar + filebar over a two-pane
   splitter (editor | tabbed output). */

:root, html[data-theme="light"] {
  --bg: #ffffff;
  --bg-soft: #f5f6f8;
  --bg-hover: #e9ebef;
  --fg: #1c1e21;
  --fg-dim: #5f6673;
  --border: #d7dae0;
  --accent: #2f6fed;
  --accent-fg: #ffffff;
  --ok: #1a7f37;
  --ok-bg: #e6f4ea;
  --bad: #c62828;
  --bad-bg: #fdecea;
  --warn: #9a6700;
  --warn-bg: #fff8e1;
  --navbar-bg: #23272e;
  --navbar-fg: #f0f2f5;
  /* floating popups and heap-diagram boxes */
  --panel-bg: #ffffff;
  /* per-thread trace colors */
  --tid-1: #1f5fd0;
  --tid-2: #b3261e;
  --tid-3: #1a7f37;
  --tid-4: #8033b0;
  --tid-5: #0e7d80;
  --tid-6: #a35a00;
}

html[data-theme="dark"] {
  --bg: #1e2227;
  --bg-soft: #23282e;
  --bg-hover: #2d333b;
  --fg: #e6e8ea;
  --fg-dim: #9aa4b2;
  --border: #3a4048;
  --accent: #4d8dff;
  --accent-fg: #ffffff;
  --ok: #4caf7d;
  --ok-bg: #16301f;
  --bad: #ef6a6a;
  --bad-bg: #3a1f1f;
  --warn: #d9a941;
  --warn-bg: #33290f;
  --navbar-bg: #15181c;
  --navbar-fg: #f0f2f5;
  /* floating popups and heap-diagram boxes */
  --panel-bg: #2d333b;
  /* per-thread trace colors (brighter on dark) */
  --tid-1: #6ea8ff;
  --tid-2: #ff7b72;
  --tid-3: #56d364;
  --tid-4: #d2a8ff;
  --tid-5: #4dd0d0;
  --tid-6: #e3b341;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--fg);
}

body { display: flex; flex-direction: column; overflow: hidden; }

/* ------------------------------------------------------------- navbar */

.navbar {
  display: flex; align-items: center; gap: 12px;
  background: var(--navbar-bg); color: var(--navbar-fg);
  padding: 6px 14px; flex: none;
}
.brand { display: flex; align-items: center; gap: 9px; }
.brand img { height: 30px; width: 30px; border-radius: 6px; }
.brand-name { font-size: 17px; font-weight: 650; letter-spacing: .2px; }
.brand-sub { color: #aab3c0; font-size: 12.5px; margin-left: 2px; }

.menus { display: flex; align-items: center; gap: 2px; margin-left: 10px; }
.menu { position: relative; }
.menu-btn {
  background: none; border: none; color: var(--navbar-fg);
  font: inherit; padding: 7px 10px; cursor: pointer; border-radius: 6px;
}
.menu-btn:hover, .menu.open .menu-btn { background: rgba(255,255,255,.12); }
.menu-list {
  display: none; position: absolute; top: 100%; left: 0; z-index: 50;
  min-width: 260px; max-height: 70vh; overflow-y: auto;
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
  padding: 6px;
}
.menu.open .menu-list { display: block; }
.menu-list a, .menu-item {
  display: block; width: 100%; text-align: left; text-decoration: none;
  background: none; border: none; font: inherit; color: var(--fg);
  padding: 7px 10px; border-radius: 6px; cursor: pointer;
}
.menu-list a:hover, .menu-item:hover { background: var(--bg-hover); }
.menu-list hr { border: none; border-top: 1px solid var(--border); margin: 6px 4px; }
.menu-group {
  font-size: 11px; text-transform: uppercase; letter-spacing: .6px;
  color: var(--fg-dim); padding: 8px 10px 3px;
}
.menu-example small { display: block; color: var(--fg-dim); font-size: 12px; }
.menu-loading { display: block; padding: 8px 10px; color: var(--fg-dim); }

/* Examples menu: one flyout submenu per category (opens on hover or click) */
#examples-menu { overflow: visible; max-height: none; min-width: 250px; }
.submenu { position: relative; }
.submenu-head { position: relative; cursor: pointer; padding-right: 24px; }
.submenu-head::after { content: "\25B8"; position: absolute; right: 10px;
  top: 7px; color: var(--fg-dim); }
.submenu-head small { display: block; color: var(--fg-dim); font-size: 12px; }
.submenu-list {
  display: none; position: absolute; left: 100%; top: -7px; z-index: 60;
  min-width: 320px; max-width: 400px;
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.25); padding: 6px;
}
.submenu:hover > .submenu-list, .submenu.open > .submenu-list { display: block; }
.submenu:hover > .submenu-head, .submenu.open > .submenu-head { background: var(--bg-hover); }

.toolbar { margin-left: auto; display: flex; gap: 8px; }

/* ------------------------------------------------------------- buttons */

.btn {
  font: inherit; padding: 6px 14px; border-radius: 7px; cursor: pointer;
  border: 1px solid var(--border); background: var(--bg-soft); color: var(--fg);
}
.navbar .btn { border-color: rgba(255,255,255,.25); background: rgba(255,255,255,.08); color: var(--navbar-fg); }
.navbar .btn:hover { background: rgba(255,255,255,.16); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.1); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.small { padding: 3px 10px; font-size: 12.5px; }

/* ------------------------------------------------------------- filebar */

.filebar {
  display: flex; align-items: center; gap: 10px; flex: none;
  padding: 5px 14px; background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
#file-name { font-weight: 600; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.modified-dot { color: var(--warn); font-size: 12px; }
.filebar-spacer { flex: 1; }

.status { display: flex; align-items: center; gap: 8px; color: var(--fg-dim); }
.status .spinner {
  width: 13px; height: 13px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.badge {
  display: inline-block; padding: 1px 10px; border-radius: 999px;
  font-size: 12.5px; font-weight: 600;
}
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.bad { color: var(--bad); background: var(--bad-bg); }
.badge.warn { color: var(--warn); background: var(--warn-bg); }

/* ------------------------------------------------------------- layout */

#main { flex: 1; display: flex; min-height: 0; }
#editor-pane { flex: 1 1 58%; min-width: 200px; display: flex; min-height: 0; }
#divider { flex: none; width: 5px; cursor: col-resize; background: var(--border); }
#divider:hover { background: var(--accent); }
#output-pane {
  flex: 1 1 42%; min-width: 220px; display: flex; flex-direction: column;
  min-height: 0; background: var(--bg-soft);
}

.CodeMirror { flex: 1; height: 100%; font-size: 15px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

/* ------------------------------------------------------------- tabs */

.tabs { display: flex; gap: 2px; padding: 6px 8px 0; border-bottom: 1px solid var(--border); flex: none; }
.tab {
  font: inherit; border: none; background: none; color: var(--fg-dim);
  padding: 7px 14px; cursor: pointer;
  border-radius: 7px 7px 0 0; border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--fg); }
.tab.active { color: var(--fg); font-weight: 600; border-bottom-color: var(--accent); }

.tab-panel { display: none; flex: 1; overflow: auto; padding: 12px 14px; min-height: 0; }
.tab-panel.active { display: block; }

.placeholder { color: var(--fg-dim); max-width: 46em; }

.panel-actions { margin-bottom: 8px; }
.code-block {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px; white-space: pre; margin: 0;
  color: var(--fg);
}

/* ------------------------------------------------------------- results */

.result-banner {
  padding: 10px 14px; border-radius: 8px; margin-bottom: 12px;
  font-weight: 600;
}
.result-banner.ok { background: var(--ok-bg); color: var(--ok); }
.result-banner.bad { background: var(--bad-bg); color: var(--bad); }
.result-banner.warn { background: var(--warn-bg); color: var(--warn); }
.result-banner small { font-weight: 400; display: block; margin-top: 2px; }

.diag-list { list-style: none; margin: 0; padding: 0; }
.diag {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  margin-bottom: 8px; cursor: pointer; background: var(--bg);
}
.diag:hover { border-color: var(--accent); }
.diag .diag-loc {
  font-family: ui-monospace, Menlo, monospace; font-size: 12px;
  color: var(--accent); font-weight: 600;
}
.diag .diag-msg { margin-top: 2px; }

.trace-list { list-style: none; margin: 8px 0 0; padding: 0;
  font-family: ui-monospace, Menlo, monospace; font-size: 13px; }
.trace-list li { padding: 2px 8px; border-left: 3px solid var(--border);
  white-space: pre; }    /* no wrapping: long lines scroll horizontally */
/* each thread's steps in their own color (palette cycles for tid > 6) */
li.trace-tid-1 { color: var(--tid-1); }
li.trace-tid-2 { color: var(--tid-2); }
li.trace-tid-3 { color: var(--tid-3); }
li.trace-tid-4 { color: var(--tid-4); }
li.trace-tid-5 { color: var(--tid-5); }
li.trace-tid-6 { color: var(--tid-6); }
.trace-bad .trace-list li:last-child { border-left-color: var(--bad); font-weight: 600; }

/* -------------------------------------------------- editor annotations */

.mml-squiggle {
  text-decoration: underline wavy var(--bad);
  text-decoration-skip-ink: none;
}
.gutter-error { color: var(--bad); font-weight: 700; cursor: pointer; }
.melvin-errors { width: 14px; }
.melvin-movers { width: 20px; }

/* Vertically center gutter markers (mover chips, error dots) in their line:
   CodeMirror positions marker elements at the line top with content height,
   so stretch the wrapper + marker over the full line and flex-center. */
.CodeMirror-gutter-wrapper { top: 0; bottom: 0; }
.CodeMirror-gutter-wrapper .CodeMirror-gutter-elt:not(.CodeMirror-linenumber) {
  top: 0; bottom: 0;
  display: flex; align-items: center; justify-content: center;
}

.mover-chip {
  width: 16px; height: 16px;
  border-radius: 4px; text-align: center;
  font: 700 11px/16px ui-monospace, Menlo, monospace;
  color: #fff; cursor: default;
}
.mover-B { background: #3b78c4; }   /* both-mover   */
.mover-R { background: #2e9e56; }   /* right-mover  */
.mover-L { background: #e08a1a; }   /* left-mover   */
.mover-N { background: #8451c9; }   /* non-mover    */
.mover-Y { background: #8a939e; }   /* yield        */
.mover-E { background: #c62828; }   /* no mover applies */

/* ------------------------------------------------------------- narrow */

@media (max-width: 800px) {
  #main { flex-direction: column; }
  #divider { width: auto; height: 5px; cursor: row-resize; }
  .brand-sub, kbd { display: none; }
}

/* ---- feature additions: hover popups, snapshots, trace scrubber, finals */


.mover-popup { position: fixed; z-index: 1000; max-width: 440px;
  background: var(--panel-bg, #fff); border: 1px solid rgba(128,128,128,.45);
  border-radius: 6px; box-shadow: 0 4px 18px rgba(0,0,0,.25);
  padding: 8px 10px; font-size: 12px; line-height: 1.45; }
.mover-popup-head { font-weight: 700; margin-bottom: 4px; }
.mover-popup-action { font-family: monospace; margin-bottom: 4px; }
.mover-popup-clauses { margin: 4px 0; padding-left: 16px; font-family: monospace; }
.mover-popup-clauses li.clause-matches { color: #2e7d32; }
.mover-popup-clauses li.clause-ruled-out { color: #b71c1c; text-decoration: line-through; }
.mover-popup-clauses li.clause-possible { color: #8a6d00; }
.mover-popup-note { opacity: .75; font-style: italic; }
.mover-popup-storelabel { margin-top: 6px; opacity: .75; }

.snapshot { margin: 6px 0; }
.snap-title { font-weight: 600; font-size: 12px; margin-bottom: 3px; }
.snap-scalars { display: flex; gap: 12px; flex-wrap: wrap; }
.snap-table table { border-collapse: collapse; font-family: monospace; font-size: 12px; }
.snap-table-title { font-size: 11px; opacity: .7; margin-bottom: 2px; }
.snap-table td { border: 1px solid rgba(128,128,128,.35); padding: 1px 8px; }
.snap-k { opacity: .8; }
.snap-v { font-weight: 600; }
.snap-empty { opacity: .6; font-size: 12px; }
.snap-heap { display: block; margin-top: 6px; }
.snap-box { fill: var(--panel-bg, #fff); stroke: rgba(100,100,100,.8); }
.snap-box-title-bg { fill: rgba(100,140,220,.25); stroke: rgba(100,100,100,.8); }
.snap-box-title { font: 700 11px monospace; fill: currentColor; }
.snap-field { font: 11px monospace; fill: currentColor; }
.snap-edge { fill: none; stroke: rgba(100,100,100,.9); stroke-width: 1.2; }
.snap-arrowhead { fill: rgba(100,100,100,.9); }
.snap-edge-label { font: 10px monospace; fill: currentColor; opacity: .75; }
.snap-scope { stroke-dasharray: 4 3; }
.snap-scope-title-bg { fill: rgba(120,120,140,.18); }
/* a thread's call stack: innermost frame on top, drawn as stacked cards */
.snap-stack { display: flex; flex-direction: column; gap: 0; }
.snap-stack .snap-table { border: 1px solid var(--border); border-radius: 4px;
  padding: 2px 6px 4px; margin: 0; }
.snap-stack .snap-table + .snap-table { margin-top: -1px; border-top-left-radius: 0;
  border-top-right-radius: 0; }
.snap-stack .snap-table:first-child { background: rgba(120,120,220,.07); }

.trace-wrap { display: flex; gap: 14px; align-items: flex-start; }
/* the step list is user-resizable: drag the grip in its bottom-right corner */
.trace-wrap .trace-list { flex: none; width: 55%; min-width: 180px;
  max-width: 95%; height: 340px; max-height: none; overflow: auto;
  resize: both; border: 1px solid var(--border); border-radius: 4px; }
.trace-snap { flex: 1 1 auto; min-width: 0; position: sticky; top: 0; }
li.trace-step { cursor: pointer; border-radius: 3px; padding: 0 4px; }
li.trace-call, li.trace-return { font-style: italic; opacity: .9; }
li.trace-step:hover { background: rgba(120,120,220,.12); }
li.trace-step.selected { background: rgba(120,120,220,.25); }

.finals-head { font-weight: 600; margin-top: 12px; }
.finals-list { display: flex; flex-wrap: wrap; gap: 16px; }
.final-cell { display: flex; flex-direction: column; gap: 4px; }
.final-trace summary { cursor: pointer; font-size: 12px; opacity: .75; }
.final-trace[open] summary { opacity: 1; }
.final-trace .trace-wrap { margin-top: 6px; }
.diag-cex { margin-top: 4px; }
.diag-cex-label { font-size: 11px; opacity: .7; }
.diag-cex table { border-collapse: collapse; font-family: monospace; font-size: 12px; }
.diag-cex td { border: 1px solid rgba(128,128,128,.35); padding: 1px 8px; }

/* ------------------------------------------------------------- tour */

/* While the tour is active, let the app's own popups escape the dimming
   overlay (driver.js draws it at z-index 10000). */
.driver-active .mover-popup { z-index: 10001; }
.driver-active .menu.open { z-index: 10001; }

/* driver.js puts overflow:hidden!important on the active element's parent,
   which would clip a spotlighted dropdown to its button-sized .menu wrapper;
   undo it (this selector out-specifies driver's :not(body):has(...) rule). */
.menu:has(> .driver-active-element) { overflow: visible !important; }

/* driver.js popover, dark theme (defaults are white-on-light) */
html[data-theme="dark"] .driver-popover {
  background-color: var(--bg-soft); color: var(--fg);
}
html[data-theme="dark"] .driver-popover-title { color: var(--fg); }
html[data-theme="dark"] .driver-popover-description,
html[data-theme="dark"] .driver-popover-progress-text { color: var(--fg-dim); }
html[data-theme="dark"] .driver-popover-close-btn { color: var(--fg-dim); }
html[data-theme="dark"] .driver-popover-close-btn:hover,
html[data-theme="dark"] .driver-popover-close-btn:focus { color: var(--fg); }
html[data-theme="dark"] .driver-popover-footer button {
  background-color: var(--bg); color: var(--fg);
  border-color: var(--border); text-shadow: none;
}
html[data-theme="dark"] .driver-popover-footer button:hover,
html[data-theme="dark"] .driver-popover-footer button:focus {
  background-color: var(--bg-hover);
}
/* arrow: recolor the base, re-blank the three non-pointing sides */
html[data-theme="dark"] .driver-popover-arrow { border-color: var(--bg-soft); }
html[data-theme="dark"] .driver-popover-arrow-side-left {
  border-right-color: transparent; border-bottom-color: transparent; border-top-color: transparent; }
html[data-theme="dark"] .driver-popover-arrow-side-right {
  border-left-color: transparent; border-bottom-color: transparent; border-top-color: transparent; }
html[data-theme="dark"] .driver-popover-arrow-side-top {
  border-right-color: transparent; border-bottom-color: transparent; border-left-color: transparent; }
html[data-theme="dark"] .driver-popover-arrow-side-bottom {
  border-left-color: transparent; border-top-color: transparent; border-right-color: transparent; }

.driver-popover code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

/* first-visit "take the tour" toast */
.tour-toast {
  position: fixed; right: 16px; bottom: 16px; z-index: 500;
  max-width: 300px; padding: 12px 14px; font-size: 13.5px;
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.tour-toast-actions { display: flex; gap: 8px; margin-top: 10px; }
