:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #6b7280;
  --border: #e3e6eb;
  --accent: #e95420;
  --accent-soft: #fdece5;
  --ok: #1f9d55;
  --ok-soft: #e3f6ec;
  --ng: #c53030;
  --ng-soft: #fdeaea;
  --code-bg: #eef0f3;
  --term-bg: #1e1f29;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --panel: #1b1e24;
    --text: #e6e8ec;
    --muted: #9aa1ad;
    --border: #2c3039;
    --accent-soft: #3a2219;
    --ok-soft: #173325;
    --ng-soft: #3a1c1c;
    --code-bg: #2a2e37;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", Meiryo, sans-serif;
  display: flex;
  flex-direction: column;
}

code, kbd {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 0.92em;
}
code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
kbd {
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 5px;
  background: var(--panel);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand {
  font: inherit;
  font-weight: 700;
  font-size: 1.1rem;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.logo {
  font-family: ui-monospace, Consolas, monospace;
  background: var(--accent);
  color: #fff;
  padding: 2px 6px;
  border-radius: 5px;
  margin-right: 4px;
}
.progress { margin-left: auto; color: var(--muted); font-size: 0.9rem; text-align: right; }
.topbar .brand, .topbar .btn, .switch, .term-bar .btn { white-space: nowrap; }
.term-bar .status { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.term-bar { flex-wrap: wrap; row-gap: 4px; }
@media (max-width: 600px) {
  .topbar { gap: 10px; padding: 8px 12px; }
  .brand .name { display: none; }
  .progress { font-size: 0.78rem; }
}

.layout {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 220px minmax(320px, 1fr) minmax(420px, 1.3fr);
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--panel);
  overflow-y: auto;
  padding: 8px;
}
.lesson-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.lesson-item:hover { background: var(--code-bg); }
.lesson-item.active { background: var(--accent-soft); }
.lesson-item .name { font-weight: 600; font-size: 0.95rem; }
.lesson-item .name .done { color: var(--ok); margin-left: 4px; }
.lesson-item .sub { color: var(--muted); font-size: 0.8rem; margin-top: 2px; }

.lesson {
  min-width: 0;
  overflow-y: auto;
  padding: 20px 24px;
  line-height: 1.75;
}
.lesson-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.85rem; }
.lesson h2 { margin: 6px 0 12px; font-size: 1.35rem; }
.step-body ul, .step-body ol { padding-left: 1.3em; }
.step-body li { margin: 2px 0; }
.step-body pre {
  background: var(--term-bg);
  color: #e6e6e6;
  border-radius: 6px;
  padding: 10px 14px;
  overflow-x: auto;
  line-height: 1.5;
}
.step-body pre code { background: none; padding: 0; color: inherit; }
.step-body details { margin: 12px 0; }
.step-body .cmp { border-collapse: collapse; font-size: 0.85rem; margin: 10px 0; width: 100%; display: block; overflow-x: auto; }
.step-body .cmp th, .step-body .cmp td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; white-space: nowrap; }
.step-body .cmp th { background: var(--code-bg); }
.step-body summary { cursor: pointer; color: var(--accent); }

.section-label {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 0.04em;
  padding: 12px 10px 4px;
}
.section-label:first-child { padding-top: 4px; }

.commands { display: flex; flex-direction: column; gap: 6px; margin: 16px 0; }
.cmd {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--term-bg);
  color: #e6e6e6;
  border-radius: 6px;
  padding: 6px 6px 6px 12px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 0.9rem;
}
.cmd::before { content: "$"; color: #8be28b; }
.cmd .text { flex: 1; white-space: pre-wrap; word-break: break-all; }
.cmd button {
  border: 0;
  border-radius: 4px;
  background: #3b3f4d;
  color: #fff;
  font: inherit;
  font-family: system-ui, sans-serif;
  font-size: 0.78rem;
  padding: 4px 8px;
  cursor: pointer;
  white-space: nowrap;
}
.cmd button:hover { background: var(--accent); }

/* ---- コマンド解説 ---- */
.switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  cursor: pointer;
  user-select: none;
}
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track {
  position: relative;
  width: 34px;
  height: 20px;
  border-radius: 10px;
  background: var(--border);
  transition: background 0.15s;
}
.switch .knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track .knob { transform: translateX(14px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

.cmd { position: relative; }
.explain-on .cmdword {
  background: rgba(250, 204, 21, 0.28);
  border-radius: 3px;
  box-shadow: 0 0 0 2px rgba(250, 204, 21, 0.28);
  cursor: help;
}
.explain-on .cmdword:hover { background: rgba(250, 204, 21, 0.5); }
.cmd-tip {
  position: absolute;
  right: 0;
  bottom: calc(100% + 6px);
  z-index: 20;
  max-width: min(360px, 90%);
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  padding: 8px 12px;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", Meiryo, sans-serif;
  font-size: 0.85rem;
  line-height: 1.6;
  white-space: normal;
  pointer-events: none;
}
.cmd-tip[hidden] { display: none; }
/* 図解のボタンの中の吹き出し (ボタンは幅が狭いので左端にそろえる) */
.sim-cmd { position: relative; }
.sim-cmd .cmd-tip { display: block; left: 0; right: auto; width: max-content; max-width: min(340px, 80vw); text-align: left; font-weight: 400; }
.sim-cmd .cmd-tip[hidden] { display: none; }
.cmd-tip .tip-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.cmd-tip .tip-head code { font-weight: 700; }
.cmd-tip .tip-en { color: var(--muted); font-size: 0.78rem; }

.check { margin: 16px 0; }
.check-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.check-answer { width: 100%; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border); }
.check-answer[hidden] { display: none; }
.answer-label { font-weight: 700; font-size: 0.85rem; color: var(--accent); }
.check-answer .commands { margin: 8px 0; }
.answer-note { font-size: 0.88rem; margin: 6px 0 0; }
.check-box {
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.check-box .label { color: var(--muted); font-size: 0.9rem; }
.check-result { font-size: 0.9rem; }
.check-result.ok { color: var(--ok); font-weight: 600; }
.check-result.ng { color: var(--ng); }
.check-box.passed { border-style: solid; border-color: var(--ok); background: var(--ok-soft); }

.step-nav { display: flex; justify-content: space-between; margin-top: 24px; }

/* ---- 確認ダイアログ ---- */
.modal {
  width: min(420px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}
.modal::backdrop { background: rgba(10, 12, 18, 0.55); }
.modal[open] { animation: modal-in 0.15s ease-out; }
@keyframes modal-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@media (prefers-reduced-motion: reduce) { .modal[open] { animation: none; } }
.modal form { padding: 20px 22px 18px; }
.modal h3 { margin: 0 0 10px; font-size: 1.1rem; }
.modal-body { font-size: 0.92rem; line-height: 1.7; }
.modal-body p { margin: 0 0 8px; }
.modal-body .muted { color: var(--muted); font-size: 0.85rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.modal .btn.danger-fill { background: var(--ng); border-color: var(--ng); color: #fff; }
.modal .btn.danger-fill:hover { filter: brightness(1.1); }

/* ---- 図解シミュレーター ---- */
.sim:empty { display: none; }
.sim {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  padding: 12px 14px;
  margin: 16px 0;
}
.sim-label { font-weight: 700; font-size: 0.9rem; margin-bottom: 8px; }
.sim-label span { font-weight: 400; color: var(--muted); font-size: 0.8rem; }
/* コマンド例 (.cmd) と同じく縦に1つずつ並べる (吹き出しが左端から出て、右のターミナルに隠れない) */
.sim-cmds { display: flex; flex-direction: column; gap: 6px; }
.sim-cmd {
  display: flex;
  align-items: center;
  width: 100%;
  text-align: left;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 0.9rem;
  color: #e6e6e6;
  background: var(--term-bg);
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: pre-wrap;
  word-break: break-all;
}
.sim-cmd:hover { border-color: #555a6b; }
.sim-cmd.active { border-color: var(--accent); }
.sim-cmd .p { color: #8be28b; margin-right: 8px; flex: none; }
.sim-cmd .tick { color: #4ade80; margin-left: auto; padding-left: 10px; flex: none; }
.sim-out {
  margin: 10px 0 0;
  background: var(--term-bg);
  color: #e6e6e6;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 0.8rem;
  line-height: 1.5;
  overflow-x: auto;
  white-space: pre;
}
.sim-out .p { color: #8be28b; }
.sim-hint { color: var(--muted); font-size: 0.85rem; margin: 10px 0 0; }
.sim-diagram { margin-top: 10px; cursor: zoom-in; position: relative; }
.sim-diagram::after {
  content: "クリックで拡大";
  position: absolute;
  right: 4px;
  bottom: 2px;
  font-size: 0.7rem;
  color: var(--muted);
}
.sim-diagram svg { display: block; width: 100%; height: auto; }
.sim-diagram.zoom {
  position: fixed;
  inset: 0;
  z-index: 100;
  margin: 0;
  padding: 24px;
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}
.sim-diagram.zoom::after { content: "クリックで戻る"; right: 16px; bottom: 12px; font-size: 0.85rem; }
.sim-diagram.zoom svg { max-width: 1200px; max-height: 100%; }
.sim-steps { counter-reset: s; list-style: none; padding: 0; margin: 8px 0 0; font-size: 0.9rem; }
.sim-steps li { counter-increment: s; display: flex; gap: 8px; margin: 6px 0; line-height: 1.6; }
.sim-steps li::before {
  content: counter(s);
  flex: none;
  width: 20px;
  height: 20px;
  margin-top: 2px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.sim-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--muted);
}

/* 図 (SVG) */
.sim-diagram .node rect, .sim-diagram .node ellipse { fill: var(--bg); stroke: var(--muted); stroke-width: 1.5; }
.sim-diagram .node.on rect { fill: var(--ok-soft); stroke: var(--ok); }
.sim-diagram .node.off rect { stroke-dasharray: 4 3; }
.sim-diagram .node.focus rect { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2.5; }
.sim-diagram .node.cloud ellipse { stroke-dasharray: 5 4; }
.sim-diagram text { fill: var(--text); text-anchor: middle; font-size: 12px; font-family: inherit; }
.sim-diagram .t { font-weight: 700; font-size: 13px; }
.sim-diagram .s { fill: var(--muted); font-size: 11.5px; }
.sim-diagram .node.on .s, .sim-diagram .node.focus .s { fill: var(--text); }
.sim-diagram .note { fill: var(--muted); font-size: 10px; }
.sim-diagram .zone { fill: none; stroke: var(--border); stroke-width: 1.5; stroke-dasharray: 6 4; }
.sim-diagram .zone-t { text-anchor: start; fill: var(--muted); font-size: 11px; }
.sim-diagram .tree-line { stroke: var(--muted); stroke-width: 1.5; }
.sim-diagram .node rect.bar-bg { fill: var(--code-bg); stroke: none; }
.sim-diagram .node rect.bar-fill { fill: var(--ok); stroke: none; opacity: 0.55; }
.sim-diagram .port rect { stroke-width: 2; }
.sim-diagram .port.open rect { fill: var(--ok-soft); stroke: var(--ok); }
.sim-diagram .port.closed rect { fill: var(--bg); stroke: var(--muted); stroke-dasharray: 3 3; }
.sim-diagram .port text { font-weight: 700; font-size: 12px; }

.sim-diagram .flow line, .sim-diagram .flow path { fill: none; stroke-width: 2.5; stroke-dasharray: 7 5; animation: flow 0.9s linear infinite; }
.sim-diagram .flow.req line, .sim-diagram .flow.req path { stroke: var(--accent); }
.sim-diagram .flow.res line, .sim-diagram .flow.res path { stroke: var(--ok); }
.sim-diagram .flow.err line, .sim-diagram .flow.err path { stroke: var(--ng); }
.sim-diagram .ah.req, .sim-diagram .badge.req circle { fill: var(--accent); }
.sim-diagram .ah.res, .sim-diagram .badge.res circle { fill: var(--ok); }
.sim-diagram .ah.err, .sim-diagram .badge.err circle { fill: var(--ng); }
.sim-diagram .badge text { fill: #fff; font-size: 11px; font-weight: 700; }
.sim-diagram .lbl { text-anchor: start; font-size: 11.5px; font-weight: 600; paint-order: stroke; stroke: var(--panel); stroke-width: 4px; }
@keyframes flow { to { stroke-dashoffset: -24; } }
@media (prefers-reduced-motion: reduce) {
  .sim-diagram .flow line, .sim-diagram .flow path { animation: none; }
}

/* ---- 確認テスト ---- */
.quiz-q {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  margin: 12px 0;
  background: var(--panel);
}
.quiz-q.correct { border-color: var(--ok); }
.quiz-q .q { font-weight: 600; margin-bottom: 8px; }
.quiz-choices { display: flex; flex-direction: column; gap: 6px; }
.quiz-choices button {
  text-align: left;
  font: inherit;
  font-size: 0.92rem;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}
.quiz-choices button:hover:not(:disabled) { border-color: var(--accent); }
.quiz-choices button.right { border-color: var(--ok); background: var(--ok-soft); }
.quiz-choices button.wrong { border-color: var(--ng); background: var(--ng-soft); }
.quiz-choices button:disabled { cursor: default; }
.quiz-feedback { margin-top: 8px; font-size: 0.9rem; }
.quiz-feedback .ok { color: var(--ok); font-weight: 600; }
.quiz-feedback .ng { color: var(--ng); font-weight: 600; }
.quiz-summary {
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--code-bg);
  font-size: 0.95rem;
}
.quiz-summary.passed { background: var(--ok-soft); color: var(--ok); font-weight: 600; }

/* ---- 目次 ---- */
.toc {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  background: var(--bg);
  padding: 24px 16px 40px;
  z-index: 10;
}
.toc[hidden] { display: none; }
.toc-inner { max-width: 960px; margin: 0 auto; }
.toc-hero {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 20px;
  margin-bottom: 20px;
}
.toc-hero h1 { font-size: 1.3rem; margin: 0 0 4px; }
.toc-hero .meta { color: var(--muted); font-size: 0.9rem; }
.toc-hero .grow { flex: 1; min-width: 220px; }
.bar { height: 8px; border-radius: 4px; background: var(--code-bg); overflow: hidden; margin-top: 8px; }
.bar > span { display: block; height: 100%; background: var(--accent); }
.toc-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}
.toc-section {
  grid-column: 1 / -1;
  font-size: 1.05rem;
  margin: 12px 0 0;
  padding-bottom: 6px;
  border-bottom: 2px solid var(--accent);
}
.toc-section:first-child { margin-top: 0; }
.toc-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}
.toc-card.complete { border-color: var(--ok); }
.toc-card h3 { margin: 0; font-size: 1.02rem; }
.toc-card h3 .num { color: var(--accent); margin-right: 6px; }
.toc-card .sum { color: var(--muted); font-size: 0.85rem; margin: 2px 0 8px; }
.toc-card .count { font-size: 0.8rem; color: var(--muted); }
.toc-steps { list-style: none; padding: 0; margin: 10px 0 0; }
.toc-steps button {
  display: flex;
  gap: 8px;
  width: 100%;
  text-align: left;
  font: inherit;
  font-size: 0.88rem;
  color: inherit;
  background: none;
  border: 0;
  border-radius: 5px;
  padding: 3px 6px;
  cursor: pointer;
}
.toc-steps button:hover { background: var(--code-bg); }
.toc-steps .mark { width: 1.1em; color: var(--muted); flex: none; }
.toc-steps .mark.on { color: var(--ok); }
.toc-steps .tag {
  margin-left: auto;
  font-size: 0.72rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 5px;
  flex: none;
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.small { font-size: 0.8rem; padding: 3px 10px; }
.btn.danger:hover:not(:disabled) { border-color: var(--ng); color: var(--ng); }

.term-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--term-bg);
}
.term-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: #16171f;
  color: #aab;
  font-size: 0.8rem;
}
.term-bar .spacer { flex: 1; }
.term-bar .btn { background: #262833; border-color: #363948; color: #ddd; }
.status::before { content: "●"; margin-right: 5px; color: #888; }
.env-notice {
  color: #fbbf77;
  border: 1px solid #7a5a2e;
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 0.75rem;
  white-space: nowrap;
}
.env-notice[hidden] { display: none; }
.status.connected::before { color: #4ade80; }
.status.error::before { color: #f87171; }
#terminal { flex: 1; min-height: 0; padding: 6px 0 0 8px; }

/* ---- 現在地マップ ---- */
.fsmap { background: #1a1b24; border-bottom: 1px solid #2c2e3b; color: #cfd3dc; font-size: 0.8rem; }
.fsmap-head { display: flex; align-items: center; gap: 10px; padding: 4px 10px; }
.fsmap-toggle {
  background: none;
  border: 0;
  color: #cfd3dc;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  padding: 2px 0;
}
.fsmap-toggle .caret { display: inline-block; transition: transform 0.15s; }
.fsmap.closed .caret { transform: rotate(-90deg); }
.fsmap-move { color: #fbbf77; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fsmap-body {
  max-height: 28vh;
  overflow-y: auto;
  padding: 2px 0 6px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
}
.fsmap.closed .fsmap-body { display: none; }
.fsmap-empty { color: #8a90a0; padding: 4px 12px; font-family: system-ui, sans-serif; }
.fs-row { display: flex; align-items: center; gap: 5px; line-height: 1.55; white-space: nowrap; color: #9aa1b0; }
.fs-row .ic { font-size: 0.85em; }
.fs-row.path { color: #e6e8ec; }
.fs-row.here { background: rgba(233, 84, 32, 0.22); color: #fff; font-weight: 700; }
.fs-row.new { color: #86efac; }
.fs-row.gone { color: #f87171; }
.fs-row.gone .nm { text-decoration: line-through; }
.fs-row.more { color: #6b7280; font-style: italic; }
.fs-row .tag { font-family: system-ui, sans-serif; font-size: 0.72rem; font-weight: 700; margin-left: 6px; }
.fs-row .tag.here { color: #fbbf77; }
.fs-row .tag.new { color: #86efac; }
.fs-row .tag.gone { color: #f87171; }

@media (max-width: 1000px) {
  .layout { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto 60vh; overflow-y: auto; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); display: flex; overflow-x: auto; }
  .lesson-item { flex: 0 0 auto; width: auto; }
  .lesson-item .sub { display: none; }
  .lesson { overflow: visible; padding: 16px; }
}
