:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --canvas: #fbfaf7;
  --grid: #e9e6de;
  --ink: #1f2328;
  --ink-2: #5b6168;
  --ink-3: #8b9097;
  --line: #dedad0;
  --accent: #2f6f6a;
  --accent-soft: #e2eeec;
  --pos: #2f6f6a;
  --neg: #b4492f;
  --unk: #a07a12;
  --r: #2f6f6a;
  --b: #6a4fa3;
  --node-bg: #ffffff;
  --node-line: #cfcabd;
  --sel: #2f6f6a;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --on-accent: #ffffff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181b; --surface: #1d2024; --canvas: #191b1e; --grid: #25282d;
    --ink: #e8e6e1; --ink-2: #a9adb3; --ink-3: #7b8087; --line: #30343a;
    --accent: #6fb3ab; --accent-soft: #23302f; --pos: #6fb3ab; --neg: #e08a72; --unk: #d6b04e;
    --r: #6fb3ab; --b: #a993dd; --node-bg: #22262b; --node-line: #3a3f46; --sel: #6fb3ab; --on-accent: #12201f;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16181b; --surface: #1d2024; --canvas: #191b1e; --grid: #25282d;
  --ink: #e8e6e1; --ink-2: #a9adb3; --ink-3: #7b8087; --line: #30343a;
  --accent: #6fb3ab; --accent-soft: #23302f; --pos: #6fb3ab; --neg: #e08a72; --unk: #d6b04e;
  --r: #6fb3ab; --b: #a993dd; --node-bg: #22262b; --node-line: #3a3f46; --sel: #6fb3ab; --on-accent: #12201f;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.5 "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", system-ui, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button {
  font: inherit; color: var(--ink); background: transparent; border: 1px solid transparent;
  border-radius: 6px; padding: 5px 10px; cursor: pointer; white-space: nowrap;
}
button:hover { background: var(--accent-soft); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button:disabled { opacity: .35; cursor: default; background: transparent; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: var(--surface); border-bottom: 1px solid var(--line); flex: none;
}
.brand { font-weight: 700; letter-spacing: .02em; white-space: nowrap; }
.tools { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; flex: 1; min-width: 0; }
.group { display: flex; align-items: center; gap: 2px; padding-right: 8px; margin-right: 2px; border-right: 1px solid var(--line); }
.group:last-child { border-right: 0; margin-left: auto; }
.zoom { min-width: 44px; text-align: center; color: var(--ink-2); font-variant-numeric: tabular-nums; font-size: 12px; }

/* layout */
.layout { flex: 1; display: grid; grid-template-columns: 1fr 320px; min-height: 0; }
.stage { position: relative; min-width: 0; min-height: 0; background: var(--canvas); overflow: hidden; }
#canvas {
  width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none;
  background-image: radial-gradient(var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
}
.stage.panning #canvas { cursor: grabbing; }

.empty-hint {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  pointer-events: none; color: var(--ink-3); text-align: center; padding: 16px;
}
.empty-hint p { margin: 0; font-size: 16px; }
.empty-hint .sub { font-size: 13px; margin-top: 6px; }
.empty-hint[hidden] { display: none; }

.inline-edit {
  position: absolute; transform: translate(-50%, -50%); z-index: 5;
  font: 500 14px "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", system-ui, sans-serif;
  text-align: center; padding: 6px 12px; border-radius: 17px; line-height: 18px;
  border: 2px solid var(--accent); background: var(--node-bg); color: var(--ink); min-width: 140px;
  resize: none; overflow: hidden; display: block;
}
.inline-edit[hidden] { display: none; }

/* svg elements */
.node rect { fill: var(--node-bg); stroke: var(--node-line); stroke-width: 1.2; }
.node text { fill: var(--ink); font: 500 14px "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", system-ui, sans-serif; pointer-events: none; }
.node { cursor: grab; }
.node.sel rect { stroke: var(--sel); stroke-width: 2.2; }
.node.target rect { stroke: var(--accent); stroke-width: 2.5; fill: var(--accent-soft); }
.node .handle { fill: var(--accent); stroke: var(--node-bg); stroke-width: 2; opacity: 0; cursor: crosshair; transition: opacity .12s; }
.node:hover .handle, .node.sel .handle { opacity: 1; }

.link .stroke { fill: none; stroke: var(--ink-2); stroke-width: 1.6; }
.link .head { fill: var(--ink-2); }
.link .delay { stroke: var(--ink-2); stroke-width: 1.6; fill: none; }
.link .hit { fill: none; stroke: transparent; stroke-width: 14; cursor: pointer; }
.link .pol { font: 700 16px system-ui, sans-serif; text-anchor: middle; dominant-baseline: central; cursor: pointer; }
.link.p-plus .pol { fill: var(--pos); }
.link.p-minus .pol { fill: var(--neg); }
.link.p-none .pol { fill: var(--unk); }
.link.p-none .stroke { stroke-dasharray: 5 4; }
.link.sel .stroke { stroke: var(--sel); stroke-width: 2.4; }
.link.sel .head { fill: var(--sel); }
.link.sel .delay { stroke: var(--sel); }
.link .bend { fill: var(--surface); stroke: var(--sel); stroke-width: 2; cursor: move; }

.loopmark { cursor: move; }
.loopmark .arc { fill: none; stroke-width: 1.3; }
.loopmark .tip { stroke: none; }
.loopmark .letter { font: 700 9px system-ui, sans-serif; text-anchor: middle; dominant-baseline: central; }
.loopmark .name { font: 500 16px "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", system-ui, sans-serif; text-anchor: middle; dominant-baseline: central; fill: var(--ink); }
.loopmark .bg { fill: var(--canvas); opacity: .85; }
.loopmark.t-R .arc { stroke: var(--r); } .loopmark.t-R .tip, .loopmark.t-R .letter { fill: var(--r); }
.loopmark.t-B .arc { stroke: var(--b); } .loopmark.t-B .tip, .loopmark.t-B .letter { fill: var(--b); }
.loopmark.t-U .arc { stroke: var(--unk); } .loopmark.t-U .tip, .loopmark.t-U .letter { fill: var(--unk); }
.loopmark.sel .bg { fill: var(--accent-soft); opacity: 1; }

.dim { opacity: .18; }
.temp-link { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 4; fill: none; pointer-events: none; }
.band { fill: var(--accent-soft); fill-opacity: .5; stroke: var(--accent); stroke-dasharray: 4 3; pointer-events: none; }

/* panel */
.panel { background: var(--surface); border-left: 1px solid var(--line); overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 24px; }
.panel h3 { margin: 0 0 10px; font-size: 12px; letter-spacing: .08em; color: var(--ink-3); font-weight: 600; }
.panel .muted { color: var(--ink-3); font-size: 13px; margin: 0; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field label { font-size: 12px; color: var(--ink-2); }
.panel input[type=text], .panel textarea {
  font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; width: 100%;
}
.pol-seg { display: flex; gap: 4px; }
.pol-seg button { flex: 1; border: 1px solid var(--line); font-weight: 700; }
.pol-seg button.on[data-pol="+"] { background: var(--pos); color: var(--on-accent); border-color: var(--pos); }
.pol-seg button.on[data-pol="-"] { background: var(--neg); color: var(--on-accent); border-color: var(--neg); }
.pol-seg button.on[data-pol=""] { background: var(--unk); color: var(--on-accent); border-color: var(--unk); }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.actions button { border: 1px solid var(--line); font-size: 13px; }
.actions button.danger { color: var(--neg); }

.summary { display: flex; gap: 14px; margin-bottom: 10px; font-size: 13px; color: var(--ink-2); }
.summary b { font-variant-numeric: tabular-nums; }
.loops { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.loop { border: 1px solid var(--line); border-radius: 8px; padding: 10px; cursor: pointer; }
.loop:hover { border-color: var(--ink-3); }
.loop.sel { border-color: var(--accent); background: var(--accent-soft); }
.loop-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.badge { font: 700 12px system-ui, sans-serif; border-radius: 4px; padding: 1px 6px; color: var(--on-accent); flex: none; }
.badge.t-R { background: var(--r); } .badge.t-B { background: var(--b); } .badge.t-U { background: var(--unk); }
.loop-type { font-size: 12px; color: var(--ink-2); }
.loop input[type=text], .loop textarea { padding: 4px 6px; font-size: 13px; }
.loop-path { font-size: 12px; color: var(--ink-2); margin: 6px 0; word-break: break-all; }
.loop .actions button { padding: 2px 8px; font-size: 12px; }
.warn { font-size: 12px; color: var(--unk); margin: 8px 0 0; }

dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink); padding: 20px 24px; max-width: 460px; width: calc(100% - 32px); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,.3); }
dialog h2 { margin: 0 0 12px; font-size: 16px; }
dialog dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0 0 16px; font-size: 13px; }
dialog dt { color: var(--ink-2); }
dialog dd { margin: 0; }
dialog form { text-align: right; }
dialog form button { border: 1px solid var(--line); }

@media (max-width: 820px) {
  .brand { display: none; }
  .topbar { padding: 6px 12px; }
  .layout { grid-template-columns: 1fr; grid-template-rows: 1fr minmax(0, 38vh); }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
  .group { padding-right: 4px; }
  button { padding: 5px 8px; }
}
.panel .mb { margin-bottom: 12px; }

.rot-line { stroke: var(--accent); stroke-width: 1.2; stroke-dasharray: 3 3; pointer-events: none; }
.rot-guide { fill: none; stroke: var(--accent); stroke-width: 1; stroke-dasharray: 2 4; opacity: .6; pointer-events: none; }
.rot-handle { cursor: grab; }
.rot-handle circle { fill: var(--surface); stroke: var(--accent); stroke-width: 1.8; }
.rot-handle path { fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-linecap: round; }
.rot-handle .rot-tip { fill: var(--accent); stroke: none; }
.rot-deg { font: 600 12px system-ui, sans-serif; fill: var(--accent); text-anchor: middle; pointer-events: none; }
.loop-note { font-size: 12px; color: var(--ink-3); margin: 0 0 6px; }

.panel textarea { resize: none; line-height: 1.5; display: block; }

.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 30px; padding: 0; }
.icon-btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
