:root {
  --font-size: 15px;
  --mono: "SF Mono", "Consolas", "MS Gothic", "Osaka-Mono", monospace;
  --bg: #1e1e1e;
  --bg2: #252526;
  --fg: #d4d4d4;
  --gutter-bg: #1a1a1a;
  --gutter-fg: #6a6a6a;
  --kw: #569cd6;
  --builtin: #4ec9b0;
  --str: #ce9178;
  --num: #b5cea8;
  --com: #6a9955;
  --op: #d4d4d4;
  --err: #f14c4c;
  --line: 23px; /* 整数px（15px font × 1.5=22.5 の端数を排除）。JSがfont変更時に再計算 */
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--mono);
  display: flex;
  flex-direction: column;
}

#toolbar, #status, #msxToolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg2);
  border-bottom: 1px solid #333;
}
.brand { font-weight: bold; color: #9cdcfe; }
.spacer { flex: 1; }

/* メニューバー */
#menubar {
  display: flex; align-items: stretch;
  background: var(--bg2); border-bottom: 1px solid #333;
  font-size: 13px; user-select: none; position: relative; z-index: 100;
}
.menu { position: relative; }
.mtitle { display: block; padding: 5px 12px; color: #ccc; cursor: default; }
.mtitle-app { font-weight: bold; color: #9cdcfe; }
.menu.open .mtitle, .menu:hover .mtitle { background: #0e639c; color: #fff; }
.dropdown {
  display: none; position: absolute; top: 100%; left: 0; min-width: 240px;
  background: #2b2b2b; border: 1px solid #444; border-radius: 0 0 5px 5px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.45); padding: 4px 0; z-index: 101;
}
.menu.open .dropdown { display: block; }
.mi {
  display: flex; justify-content: space-between; gap: 24px; align-items: center;
  padding: 5px 14px 5px 26px; color: #ddd; cursor: pointer; white-space: nowrap;
}
.mi:hover { background: #0e639c; color: #fff; }
.mi .sc { color: #888; font-size: 11px; }
.mi:hover .sc { color: #cde; }
.mi.checked::before { content: "✓"; position: absolute; margin-left: -16px; }
.sep { height: 1px; background: #444; margin: 4px 8px; }
#filename {
  background: #333; color: var(--fg); border: 1px solid #444;
  padding: 4px 8px; border-radius: 4px; font-family: var(--mono); width: 160px;
}
button {
  background: #0e639c; color: #fff; border: none; padding: 5px 10px;
  border-radius: 4px; cursor: pointer; font-family: var(--mono); font-size: 13px;
}
button:hover { background: #1177bb; }
.fontctl { color: #aaa; font-size: 12px; }
.fontctl button { background: #444; padding: 3px 7px; }

/* タブ列（2グループ） */
#tabstrips { display: flex; background: var(--bg2); border-bottom: 1px solid #333; }
.tabstrip { display: flex; flex: 1; min-width: 0; overflow-x: auto; }
.tabstrip[hidden] { display: none; }
.tabstrip.dropping { background: rgba(14, 99, 156, 0.18); }
#stripB { border-left: 1px solid #333; }
.tab {
  display: flex; align-items: center; gap: 8px;
  background: transparent; color: #aaa; border: none; border-radius: 0;
  border-bottom: 2px solid transparent; padding: 8px 10px 8px 14px;
  white-space: nowrap; cursor: pointer; font-family: var(--mono); font-size: 13px;
  user-select: none;
}
.tab:hover { color: #ccc; background: rgba(255,255,255,0.04); }
.tab.active { color: #fff; border-bottom-color: #0e639c; background: var(--bg); }
.tab[draggable="true"]:active { cursor: grabbing; }
.tab-x {
  background: none; border: 0; color: inherit; cursor: pointer;
  font-size: 11px; line-height: 1; padding: 2px 3px; border-radius: 3px;
  opacity: 0; color: #999;
}
.tab:hover .tab-x, .tab.active .tab-x { opacity: 1; }
.tab-x:hover { background: rgba(255,255,255,0.15); color: #fff; }

/* 作業領域: 左サイドバー＋右コンテンツ */
#main { flex: 1; display: flex; min-height: 0; }
#content { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

/* プロジェクトツリー（左サイドバー）*/
#sidebar { width: 220px; flex: none; background: var(--bg2); border-right: 1px solid #333; display: flex; flex-direction: column; min-height: 0; }
#sidebar[hidden] { display: none; }
#sidebarHead { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px; border-bottom: 1px solid #333; font-size: 12px; color: #bbb; font-weight: 600; }
.sb-btn { background: #333; color: var(--fg); border: 1px solid #555; border-radius: 4px; padding: 0 8px; font-size: 14px; cursor: pointer; line-height: 1.7; }
.sb-btn:hover { background: #454545; }
#fileTree { flex: 1; overflow: auto; padding: 4px 0; font-size: 13px; }
.ft-row { display: flex; align-items: center; gap: 6px; padding: 3px 8px 3px 12px; cursor: pointer; white-space: nowrap; color: var(--fg); }
.ft-row:hover { background: #2a2d2e; }
.ft-row.active { background: #37373d; }
.ft-ico { opacity: .85; }
.ft-main { color: #6a9955; font-size: 11px; opacity: .6; }      /* 自動判定エントリ（薄め） */
.ft-main.pinned { color: #4ec9b0; opacity: 1; }                  /* 明示mainは濃く */
.ft-name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.ft-act { visibility: hidden; color: #aaa; padding: 0 3px; border: 0; background: none; cursor: pointer; font-size: 12px; }
.ft-row:hover .ft-act { visibility: visible; }
.ft-act:hover { color: #fff; }
.ft-group { padding: 8px 10px 2px; font-size: 11px; color: #888; letter-spacing: .04em; }
/* 「データファイル」等、ヘッダに追加ボタンを持つグループ。＋は常時表示。 */
.ft-group-act { display: flex; align-items: center; justify-content: space-between; }
.ft-group-act .ft-act { visibility: visible; font-size: 14px; line-height: 1; }
/* サイドバーのインライン名前変更ボックス */
.ft-rename { font: inherit; flex: 1 1 auto; min-width: 0; background: #2a2d2e; color: #eee;
  border: 1px solid #007acc; border-radius: 2px; padding: 0 3px; margin: -1px 0; outline: none; }
.ft-row.node-webmsx { color: #9cdcfe; }

/* ペイン群: 既定は単一表示(タブ)、.split で左右2分割 */
#panes { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#panes.split { flex-direction: row; }
#panes.split > .pane + .pane { border-left: 1px solid #333; }
.pane { flex: 1; min-height: 0; min-width: 0; display: flex; overflow: hidden; }
.pane[hidden] { display: none; }
/* 構造化ペインは縦積み：上に検索バー（通常フロー）、下にエディタ行。検索バーの有無で
   テキスト層/ガターがブロックごと押し下がるだけなので、レイヤ間のズレが起きない。 */
#structuredPane { flex-direction: column; }
#editRow { display: flex; flex: 1; min-height: 0; min-width: 0; overflow: hidden; }

/* 構造化エディタ: gutter + 重ね合わせ(highlight + textarea) */
#gutter {
  width: 56px; flex: none; background: var(--gutter-bg); color: var(--gutter-fg);
  text-align: right; padding: 8px 6px 8px 0; overflow: hidden;
  font-size: var(--font-size); line-height: var(--line); user-select: none;
  white-space: pre;
}
#gutter .gl { position: relative; cursor: pointer; }
#gutter .gl:hover { color: #ccc; }
#gutter .gl.err { color: var(--err); }            /* エラー行は番号も赤く */
#gutter .gl.err:hover { color: var(--err); }
#gutter .x {
  color: var(--err); position: absolute; left: 0; top: 0; cursor: help;
  font-weight: bold; font-size: 1.35em; line-height: var(--line);
}
#gutter .bm { color: #e2c044; margin-right: 2px; font-size: 0.8em; }

#editWrap { position: relative; flex: 1; min-width: 0; }
#highlight, #src {
  margin: 0; padding: 8px 10px; border: 0;
  font-family: var(--mono); font-size: var(--font-size); line-height: var(--line);
  white-space: pre; overflow: auto; tab-size: 4;
  position: absolute; inset: 0; width: 100%; height: 100%;
}
#highlight { color: var(--fg); pointer-events: none; z-index: 1; }
#src {
  color: transparent; background: transparent; caret-color: #fff;
  resize: none; z-index: 2; outline: none;
}
#src::selection { background: #264f78; }

/* 現在行ハイライト（テキストエリア背後の帯。z-index 0 で透過テキスト越しに見える）*/
#curline {
  position: absolute; left: 0; right: 0; z-index: 0;
  background: rgba(255, 255, 255, 0.05); pointer-events: none;
}

/* MSX出力は行 <div> で描画（全幅ハイライト＋行クリック）*/
#msxOut .mln { display: block; white-space: pre; margin: 0; }
#msxOut .mln:hover { background: rgba(255, 255, 255, 0.045); }
#msxOut .mln.hl { background: rgba(90, 200, 130, 0.22); box-shadow: inset 3px 0 0 rgba(90, 200, 130, 0.9); }

/* 単純検索バー（タブ直下に全幅ドッキング。PhpStorm の in-file 検索風）*/
#findbar {
  flex: 0 0 auto;                 /* 通常フローでエディタ上部に積む（絶対配置をやめる）*/
  background: var(--bg2); border-bottom: 1px solid #444;
  padding: 5px 8px; box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
  display: flex; flex-direction: column; gap: 5px;
}
#findbar[hidden], #searchResults[hidden] { display: none; }
.findbar-row { display: flex; align-items: center; gap: 5px; }
.findbar-row[hidden] { display: none; }
#findbar input {
  flex: 1; min-width: 0;
  background: #333; color: var(--fg); border: 1px solid #555; border-radius: 4px;
  padding: 4px 8px; font-size: 13px; outline: none;
}
#findbar input:focus { border-color: #0e639c; }
#findCount { font-size: 12px; color: #999; min-width: 44px; text-align: center; }
.findbtn {
  background: #3a3a3a; color: var(--fg); border: 1px solid #555; border-radius: 4px;
  padding: 3px 7px; font-size: 12px; cursor: pointer; line-height: 1.2;
}
.findbtn:hover { background: #454545; }
.findbtn.wide { padding: 3px 10px; }
.findbtn.on { background: #0e639c; border-color: #0e639c; color: #fff; }

/* 全体検索（Find in Files）: 中央の独立ウィンドウ＋背景オーバーレイ */
#searchResults {
  position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, 0.5);
  display: flex; align-items: flex-start; justify-content: center; padding-top: 6vh;
}
.gf-window {
  width: 78%; max-width: 980px; height: 72%; background: var(--bg2);
  border: 1px solid #555; border-radius: 8px; display: flex; flex-direction: column;
  overflow: hidden; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}
.gf-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; font-size: 15px; font-weight: 600; color: var(--fg);
  border-bottom: 1px solid #383838;
}
.gf-head { display: flex; align-items: center; gap: 6px; padding: 8px 12px; border-bottom: 1px solid #383838; }
.gf-head input {
  flex: 1; min-width: 0;
  background: #333; color: var(--fg); border: 1px solid #555; border-radius: 4px;
  padding: 5px 9px; font-size: 13px; outline: none;
}
.gf-head input:focus { border-color: #0e639c; }
#gfindCount { font-size: 12px; color: #999; min-width: 90px; text-align: right; }
#gfindList { flex: 1; overflow: auto; font-family: var(--mono); font-size: 13px; }
.gf-file {
  position: sticky; top: 0; background: #2d2d30; color: #9cdcfe;
  padding: 3px 10px; font-size: 12px; border-bottom: 1px solid #383838;
}
.gf-row { display: flex; gap: 10px; padding: 2px 10px 2px 22px; cursor: pointer; white-space: pre; }
.gf-row:hover { background: #2a2d2e; }
.gf-ln { color: #6a9955; min-width: 38px; text-align: right; }
.gf-txt { color: var(--fg); overflow: hidden; text-overflow: ellipsis; }

/* 読み取り専用ライブラリ表示中のエディタ表示 */
#editWrap.readonly #src { caret-color: transparent; background: rgba(255, 200, 0, 0.03); }
#filename:read-only { color: #9cdcfe; opacity: .85; cursor: default; }

/* 設定: チェックボックス行 */
.set-check {
  display: flex; align-items: center; gap: 8px; margin: 6px 0;
  color: var(--fg); font-size: 13px; cursor: pointer;
}
.set-check input { width: 15px; height: 15px; cursor: pointer; }

/* シンタックス色 */
.t-kw { color: var(--kw); }
.t-builtin { color: var(--builtin); }
.t-str { color: var(--str); }
.t-num { color: var(--num); }
.t-com { color: var(--com); font-style: italic; }
/* ニーモニック注釈コメント('@): 通常コメントと別色。MSX変換時に除去される目印 */
.t-mnem { color: #c8924a; font-style: italic; }
.t-op { color: var(--op); }
.errline { background: rgba(241,76,76,0.12); }

/* MSXプレビュー */
#msxPane { flex-direction: column; }
#msxToolbar { justify-content: space-between; }
#msxNote { color: #aaa; font-size: 12px; }
#msxOut {
  flex: 1; margin: 0; padding: 10px; overflow: auto;
  font-family: var(--mono); font-size: var(--font-size); line-height: var(--line);
  white-space: pre; color: var(--fg);
}
#msxPane.error #msxOut { color: #888; }

/* 変換テーブル（読み取り専用）*/
#maptablePane { flex-direction: column; }
#maptableToolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 10px; background: var(--bg2); border-bottom: 1px solid #333;
}
#maptableNote { color: #aaa; font-size: 12px; }
#maptableOut { flex: 1; overflow: auto; padding: 12px 14px; font-size: 13px; color: var(--fg); }
.mt-sec { margin: 0 0 20px; }
.mt-sec h3 {
  font-size: 13px; color: #9cdcfe; font-weight: 600; margin: 0 0 8px;
  border-bottom: 1px solid #333; padding-bottom: 4px;
}
table.mt { border-collapse: collapse; font-family: var(--mono); font-size: 12px; }
table.mt th { text-align: left; color: #888; font-weight: normal; padding: 2px 20px 4px 0; }
table.mt td { padding: 1px 20px 1px 0; vertical-align: top; }
.mt-orig { color: var(--fg); }
.mt-msx { color: #4ec9b0; }                 /* MSX 2文字名（テール）*/
.mt-empty { color: #777; font-style: italic; font-size: 12px; }
.mt-fn {
  margin: 0 0 12px; padding: 8px 10px; background: rgba(255, 255, 255, 0.02);
  border-radius: 4px; border-left: 2px solid #3a3a3a;
}
.mt-fn-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.mt-fn-name { font-family: var(--mono); font-weight: 600; color: #dcdcaa; } /* 関数名（黄）*/
.mt-fn-entry { font-size: 11px; color: #888; }
.mt-fn-sig { font-family: var(--mono); font-size: 12px; margin: 5px 0 0; color: var(--fg); }
.mt-lbl { color: #888; }
.mt-ref {
  color: #ce9178; font-size: 10px; border: 1px solid #5a4a3a;
  border-radius: 3px; padding: 0 3px; margin-left: 2px;
}
.mt-locals { margin-top: 7px; }
.mt-locals .mt-lbl { display: block; margin-bottom: 3px; }

/* アプリ内 WebMSX 実行パネル */
#webmsxPane { flex-direction: column; }
#webmsxNote { color: #aaa; font-size: 12px; padding: 6px 10px; }
#webmsxFrame { flex: 1; width: 100%; border: 0; background: #000; }

#status { font-size: 13px; color: #bbb; }
#status.ok { color: #4ec9b0; }
#status.warn { color: #d7a35a; }
#status.err { color: var(--err); }
#status.clickable { cursor: pointer; }
#modalBody { white-space: pre-line; } /* リネームのプレビュー等で改行を活かす */

/* INCLUDE パス補完のドロップダウン（editWrap 基準の絶対配置） */
#acbox { position: absolute; z-index: 50; min-width: 180px; max-width: 60%; max-height: 40vh; overflow: auto;
  background: #252526; border: 1px solid #454545; border-radius: 4px; box-shadow: 0 4px 12px rgba(0,0,0,.5); font-size: 13px; }
#acbox .aci { padding: 3px 10px; cursor: pointer; white-space: nowrap; color: #ddd; }
#acbox .aci.sel { background: #094771; color: #fff; }
#acbox .aci:hover { background: #2a2d2e; }

/* Problems パネル（ステータス直上・全ファイル横断の診断一覧） */
#problems { flex: none; max-height: 34vh; overflow: auto; background: var(--bg2); border-top: 1px solid #333; font-size: 12px; }
#problems .ph { position: sticky; top: 0; background: var(--bg2); display: flex; justify-content: space-between; align-items: center; padding: 4px 10px; color: #bbb; font-weight: 600; border-bottom: 1px solid #333; }
#problems .pclose { cursor: pointer; color: #888; padding: 0 4px; }
#problems .pclose:hover { color: #fff; }
#problems .pr { display: flex; gap: 8px; align-items: baseline; padding: 3px 10px; cursor: pointer; white-space: nowrap; }
#problems .pr:hover { background: #2a2d2e; }
#problems .pr.here .pf { font-weight: 600; }
#problems .pf { color: #6ab0f3; }
#problems .pl { color: #888; }
#problems .pr.err .pc { color: var(--err); }
#problems .pr.warn .pc { color: #d7a35a; }
#problems .pc { font-weight: 600; }
#problems .pm { color: #ddd; overflow: hidden; text-overflow: ellipsis; }
#problems .pcreate { margin-left: auto; flex: none; background: #0e639c; color: #fff; border: 0; border-radius: 3px; padding: 1px 8px; font-size: 11px; cursor: pointer; }
#problems .pcreate:hover { background: #1177bb; }

/* 自前モーダル */
#modal {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, 0.5);
  display: flex; align-items: center; justify-content: center;
}
#modal[hidden] { display: none; }
.modal-box {
  background: #2b2b2b; border: 1px solid #444; border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  min-width: 360px; max-width: 90vw; max-height: 85vh; overflow: auto;
  padding: 18px 20px;
}
.modal-title { font-weight: bold; color: #9cdcfe; font-size: 15px; margin-bottom: 10px; }
.modal-body {
  margin: 0; color: var(--fg); font-family: var(--mono); font-size: 13px;
  line-height: 1.5; white-space: pre-wrap;
}
.modal-input {
  margin-top: 12px; width: 100%; background: #333; color: var(--fg);
  border: 1px solid #555; border-radius: 4px; padding: 6px 8px;
  font-family: var(--mono); font-size: 14px;
}
.modal-input[hidden] { display: none; }
.modal-actions { margin-top: 16px; text-align: right; display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.modal-custom-btn { background: #3a3a3a; }
.modal-custom-btn:hover { background: #4a4a4a; }
.modal-actions button { padding: 6px 18px; }
.modal-actions button[hidden] { display: none; }
#modalCancel { background: #444; }
#modalCancel:hover { background: #555; }

/* 設定ダイアログ（#modal と同じオーバーレイ体裁を流用） */
#settings {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, 0.5);
  display: flex; align-items: center; justify-content: center;
}
#settings[hidden] { display: none; }
.settings-box { min-width: 420px; }
.set-section {
  margin: 16px 0 6px; padding-top: 10px; border-top: 1px solid #444;
  font-weight: bold; color: #9cdcfe; font-size: 13px;
}
.set-row {
  display: flex; align-items: center; gap: 10px; margin-top: 10px;
}
.set-label { flex: 0 0 130px; color: var(--fg); font-size: 13px; }
.set-row select, .set-row input {
  flex: 1; background: #333; color: var(--fg);
  border: 1px solid #555; border-radius: 4px; padding: 6px 8px;
  font-family: var(--mono); font-size: 13px;
}
.set-row input[type="number"] { flex: 0 0 80px; }
.set-row-top { align-items: flex-start; }
.set-row textarea {
  flex: 1; background: #333; color: var(--fg);
  border: 1px solid #555; border-radius: 4px; padding: 6px 8px;
  font-family: var(--mono); font-size: 13px; resize: vertical; min-height: 3em;
}
.set-hint { margin: 4px 0 0 140px; color: #9a9a9a; font-size: 11px; }

/* エラー箇所推測ダイアログ（#modal と同じオーバーレイ体裁を流用） */
#errEst {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0, 0, 0, 0.5);
  display: flex; align-items: center; justify-content: center;
}
#errEst[hidden] { display: none; }
.errest-box { min-width: 520px; max-width: 760px; }
.ee-desc { color: #9a9a9a; font-size: 12px; line-height: 1.5; margin-bottom: 10px; }
#eeRun { flex: 0 0 auto; padding: 6px 16px; }
.ee-result { margin-top: 14px; font-family: var(--mono); font-size: 12.5px; line-height: 1.55; max-height: 44vh; overflow: auto; }
.ee-result:empty { display: none; }
.ee-msx { background: #1e1e1e; border: 1px solid #444; border-radius: 6px; padding: 8px 10px; margin-bottom: 10px; white-space: pre-wrap; word-break: break-all; }
.ee-lineno { color: #dcdcaa; font-weight: bold; }
.ee-hit { border-left: 3px solid #c586c0; padding: 3px 0 3px 10px; margin: 6px 0; }
.ee-op { color: #9cdcfe; font-weight: bold; }
.ee-arg { color: #ce9178; }
.ee-con { color: var(--fg); }
.ee-src { color: #6a9955; margin: 6px 0; }
.ee-src a, .ee-nearby a { color: #4ec9b0; cursor: pointer; text-decoration: underline; }
.ee-note { color: #d7ba7d; margin: 8px 0; }
.ee-nearby { margin-top: 10px; border-top: 1px solid #444; padding-top: 8px; }
.ee-nearby-item { padding: 2px 0; }
.ee-empty { color: #9a9a9a; }
