:root {
  --bg: #eef2f7; --panel: #ffffff; --panel2: #f6f8fb; --fg: #172033; --muted: #64708a; --line: #dde3ec;
  --accent: #00a091; --accent-2: #00786d; --primary: #2563eb; --primary-2: #1d4ed8; --danger: #dc2626; --run: #16a34a;
  --chip: #e8edf4; --serial-bg: #0f1726; --serial-fg: #c9f7d5; --sel: #e6f6f4; --sel-line: #00a091;
  --stage-bg: #eef2f6; --grid: #dfe5ee; --label: #344055; --shadow: 0 8px 26px rgba(15, 23, 42, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220; --panel: #121b2d; --panel2: #0f1727; --fg: #e5ebf5; --muted: #93a0b8; --line: #24314a;
    --chip: #1c2840; --serial-bg: #070c16; --sel: #0f2e2c; --stage-bg: #1a2436; --grid: #232f45; --label: #cdd6e6;
    --shadow: 0 8px 26px rgba(0, 0, 0, .35); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1220; --panel: #121b2d; --panel2: #0f1727; --fg: #e5ebf5; --muted: #93a0b8; --line: #24314a;
  --chip: #1c2840; --serial-bg: #070c16; --sel: #0f2e2c; --stage-bg: #1a2436; --grid: #232f45; --label: #cdd6e6;
  --shadow: 0 8px 26px rgba(0, 0, 0, .35); color-scheme: dark;
}

* { box-sizing: border-box; }
html { height: 100%; }
body { margin: 0; min-height: 100%; }
body {
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  color: var(--fg); background: var(--bg); overflow-x: hidden;
}
a { color: var(--primary); }
code, pre { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; }
.pad { padding: 24px; }
.spacer { flex: 1; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
[hidden] { display: none !important; }

/* ---------- 상단 ---------- */
.topbar {
  height: 52px; display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--fg); text-decoration: none; white-space: nowrap; }
.brand svg { width: 34px; height: 22px; color: var(--accent); }
.nav { display: flex; gap: 4px; }
.nav a { padding: 6px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 600; }
.nav a.on { background: var(--sel); color: var(--accent-2); }
:root[data-theme="dark"] .nav a.on { color: #5eead4; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.icon-btn { width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; }
.chip { border: 1px solid var(--line); background: var(--chip); border-radius: 999px; padding: 4px 10px; cursor: pointer; font-size: 12px; display: inline-flex; align-items: center; gap: 6px; max-width: 260px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #9aa3b2; flex: none; }
.dot.ok { background: #16a34a; } .dot.bad { background: #dc2626; } .dot.warn { background: #f59e0b; }

/* ---------- 버튼 ---------- */
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 14px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-2); }
.btn.primary:disabled { opacity: .6; cursor: progress; }
.btn.run { background: var(--run); border-color: var(--run); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 4px 10px; font-size: 12px; }
.btn.key { min-width: 34px; }
.link-btn { border: 0; background: none; color: var(--primary); cursor: pointer; font-size: 12px; padding: 0 4px; margin-left: auto; }

/* ---------- 작업공간 (한 화면 3단) ---------- */
.workspace {
  display: grid; grid-template-columns: minmax(300px, 340px) minmax(360px, 1fr) minmax(400px, 1.15fr);
  gap: 10px; padding: 10px; height: calc(100vh - 52px);
}
.pane { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; min-height: 0; min-width: 0; overflow: hidden; }
.pane-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); min-height: 50px; }
.pane-head h2 { font-size: 15px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pane-body { overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; min-height: 0; flex: 1; }
.block { display: flex; flex-direction: column; gap: 8px; }
.block-title { font-weight: 700; font-size: 13px; display: flex; align-items: center; gap: 6px; }

/* 구성품 카드 */
.parts-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 6px; }
.part {
  border: 1.5px solid var(--line); background: var(--panel2); border-radius: 10px; padding: 4px 4px 6px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11.5px; line-height: 1.25; text-align: center; position: relative;
}
.part svg { width: 100%; height: 58px; }
.part:hover { border-color: var(--muted); }
.part.on { border-color: var(--sel-line); background: var(--sel); box-shadow: inset 0 0 0 1px var(--sel-line); }
.part.on::after { content: "✓"; position: absolute; top: 3px; right: 6px; color: var(--accent); font-weight: 800; }
.part.acc { opacity: .55; cursor: default; }

textarea, input[type=text], input:not([type]), input[type=password], select {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; background: var(--panel2); color: var(--fg); padding: 8px 10px; font: inherit;
}
textarea { resize: vertical; min-height: 64px; }
.suggest { display: flex; flex-wrap: wrap; gap: 5px; }
.suggest button { border: 1px solid var(--line); background: var(--chip); border-radius: 999px; padding: 2px 9px; font-size: 12px; cursor: pointer; }
.gen-row { display: flex; align-items: center; gap: 8px; }
.gen-row .btn.primary { margin-left: auto; }
.check { display: inline-flex; gap: 5px; align-items: center; font-size: 12.5px; color: var(--muted); cursor: pointer; }

/* 에이전트 기록 */
.log { display: flex; flex-direction: column; gap: 8px; }
.msg { border-radius: 10px; padding: 8px 10px; font-size: 13px; border: 1px solid var(--line); background: var(--panel2); }
.msg.user { background: var(--chip); align-self: flex-end; max-width: 92%; }
.msg.err { border-color: #fca5a5; background: #fef2f2; color: #991b1b; }
:root[data-theme="dark"] .msg.err { background: #2a1215; color: #fecaca; border-color: #7f1d1d; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .msg.err { background: #2a1215; color: #fecaca; border-color: #7f1d1d; } }
.msg h4 { margin: 0 0 4px; font-size: 14px; }
.msg ul { margin: 4px 0 0; padding-left: 18px; }
.msg .warn { color: #b45309; }
.msg .stream { font-family: ui-monospace, Consolas, monospace; font-size: 11px; color: var(--muted); max-height: 80px; overflow: hidden; white-space: pre-wrap; word-break: break-all; }
.spin { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: k-spin .8s linear infinite; vertical-align: -2px; margin-right: 4px; }
.history summary { cursor: pointer; font-weight: 700; font-size: 13px; }
.history ul { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.history li button { width: 100%; text-align: left; border: 1px solid var(--line); background: var(--panel2); border-radius: 8px; padding: 6px 8px; cursor: pointer; font-size: 12.5px; }
.history li button small { color: var(--muted); display: block; }

/* ---------- 코드 ---------- */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; margin-left: auto; flex: none; }
.seg button { border: 0; background: var(--panel); padding: 6px 12px; cursor: pointer; font-weight: 700; font-size: 12.5px; }
.seg button.on { background: var(--accent); color: #fff; }
.code-tabs { display: flex; align-items: center; gap: 2px; padding: 6px 10px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.code-tabs button { border: 0; background: none; padding: 6px 10px; cursor: pointer; border-bottom: 2px solid transparent; font-weight: 600; color: var(--muted); }
.code-tabs button.on { color: var(--fg); border-bottom-color: var(--accent); }
.code-tabs .tool { font-size: 12px; font-weight: 500; color: var(--primary); }
.code-body { flex: 1; min-height: 0; position: relative; }
.editor-host, .blocks-view { position: absolute; inset: 0; }
.editor-host .CodeMirror { height: 100%; font-size: 13px; }
.blocks-view { overflow: auto; background: #f4f6fa; padding: 10px; }
:root[data-theme="dark"] .blocks-view { background: #e9edf3; }
.blocks-view img { max-width: none; display: block; }
.ex-info { border-top: 1px solid var(--line); padding: 8px 12px; font-size: 12.5px; max-height: 32%; overflow: auto; }
.ex-info p { margin: 0 0 4px; }
.ex-info .tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.ex-info .tag { background: var(--chip); border-radius: 999px; padding: 1px 8px; font-size: 11.5px; }
.ex-info .warn { color: #b45309; }

/* ---------- 시뮬레이션 ---------- */
.run-ctl { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.status { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--chip); color: var(--muted); }
.status.running { background: #dcfce7; color: #166534; }
.status.error { background: #fee2e2; color: #991b1b; }
.stage { background: var(--stage-bg); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; flex: none; }
.stage-svg { width: 100%; height: auto; display: block; touch-action: manipulation; user-select: none; }
.stage-gridline { stroke: var(--grid); stroke-width: 1; }
.k-label-text { font-size: 17px; font-weight: 700; fill: var(--label); }
.k-value-text { font-size: 16px; fill: var(--muted); font-family: ui-monospace, Consolas, monospace; }
.k-key.down .k-cap { transform-box: fill-box; transform-origin: center; transform: scale(.88); }
@keyframes k-spin { to { transform: rotate(360deg); } }
.mb-svg .mb-touch { cursor: pointer; }
.mb-svg .mb-button.down .btn-cap { fill: #000; }
.mb-label { fill: #e9e9e9; font: 700 30px system-ui, sans-serif; }
.mb-pinlabel { fill: #3a2a05; font: 700 22px system-ui, sans-serif; }

.controls { display: flex; flex-direction: column; gap: 6px; }
.ctl { display: flex; gap: 8px; align-items: center; border: 1px solid var(--line); border-radius: 10px; padding: 6px 8px; background: var(--panel2); }
.ctl-ic { width: 40px; height: 40px; flex: none; }
.ctl-ic svg { width: 100%; height: 100%; }
.ctl-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.ctl-head { display: flex; gap: 6px; align-items: center; font-size: 12.5px; flex-wrap: wrap; }
.pinbadge { font: 600 11px ui-monospace, Consolas, monospace; background: #fff6cc; color: #6b5300; border-radius: 6px; padding: 0 6px; }
:root[data-theme="dark"] .pinbadge { background: #3d3410; color: #ffe58a; }
.ctl-out { margin-left: auto; font: 12px ui-monospace, Consolas, monospace; color: var(--accent-2); }
.ctl-input { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-size: 12px; }
.ctl-input input[type=range] { flex: 1; min-width: 120px; accent-color: var(--accent); }
.ctl-val { min-width: 92px; font-family: ui-monospace, Consolas, monospace; }
.board-ctl { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; font-size: 12.5px; padding-top: 6px; }
.board-ctl label { display: flex; align-items: center; gap: 6px; }
.board-ctl input { flex: 1; min-width: 0; accent-color: var(--accent); }
.board-ctl output { min-width: 40px; font-family: ui-monospace, monospace; }
#board-ctl summary { cursor: pointer; font-weight: 700; font-size: 13px; }
.serial pre { margin: 0; background: var(--serial-bg); color: var(--serial-fg); border-radius: 10px; padding: 8px 10px; height: 120px; overflow: auto; font-size: 12px; white-space: pre-wrap; word-break: break-all; }
.serial-in input { margin-top: 4px; font-size: 12px; padding: 5px 8px; }

/* ---------- 문서 (사용법 · 튜토리얼) ---------- */
.doc { padding: 20px 16px 60px; }
.doc-body { max-width: 920px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px 32px; box-shadow: var(--shadow); }
.doc-body h1 { margin-top: 0; font-size: 26px; }
.doc-body h2 { margin-top: 32px; border-bottom: 1px solid var(--line); padding-bottom: 6px; font-size: 19px; }
.doc-body h3 { font-size: 15.5px; margin-top: 20px; }
.doc-body table { border-collapse: collapse; width: 100%; font-size: 13px; }
.doc-body th, .doc-body td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
.doc-body th { background: var(--panel2); }
.doc-body code { background: var(--chip); padding: 1px 5px; border-radius: 5px; font-size: 12.5px; }
.doc-body pre { background: var(--serial-bg); color: #e2e8f0; padding: 12px 14px; border-radius: 10px; overflow: auto; }
.doc-body pre code { background: none; padding: 0; color: inherit; }
.doc-body .note { background: var(--sel); border-left: 4px solid var(--accent); padding: 10px 14px; border-radius: 8px; }
.toc { display: flex; flex-wrap: wrap; gap: 6px; }
.toc a { background: var(--chip); padding: 3px 10px; border-radius: 999px; font-size: 12.5px; text-decoration: none; color: var(--fg); }
.part-table svg { width: 56px; height: 56px; }
.steps { counter-reset: step; display: flex; flex-direction: column; gap: 14px; padding: 0; list-style: none; }
.steps > li { border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px 14px 60px; position: relative; background: var(--panel2); }
.steps > li::before { counter-increment: step; content: counter(step); position: absolute; left: 16px; top: 14px; width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: #fff; font-weight: 800; display: grid; place-items: center; }
.steps h3 { margin: 2px 0 6px; }
.steps .do { margin-top: 8px; }
.layout-fig { display: grid; grid-template-columns: 1fr 1.3fr 1.4fr; gap: 6px; margin: 12px 0; }
.layout-fig div { border: 2px dashed var(--line); border-radius: 10px; padding: 14px 8px; text-align: center; font-weight: 700; font-size: 13px; }

/* ---------- 모달 · 토스트 ---------- */
.modal { border: 0; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 40px); background: var(--panel); color: var(--fg); box-shadow: 0 30px 80px rgba(0, 0, 0, .35); }
.modal.wide { width: min(1200px, calc(100vw - 24px)); height: calc(100vh - 40px); }
.modal::backdrop { background: rgba(10, 16, 28, .5); }
.modal-body { padding: 20px 22px; display: flex; flex-direction: column; gap: 10px; height: 100%; }
.modal h2 { margin: 0; font-size: 18px; }
.providers { border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; display: flex; flex-direction: column; gap: 4px; }
.providers legend { font-weight: 700; font-size: 13px; padding: 0 4px; }
.providers label { cursor: pointer; font-size: 13.5px; }
.pv { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--panel2); }
.pv label { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; font-weight: 600; }
.pv p { margin: 0; }
pre.cmd { position: relative; background: var(--serial-bg); color: #e2e8f0; padding: 9px 70px 9px 12px; border-radius: 8px; margin: 0; overflow: auto; font-size: 12.5px; }
pre.cmd .copy { position: absolute; right: 6px; top: 5px; border: 0; border-radius: 6px; padding: 3px 9px; font-size: 11.5px; cursor: pointer; background: #334155; color: #fff; }
.modal-foot { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
#mc-frame { flex: 1; width: 100%; border: 1px solid var(--line); border-radius: 10px; background: #fff; min-height: 0; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 100; font-size: 13.5px; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }
#mc-renderer { position: absolute; left: -10px; top: 0; width: 1px; height: 1px; border: 0; opacity: 0; pointer-events: none; }

/* ---------- 반응형 ---------- */
@media (max-width: 1240px) {
  .workspace { grid-template-columns: minmax(280px, 320px) 1fr; grid-template-rows: 1fr 1fr; height: auto; min-height: calc(100vh - 52px); }
  .agent-pane { grid-row: 1 / span 2; }
  .code-pane { min-height: 520px; }
  .sim-pane { min-height: 600px; }
}
@media (max-width: 860px) {
  .topbar { gap: 8px; padding: 0 10px; }
  .brand span { display: none; }
  .nav a { padding: 6px 8px; }
  .provider-chip { display: none; }
  .workspace { display: flex; flex-direction: column; padding: 8px; height: auto; }
  .pane { min-height: 0; }
  .agent-pane .pane-body { max-height: none; }
  .code-pane { height: 560px; }
  .doc-body { padding: 18px 16px; }
  .layout-fig { grid-template-columns: 1fr; }
  .board-ctl { grid-template-columns: 1fr; }
}

/* Claude CLI 브리지 안내 */
.cli-banner { border: 1px solid #f59e0b; background: #fffbeb; color: #78350f; border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; }
.cli-banner p { margin: 0; }
.cli-banner code { background: rgba(0, 0, 0, .06); padding: 0 4px; border-radius: 4px; }
.cli-banner pre.cmd code { background: none; padding: 0; }
.cli-banner .muted { color: #92400e; }
:root[data-theme="dark"] .cli-banner { background: #2a2108; color: #fde68a; border-color: #a16207; }
:root[data-theme="dark"] .cli-banner .muted { color: #fcd34d; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .cli-banner { background: #2a2108; color: #fde68a; border-color: #a16207; } :root:not([data-theme="light"]) .cli-banner .muted { color: #fcd34d; } }

/* 예제 개수 */
.gen-row { flex-wrap: wrap; }
.count { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; color: var(--muted); }
.count input { width: 52px; padding: 5px 6px; text-align: center; }
.msg.open { border-color: var(--sel-line); box-shadow: inset 3px 0 0 var(--sel-line); }

/* 시뮬레이션 무대는 위에 고정 — 아래 센서 조절·시리얼을 스크롤해도 결과가 계속 보임 */
.sim-body .stage { position: sticky; top: 0; z-index: 3; box-shadow: 0 -13px 0 0 var(--panel), 0 6px 14px -10px rgba(15, 23, 42, .45); }
.sim-body .stage-svg { max-height: 52vh; }
@media (max-height: 760px) { .sim-body .stage-svg { max-height: 46vh; } }
@media (max-width: 860px) {
  .sim-pane, .sim-pane .pane-body { overflow: visible; }
  .sim-pane .pane-head { position: sticky; top: 52px; z-index: 4; background: var(--panel); border-radius: 14px 14px 0 0; }
  .sim-body .stage { top: 110px; }
  .sim-body .stage-svg { max-height: 36vh; }
}

/* 무대 확대 · 이동 */
.stage { position: relative; }
.stage-svg { cursor: grab; }
.stage-svg.panning { cursor: grabbing; }
.stage-zoom { position: absolute; right: 8px; bottom: 8px; display: flex; align-items: center; gap: 2px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 2px; box-shadow: var(--shadow); z-index: 2; }
.stage-zoom button { width: 28px; height: 26px; border: 0; background: none; cursor: pointer; font-size: 15px; border-radius: 7px; color: var(--fg); }
.stage-zoom button:hover { background: var(--chip); }
.stage-zoom .zl { font: 11px ui-monospace, Consolas, monospace; color: var(--muted); min-width: 38px; text-align: center; }
/* OLED 확대 화면 */
.oled-big { width: 100%; max-width: 384px; aspect-ratio: 2 / 1; image-rendering: pixelated; border-radius: 6px; border: 3px solid #1b3a70; background: #03060c; }

/* 모델 선택 */
.pv .model-row { display: flex; align-items: flex-end; gap: 8px; }
.pv .model-row label { flex: 1; }
.pv .model-row .btn { margin-bottom: 1px; }
.pv .model-state { margin: 0; min-height: 1em; }
.pv .model-state.warn { color: #b45309; }
