:root {
  --bg: #0d1017;
  --panel: #11151f;
  --panel-2: #161b26;
  --line: #232a3a;
  --text: #e6e9f0;
  --muted: #7c869d;
  --accent: #7c5cff;
  --accent-2: #4cc9f0;
  --good: #4ade80;
  --warn: #fbbf24;
  --bad: #fb7185;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: grid;
  grid-template-columns: 340px 1fr 320px;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.train-panel { border-right: none; border-left: 1px solid var(--line); overflow-y: auto; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.panel-head h1 { font-size: 15px; margin: 0; letter-spacing: 0.2px; }
.panel-head h2 { font-size: 13px; margin: 0; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); }

.pill {
  font: 11px/1 var(--mono);
  padding: 5px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  white-space: nowrap;
}
.pill-muted { color: var(--muted); }
.pill-ok { color: var(--good); border-color: #1d4f36; background: #10241b; }
.pill-bad { color: var(--bad); border-color: #52242f; background: #24141a; }
.pill-live { color: var(--accent-2); border-color: #1d3f52; background: #0f1f28; }

/* ---------- chat ---------- */

.share-bar {
  display: flex;
  align-items: baseline;
  gap: 8px;
  width: 100%;
  padding: 8px 16px;
  background: #101c17;
  border: none;
  border-bottom: 1px solid var(--line);
  border-left: 2px solid var(--good);
  color: var(--good);
  font: inherit;
  text-align: left;
  cursor: pointer;
  flex-shrink: 0;
}
.share-bar:hover { background: #12241c; }
.share-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.8px; opacity: 0.8; flex-shrink: 0; }
.share-url { font: 11px var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  padding: 9px 12px;
  border-radius: var(--radius);
  max-width: 95%;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: rise 0.18s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(4px); } }

.msg-user { background: var(--accent); color: #fff; align-self: flex-end; }
.msg-bot { background: var(--panel-2); border: 1px solid var(--line); }
.msg-sys { background: transparent; color: var(--muted); font-size: 12px; padding: 2px 4px; border-left: 2px solid var(--line); border-radius: 0; }
.msg-err { background: #24141a; border: 1px solid #52242f; color: #fca5b0; font-size: 13px; }

.msg-bot .meta {
  display: block;
  margin-top: 6px;
  font: 11px/1.4 var(--mono);
  color: var(--muted);
}

.thinking span {
  display: inline-block;
  width: 5px; height: 5px;
  margin-right: 3px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite;
}
.thinking span:nth-child(2) { animation-delay: 0.2s; }
.thinking span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 60%, 100% { opacity: 0.25; } 30% { opacity: 1; } }

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 14px 10px;
  flex-shrink: 0;
}
.suggestions button {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--muted);
  cursor: pointer;
}
.suggestions button:hover { color: var(--text); border-color: var(--accent); }

.chat-form {
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}
.chat-form textarea {
  flex: 1;
  resize: none;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  padding: 9px 10px;
  font: inherit;
}
.chat-form textarea:focus { outline: none; border-color: var(--accent); }
.chat-form button {
  align-self: stretch;
  padding: 0 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.chat-form button:disabled { opacity: 0.45; cursor: default; }

/* ---------- stage ---------- */

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  position: relative;
}

.stage-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.stage-title { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.stage-title strong { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.stage-controls { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.ctl { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); cursor: pointer; }
.ctl input { accent-color: var(--accent); }

.seg { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button {
  background: var(--panel-2);
  border: none;
  color: var(--muted);
  padding: 5px 9px;
  font: 12px var(--mono);
  cursor: pointer;
}
.seg button.on { background: var(--accent); color: #fff; }

.btn {
  border-radius: 8px;
  padding: 6px 12px;
  font: 500 13px inherit;
  cursor: pointer;
  border: 1px solid var(--line);
}
.btn-ghost { background: var(--panel-2); color: var(--muted); }
.btn-ghost:hover { color: var(--text); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:disabled { opacity: 0.4; cursor: default; }
.btn-stop { background: var(--bad); border-color: var(--bad); color: #fff; font-weight: 600; }

#view {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: block;
  cursor: grab;
  touch-action: none;      /* let dragging pan the camera, not scroll the page */
}
#view.grabbing { cursor: grabbing; }

.hud {
  position: absolute;
  left: 14px;
  bottom: 14px;
  display: flex;
  gap: 18px;
  padding: 10px 14px;
  background: rgba(17, 21, 31, 0.88);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(6px);
}
.hud-item { display: flex; flex-direction: column; gap: 2px; }
.hud-item label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); }
.hud-item span { font: 14px var(--mono); }

/* ---------- training ---------- */

.train-actions { display: flex; gap: 8px; padding: 12px 14px; }
.train-actions .btn { flex: 1; }

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--line);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.stat { background: var(--panel); padding: 9px 14px; }
.stat label { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: 0.7px; color: var(--muted); }
.stat span { font: 15px var(--mono); }

.chart { width: 100%; height: 110px; display: block; margin-top: 12px; }
.chart-legend { display: flex; align-items: center; gap: 6px; margin: 6px 14px 0; font-size: 11px; color: var(--muted); }
.key { width: 12px; height: 2px; display: inline-block; }
.key-best { background: var(--good); }
.key-mean { background: #3d4b6b; margin-left: 8px; }

.settings { border-top: 1px solid var(--line); margin-top: 14px; }
.settings summary {
  padding: 10px 14px;
  cursor: pointer;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--muted);
  user-select: none;
}
.field { display: block; padding: 6px 14px 10px; font-size: 12px; color: var(--muted); }
.field span { display: block; margin-bottom: 5px; }
.field b { color: var(--text); font-family: var(--mono); }
.field select, .field input[type="range"] { width: 100%; }
.field select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 8px;
  font: inherit;
}
.field input[type="range"] { accent-color: var(--accent); }
.hint { display: block; margin-top: 5px; font-size: 11px; color: #5a6479; line-height: 1.4; }
.hint.warn { color: var(--warn); }

.io-row { display: flex; gap: 6px; padding: 4px 14px 10px; }
.io-row .btn { flex: 1; font-size: 12px; padding: 5px 6px; }

.preset-list { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 10px; }
.preset-list button {
  font-size: 12px; padding: 5px 10px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--muted); cursor: pointer;
}
.preset-list button:hover { color: var(--text); border-color: var(--accent); }
.hidden { display: none; }

.spec-view {
  margin: 0 14px 14px;
  padding: 10px;
  max-height: 200px;
  overflow: auto;
  background: #0a0d13;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: 11px/1.5 var(--mono);
  color: var(--muted);
  white-space: pre-wrap;
}

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #262e40; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #333d54; }

/* The stage must keep a floor at every size. With a fixed-height training panel
   and a 1fr stage row, a short window gave the panel its full height and left
   the simulation 15px tall — a canvas nobody can see. */
@media (max-width: 1180px) {
  body {
    grid-template-columns: minmax(0, 300px) minmax(0, 1fr);
    grid-template-rows: minmax(300px, 1fr) auto;
    overflow-y: auto;
  }
  .train-panel {
    grid-column: 1 / -1;
    flex-direction: row;
    flex-wrap: wrap;
    max-height: min(38vh, 260px);
    border-left: none;
    border-top: 1px solid var(--line);
  }
  .train-panel > * { flex: 1 1 240px; }
}

/* Phone / very narrow: stack everything and let the page scroll. */
@media (max-width: 760px) {
  body {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(280px, 60vh) minmax(240px, auto) auto;
  }
  .chat-panel { order: 2; border-right: none; border-top: 1px solid var(--line); }
  .stage { order: 1; }
  .train-panel { order: 3; }
  .stage-controls { flex-wrap: wrap; gap: 6px; }
  .hud { left: 8px; right: 8px; bottom: 8px; gap: 12px; overflow-x: auto; }
}
