:root {
  --bg: #0d121b;
  --panel: #141b27;
  --panel2: #1a2333;
  --line: #263248;
  --text: #dfe7f3;
  --muted: #8a99b0;
  --accent: #39b5ff;
  --accent2: #ff9d3c;
  --good: #3ddc84;
  --bad: #ff5c5c;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
button, select, input { font: inherit; color: inherit; }

#app {
  display: grid;
  grid-template-columns: 76px 1fr 300px;
  grid-template-rows: auto 1fr;
  grid-template-areas: "top top top" "tools stage panel";
  height: 100vh;
}

/* ---------------------------------------------------------- top bar */
#top {
  grid-area: top;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 8px 14px;
  background: linear-gradient(180deg, #172133, #121a28);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; font-size: 18px; letter-spacing: 0.3px; white-space: nowrap; }
.brand .logo { color: var(--accent); font-size: 22px; margin-right: 2px; }
.brand .amp { color: var(--accent2); }
.brand small { font-weight: 500; color: var(--muted); font-size: 12px; margin-left: 4px; }
.brand .beta {
  display: inline-block; vertical-align: 3px; margin-left: 6px; padding: 2px 7px; border-radius: 999px;
  font-size: 10px; font-weight: 800; letter-spacing: 1px; color: #241300;
  background: linear-gradient(135deg, #ffd23f, #ff9d3c); cursor: help;
}
.mmg-logo { display: flex; align-items: center; gap: 6px; text-decoration: none; font-weight: 800; font-size: 13px; opacity: 0.9; white-space: nowrap; }
.mmg-logo:hover { opacity: 1; }
.mmg-logo svg { width: 22px; height: 22px; }
.mmg-logo b { font-weight: 800; }
.mmg-logo i { font-style: normal; color: var(--muted); }
.mmg-logo.static { pointer-events: none; }
button.exit { background: #3a1c1f; border-color: #6a2b2b; }
.grp { display: flex; align-items: center; gap: 6px; }
.lbl { color: var(--muted); font-size: 12px; }
button {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 6px;
  padding: 5px 10px; cursor: pointer; font-size: 13px; transition: background 0.12s, border-color 0.12s;
}
button:hover { background: #22304a; border-color: #34466a; }
button.primary { background: #1d6fb8; border-color: #2b86d6; font-weight: 600; min-width: 84px; }
button.primary:hover { background: #2380d2; }
button.primary.running { background: #b8641d; border-color: #d97a2b; }
select { background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; font-size: 13px; }
.seg { gap: 0; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.on { background: #1d4f7a; border-color: #2b6ea6; color: #fff; position: relative; z-index: 1; }
#stats { margin-left: auto; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
#stats b { color: var(--text); font-weight: 600; }

/* ---------------------------------------------------------- tools */
#tools {
  grid-area: tools; background: var(--panel); border-right: 1px solid var(--line);
  overflow-y: auto; padding: 8px 6px; display: flex; flex-direction: column; gap: 3px;
}
#tools .sep { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); text-align: center; margin: 8px 0 2px; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 6px 2px; border-radius: 8px; border: 1px solid transparent; background: transparent;
  font-size: 10px; color: var(--muted);
}
.tool .ic { font-size: 20px; line-height: 22px; filter: saturate(1.1); }
.tool:hover { background: var(--panel2); color: var(--text); }
.tool.on { background: #1d3550; border-color: #2b6ea6; color: #fff; }

/* ---------------------------------------------------------- stage */
#stage {
  grid-area: stage; position: relative; overflow: hidden;
  display: flex; align-items: center; justify-content: center; flex-direction: column;
  background: radial-gradient(ellipse at 50% 30%, #182235 0%, #0d121b 75%);
}
#wrap {
  position: relative; border-radius: 6px; overflow: hidden;
  box-shadow: 0 0 0 1px #2a3850, 0 12px 40px rgba(0, 0, 0, 0.45);
  background-color: #1a2436;
  background-image:
    linear-gradient(180deg, rgba(120, 170, 230, 0.10), rgba(0, 0, 0, 0) 40%),
    linear-gradient(rgba(255, 255, 255, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.045) 1px, transparent 1px);
}
#wrap canvas { position: absolute; left: 0; top: 0; display: block; }
#cvFg { cursor: crosshair; touch-action: none; }
#sceneInfo {
  max-width: 900px; margin-top: 10px; padding: 8px 14px; font-size: 13px; color: #b9c6d8;
  background: rgba(20, 27, 39, 0.8); border: 1px solid var(--line); border-radius: var(--radius); line-height: 1.45;
}
#sceneInfo b { color: var(--text); }

/* ---------------------------------------------------------- panel */
#panel { grid-area: panel; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 10px 12px 30px; }
#panel section { margin-bottom: 14px; }
#panel h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 4px 0 8px; font-weight: 700; }
#panel h3 .hint { text-transform: none; letter-spacing: 0; font-weight: 400; float: right; }
.hintbox { font-size: 12px; color: #a9b8cc; line-height: 1.45; background: var(--panel2); border-radius: 6px; padding: 7px 9px; margin-bottom: 8px; }
.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; font-size: 13px; }
.row > label { flex: 0 0 96px; color: #b9c6d8; }
.row input[type=range] { flex: 1; accent-color: var(--accent); min-width: 0; }
.row .val { flex: 0 0 62px; text-align: right; font-variant-numeric: tabular-nums; color: var(--text); font-size: 12px; }
.row select { flex: 1; }
.row .btns { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.row .btns button { padding: 3px 8px; }
.row .btns button.on { background: #1d4f7a; border-color: #2b6ea6; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; margin: 6px 0; color: #b9c6d8; cursor: pointer; }
.check input { accent-color: var(--accent); }
.actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
button.danger { border-color: #6a2b2b; background: #3a1c1f; }
button.danger:hover { background: #532226; }
button.toggle-on { background: #1f5e3a; border-color: #2f8a55; }
.probe { font-size: 12px; font-variant-numeric: tabular-nums; line-height: 1.6; color: #b9c6d8; }
.probe b { color: var(--text); font-weight: 600; }
.probe .muted { color: var(--muted); }
.legend { height: 10px; border-radius: 3px; margin: 4px 0 2px; }
.legend-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: #22324a; border: 1px solid #34507a; padding: 8px 16px; border-radius: 8px; font-size: 13px;
  opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; z-index: 10;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.modal-back { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5); display: flex; align-items: center; justify-content: center; z-index: 20; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; width: 360px; max-height: 70vh; overflow: auto; }
.modal h2 { margin: 0 0 10px; font-size: 16px; }
.modal .item { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.modal .item span { flex: 1; }
.modal-input {
  width: 100%; margin: 4px 0 6px; padding: 7px 9px; border-radius: 6px; font-size: 14px;
  background: var(--bg); border: 1px solid var(--line); color: var(--text);
}
.modal-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

@media (max-width: 1100px) {
  #app { grid-template-columns: 64px 1fr 250px; }
  .brand small { display: none; }
  #top { gap: 8px; padding: 6px 10px; }
  .mmg-logo span { display: none; }
}
/* short windows (e.g. embedded in the site): tighter chrome so the world stays big */
@media (max-height: 760px) {
  #top { padding: 5px 10px; gap: 8px; }
  button, select { padding: 4px 8px; font-size: 12px; }
  .brand { font-size: 16px; }
  #sceneInfo { margin-top: 6px; padding: 5px 10px; font-size: 12px; line-height: 1.35; }
  .tool { padding: 4px 2px; }
  .tool .ic { font-size: 17px; line-height: 19px; }
  #tools .sep { margin: 4px 0 1px; }
}
@media (max-height: 560px) {
  #sceneInfo { display: none; }
  #stats { display: none; }
}
@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr auto; grid-template-areas: "top" "tools" "stage" "panel"; height: auto; min-height: 100vh; }
  html, body { overflow: auto; }
  #tools { flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  #tools .sep { display: none; }
  #stage { min-height: 60vh; padding: 8px 16px; }
  #panel { border-left: 0; border-top: 1px solid var(--line); }
}
