/* B-tree demo layout. Colours and controls come from ../shared/theme.css. */

html,
body { height: 100%; overflow: hidden; }

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 18px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 2;
}

.home {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
  padding: 4px 8px 4px 2px;
  border-radius: 6px;
}
.home:hover { color: var(--ink); background: var(--panel-2); }

.title {
  margin: 0 6px 0 0;
  font-size: 20px;
  font-weight: 750;
  letter-spacing: -0.01em;
}

.group { display: flex; align-items: center; gap: 6px; }
.field { gap: 8px; font-size: 14px; font-weight: 600; color: var(--ink-2); }
.key-input { width: 230px; font-size: 15px; }
.key-input.flash { animation: flash 0.6s; }
.count { width: 64px; }
.tools { display: flex; gap: 2px; margin-left: auto; }

@keyframes flash {
  0%, 100% { box-shadow: 0 0 0 0 transparent; }
  30% { box-shadow: 0 0 0 4px var(--accent-soft); border-color: var(--accent); }
}

/* ---------- Main area ---------- */

.main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  min-height: 0;
}
body.no-sidebar .main { grid-template-columns: minmax(0, 1fr) 0; }
body.no-sidebar .sidebar { display: none; }

.stage {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
}

.canvas-wrap {
  --watermark-bottom: 136px; /* sit above the key tray and drop zones */
  position: relative;
  overflow: hidden;
  background: var(--bg);
  min-height: 260px;
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.canvas-wrap.drop-insert::after {
  content: "";
  position: absolute;
  inset: 6px;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: color-mix(in srgb, var(--accent) 5%, transparent);
  pointer-events: none;
}

/* ---------- Caption (current step explanation) ---------- */

.caption {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 64px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 10px 14px 10px 16px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(6px);
  box-shadow: var(--shadow);
  pointer-events: none;
  transition: border-color 0.25s;
}
.caption[data-status="success"] { border-left-color: var(--good); }
.caption[data-status="warn"] { border-left-color: var(--warn); }
.caption[data-status="error"] { border-left-color: var(--bad); }

.caption-text {
  flex: 1 1 380px;
  margin: 0;
  font-size: 17px;
  font-weight: 550;
  line-height: 1.4;
}

.rules { display: flex; flex-wrap: wrap; gap: 6px; }
.rules span {
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ink-2);
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.rules span b { color: var(--ink); }

.toast {
  position: absolute;
  left: 50%;
  bottom: 104px;
  max-width: min(520px, calc(100% - 32px));
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--panel);
  font-size: 14px;
  font-weight: 550;
  box-shadow: var(--shadow);
  transform: translate(-50%, 8px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 5;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- View controls ---------- */

.view-ctrls {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.view-ctrls .btn { box-shadow: var(--shadow-sm); }

/* ---------- Bottom overlay: key tray + drop zones ---------- */

.bottom {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: flex;
  align-items: flex-end;
  gap: 12px;
  pointer-events: none;
}
.bottom > * { pointer-events: auto; }

.tray {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px 8px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  backdrop-filter: blur(6px);
  box-shadow: var(--shadow);
}

.tray-label {
  display: flex;
  flex-direction: column;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}
.tray-label span { font-size: 11.5px; font-weight: 500; color: var(--muted); }

.tray-chips {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 84px;
  overflow-y: auto;
  padding: 2px;
}

.tray-empty { color: var(--muted); font-size: 13px; padding: 8px 0; }

.chip {
  min-width: 42px;
  height: 36px;
  padding: 0 9px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--key-bg);
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: grab;
  user-select: none;
  touch-action: none;
  transition: transform 0.12s, border-color 0.12s, opacity 0.12s;
}
.chip:hover { border-color: var(--accent); transform: translateY(-2px); }
.chip.lifted { opacity: 0.3; transform: none; }

.tray-actions { display: flex; align-items: center; gap: 4px; }
.tray-actions .input { width: 76px; height: 32px; }
.tray-actions .btn { height: 32px; width: 32px; }

.zones { display: flex; gap: 10px; flex: none; }

.zone {
  width: 92px;
  height: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: 2px dashed var(--line-strong);
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  backdrop-filter: blur(6px);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  user-select: none;
  transition: transform 0.15s, background 0.15s, border-color 0.15s, color 0.15s;
}
.zone .icon { width: 22px; height: 22px; }
body.dragging .zone-search { color: var(--warn); border-color: var(--warn); transform: translateY(-4px); }
body.dragging .zone-delete { color: var(--bad); border-color: var(--bad); transform: translateY(-4px); }
.zone-search.hover { background: var(--warn-soft); border-style: solid; transform: translateY(-4px) scale(1.08) !important; }
.zone-delete.hover { background: var(--bad-soft); border-style: solid; transform: translateY(-4px) scale(1.08) !important; }

/* Floating chip that follows the pointer while dragging a key. */
.drag-chip {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
  pointer-events: none;
}
.drag-chip .num {
  position: absolute;
  transform: translate(-50%, -50%) scale(1.12);
  min-width: 44px;
  height: 38px;
  padding: 0 10px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  background: var(--accent);
  color: var(--on-strong);
  font-size: 18px;
  font-weight: 750;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.25);
}
.drag-chip.tree .num { background: var(--panel); color: var(--ink); border: 2px solid var(--accent); }
.drag-chip .hint {
  position: absolute;
  top: 28px;
  transform: translateX(-50%);
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--panel);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.drag-chip .hint:empty { display: none; }
.drag-chip[data-action="insert"] .hint { background: var(--accent); color: var(--on-strong); }
.drag-chip[data-action="search"] .hint { background: var(--warn); color: var(--on-strong); }
.drag-chip[data-action="delete"] .hint,
.drag-chip[data-action="discard"] .hint { background: var(--bad); color: var(--on-strong); }

body.dragging,
body.dragging * { cursor: grabbing !important; }

/* ---------- Player ---------- */

.player {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 20px;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.transport { display: flex; align-items: center; gap: 6px; }
.transport .btn { width: 40px; height: 40px; }
.transport .play { width: 48px; height: 48px; border-radius: 50%; }
.transport .play .icon { width: 22px; height: 22px; }

.progress { flex: 1 1 220px; min-width: 180px; }
.progress-label {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 14px;
}
.progress-label span { color: var(--ink-2); font-variant-numeric: tabular-nums; }
.progress-label .queue { color: var(--accent); font-weight: 600; }
.progress-bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
#progressFill { height: 100%; width: 0; background: var(--accent); border-radius: 3px; transition: width 0.3s; }

.speed { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; }
.speed input { width: 110px; accent-color: var(--accent); }
.speed output { width: 40px; font-variant-numeric: tabular-nums; color: var(--ink-2); }

/* ---------- Sidebar ---------- */

.sidebar {
  min-height: 0;
  overflow-y: auto;
  border-left: 1px solid var(--line);
  background: var(--panel);
}

.panel { padding: 14px 16px 16px; border-bottom: 1px solid var(--line); }
.panel h2 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 750;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.panel h2 span { text-transform: none; letter-spacing: 0; color: var(--ink); margin-left: 4px; }

.step-log {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 32vh;
  overflow-y: auto;
  counter-reset: step;
}
.step-log li {
  display: grid;
  grid-template-columns: 22px 1fr;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 13.5px;
  line-height: 1.4;
  color: var(--ink-2);
  cursor: pointer;
}
.step-log li::before {
  counter-increment: step;
  content: counter(step);
  font-weight: 700;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.step-log li:hover { background: var(--panel-2); }
.step-log li.current { background: var(--accent-soft); color: var(--ink); }
.step-log li.current::before { color: var(--accent); }
.step-log li.s-success::before { color: var(--good); }
.step-log li.s-warn::before { color: var(--warn); }
.step-log li.s-error::before { color: var(--bad); }

.pseudo {
  margin: 0;
  padding: 8px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  font: 12.5px/1.6 var(--mono);
}
.pseudo .ln {
  padding: 1px 10px 1px calc(9px + var(--indent, 0ch) + 2ch);
  text-indent: -2ch;
  border-left: 3px solid transparent;
  color: var(--ink-2);
}
.pseudo .ln.on { background: var(--accent-soft); border-left-color: var(--accent); color: var(--ink); font-weight: 650; }
.pseudo .ln .cm { color: var(--muted); font-weight: 400; }
.pseudo .empty { margin: 0; padding: 4px 12px; font-family: var(--font); font-size: 13px; color: var(--muted); }

.formula {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin-bottom: 14px;
  font-size: 13.5px;
}
.formula dt { color: var(--ink-2); }
.formula dd { margin: 0; font-family: var(--mono); font-size: 12.5px; text-align: right; }
.formula dd b { font-family: var(--font); font-size: 14px; color: var(--accent); }

.setting { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; margin-top: 12px; }
.setting-label { font-size: 13.5px; font-weight: 600; }
.setting-label small { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }

.legend {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px 10px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.legend li { display: flex; align-items: center; gap: 7px; }
.sw { width: 20px; height: 16px; border-radius: 4px; flex: none; }
.sw-compare { background: var(--warn-soft); border: 2px solid var(--warn); }
.sw-new { background: var(--accent); }
.sw-found { background: var(--good); }
.sw-median { background: var(--violet); }
.sw-delete { background: var(--bad); }
.sw-pred { background: var(--teal); }
.sw-sep { background: var(--orange); }
.sw-visit { border: 2.5px solid var(--accent); }
.sw-flow { background: var(--bad-soft); border: 2.5px solid var(--bad); }
.sw-sib { border: 2.5px solid var(--teal); }

/* ---------- Help modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(10, 14, 22, 0.45);
}
.modal-card {
  position: relative;
  width: min(880px, 100%);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 24px 28px;
  border-radius: 14px;
  background: var(--panel);
  box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 12px; font-size: 20px; }
.modal-card h3 { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.modal-card ul { margin: 0; padding-left: 18px; }
.modal-card li { margin: 4px 0; font-size: 14px; }
.modal-card code { font-family: var(--mono); font-size: 13px; background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }
.modal-close { position: absolute; top: 14px; right: 14px; }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 32px; }
.credit { margin: 20px 0 0; padding-top: 14px; border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.credit b { color: var(--ink-2); }
.keys { border-collapse: collapse; font-size: 14px; }
.keys td { padding: 4px 12px 4px 0; }
.keys td:first-child { white-space: nowrap; }

/* ---------- Smaller screens ---------- */

@media (max-width: 1100px) {
  .main { grid-template-columns: minmax(0, 1fr) 300px; }
  .key-input { width: 180px; }
}

@media (max-width: 860px) {
  html, body { overflow: auto; }
  .app { height: auto; min-height: 100vh; }
  .main { grid-template-columns: 1fr; }
  body.no-sidebar .main { grid-template-columns: 1fr; }
  .canvas-wrap { height: 72vh; }
  .sidebar { border-left: 0; border-top: 1px solid var(--line); }
  .help-grid { grid-template-columns: 1fr; }
  .tray-label { display: none; }
  .zone { width: 70px; height: 56px; font-size: 12px; }
  .caption { right: 60px; }
  .caption-text { font-size: 15px; flex-basis: 100%; }
  .key-input { flex: 1; width: auto; }
  .ops { flex: 1 1 100%; }
}
.sw-link { background: linear-gradient(transparent 45%, var(--teal) 45%, var(--teal) 60%, transparent 60%); }

/* ---------- Disk view: node anatomy panel ---------- */

.anatomy p { margin: 6px 0; font-size: 13px; color: var(--ink-2); line-height: 1.45; }
.anatomy p.small { font-size: 12px; color: var(--muted); }
.layout-row { display: flex; gap: 3px; margin: 8px 0 4px; font: 700 12px var(--mono); }
.layout-row span { padding: 4px 7px; border-radius: 5px; }
.layout-row .ptr { background: var(--violet-soft); color: var(--violet); }
.layout-row .key { background: var(--key-bg); color: var(--ink); border: 1px solid var(--line-strong); }
.layout-row .rid { background: var(--good-soft); color: var(--good); }
.layout-row .nxt { background: var(--teal-soft); color: var(--teal); }
.layout-row .dots { color: var(--muted); }
