:root {
  --ink: #22283a;
  --muted: #6a7287;
  --line: #e2e6ef;
  --panel: #ffffff;
  --paper: #fcfdff;
  --grid-minor: #e4ecf6;
  --grid-major: #c9d8ea;
  --accent: #3b6fe0;
  --accent-ink: #ffffff;
  --danger: #d6455d;
  --shadow: 0 8px 28px rgba(30, 40, 70, .14);
  --radius-node: 14px;
  --topbar-h: 56px;
  --font: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--paper);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

/* ── 上のバー ── */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h); z-index: 30;
  display: flex; align-items: center; gap: 8px; padding: 0 12px;
  background: rgba(255,255,255,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.map-title {
  flex: 1; min-width: 0; font: inherit; font-weight: 700; font-size: 17px;
  border: 1px solid transparent; border-radius: 8px; padding: 6px 8px; background: transparent; color: var(--ink);
}
.map-title:hover, .map-title:focus { border-color: var(--line); outline: none; background: #fff; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px;
  border: 0; border-radius: 10px; background: transparent;
}
.icon-btn:hover { background: #eef1f7; }
.icon-btn:disabled { opacity: .35; cursor: default; background: transparent; }
.btn-ghost, .btn-primary {
  display: inline-flex; align-items: center; gap: 6px; height: 38px; padding: 0 14px;
  border-radius: 10px; font-weight: 700; font-size: 14px; white-space: nowrap;
}
.btn-ghost { border: 1px solid var(--line); background: #fff; }
.btn-ghost:hover { background: #f4f6fb; }
.btn-primary { border: 0; background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.06); }
.btn-primary:disabled { opacity: .5; cursor: default; }
.btn-primary.wide { width: 100%; justify-content: center; margin: 4px 0 12px; }

/* ── 方眼紙のキャンバス ── */
.viewport {
  position: fixed; inset: var(--topbar-h) 0 0 0; overflow: hidden; touch-action: none; cursor: grab;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-major) 1px, transparent 1px),
    linear-gradient(var(--grid-minor) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-minor) 1px, transparent 1px);
  background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;
}
.viewport.panning { cursor: grabbing; }
.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.edges { position: absolute; left: 0; top: 0; overflow: visible; width: 1px; height: 1px; pointer-events: none; }
.edges path { fill: none; stroke-linecap: round; }

/* ── ノード ── */
.node {
  position: absolute; left: 0; top: 0;
  width: max-content; max-width: 260px; padding: 8px 14px;
  border: 2px solid var(--c, #888); border-radius: var(--radius-node);
  background: #fff; color: var(--ink);
  font-size: 14px; line-height: 1.45; font-weight: 500;
  white-space: pre-wrap; word-break: break-word; text-align: center;
  cursor: pointer; user-select: none;
  box-shadow: 0 1px 2px rgba(30,40,70,.06);
  transition: box-shadow .15s;
}
.shape-square .node { border-radius: 3px; }
.node:hover { box-shadow: 0 4px 14px rgba(30,40,70,.14); }
.node.root {
  max-width: 300px; padding: 14px 24px; font-size: 20px; font-weight: 700;
  background: var(--c); color: var(--on-c, #fff); border-width: 0;
  box-shadow: 0 6px 18px rgba(30,40,70,.18);
}
.node.lv1 { background: var(--soft); color: var(--ink); font-weight: 700; font-size: 15px; padding: 9px 16px; }
.node.lv1.filled { background: var(--c); color: var(--on-c); }
.node.lv2plus { border-width: 1.5px; }
.node.selected { outline: 3px solid var(--accent); outline-offset: 3px; }
.node.drop { outline: 3px dashed var(--accent); outline-offset: 3px; }
.node .label:empty::before { content: attr(data-ph); opacity: .45; }
.node.editing { cursor: text; user-select: text; }
.node.editing .label { outline: none; }
.node .img {
  display: block; margin: 6px auto 2px; border-radius: calc(var(--radius-node) - 6px);
  width: var(--img-w, 220px); max-width: none; height: auto; pointer-events: none;
  background: #f3f5fa;
}
.shape-square .node .img { border-radius: 2px; }
.node.img-only { padding: 6px; max-width: none; background: #fff; color: var(--ink); border-width: 2px; }
.node.img-only .img { margin: 0; }
.node.img-only .label:empty { display: none; }
.node.img-only.editing .label:empty, .node.img-only.selected .label:empty { display: block; min-height: 1.4em; }
.node.loading .img { animation: pulse 1.2s infinite ease-in-out; }
@keyframes pulse { 50% { opacity: .5; } }

/* 枝を足す「＋」 */
.plus {
  position: absolute; top: 50%; width: 26px; height: 26px; margin-top: -13px;
  border-radius: 50%; border: 2px solid var(--c, #888); background: #fff; color: var(--c, #888);
  display: none; place-items: center; padding: 0; z-index: 2;
}
.plus svg { width: 14px; height: 14px; stroke-width: 3; }
.plus.r { right: -38px; }
.plus.l { left: -38px; }
.node:hover .plus, .node.selected .plus { display: grid; }
.plus:hover { background: var(--c); color: #fff; }
/* たたんだ印 */
.fold {
  position: absolute; top: 50%; transform: translateY(-50%);
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: var(--c, #888); color: #fff; font-size: 12px; font-weight: 700; line-height: 22px; text-align: center;
  border: 0;
}
.fold.r { right: -34px; } .fold.l { left: -34px; }
.node:hover .fold, .node.selected .fold { display: none; }

/* ── 選んだノードの道具 ── */
.node-bar {
  position: fixed; z-index: 25; display: flex; gap: 2px; padding: 4px;
  background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  max-width: calc(100vw - 16px); overflow-x: auto; scrollbar-width: none;
}
.node-bar[hidden] { display: none; }
.node-bar button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 52px; padding: 5px 6px; border: 0; border-radius: 8px; background: transparent;
  font-size: 11px; white-space: nowrap;
}
.node-bar button:hover { background: #eef1f7; }
.node-bar button.danger { color: var(--danger); }
.node-bar button[hidden] { display: none; }

/* ── ズーム ── */
.zoom-ctrl {
  position: fixed; left: 16px; bottom: 16px; z-index: 20; display: flex;
  background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden;
}
.zoom-ctrl button { width: 42px; height: 40px; border: 0; background: #fff; display: grid; place-items: center; }
.zoom-ctrl button + button { border-left: 1px solid var(--line); }
.zoom-ctrl button:hover { background: #f1f4fa; }

/* ── 動かし方の切り替え ── */
.move-ctrl {
  position: fixed; left: 150px; bottom: 16px; z-index: 20; display: flex; gap: 2px; padding: 3px;
  background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.move-ctrl button {
  display: flex; align-items: center; gap: 6px; height: 34px; padding: 0 12px;
  border: 0; border-radius: 9px; background: transparent; font-size: 13px; font-weight: 700; color: var(--muted); white-space: nowrap;
}
.move-ctrl button.on { background: var(--accent); color: #fff; }
.move-ctrl button:not(.on):hover { background: #f1f4fa; }
.move-ctrl button[hidden] { display: none; }
.viewport.mode-node .node:not(.root) { cursor: move; }
.node.dragging { box-shadow: 0 10px 26px rgba(30,40,70,.25); z-index: 5; }

.hint {
  position: fixed; left: 50%; bottom: 70px; transform: translateX(-50%); z-index: 15; margin: 0;
  max-width: calc(100vw - 200px); padding: 8px 14px; border-radius: 999px;
  background: rgba(34,40,58,.82); color: #fff; font-size: 13px; text-align: center; pointer-events: none;
  transition: opacity .4s;
}
.hint.hide { opacity: 0; }

/* ── 横から出るパネル ── */
.chat-panel, .maps-panel {
  position: fixed; top: calc(var(--topbar-h) + 8px); bottom: 8px; z-index: 28;
  width: 380px; max-width: calc(100vw - 16px);
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
  transition: transform .25s ease, opacity .25s;
}
.chat-panel { right: 8px; transform: translateX(calc(100% + 16px)); opacity: 0; pointer-events: none; }
.maps-panel { left: 8px; width: 300px; padding: 0 14px 14px; transform: translateX(calc(-100% - 16px)); opacity: 0; pointer-events: none; }
.chat-panel.open, .maps-panel.open { transform: none; opacity: 1; pointer-events: auto; }
.chat-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 10px 12px 16px; border-bottom: 1px solid var(--line); }
.maps-panel .chat-head { padding: 12px 0; margin-bottom: 10px; }
.chat-head small { display: block; color: var(--muted); font-size: 12px; font-weight: 500; }
.chat-log { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; padding: 10px 13px; border-radius: 14px; font-size: 14px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; }
.msg.ai { align-self: flex-start; background: #f1f4fa; border-top-left-radius: 4px; }
.msg.me { align-self: flex-end; background: var(--accent); color: #fff; border-top-right-radius: 4px; }
.msg.err { align-self: flex-start; background: #fdecef; color: #9b2338; }
.msg .note { display: block; margin-top: 6px; font-size: 12px; color: var(--muted); }
.msg.typing { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips[hidden] { display: none; }
.chips button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; font-size: 13px; }
.chips button:hover { border-color: var(--accent); color: var(--accent); }
.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); }
.chat-form textarea {
  flex: 1; resize: none; font: inherit; font-size: 15px; line-height: 1.5;
  border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; color: var(--ink);
}
.chat-form textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
.chat-form .btn-primary { height: auto; padding: 0 14px; }

.map-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.map-list li { display: flex; align-items: center; gap: 4px; border-radius: 10px; }
.map-list li.current { background: #eef3ff; }
.map-list .open { flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; padding: 10px; border-radius: 10px; }
.map-list .open:hover { background: #f1f4fa; }
.map-list .open strong { display: block; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.map-list .open small { color: var(--muted); font-size: 12px; }
.map-list .del { border: 0; background: transparent; width: 34px; height: 34px; border-radius: 8px; color: var(--muted); display: grid; place-items: center; }
.map-list .del:hover { color: var(--danger); background: #fdecef; }
.small-note { color: var(--muted); font-size: 12px; line-height: 1.6; margin: 8px 0 0; }

/* ── ポップオーバー ── */
.popover {
  position: fixed; top: calc(var(--topbar-h) + 6px); z-index: 35; width: 330px; max-width: calc(100vw - 16px);
  padding: 14px; background: #fff; border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
}
.popover[hidden] { display: none; }
.popover h3 { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.popover h3 + * { margin-bottom: 14px; }
.theme-list { display: grid; gap: 6px; }
.theme-list button {
  display: flex; align-items: center; gap: 10px; padding: 7px 10px; border: 2px solid transparent; border-radius: 10px;
  background: #f6f8fc; text-align: left; font-size: 14px;
}
.theme-list button.on { border-color: var(--accent); background: #fff; }
.swatches { display: flex; gap: 3px; }
.swatches i { width: 16px; height: 16px; border-radius: 50%; display: block; border: 1px solid rgba(0,0,0,.06); }
.seg { display: flex; gap: 6px; margin-top: 6px; }
.seg button { flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; padding: 8px; border: 2px solid var(--line); border-radius: 10px; background: #fff; font-size: 14px; }
.seg button.on { border-color: var(--accent); color: var(--accent); font-weight: 700; }
.shape-sample { width: 22px; height: 14px; border: 2px solid currentColor; display: inline-block; }
.shape-sample.round { border-radius: 6px; }
.shape-sample.square { border-radius: 1px; }
.menu-item { display: block; width: 100%; text-align: left; padding: 10px 12px; border: 0; border-radius: 8px; background: transparent; font-size: 14px; }
.menu-item:hover { background: #f1f4fa; }
#exportPop { width: 240px; padding: 6px; }

/* ── ダイアログ ── */
.dialog { border: 0; border-radius: 18px; padding: 22px; width: 440px; max-width: calc(100vw - 24px); box-shadow: var(--shadow); color: var(--ink); }
.dialog::backdrop { background: rgba(20,26,40,.4); }
.dialog h2 { margin: 0 0 4px; font-size: 18px; }
.field { display: block; margin-top: 14px; font-size: 13px; font-weight: 700; color: var(--muted); }
.field textarea, .text-input {
  display: block; width: 100%; margin-top: 6px; font: inherit; font-size: 15px; font-weight: 500; color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; resize: vertical;
}
.text-input { margin-top: 14px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.error { color: var(--danger); font-size: 13px; margin: 8px 0 0; }

.toast {
  position: fixed; left: 50%; top: calc(var(--topbar-h) + 14px); transform: translate(-50%, -10px); z-index: 60;
  padding: 10px 16px; border-radius: 12px; background: var(--ink); color: #fff; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.busy { position: fixed; inset: 0; z-index: 70; display: grid; place-content: center; justify-items: center; gap: 12px; background: rgba(255,255,255,.6); backdrop-filter: blur(2px); }
.busy[hidden] { display: none; }
.busy p { margin: 0; font-weight: 700; }
.spinner { width: 40px; height: 40px; border-radius: 50%; border: 4px solid #d6dff0; border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── スマホ ── */
@media (max-width: 720px) {
  .topbar { padding: 0 8px; gap: 4px; }
  .btn-ghost span, .btn-primary span { display: none; }
  .btn-ghost, .btn-primary { padding: 0 10px; }
  #btnRedo { display: none; }
  .map-title { font-size: 15px; }
  .chat-panel { left: 8px; right: 8px; width: auto; top: auto; height: 62vh; transform: translateY(calc(100% + 16px)); }
  .maps-panel { width: calc(100vw - 16px); }
  .hint { display: none; }
  .zoom-ctrl { left: 8px; bottom: 8px; }
  .move-ctrl { left: auto; right: 8px; bottom: 8px; }
  .move-ctrl span { display: none; }
  .move-ctrl button { padding: 0 10px; }
}
