/* rungkit — a CAD-style workbench: blue-gray chrome, flat docked panels with
   thin borders, a gridded viewport for the ladder, a tabbed ribbon. Dark by
   default, light on request. All colors are the variables below. */

:root, :root[data-theme="dark"] {
  --chrome: #22262e;        /* title bar, status bar */
  --ribbon: #2a2f38;
  --view: #1b1e24;          /* viewport behind the ladder */
  --grid: rgba(255, 255, 255, 0.035);
  --grid-major: rgba(255, 255, 255, 0.06);
  --panel: #262a32;
  --panel-2: #2e333d;
  --hover: #353b46;
  --line: #363c47;
  --line-2: #4a5260;
  --ink: #e3e6eb;
  --ink-2: #c3c8d1;
  --muted: #949dab;
  --accent: #5b9cf5;
  --accent-ink: #ffffff;
  --accent-soft: rgba(91, 156, 245, 0.16);
  --sel: rgba(91, 156, 245, 0.22);
  --rail: #aab2bf;
  --ic-add: #4fbf85;
  --ok: #4fbf85;
  --warn: #e0ab45;
  --err: #ec5f57;
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  --mono: "Cascadia Mono", "JetBrains Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;
  --sans: "Segoe UI", system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", sans-serif;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --chrome: #eef0f3;
  --ribbon: #f8f9fb;
  --view: #f4f6f9;
  --grid: rgba(20, 40, 70, 0.045);
  --grid-major: rgba(20, 40, 70, 0.08);
  --panel: #ffffff;
  --panel-2: #f2f4f7;
  --hover: #e8ecf1;
  --line: #d9dee5;
  --line-2: #c4cbd5;
  --ink: #1d222a;
  --ink-2: #3a414c;
  --muted: #5f6877;
  --accent: #2f6fdb;
  --accent-ink: #ffffff;
  --accent-soft: rgba(47, 111, 219, 0.10);
  --sel: rgba(47, 111, 219, 0.16);
  --rail: #4b5563;
  --ic-add: #23955b;
  --ok: #23955b;
  --warn: #a86d0c;
  --err: #cf3a32;
  --shadow: 0 6px 18px rgba(20, 30, 45, 0.12);
  color-scheme: light;
}

* { box-sizing: border-box; scrollbar-width: thin; scrollbar-color: var(--line-2) transparent; }
html { height: 100%; }
body {
  margin: 0; height: 100%; min-height: 100vh;
  display: grid; grid-template-rows: auto auto 1fr auto; grid-template-columns: minmax(0, 1fr);
  background: var(--chrome); color: var(--ink);
  font: 12.5px/1.45 var(--sans); font-variant-ligatures: none;
}
/* not display:none, so gradients inside the icons still render */
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
svg { flex: none; }

/* ---- controls ---- */
button, select {
  font: inherit; color: var(--ink); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 4px; padding: 4px 9px; cursor: pointer;
}
button:hover:not(:disabled), select:hover { background: var(--hover); }
button:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible, .svg:focus-visible {
  outline: 1px solid var(--accent); outline-offset: 0;
}
button:disabled { opacity: .4; cursor: default; }
button svg { width: 16px; height: 16px; }
.primary {
  display: inline-flex; align-items: center; gap: 5px; font-weight: 600;
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
}
.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.1); }
.primary svg { width: 13px; height: 13px; }

/* ---- title bar ---- */
.titlebar {
  display: flex; align-items: center; gap: 10px; height: 38px; padding: 0 8px 0 12px;
  background: var(--chrome); border-bottom: 1px solid var(--line); min-width: 0; overflow: hidden;
}
.brand { display: flex; align-items: center; gap: 8px; }
.logo { width: 20px; height: 20px; }
.brand strong { font-size: 13.5px; font-weight: 600; }
.tb-sep { width: 1px; height: 18px; background: var(--line); }
.doctab {
  display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 12px;
  background: var(--panel-2); border: 1px solid var(--line); border-bottom-color: transparent; border-radius: 5px 5px 0 0;
  margin-top: 9px; font-weight: 600; max-width: 30ch; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.doctab svg { width: 15px; height: 15px; }
.tagline { color: var(--muted); font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.title-actions { margin-left: auto; display: flex; align-items: center; gap: 2px; }
.tb-btn { background: transparent; border-color: transparent; color: var(--muted); padding: 3px 8px; }
.tb-btn:hover:not(:disabled) { color: var(--ink); background: var(--hover); }
.tb-btn.icon { display: inline-grid; place-items: center; width: 30px; height: 28px; padding: 0; }
.tb-btn svg { width: 17px; height: 17px; }
a.tb-btn { color: var(--muted); border-radius: 4px; text-decoration: none; }
a.tb-btn:hover { color: var(--ink); background: var(--hover); }
.ver { color: var(--muted); font: 11px var(--mono); padding: 0 6px; border-left: 1px solid var(--line); margin-left: 4px; }

/* ---- ribbon ---- */
.ribbon { background: var(--ribbon); border-bottom: 1px solid var(--line); }
.ribbon-tabs { display: flex; gap: 2px; padding: 5px 10px 0; }
.rtab {
  background: transparent; border: 1px solid transparent; border-radius: 4px; padding: 3px 12px;
  color: var(--ink-2); font-weight: 500;
}
.rtab:hover:not(.active) { background: var(--hover); }
.rtab.active { background: var(--accent-soft); color: var(--accent); border-color: color-mix(in srgb, var(--accent) 35%, transparent); font-weight: 600; }
.ribbon-body { display: flex; align-items: stretch; padding: 4px 6px 3px; overflow-x: auto; min-height: 82px; }
.group { display: flex; flex-direction: column; padding: 0 8px; border-right: 1px solid var(--line); }
.group[hidden] { display: none; }
.group-body { display: flex; align-items: center; gap: 1px; flex: 1; }
.group-gap { width: 1px; align-self: stretch; margin: 6px 5px; background: var(--line); }
.group-names { display: flex; justify-content: space-around; gap: 10px; color: var(--muted); font-size: 11px; padding: 1px 0 0; }
.tool {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 4px;
  min-width: 56px; padding: 5px 5px 3px; background: transparent; border-color: transparent;
  font-size: 11.5px; color: var(--ink-2); white-space: nowrap;
}
.tool svg { width: 26px; height: 26px; }
.tool:hover:not(:disabled) { background: var(--hover); color: var(--ink); }
.tool:active:not(:disabled), .tool.toggle[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.field { display: flex; align-items: center; margin: 0 6px 0 2px; }
.field select { min-width: 140px; padding: 5px 8px; }

/* ---- workspace ---- */
.workspace {
  min-height: 0; display: grid; gap: 4px; padding: 4px;
  grid-template-columns: minmax(280px, 30%) 1fr;
  grid-template-rows: minmax(0, 1fr) minmax(200px, 33%);
  grid-template-areas: "editor drawing" "scenario scenario";
  background: var(--chrome);
}
.editor { grid-area: editor; }
.drawing { grid-area: drawing; }
.scenario { grid-area: scenario; }

.panel {
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
}
.panel-head {
  display: flex; align-items: center; gap: 10px; padding: 0 8px 0 10px; height: 34px; flex: none;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.panel-title { font-weight: 600; font-size: 12.5px; }
.seg { margin-left: auto; display: inline-flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 5px; padding: 2px; }
.seg-btn { border: 0; background: transparent; padding: 2px 10px; border-radius: 3px; color: var(--muted); font-size: 11.5px; }
.seg-btn.active { background: var(--panel); color: var(--ink); box-shadow: 0 1px 2px rgba(0, 0, 0, .25); }
.pane-text, .pane-tree { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.pane-text[hidden], .pane-tree[hidden] { display: none; }

textarea {
  flex: 1; min-height: 0; width: 100%; resize: none; border: 0; padding: 8px 10px;
  font: 12.5px/1.6 var(--mono); color: var(--ink); background: transparent; tab-size: 2;
  font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; /* show -> as typed */
}
textarea::selection { background: var(--sel); }

.diags { list-style: none; margin: 0; padding: 2px 0; max-height: 32%; overflow: auto; border-top: 1px solid var(--line); background: var(--panel-2); }
.diags:empty { display: none; }
.diags li { display: flex; gap: 8px; align-items: baseline; padding: 4px 10px; cursor: pointer; font-size: 12px; }
.diags li:hover { background: var(--hover); }
.diags code { font: 600 11px var(--mono); }
.diags .error code { color: var(--err); }
.diags .warning code { color: var(--warn); }
.diags .at { color: var(--muted); font-family: var(--mono); font-size: 11px; }

/* structure tree */
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree { padding: 4px 0; overflow: auto; flex: 1; }
.tree li > .row {
  display: flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px 0 calc(10px + var(--depth, 0) * 16px);
  cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tree .row:hover { background: var(--hover); }
.tree .row.selected { background: var(--accent-soft); }
.tree .row svg { width: 16px; height: 16px; }
.tree .chev { width: 11px; height: 11px; color: var(--muted); transition: transform .12s; }
.tree li.open > .row .chev { transform: rotate(90deg); }
.tree li:not(.open) > ul { display: none; }
.tree .id { color: var(--muted); font: 11px var(--mono); }
.tree .count { margin-left: auto; color: var(--muted); font-size: 11px; }

/* ---- viewport ---- */
.canvas {
  position: relative; min-height: 0; min-width: 0; overflow: hidden;
  border: 1px solid var(--line); border-radius: 6px; background-color: var(--view);
  background-image:
    linear-gradient(var(--grid-major) 1px, transparent 1px), linear-gradient(90deg, var(--grid-major) 1px, transparent 1px),
    linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 120px 120px, 120px 120px, 24px 24px, 24px 24px;
}
.viewport-head {
  position: absolute; top: 0; left: 0; right: 0; z-index: 2; display: flex; align-items: center; gap: 10px;
  height: 30px; padding: 0 12px; pointer-events: none;
}
.hint { color: var(--muted); font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sel { margin-left: auto; font: 11px var(--mono); color: var(--accent); background: var(--panel); border: 1px solid var(--line); border-radius: 4px; padding: 1px 7px; }
.sel:empty { display: none; }

.svg { position: absolute; inset: 30px 0 0 62px; overflow: auto; padding: 6px 16px 56px 8px; color: var(--ink); }
.svg svg { display: block; }
.svg line.rail { stroke: var(--rail); stroke-width: 3px; }
.svg text.rung-title { fill: var(--muted); font-family: var(--sans); font-weight: 600; }
.svg g.rung.selected > text.rung-title { fill: var(--accent); }
.svg g.elem { cursor: pointer; }
.svg g.elem rect.hit { fill: transparent; rx: 3px; }
.svg g.elem:hover rect.hit { fill: var(--accent-soft); }
.svg g.elem.selected rect.hit { fill: var(--sel); stroke: var(--accent); stroke-opacity: 1; stroke-width: 1px; }
.svg g.elem.selected text.label { fill: var(--accent); }
.svg .placeholder { color: var(--muted); padding: 24px; }

/* parts palette, floating at the left of the viewport */
.palette {
  position: absolute; z-index: 3; top: 36px; left: 8px; bottom: 8px; width: 46px;
  display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 5px 0; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px; box-shadow: var(--shadow);
}
.palette-title { font-size: 10px; color: var(--muted); margin-bottom: 2px; white-space: nowrap; }
.palette-sep { width: 26px; height: 1px; background: var(--line); margin: 4px 0; flex: none; }
.part { width: 38px; height: 32px; padding: 0; display: grid; place-items: center; background: transparent; border-color: transparent; cursor: grab; flex: none; }
.part svg { width: 26px; height: 26px; }
.part:hover { background: var(--hover); }
.part:active { cursor: grabbing; }
.svg g.elem.drop rect.hit { fill: var(--accent-soft); stroke: var(--accent); stroke-dasharray: 4 3; stroke-opacity: 1; stroke-width: 1.2px; }
.svg g.rung.drop > text.rung-title { fill: var(--accent); }
.droptip {
  position: absolute; pointer-events: none; z-index: 5; padding: 2px 8px; border-radius: 4px;
  background: var(--accent); color: var(--accent-ink); font: 600 11px var(--sans); white-space: nowrap;
}
body.dragging .canvas { border-color: var(--accent); }

/* view toolbar, floating at the bottom of the viewport */
.viewbar {
  position: absolute; z-index: 3; left: 50%; bottom: 10px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 1px; padding: 3px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px; box-shadow: var(--shadow);
}
.vb { background: transparent; border-color: transparent; padding: 0; width: 30px; height: 26px; display: grid; place-items: center; color: var(--ink-2); }
.vb svg { width: 16px; height: 16px; }
.vb:hover { background: var(--hover); color: var(--ink); }
.vb.pct { width: auto; padding: 0 8px; font: 11px var(--mono); }
.vb-sep { width: 1px; height: 16px; background: var(--line); margin: 0 3px; }

/* ---- scenario dock ---- */
.summary { font: 600 11.5px var(--mono); }
.summary .ok { color: var(--ok); }
.summary .ng { color: var(--err); }
.check { display: flex; gap: 5px; align-items: center; margin-left: auto; color: var(--muted); font-size: 11.5px; }
.check input { accent-color: var(--accent); }
.scenario-body { display: grid; grid-template-columns: minmax(240px, 38%) 1fr; min-height: 0; flex: 1; }
.scenario-body textarea { border-right: 1px solid var(--line); }
.results { overflow: auto; padding: 4px 0; font-size: 12px; }
.results .sum { display: none; }
.results .test { padding: 3px 12px; border-bottom: 1px solid var(--line); }
.results .test b { display: inline-block; min-width: 3.2em; font: 700 10.5px var(--mono); margin-right: 6px; }
.results .pass b { color: var(--ok); }
.results .fail b { color: var(--err); }
.results .msg { color: var(--muted); margin: 2px 0 0 3.9em; cursor: pointer; font-family: var(--mono); font-size: 11.5px; }
.results table { border-collapse: collapse; margin: 5px 0 4px 3.9em; font-family: var(--mono); font-size: 11px; }
.results th, .results td { border: 1px solid var(--line); padding: 1px 7px; text-align: right; }
.results th { font-weight: 600; background: var(--panel-2); color: var(--muted); }
.results td.changed { background: var(--accent-soft); color: var(--accent); }
.results caption { caption-side: bottom; color: var(--muted); text-align: left; padding-top: 2px; }
.results .error { color: var(--err); font-family: var(--mono); padding: 4px 12px; margin: 0; }

/* ---- status bar ---- */
.statusbar {
  display: flex; align-items: center; gap: 8px; height: 24px; padding: 0 10px;
  background: var(--chrome); border-top: 1px solid var(--line); color: var(--muted); font-size: 11px;
}
.statusbar .spacer { flex: 1; }
.dot { width: 7px; height: 7px; border-radius: 2px; background: var(--muted); }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--err); }
.status { font-family: var(--mono); color: var(--ink-2); }
.status.bad { color: var(--err); }

/* ---- notice and dialog ---- */
.toast {
  position: fixed; left: 50%; bottom: 34px; transform: translateX(-50%); z-index: 20;
  max-width: min(720px, calc(100vw - 32px)); padding: 9px 12px; border-radius: 6px;
  background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-left: 3px solid var(--err);
  box-shadow: var(--shadow); font-size: 12px;
}
.ask {
  border: 1px solid var(--line-2); border-radius: 8px; padding: 0; width: min(420px, calc(100vw - 32px));
  background: var(--panel); color: var(--ink); box-shadow: 0 16px 48px rgba(0, 0, 0, .45);
}
.ask::backdrop { background: rgba(8, 10, 14, .5); }
.ask form { display: flex; flex-direction: column; gap: 8px; padding: 14px; }
.ask label { font-weight: 600; }
.ask input {
  font: 13px var(--mono); color: var(--ink); background: var(--panel-2); border: 1px solid var(--line-2);
  border-radius: 4px; padding: 7px 9px; font-variant-ligatures: none;
}
.ask input:focus { outline: 1px solid var(--accent); border-color: var(--accent); }
.ask-hint { margin: 0; color: var(--muted); font-size: 11.5px; }
.ask-hint:empty { display: none; }
.ask-actions { display: flex; justify-content: flex-end; gap: 6px; margin-top: 4px; }
.ask-actions button { min-width: 76px; justify-content: center; }

/* ---- small screens ---- */
@media (max-width: 900px) {
  body { height: auto; }
  .tagline, .foot-note, .ver, .tb-sep { display: none; }
  .doctab { max-width: 16ch; }
  .ribbon-tabs { overflow-x: auto; }
  .rtab { white-space: nowrap; }
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto; grid-template-areas: "editor" "drawing" "scenario"; }
  .editor { min-height: 300px; }
  .canvas { min-height: 440px; }
  .scenario-body { grid-template-columns: 1fr; }
  .scenario-body textarea { border-right: 0; border-bottom: 1px solid var(--line); min-height: 180px; }
  .group-names { display: none; }
  .tool { min-width: 50px; }
}
