* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN",
               "Yu Gothic", Meiryo, sans-serif;
}
body { background: #f3f4f6; }

/* ---------- toolbar ---------- */
#toolbar {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 8px; align-items: center;
  background: #fff; border: 1px solid #e5e7eb; border-radius: 12px;
  padding: 6px; box-shadow: 0 6px 24px rgba(0,0,0,.08); z-index: 100;
}
.tool-group {
  display: flex; gap: 4px; align-items: center;
  padding: 0 6px; border-right: 1px solid #eee;
}
.tool-group:last-child { border-right: none; }
#toolbar button {
  min-width: 34px; height: 34px; padding: 0 8px; border: none; border-radius: 8px;
  background: transparent; cursor: pointer; font-size: 16px; color: #374151;
  display: inline-flex; align-items: center; justify-content: center;
  white-space: nowrap;
}
#toolbar button:hover { background: #f3f4f6; }
#toolbar button.tool.active { background: #111827; color: #fff; }
#zoomLabel { font-size: 12px; min-width: 38px; display: inline-block; }

.swatch {
  position: relative; width: 34px; height: 34px;
  border-radius: 8px; overflow: hidden; display: inline-flex;
  border: 1px solid #e5e7eb;
}
.swatch input[type=color] {
  position: absolute; inset: -6px; width: 46px; height: 46px;
  border: none; padding: 0; cursor: pointer; background: none;
}
.width-wrap input { width: 90px; }

/* ホバーで開くサブメニュー */
.tool-wrap { position: relative; display: inline-flex; }
.tool.has-options { position: relative; }
.tool.has-options::after {
  content: ''; position: absolute; right: 3px; bottom: 3px;
  border-left: 5px solid transparent; border-top: 5px solid currentColor; opacity: .35;
}
.has-caret::after { content: '▾'; margin-left: 4px; opacity: .6; font-size: 11px; }

.flyout {
  position: absolute; top: 100%; left: 0; padding-top: 8px;
  display: none; z-index: 200;
}
.tool-wrap:hover .flyout,
.tool-wrap:focus-within .flyout,
.tool-wrap.open .flyout { display: block; }
.flyout-card {
  background: #fff; border: 1px solid #e5e7eb; border-radius: 10px;
  box-shadow: 0 10px 28px rgba(0,0,0,.14); padding: 10px; min-width: 200px;
  display: flex; flex-direction: column; gap: 8px;
}
.flyout-title { font-size: 11px; font-weight: 600; color: #9ca3af; padding: 0 2px 2px; }
.opt { display: flex; align-items: center; justify-content: space-between; gap: 14px; white-space: nowrap; }
.opt-label { font-size: 13px; color: #374151; }
.opt input[type=range] { width: 120px; }

/* 色ピッカー（プリセット / 手動） */
.opt-col { display: flex; flex-direction: column; gap: 6px; }
.color-ctl { display: flex; flex-direction: column; gap: 6px; }
#toolbar .seg { display: inline-flex; align-self: flex-start; border: 1px solid #e5e7eb; border-radius: 8px; overflow: hidden; }
#toolbar .seg-btn { height: 24px; min-width: 0; padding: 0 8px; font-size: 11px; background: #fff; color: #374151; border-radius: 0; }
#toolbar .seg-btn.active { background: #111827; color: #fff; }
.presets { display: flex; flex-wrap: wrap; gap: 6px; }
#toolbar .preset { width: 22px; height: 22px; min-width: 0; padding: 0; border: 1px solid rgba(0,0,0,.2); border-radius: 50%; }
#toolbar .preset.selected { outline: 2px solid #111827; outline-offset: 1px; }
.presets[hidden], .manual-swatch[hidden] { display: none !important; }
.menu-item {
  justify-content: flex-start !important; width: 100%; height: auto !important;
  min-width: 0 !important; padding: 8px 10px !important; font-size: 13px !important;
  border-radius: 8px; text-align: left;
}

/* ---------- stage ---------- */
#stage {
  position: fixed; inset: 0; overflow: hidden;
  touch-action: none;
  background-image: radial-gradient(circle, #d7d9de 1px, transparent 1px);
  background-size: 24px 24px;
  background-position: 0 0;
}
#penLayer {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; overflow: visible;
}
#penRoot path { fill: none; stroke-linecap: round; stroke-linejoin: round; }

#canvasLayer { position: absolute; top: 0; left: 0; transform-origin: 0 0; }

/* ---------- memo ---------- */
.memo {
  position: absolute; width: 220px; min-height: 120px;
  background: #fff7cc; border: 1px solid #f1e58a; border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,.12);
  display: flex; flex-direction: column; overflow: hidden;
}
.memo-header {
  height: 24px; background: rgba(0,0,0,.06); cursor: move; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: flex-end;
  padding: 0 6px; user-select: none;
}
.memo-close {
  cursor: pointer; font-size: 14px; line-height: 1; color: #6b7280;
  padding: 2px 5px; border-radius: 4px;
}
.memo-close:hover { background: rgba(0,0,0,.12); color: #111; }
.memo-body {
  flex: 1; padding: 8px 10px; outline: none;
  font-size: 14px; line-height: 1.5; color: #333;
  white-space: normal; word-break: break-word; overflow: auto;
}
.memo-body:empty::before { content: "メモを入力…"; color: #b9a94a; }

/* 編集中は生Markdownを等幅で表示 */
.memo-body.editing {
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Courier New", monospace;
}

/* Markdownレンダリング */
.memo-body h1, .memo-body h2, .memo-body h3, .memo-body h4 { margin: .3em 0; line-height: 1.25; }
.memo-body h1 { font-size: 1.4em; }
.memo-body h2 { font-size: 1.2em; }
.memo-body h3 { font-size: 1.05em; }
.memo-body p  { margin: .35em 0; }
.memo-body ul, .memo-body ol { margin: .35em 0; padding-left: 1.3em; }
.memo-body li.task { list-style: none; margin-left: -1.15em; }
.memo-body li.task input { margin-right: .35em; }
.memo-body code {
  background: rgba(0,0,0,.07); padding: 0 .25em; border-radius: 3px;
  font-family: ui-monospace, Consolas, monospace; font-size: .92em;
}
.memo-body pre { background: rgba(0,0,0,.07); padding: 6px 8px; border-radius: 6px; overflow: auto; }
.memo-body pre code { background: none; padding: 0; }
.memo-body blockquote { margin: .35em 0; padding-left: .6em; border-left: 3px solid rgba(0,0,0,.2); color: #555; }
.memo-body a { color: #2563eb; text-decoration: underline; }
.memo-body hr { border: none; border-top: 1px solid rgba(0,0,0,.25); margin: .45em 0; }
.memo-body table { border-collapse: collapse; margin: .4em 0; font-size: .92em; }
.memo-body th, .memo-body td { border: 1px solid rgba(0,0,0,.25); padding: 2px 6px; }
.memo-body th { background: rgba(0,0,0,.06); }

/* 右下リサイズハンドル */
.memo-resize {
  position: absolute; right: 0; bottom: 0; width: 16px; height: 16px;
  cursor: nwse-resize;
  background: linear-gradient(135deg, transparent 0 50%, rgba(0,0,0,.28) 50% 100%);
  border-bottom-right-radius: 8px;
}

/* ---------- text node ---------- */
.text-node {
  position: absolute; outline: none; padding: 2px 4px; min-width: 8px;
  font-size: 20px; line-height: 1.3; color: #1f2937;
  white-space: pre; width: max-content; cursor: text; border-radius: 4px;
}
.text-node:empty::before { content: "文字"; color: #9ca3af; }

.text-node.selected, .memo.selected { outline: 2px solid #3b82f6; outline-offset: 2px; }

/* ---------- cursors per tool ---------- */
#stage[data-tool="select"] { cursor: default; }
#stage[data-tool="pen"]    { cursor: crosshair; }
#stage[data-tool="eraser"] { cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Ccircle cx='12' cy='12' r='9' fill='rgba(255,255,255,0.65)' stroke='%23555' stroke-width='1.5'/%3E%3C/svg%3E") 12 12, crosshair; }
#stage[data-tool="text"]   { cursor: text; }
#stage[data-tool="memo"]   { cursor: copy; }
#stage[data-tool="line"],
#stage[data-tool="ellipse"],
#stage[data-tool="rect"],
#stage[data-tool="marquee"] { cursor: crosshair; }
#stage.panning             { cursor: grabbing !important; }

/* in pen mode, let strokes be drawn over memos/texts */
#stage[data-tool="pen"] .memo,    #stage[data-tool="pen"] .text-node,
#stage[data-tool="eraser"] .memo, #stage[data-tool="eraser"] .text-node,
#stage[data-tool="line"] .memo,   #stage[data-tool="line"] .text-node,
#stage[data-tool="ellipse"] .memo,#stage[data-tool="ellipse"] .text-node,
#stage[data-tool="rect"] .memo,   #stage[data-tool="rect"] .text-node,
#stage[data-tool="marquee"] .memo,#stage[data-tool="marquee"] .text-node { pointer-events: none; }

/* ---------- hint ---------- */
/* 範囲選択 */
#marquee {
  position: absolute; z-index: 40; pointer-events: none;
  border: 1.5px dashed #3b82f6; background: rgba(59,130,246,.12);
}
#selbox {
  position: absolute; pointer-events: none;
  border: 2px dashed #3b82f6; background: rgba(59,130,246,.06);
}

/* トースト通知（容量警告など） */
#toast {
  position: fixed; left: 50%; bottom: 18px;
  transform: translateX(-50%) translateY(20px);
  max-width: min(520px, calc(100vw - 24px));
  background: #111827; color: #fff; padding: 11px 15px; border-radius: 10px;
  font-size: 13px; line-height: 1.5; box-shadow: 0 8px 24px rgba(0,0,0,.25);
  z-index: 300; opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; cursor: pointer; }
#toast.warn  { background: #92400e; }   /* 予防警告（アンバー） */
#toast.error { background: #991b1b; }   /* 保存失敗（レッド） */

/* ボード一覧（上バーの flyout 内） */
#boardsBtn { max-width: 160px; overflow: hidden; text-overflow: ellipsis; }
#boardsList {
  display: flex; flex-direction: column; gap: 4px;
  max-height: 50vh; overflow: auto; min-width: 200px;
}
.board-row {
  display: flex; align-items: center; gap: 4px;
  border-radius: 8px; padding: 2px;
}
.board-row.current { background: #eef2ff; }
.board-name {
  flex: 1; min-width: 0; text-align: left; cursor: pointer;
  border: none; background: transparent; border-radius: 6px;
  padding: 8px 10px; font-size: 14px; color: #374151;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.board-name:hover { background: rgba(0,0,0,.05); }
.board-row.current .board-name { font-weight: 600; color: #1f2937; }
.board-act {
  flex: 0 0 auto; width: 30px; height: 30px; border: none; cursor: pointer;
  background: transparent; border-radius: 6px; font-size: 13px; color: #6b7280;
}
.board-act:hover { background: rgba(0,0,0,.08); color: #111; }
#boardsNew {
  width: 100%; padding: 9px; border: 1px dashed #c7cbd1; border-radius: 8px;
  background: #fff; cursor: pointer; font-size: 13px; color: #374151;
}
#boardsNew:hover { background: #f3f4f6; }

/* ヘルプ */
#helpBtn {
  position: fixed; bottom: 12px; left: 12px; z-index: 60;
  width: 34px; height: 34px; border-radius: 50%;
  border: 1px solid #e5e7eb; background: #fff; cursor: pointer;
  font-size: 15px; color: #374151; box-shadow: 0 4px 12px rgba(0,0,0,.12);
}
#helpBtn:hover { background: #f3f4f6; }
#helpPanel {
  position: fixed; bottom: 56px; left: 12px; z-index: 61;
  width: 330px; max-height: 60vh; overflow: auto;
  background: #fff; border: 1px solid #e5e7eb; border-radius: 12px;
  box-shadow: 0 10px 28px rgba(0,0,0,.18);
}
.help-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; border-bottom: 1px solid #eee; font-weight: 600; font-size: 14px;
}
.help-head button { border: none; background: transparent; cursor: pointer; font-size: 18px; color: #6b7280; }
#helpBody {
  margin: 0; padding: 12px; white-space: pre-wrap; word-break: break-word;
  font-family: inherit; font-size: 13px; line-height: 1.6; color: #333;
}

/* ===== スマホ: ツールバーを右側に縦並び ===== */
@media (max-width: 600px) {
  #toolbar {
    top: 8px; right: 8px; left: auto; bottom: auto; transform: none;
    flex-direction: column; gap: 6px; padding: 6px 4px;
    max-width: calc(100vw - 16px);
  }
  .tool-group {
    flex-direction: column; gap: 4px;
    padding: 4px 0; border-right: none; border-bottom: 1px solid #eee;
  }
  .tool-group:last-child { border-bottom: none; }

  /* サブメニューは左側に開く */
  .flyout { top: 0; right: 100%; left: auto; padding-top: 0; padding-right: 8px; }
  .flyout-card { min-width: 170px; }
  .has-caret::after { content: ''; margin: 0; }

  #helpPanel { width: min(330px, calc(100vw - 24px)); }
}
