/* MDPop2 — super explorer style viewer & editor
   設計方針: ドキュメントが主役。UIは静かに、本文はGitHubスタイルで読みやすく。
   テーマはCSS変数で一元管理し、html[data-theme=dark] で切り替える。 */

:root {
  --bg: #ffffff;
  --bar-bg: #fafaf8;
  --border: #e4e2dd;
  --text: #1f2328;
  --muted: #6e7681;
  --accent: #2da44e;      /* 保存・正常系 */
  --accent-edit: #bc4c00; /* 編集モードの注意色 */
  --focus: #0969da;
  --btn-bg: #ffffff;
  --editor-bg: #fffdf9;   /* 編集中であることが分かる、ごく薄い紙色 */
  --hover: rgba(0, 0, 0, 0.05);
  --content-zoom: 1;      /* Ctrl+ホイール/±のコンテンツズーム */
  /* タブグループの色プリセット(GitHub系) */
  --g1: #cf222e; --g2: #bc4c00; --g3: #9a6700;
  --g4: #1a7f37; --g5: #0969da; --g6: #8250df;
}

/* ---------- ダークテーマ ---------- */
html[data-theme="dark"] {
  --bg: #0d1117;
  --bar-bg: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8d96a0;
  --accent: #3fb950;
  --accent-edit: #d29922;
  --focus: #4493f8;
  --btn-bg: #21262d;
  --editor-bg: #10151c;
  --hover: rgba(255, 255, 255, 0.07);
}
html[data-theme="dark"] body { color-scheme: dark; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Segoe UI", "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

/* ---------- ツールバー ---------- */
#toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  background: var(--bar-bg);
  border-bottom: 1px solid var(--border);
  user-select: none;
}

.file-info {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

#filename {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dirty {
  font-size: 11px;
  color: var(--accent-edit);
  white-space: nowrap;
}

.actions { display: flex; gap: 6px; }

button {
  font: inherit;
  font-size: 13px;
  padding: 5px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--btn-bg);
  color: var(--text);
  cursor: pointer;
}
.icon-btn { padding: 5px 8px; }
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.4; cursor: default; }
button:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

#btn-save:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* 編集モード中はトグルボタンの色で状態を示す */
body.editing #btn-toggle {
  background: var(--accent-edit);
  border-color: var(--accent-edit);
  color: #fff;
}

/* ---------- タブバー ---------- */
#tabbar {
  flex: 0 0 auto;
  display: flex;
  overflow-x: auto;
  background: var(--bar-bg);
  border-bottom: 1px solid var(--border);
  user-select: none;
  scrollbar-width: thin;
}

.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 220px;
  padding: 5px 6px 5px 12px;
  font-size: 12px;
  border-right: 1px solid var(--border);
  border-top: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  color: var(--muted);
}
.tab:hover { background: rgba(0, 0, 0, 0.04); }
.tab.active {
  background: var(--bg);
  color: var(--text);
  border-top-color: var(--focus);
  font-weight: 600;
}

/* フォルダ色分け: 左端の色帯＋タブ全面のごく薄いティント。
   ::before を z-index:-1 に置いて背景の上・文字の下へ潜り込ませる
   (そのため .tab 側で position:relative + z-index:0 のスタッキング文脈を作る)。 */
.tab.folder-tinted {
  position: relative;
  z-index: 0;
}
.tab.folder-tinted::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--tab-folder-color);
  opacity: 0.1;
  pointer-events: none;
}
.tab.folder-tinted.active::before { opacity: 0.16; }
html[data-theme="dark"] .tab.folder-tinted::before { opacity: 0.2; }
html[data-theme="dark"] .tab.folder-tinted.active::before { opacity: 0.3; }
/* グループ色ボーダーが付いているタブは、そちらを優先して二重に色を出さない */
.tab.folder-tinted:not(.grouped) { border-left: 3px solid var(--tab-folder-color); }

/* 複数選択(Ctrl/Shift+左クリック)中のタブ */
.tab.selected {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
  color: var(--text);
}

.tab-label {
  overflow: hidden;
  text-overflow: ellipsis;
}
.tab-dirty {
  flex: 0 0 auto;
  color: var(--accent-edit);
  font-size: 9px;
}
.tab-close {
  flex: 0 0 auto;
  padding: 0 5px;
  border-radius: 4px;
  color: var(--muted);
  font-size: 13px;
}
.tab-close:hover {
  background: rgba(0, 0, 0, 0.1);
  color: var(--text);
}

/* ---------- プロジェクト(ワークスペース)タブバー ---------- */
/* ファイルタブ(#tabbar)の上に重なる最上位タブ行。2つ以上開いたときだけ表示する。 */
#wsbar {
  flex: 0 0 auto;
  display: flex;
  align-items: stretch;
  overflow-x: auto;
  background: var(--border); /* タブバーより一段濃くして階層感を出す */
  border-bottom: 1px solid var(--border);
  user-select: none;
  scrollbar-width: thin;
}
.ws-tab {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 200px;
  padding: 4px 6px 4px 12px;
  font-size: 12px;
  border-right: 1px solid var(--border);
  border-top: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  color: var(--muted);
  background: var(--bar-bg);
}
.ws-tab:hover { background: rgba(0, 0, 0, 0.04); }
.ws-tab.active {
  background: var(--bg);
  color: var(--text);
  border-top-color: var(--accent); /* ファイルタブ(--focus青)と区別してプロジェクトは緑 */
  font-weight: 600;
}
.ws-tab-label { overflow: hidden; text-overflow: ellipsis; }
.ws-tab-close {
  flex: 0 0 auto;
  padding: 0 5px;
  border-radius: 4px;
  color: var(--muted);
  font-size: 13px;
}
.ws-tab-close:hover { background: rgba(0, 0, 0, 0.1); color: var(--text); }
.ws-tab-add {
  flex: 0 0 auto;
  border: none;
  background: transparent;
  padding: 2px 12px;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
}
.ws-tab-add:hover { background: rgba(0, 0, 0, 0.06); color: var(--text); }

/* ---------- タブグループ ---------- */
/* グループは見出しチップ＋所属タブを1つの色枠(.tab-group-box)で囲う(Chrome風)。
   枠が色を担当するので、所属タブ側には色ボーダーもフォルダ色も付けない。 */
.tab-group-box {
  display: flex;
  align-items: stretch;
  position: relative;
  z-index: 0;
  flex: 0 0 auto;
  margin: 3px 6px 3px 3px;
  border: 2px solid var(--tab-group-color, var(--muted));
  border-radius: 9px;
  overflow: hidden;
}
.tab-group-box::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--tab-group-color, var(--muted));
  opacity: 0.09;
  pointer-events: none;
}
html[data-theme="dark"] .tab-group-box::before { opacity: 0.22; }
.tab-group-box.dragging { opacity: 0.5; }

.tab-group {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: grab;
  white-space: nowrap;
  color: var(--text);
  user-select: none;
}
.tab-group:hover { background: rgba(0, 0, 0, 0.06); }
html[data-theme="dark"] .tab-group:hover { background: rgba(255, 255, 255, 0.08); }
.tab-group-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--tab-group-color, var(--muted));
  flex: 0 0 auto;
}
.tab-group-name {
  font-weight: 600;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* 折りたたみ中は見出しチップだけが残るので、単体のピルとして見えるようにする */
.tab-group-box.collapsed { border-radius: 999px; }
.tab-group-box.collapsed .tab-group { padding: 5px 12px; }

/* 枠の中のタブは外枠と地続きに見せる(左端の色ボーダーは枠に任せる) */
.tab.grouped {
  border-left: 1px solid var(--border);
  border-right: none;
}
.tab.grouped:last-child { border-right: none; }

/* ---------- パンくず(アドレスバー) ---------- */
#breadcrumb {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  background: var(--bar-bg);
  border-bottom: 1px solid var(--border);
  user-select: none;
}
.bc-nav { display: flex; gap: 2px; flex: 0 0 auto; }
.bc-nav button {
  padding: 3px 9px;
  font-size: 13px;
  line-height: 1.2;
}
.bc-path {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  height: 28px;
  padding: 0 4px;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: thin;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.bc-seg { display: inline-flex; align-items: center; flex: 0 0 auto; }
.bc-label,
.bc-chevron {
  border: none;
  background: transparent;
  padding: 3px 6px;
  font-size: 12px;
  color: var(--text);
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
}
.bc-chevron { padding: 3px 4px; color: var(--muted); }
.bc-label:hover,
.bc-chevron:hover { background: rgba(9, 105, 218, 0.10); color: var(--focus); }
.bc-filter {
  flex: 0 0 auto;
  width: 180px;
  padding: 3px 8px;
  font-size: 12px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.bc-filter:focus { outline: none; border-color: var(--focus); }

/* 絞り込みで非表示にするツリー項目 */
.tree-item.filtered-out { display: none !important; }

/* ---------- バージョン履歴バー ---------- */
#history-bar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 5px 10px;
  background: var(--bar-bg);
  border-bottom: 1px solid var(--border);
  user-select: none;
}
.hist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.hist-act {
  flex: 0 0 auto;
  padding: 3px 10px;
  font-size: 12px;
  line-height: 1.3;
  white-space: nowrap;
}
/* チップ列。版が増えたら横スクロールで逃がす(offsetLeftの基準にするため relative) */
.hist-chips {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}
.hist-chip {
  flex: 0 0 auto;
  padding: 3px 10px;
  font-size: 12px;
  line-height: 1.3;
  border-radius: 999px;
  color: var(--muted);
  white-space: nowrap;
}
.hist-chip:hover:not(:disabled) { color: var(--text); border-color: var(--muted); }
.hist-chip.active {
  background: var(--focus);
  border-color: var(--focus);
  color: #fff;
}
.hist-empty {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  padding: 3px 2px;
}
/* 閲覧モードであることを明示する帯 */
.hist-notice {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 4px 10px;
  border: 1px solid var(--accent-edit);
  border-radius: 6px;
  background: var(--hover);
}
.hist-notice-text {
  font-size: 12px;
  color: var(--accent-edit);
}

/* ---------- ワークスペース(サイドバー + メイン) ---------- */
#workspace {
  flex: 1 1 auto;
  display: flex;
  min-height: 0; /* flex子のoverflowを有効にする */
}

#sidebar {
  flex: 0 0 auto;
  width: 240px;
  display: flex;
  flex-direction: column;
  background: var(--bar-bg);
  border-right: 1px solid var(--border);
  user-select: none;
  overflow: hidden;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 8px 6px 12px;
  border-bottom: 1px solid var(--border);
}

#sidebar-root {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-actions { display: flex; gap: 4px; }
.sidebar-actions button { padding: 2px 8px; font-size: 12px; }

#btn-sidebar { padding: 5px 10px; }

/* 並び替えコントロール(エクスプローラー風) */
.sidebar-sort {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px 4px 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-sort select {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 12px;
  padding: 2px 4px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
}
.sidebar-sort #sort-dir {
  flex: 0 0 auto;
  padding: 2px 7px;
  font-size: 11px;
  line-height: 1.2;
}

.tree {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 0 16px;
  font-size: 13px;
}

.tree-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-item:hover { background: rgba(0, 0, 0, 0.05); }
.tree-item.active {
  background: rgba(9, 105, 218, 0.12);
  color: var(--focus);
  font-weight: 600;
}
/* エクスプローラ風の選択(.active=開いているタブの項目 とは別概念。併存可) */
.tree-item.selected {
  background: rgba(9, 105, 218, 0.18);
}
.tree-item.selected.active {
  background: rgba(9, 105, 218, 0.26);
}
/* アンカー/フォーカス項目に控えめなアウトライン */
.tree-item.sel-anchor {
  box-shadow: inset 0 0 0 1px rgba(9, 105, 218, 0.5);
}
/* D&D取り込み先フォルダのハイライト(選択より優先して見せる) */
.tree-item.drop-target {
  background: rgba(45, 164, 78, 0.18);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.tree-arrow {
  width: 12px;
  flex: 0 0 auto;
  font-size: 10px;
  color: var(--muted);
  text-align: center;
}
.tree-icon { flex: 0 0 auto; font-size: 12px; }
.tree-label { overflow: hidden; text-overflow: ellipsis; }

/* md系以外(既定アプリで開くファイル)はラベルを淡色にして見分けやすくする */
.tree-item.is-external .tree-label { color: var(--muted); }

.tree-empty {
  padding: 12px;
  color: var(--muted);
  font-size: 12px;
}

#sidebar-resizer {
  flex: 0 0 4px;
  cursor: col-resize;
  background: transparent;
}
#sidebar-resizer:hover { background: rgba(9, 105, 218, 0.25); }

/* ---------- メイン ---------- */
#container {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  position: relative;
}

#viewer {
  height: 100%;
  overflow-y: auto;
  padding: 32px clamp(20px, 6vw, 64px) 80px;
  max-width: 920px;
  margin: 0 auto;
}

/* タスクリストのチェックボックス: 表示モードでクリックしてON/OFFできる */
#viewer .task-list-item-checkbox {
  cursor: pointer;
}

#editor {
  width: 100%;
  height: 100%;
  border: none;
  resize: none;
  padding: 28px clamp(20px, 6vw, 64px);
  font-family: "Cascadia Code", Consolas, "BIZ UDGothic", monospace;
  font-size: 14px;
  line-height: 1.75;
  color: var(--text);
  background: var(--editor-bg);
  zoom: var(--content-zoom);
}
#editor:focus { outline: none; }

/* コンテンツズーム(本文のみ。UIバー類はそのまま) */
#viewer { zoom: var(--content-zoom); }

/* markdown-body の背景はアプリ側テーマに合わせる(vendor cssの白/黒を上書き) */
#viewer.markdown-body { background: transparent; }

/* ---------- 空状態 ---------- */
.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.9;
}
.empty-title {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--text);
  margin: 0;
}
.empty-hint { font-size: 12px; }

/* ---------- ホバー操作ガイド ---------- */
#hovertip {
  position: fixed;
  z-index: 1000;
  max-width: 460px;
  padding: 6px 10px;
  font-size: 11px;
  line-height: 2;
  color: #fff;
  background: rgba(31, 35, 40, 0.94);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  pointer-events: none; /* チップ自体はクリックを邪魔しない */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#hovertip kbd {
  display: inline-block;
  padding: 0 6px;
  margin: 0 2px;
  font-family: inherit;
  font-size: 10px;
  line-height: 1.6;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-bottom-width: 2px;
  border-radius: 4px;
}

#hovertip .tip-meta {
  color: rgba(255, 255, 255, 0.65);
  font-size: 10px;
}

/* ---------- ステータスバー ---------- */
#statusbar {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  padding: 4px 14px;
  font-size: 11px;
  color: var(--muted);
  background: var(--bar-bg);
  border-top: 1px solid var(--border);
  user-select: none;
}

/* ---------- 右クリック コンテキストメニュー ---------- */
.ctxmenu {
  position: fixed;
  z-index: 1100;
  min-width: 200px;
  max-width: 320px;
  max-height: 60vh;
  overflow-y: auto;
  padding: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  font-size: 13px;
  user-select: none;
}
.ctxmenu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  color: var(--text);
}
.ctxmenu-item:hover { background: rgba(9, 105, 218, 0.12); color: var(--focus); }
.ctxmenu-item.danger { color: #b42318; }
.ctxmenu-item.danger:hover { background: rgba(180, 35, 24, 0.1); color: #b42318; }
.ctxmenu-item.disabled {
  color: var(--muted);
  cursor: default;
  opacity: 0.5;
  pointer-events: none;
}
.ctxmenu-sep {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
}
/* 色見本の行(タブグループの色選択) */
.ctxmenu-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px 10px 8px;
}
.ctx-swatch {
  width: 20px;
  height: 20px;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}
.ctx-swatch:hover { border-color: var(--muted); }
.ctx-swatch.active { border-color: var(--text); }
.ctx-swatch:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

/* ---------- 確認/入力モーダル ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.28);
}
.modal {
  width: min(420px, 90vw);
  padding: 18px 20px 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}
.modal-title {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  cursor: move; /* タイトルバーをつかんでモーダルを移動できる */
  user-select: none;
}
.modal-message {
  margin: 0 0 12px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--muted);
  white-space: pre-wrap;
}
.modal-input {
  width: 100%;
  margin-bottom: 14px;
  padding: 7px 9px;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.modal-input:focus { outline: none; border-color: var(--focus); }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.modal-btn {
  padding: 6px 14px;
  font-size: 13px;
  border-radius: 6px;
}
.modal-btn.primary {
  color: #fff;
  background: var(--focus);
  border-color: var(--focus);
}
.modal-btn.primary.danger {
  background: #b42318;
  border-color: #b42318;
}

/* ---------- ユーティリティ ---------- */
.hidden { display: none !important; }

/* ドラッグオーバー表示 */
body.dragover #container::after {
  content: "ドロップして開く";
  position: absolute;
  inset: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 600;
  color: var(--focus);
  background: rgba(9, 105, 218, 0.06);
  border: 2px dashed var(--focus);
  border-radius: 10px;
  pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
  button { transition: border-color 0.15s, background 0.15s; }
}

/* ---------- 保存ドロップダウン(分割ボタン) ---------- */
.save-group { display: flex; }
.save-group #btn-save {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.save-group #btn-save-menu {
  padding: 5px 7px;
  font-size: 10px;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-left: 1px solid var(--border);
}
/* 保存可能時は本体ボタンと色を揃える(分割ボタンの一体感) */
.save-group #btn-save-menu:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  border-left-color: rgba(255, 255, 255, 0.4);
  color: #fff;
}

/* 編集ボタンの分割ドロップダウン(テキスト編集 / 表示まま編集) */
.edit-group { display: flex; }
.edit-group #btn-toggle {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.edit-group #btn-toggle-menu {
  padding: 5px 7px;
  font-size: 10px;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-left: 1px solid var(--border);
}
/* 表示まま編集で待機中は本体ボタンと色を揃える */
body.block-editing .edit-group #btn-toggle,
body.block-editing .edit-group #btn-toggle-menu {
  background: var(--accent-edit, var(--accent));
  border-color: var(--accent-edit, var(--accent));
  color: #fff;
}
body.block-editing .edit-group #btn-toggle-menu {
  border-left-color: rgba(255, 255, 255, 0.4);
}

/* コメントトグルがONのときの見た目 */
#btn-comments.active-toggle {
  background: rgba(210, 153, 34, 0.16);
  border-color: #d29922;
  color: #9a6a00;
}

/* ---------- コメント: 本文ハイライト ---------- */
mark.mdpop-comment {
  background: rgba(227, 179, 65, 0.27);
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
  cursor: text;
}
mark.mdpop-comment.active { background: rgba(227, 179, 65, 0.52); }
/* 番号はCSSで描くため、範囲選択や読み上げの本文へ混入しない。 */
.mdpop-comment-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  vertical-align: super;
  width: 17px;
  height: 17px;
  margin: 0 2px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  font: 600 10px/1 system-ui, sans-serif;
  color: #fff;
  background: #b78113;
  cursor: pointer;
  user-select: none;
}
.mdpop-comment-badge::before { content: attr(data-num); }
button.mdpop-comment-badge:hover { background: #926600; }
span.mdpop-comment-badge { cursor: default; }
.mdpop-comment-badge:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- コメント: サイドパネル ---------- */
#comment-panel {
  flex: 0 0 auto;
  width: 340px;
  display: flex;
  flex-direction: column;
  background: var(--bar-bg);
  border-left: 1px solid var(--border);
  overflow: hidden;
}

/* コメントパネル幅のリサイザ(左サイドバーと同じ作法) */
#comment-resizer {
  flex: 0 0 4px;
  cursor: col-resize;
  background: transparent;
}
#comment-resizer:hover { background: rgba(9, 105, 218, 0.25); }
.cp-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 8px 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 600;
}
#cp-close { padding: 2px 9px; font-size: 14px; line-height: 1; }
#comment-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.cp-empty {
  padding: 16px 12px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.7;
  text-align: center;
}

/* ---------- コメント: カード ---------- */
.comment-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  font-size: 12px;
  cursor: default;
}
.comment-card:hover { border-color: var(--muted); }
.comment-card.active {
  border-color: var(--focus);
  box-shadow: 0 0 0 1px var(--focus);
}
.comment-card.resolved { opacity: 1; }

.cc-quote {
  margin-bottom: 6px;
  padding: 2px 6px;
  font-size: 11px;
  color: var(--muted);
  background: rgba(227, 179, 65, 0.12);
  border-left: 3px solid #e3b341;
  border-radius: 0 4px 4px 0;
  overflow-wrap: anywhere;
}
.cc-num {
  display: inline-block;
  min-width: 16px;
  height: 16px;
  margin-right: 6px;
  font-size: 10px;
  font-weight: 600;
  line-height: 16px;
  text-align: center;
  color: #fff;
  background: #d29922;
  border-radius: 8px;
}
.cc-body {
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.cc-meta { margin-top: 6px; font-size: 10px; color: var(--muted); }

/* 元コメントと返信を、投稿者・日時が読める同じスレッドに表示する。 */
.cc-thread-header { display: flex; align-items: center; gap: 8px; margin-bottom: 9px; }
.cc-avatar {
  flex: 0 0 30px; height: 30px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600; background: #e4edf3; color: #285674;
}
.cc-avatar.assistant { background: #ece7f4; color: #65438d; }
.cc-person { flex: 1; min-width: 0; }
.cc-author { font-size: 12px; font-weight: 600; color: var(--text); overflow-wrap: anywhere; }
.cc-time { font-size: 10px; color: var(--muted); margin-top: 2px; }
.cc-state { font-size: 10px; color: var(--muted); background: var(--hover); padding: 2px 6px; border-radius: 4px; }
.cc-replies { margin: 12px 0 0 14px; padding-left: 12px; border-left: 2px solid var(--border); }
.cc-reply + .cc-reply { margin-top: 14px; }
.cc-reply .cc-avatar { flex-basis: 26px; height: 26px; font-size: 10px; }
.cc-reply .cc-thread-header { margin-bottom: 6px; }
.cc-reply-body { color: var(--text); white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.7; }
.cc-reply-more { margin-top: 4px; padding: 1px 6px; font-size: 10px; }
.cc-body { line-height: 1.7; }

.cc-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.cc-actions button { padding: 2px 8px; font-size: 11px; }
.cc-actions button.danger-text { color: #b42318; }
.cc-actions button.danger-text:hover { border-color: #b42318; }

/* 「Claude Codeで直す」チェック欄とカード強調 */
.cc-fix {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.cc-fix input { cursor: pointer; }
.comment-card.will-fix {
  border-color: var(--focus);
  background: rgba(9, 105, 218, 0.04);
}

/* コメントパネルのツールバー(Claude Codeで直す 実行ボタン) */
.cp-toolbar {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}
#cp-fix-btn { width: 100%; }
#cp-fix-btn:not(:disabled) {
  background: var(--focus);
  border-color: var(--focus);
  color: #fff;
}

/* ---------- 印刷 / PDF書き出し ---------- */
@media print {
  #toolbar, #wsbar, #tabbar, #breadcrumb, #history-bar, #sidebar, #sidebar-resizer, #statusbar,
  #comment-panel, #hovertip, #ctxmenu, #modal-overlay, #empty,
  #editor { display: none !important; }

  html, body { height: auto !important; overflow: visible !important; background: #fff !important; }
  body { display: block !important; }
  #workspace { display: block !important; min-height: 0 !important; }
  #container {
    display: block !important;
    overflow: visible !important;
    position: static !important;
  }
  #viewer {
    display: block !important;
    height: auto !important;
    max-width: none !important;
    overflow: visible !important;
    padding: 0 !important;
    margin: 0 !important;
  }
  /* PDFはクリーンに: コメントのハイライト/番号は印刷しない */
  mark.mdpop-comment { background: transparent !important; }
  .mdpop-comment-badge { display: none !important; }

  a { color: inherit !important; }
  pre, blockquote, table, img, .mermaid { break-inside: avoid; }
  h1, h2, h3, h4 { break-after: avoid; }
}
@page { margin: 16mm; }

/* ---------- 表示まま編集（ブロック単位インライン編集） ---------- */
/* 編集可能なブロックだけ、ホバーで触れることを示す。表示は崩さない */
body.block-editing #viewer [data-md-editable="1"] {
  cursor: text;
  border-radius: 4px;
  box-shadow: 0 0 0 1px transparent;
  transition: box-shadow 0.12s ease, background 0.12s ease;
}
body.block-editing #viewer [data-md-editable="1"]:hover {
  background: var(--hover);
  box-shadow: 0 0 0 1px var(--border);
}
/* 編集不可のブロック（コード・表・区切り線）は触れないことを示す */
body.block-editing #viewer [data-md-start]:not([data-md-editable]) {
  cursor: default;
  opacity: 0.72;
}

.block-edit-hidden { display: none !important; }

.block-edit-area {
  display: block;
  width: 100%;
  box-sizing: border-box;
  margin: 4px 0;
  padding: 8px 10px;
  font: inherit;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Courier New", monospace;
  font-size: 0.94em;
  line-height: 1.7;
  color: var(--text);
  background: var(--editor-bg);
  border: 1px solid var(--focus);
  border-radius: 6px;
  outline: none;
  resize: none;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}

/* ---------- あいまい編集の候補パネル ---------- */
#ai-panel {
  position: fixed;
  right: 20px;
  bottom: 44px;
  z-index: 1150;
  width: 420px;
  max-width: calc(100vw - 40px);
  max-height: 62vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.22);
  font-size: 13px;
}
#ai-panel.hidden { display: none; }

.ai-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.ai-title { font-weight: 600; flex: 1; }
.ai-close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}
.ai-close:hover { background: var(--hover); color: var(--text); }

.ai-quote {
  padding: 6px 10px;
  margin: 8px 10px 0;
  border-left: 3px solid var(--accent);
  background: var(--hover);
  border-radius: 0 4px 4px 0;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 5.4em;
  overflow: hidden;
}
.ai-quote.hidden, .ai-note.hidden { display: none; }

.ai-note {
  margin: 8px 10px 0;
  padding: 5px 8px;
  background: rgba(191, 135, 0, 0.12);
  border: 1px solid rgba(191, 135, 0, 0.35);
  border-radius: 4px;
  color: var(--text);
}

.ai-body { flex: 1; overflow-y: auto; padding: 8px 10px; }
.ai-loading { color: var(--muted); padding: 14px 4px; text-align: center; }
.ai-error { color: #b42318; padding: 8px 4px; }
.ai-raw {
  margin-top: 6px;
  padding: 6px 8px;
  background: var(--hover);
  border-radius: 4px;
  font-size: 11px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 12em;
  overflow-y: auto;
}

.ai-card {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
  margin-bottom: 8px;
  cursor: pointer;
  background: var(--bg);
  transition: border-color 0.12s ease, background 0.12s ease;
}
.ai-card:hover, .ai-card:focus {
  border-color: var(--focus);
  background: var(--hover);
  outline: none;
}
.ai-card-head { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.ai-card-text { white-space: pre-wrap; word-break: break-word; line-height: 1.65; }

.ai-foot {
  padding: 6px 10px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 11px;
}
