:root {
  --bg: #f5f7fa;
  --panel: #ffffff;
  --ink: #1f2933;
  --muted: #6b7785;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --warn: #b45309;
  --ctl-bg: #ffffff;
  --cv-bg: #ffffff;
  --cv-ink: #333333;
  --cv-grid: #e0e0e0;
  --row-alt: #eef2f7;
}

body.theme-dark {
  --bg: #0f1522;
  --panel: #1a2233;
  --ink: #e5eaf2;
  --muted: #94a3b8;
  --line: #2c3a52;
  --accent: #60a5fa;
  --accent-ink: #0f1522;
  --warn: #fbbf24;
  --ctl-bg: #121a2a;
  --cv-bg: #121a2a;
  --cv-ink: #cbd5e1;
  --cv-grid: #2c3a52;
  --row-alt: #202b40;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
  gap: 10px;
}
.brand h1 { margin: 0; font-size: 20px; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.lang-box { display: flex; align-items: center; gap: 8px; }
.lang-box label { color: var(--muted); font-size: 12px; }
.lang-box select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--ctl-bg); color: var(--ink); }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(380px, 520px);
  gap: 18px;
  padding: 18px 22px;
  align-items: start;
}

/* Controls: responsive multi-column masonry of cards (fills available width) */
.controls {
  columns: 300px;
  column-gap: 14px;
}
.viz-stack {
  position: sticky;
  top: 14px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
  break-inside: avoid;
  margin: 0 0 14px;
}
.card h2 {
  margin: 0 0 10px; font-size: 14px; color: var(--accent);
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer; user-select: none;
}
.card h2::after {
  content: "▾"; font-size: 11px; color: var(--muted);
  transition: transform .15s ease; margin-left: 8px;
}
.card.collapsed h2::after { transform: rotate(-90deg); }
.card.collapsed h2 { margin-bottom: 0; }
.card.collapsed > *:not(h2) { display: none !important; }
.field { display: flex; align-items: center; justify-content: space-between; margin: 7px 0; gap: 8px; }
.field label { color: var(--muted); font-size: 12.5px; flex: 1; }
.field input, .field select {
  width: 110px; padding: 5px 7px; border: 1px solid var(--line);
  border-radius: 6px; background: var(--ctl-bg); color: var(--ink); font-size: 13px;
}

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions button {
  padding: 9px 10px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--ctl-bg); color: var(--ink); cursor: pointer; font-size: 13px;
}
.actions button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.actions button:hover { filter: brightness(0.97); }

.viz { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; }
.canvas-wrap { display: flex; gap: 12px; align-items: stretch; }
#fieldCanvas {
  width: 100%; max-width: 100%; aspect-ratio: 1 / 1; height: auto;
  background: #000; border-radius: 8px; image-rendering: pixelated;
  border: 1px solid var(--line);
}
#waterfallCanvas { width: 100%; height: auto; background: #000; border-radius: 8px; border: 1px solid var(--line); }
.sideview h2 { margin: 0 0 10px; font-size: 14px; color: var(--accent); }
.colorbar { display: flex; flex-direction: column; align-items: center; justify-content: space-between; font-size: 11px; color: var(--muted); height: auto; }
.colorbar .bar {
  flex: 1; width: 14px; margin: 6px 0; border-radius: 4px;
  background: linear-gradient(to top, #000004, #420a68, #932667, #dd513a, #fca50a, #fcffa4);
}

.viz-controls { margin-top: 14px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.slider-row { display: flex; align-items: center; gap: 10px; margin: 12px 0; }
.slider-row button { padding: 6px 14px; border: 1px solid var(--line); border-radius: 6px; background: var(--ctl-bg); cursor: pointer; }
.slider-row input[type=range] { flex: 1; }
#zReadout { font-variant-numeric: tabular-nums; min-width: 90px; text-align: right; font-size: 13px; }
.readouts { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; padding: 6px 0; border-top: 1px solid var(--line); }
.readouts b { color: var(--accent); }
.status { margin: 8px 0 2px; font-weight: 600; }
.status.running { color: var(--accent); }
.status.done { color: #047857; }
.status.error { color: #dc2626; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0; }
.note { color: var(--warn); font-size: 11.5px; margin: 4px 0 0; }

.neff-list { margin-top: 6px; font-size: 12px; }
.neff-row { padding: 2px 6px; border-radius: 4px; color: var(--ink); }
.neff-row:nth-child(odd) { background: var(--row-alt); }
.neff-row.unguided { color: var(--muted); text-decoration: line-through; }
.pol-tag { display: inline-block; margin: 0 2px; padding: 0 5px; border-radius: 3px; background: #e3edfa; color: #1565c0; font-size: 11px; font-weight: 600; }

.sparam-result { margin-top: 6px; }
.sparam-table { border-collapse: collapse; width: 100%; font-size: 11.5px; }
.sparam-table th, .sparam-table td { border: 1px solid var(--line); padding: 4px 6px; text-align: center; }
.sparam-table thead th { background: var(--row-alt); color: #1565c0; font-weight: 600; }
.sparam-table tbody th { background: var(--row-alt); color: #1565c0; font-weight: 600; text-align: right; }
.sparam-table td { color: var(--ink); line-height: 1.35; }
.spbar { height: 6px; background: var(--row-alt); border-radius: 3px; overflow: hidden; margin-bottom: 3px; }
.spfill { height: 100%; background: #1565c0; }
.sparam-table td.sp-refl { background: #fff4e6; }
.sparam-table td.sp-refl.sp-refl { background: #fff4e6; }
.spfill-refl { background: #d97706; }

.live-wrap { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12px; }
.live-dot { width: 9px; height: 9px; border-radius: 50%; background: #cbd5e1; transition: background .2s, box-shadow .2s; }
.live-dot.on { background: #16a34a; box-shadow: 0 0 0 3px rgba(22, 163, 74, .25); }

@media (max-width: 1080px) {
  .layout { grid-template-columns: 1fr; }
  .viz-stack { position: static; }
}
@media (max-width: 560px) {
  .controls { columns: 1; }
}

/* ---- P6: time-domain envelope (NLSE) card ---- */
.env-btns { display: flex; gap: 8px; margin: 10px 0 4px; }
.env-btns button {
  flex: 1; padding: 9px 10px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--ctl-bg); color: var(--ink); cursor: pointer; font-size: 13px;
}
.env-btns button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.env-btns button:hover { filter: brightness(0.97); }
#envMapCanvas, #envSpecCanvas {
  display: block; width: 100%; max-width: 420px; height: auto;
  background: #000; border: 1px solid var(--line); border-radius: 8px; margin-top: 8px;
}
#envSpecCanvas { background: var(--cv-bg); }

/* ---- P8: theme toggle + toolbar ---- */
.topbar-tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.topbar-tools button {
  padding: 6px 12px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--ctl-bg); color: var(--ink); cursor: pointer; font-size: 12.5px;
}
.topbar-tools button:hover { filter: brightness(0.95); }
body.theme-dark .topbar-tools button:hover { filter: brightness(1.2); }
body.theme-dark .sparam-table td.sp-refl { background: #3a2c14; }

/* ---- P8: physics diagnostics panel ---- */
.diag-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 4px; }
.diag-table td { padding: 3px 6px; border-bottom: 1px solid var(--line); }
.diag-table td:first-child { color: var(--muted); }
.diag-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.diag-regime {
  margin-top: 8px; padding: 6px 10px; border-radius: 6px; font-size: 12.5px; font-weight: 600;
  background: var(--row-alt); color: var(--accent); border: 1px solid var(--line);
}

/* ---- P8: env canvas cursor readout ---- */
.env-cursor {
  font-size: 11.5px; color: var(--muted); min-height: 16px;
  font-variant-numeric: tabular-nums; margin: 2px 0 0;
}
#envMapCanvas, #envSpecCanvas { cursor: crosshair; }

/* ---- P8: A/B compare ---- */
.ab-tag {
  display: inline-block; padding: 1px 7px; border-radius: 4px; font-size: 11px; font-weight: 700;
  background: var(--row-alt); color: var(--muted); margin-right: 6px;
}
.ab-tag.on { background: var(--accent); color: var(--accent-ink); }
#envRecBtn.rec { color: #dc2626; border-color: #dc2626; font-weight: 700; }

/* ---- P9: parameter tooltip (data-tip) ---- */
[data-tip] { position: relative; cursor: help; }
[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute; left: 0; top: 100%; z-index: 50; margin-top: 4px;
  width: 240px; padding: 6px 8px; border-radius: 6px;
  background: #1f2933; color: #fff; font-size: 11px; font-weight: 400; line-height: 1.4;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25); white-space: normal;
}
body.theme-dark [data-tip]:hover::after { background: #cbd5e1; color: #0f1522; }
label[data-tip]::after { top: 100%; }

/* ---- P9: collapsible cards ---- */
.card.collapsed > *:not(h2):not(h3) { display: none; }
.card > h2, .card > h3 { cursor: pointer; user-select: none; }
.card.collapsed > h2::before { content: '▸ '; }
.card > h2::before { content: '▾ '; color: var(--muted); }

/* ---- P9: inline controls ---- */
.field .inline { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.field .inline select { width: auto; }

/* ---- P9: saved-case library ---- */
.lib-list { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.lib-item {
  display: flex; align-items: center; gap: 8px; padding: 5px 6px; border: 1px solid var(--line);
  border-radius: 6px; background: var(--row-alt);
}
.lib-item img { width: 90px; height: 48px; border-radius: 4px; background: var(--cv-bg); object-fit: cover; }
.lib-meta { flex: 1; min-width: 0; }
.lib-meta b { font-size: 12.5px; color: var(--ink); }
.lib-load { padding: 3px 8px; font-size: 11px; }
.lib-del { padding: 3px 7px; font-size: 11px; color: #dc2626; border-color: #dc2626; margin-left: auto; }

/* ---- P9: analysis canvases ---- */
#envSliceCanvas, #envSweepCanvas, #env3DCanvas { max-width: 100%; border: 1px solid var(--line); border-radius: 6px; background: var(--cv-bg); }

/* ---- P9: responsive / mobile ---- */
@media (max-width: 880px) {
  .controls { width: 100%; max-width: 100%; }
  .main { margin-left: 0; }
  .card { padding: 10px; }
  .field { flex-direction: column; align-items: stretch; gap: 3px; }
  .field label { width: auto; }
  .field input, .field select { width: 100%; }
}

/* ---- P2 (#112): colormap + contrast row ---- */
.cmap-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; flex-wrap: wrap; }
.cmap-row label { font-size: 12px; color: var(--muted); }
.cmap-row select { padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--ctl-bg); color: var(--ink); }
.cmap-row input[type=range] { flex: 1; min-width: 120px; }
.cmap-contrast { margin-left: 8px; }

/* ---- P2 (#113): grid quality badge ---- */
.grid-badge { padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.grid-good { background: #047857; color: #fff; }
.grid-ok { background: #b45309; color: #fff; }
.grid-coarse { background: #dc2626; color: #fff; }

/* ---- P2 (#114): local preset store ---- */
.preset-store { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 10px; }
.preset-store h3 { margin: 0 0 6px; font-size: 13px; color: var(--accent); }
.preset-store select { padding: 5px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--ctl-bg); color: var(--ink); max-width: 180px; }

/* ---- #1: mode analysis panel ---- */
#modesCanvas { display: block; margin-top: 8px; background: var(--cv-bg); border: 1px solid var(--line); border-radius: 6px; }
.mode-row { font-size: 12px; padding: 3px 0; border-bottom: 1px solid var(--line); }
.mode-row .badge { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 4px; margin-right: 6px; }
.badge.g { background: rgba(4,120,87,0.16); color: #047857; }
.badge.r { background: rgba(220,38,38,0.16); color: #dc2626; }
#modePanelReadouts { white-space: pre-line; }
