:root {
  --paper: #F6F8FA;
  --grid-minor: #E7EDF2;
  --grid-major: #DAE3EB;
  --ink: #26343F;
  --ink-soft: #5B6B78;
  --accent: #3F5BC4;
  --accent-soft: #E7EBF9;
  --danger: #C94F4F;
  --chrome: #FFFFFF;
  --line: #E2E8EE;
  --shadow: 0 2px 10px rgba(38, 52, 63, 0.10);
  --font-ui: 'Zen Kaku Gothic New', 'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', Meiryo, sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-ui);
  color: var(--ink);
  background: var(--paper);
}

body {
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; }

/* ---------- Toolbar ---------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  padding: 8px 14px;
  background: var(--chrome);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-right: 4px;
}

.brand-mark { width: 30px; height: 30px; flex: none; }

.brand-text { display: flex; flex-direction: column; line-height: 1.2; }

.brand-name { font-weight: 700; font-size: 15px; letter-spacing: 0.04em; }

.brand-tag { font-size: 10.5px; color: var(--ink-soft); }

.tool-group {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-left: 14px;
  border-left: 1px solid var(--line);
}

.tool-group-right {
  margin-left: auto;
  border-left: none;
  gap: 8px;
}

.group-label {
  font-size: 11px;
  color: var(--ink-soft);
  margin-right: 2px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  font-size: 12.5px;
  font-weight: 500;
  padding: 6px 9px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
}

.btn svg { width: 15px; height: 15px; flex: none; }

.btn:hover { background: var(--paper); }

.btn:active { background: var(--accent-soft); }

.btn:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn[disabled] { opacity: 0.35; cursor: default; }

.btn[disabled]:hover { background: transparent; }

.btn-icon { padding: 6px; }

.btn-icon svg { width: 16px; height: 16px; }

.btn-primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn-primary:hover { background: #3550AE; }

.btn-primary:active { background: #2C4498; }

.btn-small { border: 1px solid var(--line); font-size: 12px; }

.shape-btn.active,
#btn-lanes.active {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.shape-btn svg { width: 20px; height: 14px; }

.swatches { display: flex; gap: 5px; align-items: center; }

.swatch {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1.5px solid rgba(38, 52, 63, 0.25);
  cursor: pointer;
  padding: 0;
}

.swatch:hover { transform: scale(1.15); }

.swatch.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.swatch:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.swatches[data-disabled="true"] .swatch { opacity: 0.35; cursor: default; }

.swatches[data-disabled="true"] .swatch:hover { transform: none; }

/* ---------- Popover ---------- */

.popover-wrap { position: relative; }

.popover {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 250px;
  background: var(--chrome);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 50;
}

.popover-section { padding: 10px; }

.popover-section + .popover-section { border-top: 1px solid var(--line); }

.popover-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--ink-soft);
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}

.field-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 7px;
  font-size: 12.5px;
}

.field-row select {
  font-family: inherit;
  font-size: 12.5px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--chrome);
  color: var(--ink);
}

.export-size {
  font-size: 11.5px;
  color: var(--ink-soft);
  margin-bottom: 8px;
}

.btn-row { display: flex; gap: 6px; }

/* ---------- Canvas ---------- */

.canvas-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  outline: none;
}

#canvas.panning { cursor: grabbing; }

.node { cursor: pointer; }

.node.dragging { cursor: grabbing; }

.port { cursor: pointer; }

.port circle { transition: fill 0.1s; }

.port:hover circle { fill: var(--accent-soft); }

.edge-hit { cursor: pointer; }

.edge-grip { cursor: grab; }

.edge-flip { cursor: pointer; }

.edge-flip:hover circle { fill: var(--accent-soft); }

.edge-grip:hover circle:first-of-type { fill: var(--accent-soft); }

.node-resize { cursor: nwse-resize; }

.free-text { cursor: move; }

.text-ctl { cursor: pointer; }

.text-ctl:hover circle { stroke: var(--accent); }

.text-editor {
  position: absolute;
  resize: none;
  border: 2px solid var(--accent);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.95);
  font-family: -apple-system, 'Segoe UI', 'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', Meiryo, sans-serif;
  padding: 2px 4px;
  overflow: hidden;
  white-space: pre;
  outline: none;
  box-shadow: var(--shadow);
  pointer-events: auto;
}

.lane-head { cursor: pointer; }

.lane-corner { cursor: move; }

.lane-add { cursor: pointer; }

.lane-add:hover circle { fill: var(--accent-soft); }

.lane-resize[data-resize="row"] { cursor: ns-resize; }

.lane-resize[data-resize="col"] { cursor: ew-resize; }

.lane-editor {
  position: absolute;
  height: 28px;
  border: 2px solid var(--accent);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  outline: none;
  box-shadow: var(--shadow);
  pointer-events: auto;
}

/* ---------- Node text editor ---------- */

.editor-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.node-editor {
  position: absolute;
  resize: none;
  border: none;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  text-align: center;
  font-family: -apple-system, 'Segoe UI', 'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', Meiryo, sans-serif;
  overflow: hidden;
  outline: none;
  pointer-events: none;
}

.node-editor.editing {
  background: #fff;
  color: var(--ink);
  caret-color: var(--ink);
  border: 2px solid var(--accent);
  border-radius: 6px;
  box-shadow: var(--shadow);
  pointer-events: auto;
}

.edge-editor {
  position: absolute;
  transform: translate(-50%, -50%);
  border: 2px solid var(--accent);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
  text-align: center;
  outline: none;
  box-shadow: var(--shadow);
  pointer-events: auto;
}

/* ---------- Coach / hints / zoom / toast ---------- */

.coach {
  position: absolute;
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  padding: 9px 14px;
  border-radius: 9px;
  box-shadow: var(--shadow);
  pointer-events: none;
  z-index: 10;
  white-space: nowrap;
}

.coach::before {
  content: '';
  position: absolute;
  left: -6px;
  top: 50%;
  margin-top: -6px;
  border: 6px solid transparent;
  border-right-color: var(--ink);
  border-left: none;
}

.coach-key {
  display: inline-block;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 5px;
  padding: 0 7px;
  margin-right: 4px;
  font-weight: 700;
}

@media (prefers-reduced-motion: no-preference) {
  .coach { animation: coach-in 0.4s ease-out; }
  @keyframes coach-in {
    from { opacity: 0; transform: translateX(8px); }
    to { opacity: 1; transform: none; }
  }
}

.hints {
  position: absolute;
  left: 14px;
  bottom: 14px;
  pointer-events: none; /* 下に重なったノードのクリックを妨げない */
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 13px;
  font-size: 12px;
  line-height: 1.9;
  color: var(--ink);
  box-shadow: var(--shadow);
  z-index: 10;
}

.hints kbd,
.modal kbd {
  display: inline-block;
  background: var(--paper);
  border: 1px solid var(--grid-major);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 5px;
  font-family: inherit;
  font-size: 11px;
  line-height: 1.6;
}

.hint-plain {
  color: var(--accent);
  font-weight: 700;
}

.hints-foot {
  margin-top: 4px;
  padding-top: 6px;
  border-top: 1px solid var(--line);
  font-size: 10.5px;
  color: var(--ink-soft);
}

.zoom-ctrl {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  align-items: center;
  gap: 2px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 3px;
  box-shadow: var(--shadow);
  z-index: 10;
}

.zoom-level {
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 11.5px;
  color: var(--ink-soft);
  width: 44px;
  text-align: center;
  cursor: pointer;
  border-radius: 6px;
  padding: 4px 0;
}

.zoom-level:hover { background: var(--paper); }

.toast {
  position: absolute;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
  z-index: 30;
  white-space: nowrap;
}

.toast.show { opacity: 1; }

/* ---------- Help modal ---------- */

.modal-backdrop[hidden] { display: none; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(38, 52, 63, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal {
  background: var(--chrome);
  border-radius: 14px;
  box-shadow: 0 12px 40px rgba(38, 52, 63, 0.25);
  width: min(640px, 100%);
  max-height: min(80vh, 720px);
  display: flex;
  flex-direction: column;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}

.modal-head h2 { font-size: 16px; }

.modal-body {
  padding: 6px 18px 18px;
  overflow-y: auto;
}

.modal-body section { margin-top: 14px; }

.modal-body h3 {
  font-size: 12.5px;
  color: var(--accent);
  letter-spacing: 0.08em;
  margin-bottom: 6px;
}

.modal-body table { width: 100%; border-collapse: collapse; font-size: 13px; }

.modal-body td { padding: 5px 6px; vertical-align: top; border-bottom: 1px solid var(--paper); }

.modal-body td:first-child { width: 42%; color: var(--ink-soft); }

.port-mini {
  display: inline-block;
  width: 16px;
  height: 16px;
  line-height: 15px;
  text-align: center;
  border: 1.5px solid var(--accent);
  border-radius: 50%;
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  background: #fff;
}

.modal-note {
  margin-top: 16px;
  font-size: 12px;
  color: var(--ink-soft);
  background: var(--paper);
  border-radius: 8px;
  padding: 10px 12px;
}

/* ---------- Narrow screens ---------- */

@media (max-width: 980px) {
  .brand-tag { display: none; }
  .btn { font-size: 12px; padding: 6px 7px; }
}

@media (max-width: 780px) {
  .group-label { display: none; }
  .hints { display: none; }
}

.popover-note {
  font-size: 11.5px;
  color: var(--ink-soft);
  line-height: 1.6;
  max-width: 230px;
}

/* ---------- アイコンパレット ---------- */

.icon-grid {
  display: grid;
  grid-template-columns: repeat(5, 38px);
  gap: 6px;
}

.icon-btn {
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--chrome);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.icon-btn:hover { background: var(--accent-soft); border-color: var(--accent); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); }

.canvas-image { cursor: move; }
.image-resize { cursor: nwse-resize; }
.image-crop-handle[data-side="l"], .image-crop-handle[data-side="r"] { cursor: ew-resize; }
.image-crop-handle[data-side="t"], .image-crop-handle[data-side="b"] { cursor: ns-resize; }
