:root {
  --paper: #f3f0e8;
  --paper-2: #ebe7dc;
  --line: #d6cfbf;
  --line-strong: #b9b09c;
  --ink: #2b2a27;
  --ink-soft: #6d685e;
  --green: #2e9a6a;
  --green-dark: #23805a;
  --red: #c8372d;
  --amber: #f0b429;
  --blue: #3672c4;
  --mono: ui-monospace, 'D2Coding', 'SFMono-Regular', Menlo, Consolas, monospace;
  --font: 'Pretendard', 'Apple SD Gothic Neo', 'Malgun Gothic', 'Noto Sans KR', system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

button {
  -webkit-tap-highlight-color: transparent;
  font: inherit;
  color: inherit;
  cursor: pointer;
}

button:disabled { cursor: default; }

/* ── 윗줄 ── */
.topbar {
  height: 46px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  background: #fbfaf6;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--ink);
  font-weight: 800;
  font-size: 16px;
  letter-spacing: -0.02em;
  padding-right: 10px;
  border-right: 1px solid var(--line);
}

.project-name {
  width: 190px;
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  background: transparent;
  padding: 5px 7px;
  border-radius: 4px;
  color: var(--ink);
}
.project-name:hover { border-color: var(--line); }
.project-name:focus { outline: none; border-color: var(--line-strong); background: #fff; }

.file-menu { display: flex; gap: 2px; }
.file-menu > button,
.dropdown > button,
.help-btn {
  background: none;
  border: 0;
  padding: 6px 9px;
  border-radius: 4px;
  font-size: 13px;
}
.file-menu > button:hover,
.dropdown > button:hover,
.help-btn:hover { background: var(--paper-2); }

.dropdown { position: relative; }
.dropdown-list {
  position: absolute;
  z-index: 50;
  top: calc(100% + 4px);
  left: 0;
  margin: 0;
  padding: 4px;
  list-style: none;
  width: 260px;
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  box-shadow: 0 6px 18px rgba(40, 35, 25, 0.14);
}
.dropdown-list button {
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  padding: 7px 9px;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.dropdown-list button:hover { background: var(--paper); }
.dropdown-list small { color: var(--ink-soft); font-size: 12px; }

.save-state {
  margin-left: auto;
  font-size: 12px;
  color: var(--ink-soft);
}

/* ── 몸통 ── */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 502px;
  height: calc(100% - 46px);
}

.editor { position: relative; min-width: 0; }
#blocklyDiv { position: absolute; inset: 0; }

.side {
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--line);
  background: var(--paper);
  min-height: 0;
  padding: 10px 10px 10px;
  gap: 8px;
}

.runbar { display: flex; align-items: center; gap: 6px; }
.runbar-gap { flex: 1; }

.btn-run {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--green);
  color: #fff;
  border: 0;
  border-bottom: 2px solid var(--green-dark);
  padding: 7px 16px 6px 13px;
  border-radius: 5px;
  font-weight: 700;
}
.btn-run:hover:not(:disabled) { background: #2a8d61; }
.btn-run:disabled { background: #9ab8a8; border-bottom-color: #8aa898; }

.btn-icon {
  width: 32px;
  height: 32px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: 5px;
  color: var(--red);
}
.btn-icon:disabled { color: #c9c2b4; border-color: var(--line); }

.speed { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-soft); }
.speed input { width: 110px; accent-color: var(--ink); }
.speed output { width: 52px; font-variant-numeric: tabular-nums; }

.stage-wrap {
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  line-height: 0;
}
#stage { width: 100%; height: auto; aspect-ratio: 4 / 3; display: block; }
body[data-run='running'] .stage-wrap { border-color: var(--green); }
body[data-run='paused'] .stage-wrap { border-color: var(--amber); }

.stage-info {
  display: flex;
  gap: 14px;
  font-size: 12px;
  color: var(--ink-soft);
  margin-top: -2px;
  padding: 0 2px;
}
.stage-info b { color: var(--ink); font-family: var(--mono); font-weight: 600; }
.pen-info { margin-left: auto; }
.pen-info.on { color: var(--ink); font-weight: 600; }

.debugbar {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
  padding: 5px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 5px;
}
.debugbar button {
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 12.5px;
}
.debugbar button:hover:not(:disabled) { background: #fffbea; border-color: #a99c7c; }
.debugbar button:disabled { color: #aaa293; background: transparent; border-color: var(--line); }
.debugbar .sep { width: 1px; height: 18px; background: var(--line-strong); margin: 0 3px; }
body[data-run='paused'] #btnContinue:not(:disabled) { background: #fff4cc; border-color: #c9a13b; font-weight: 700; }

/* ── 출력 / 디버그 판 ── */
.panel {
  flex: 1;
  min-height: 150px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: #fff;
  overflow: hidden;
}

.tabs {
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid var(--line);
  background: #faf8f2;
}
.tab {
  border: 0;
  background: none;
  padding: 7px 13px;
  font-size: 13px;
  color: var(--ink-soft);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  display: flex;
  align-items: center;
  gap: 5px;
}
.tab.active { color: var(--ink); font-weight: 700; border-bottom-color: var(--ink); }
.tab-spacer { flex: 1; }
.link-btn { border: 0; background: none; font-size: 12px; color: var(--ink-soft); padding: 0 10px; }
.link-btn:hover { color: var(--ink); text-decoration: underline; }
.badge { width: 7px; height: 7px; border-radius: 50%; background: var(--blue); display: inline-block; }
.badge.err { background: var(--red); }

.tab-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.tab-body[hidden] { display: none; }

.console {
  flex: 1;
  margin: 0;
  padding: 6px 0;
  list-style: none;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.55;
}
.line { padding: 0 10px; white-space: pre-wrap; word-break: break-all; }
.line.out { color: var(--ink); }
.line.info { color: #8b8578; font-family: var(--font); font-size: 12px; }
.line.warn { color: #8a5a00; background: #fff8e3; font-family: var(--font); }
.line.sep { color: #b3ab9b; font-size: 11px; margin-top: 4px; }
.line.log { color: #6b6f78; }
.line.log::before { content: '· '; }
.line.q { color: var(--blue); }
.line.a { color: var(--blue); }
.line.a::before { content: '> '; }
.line.error { color: var(--red); background: #fdf0ee; font-weight: 600; margin-top: 2px; }
.line.error-sub { color: #9b3b33; background: #fdf0ee; }
.line[data-block] { cursor: pointer; }
.line[data-block]:hover { text-decoration: underline; }

.ask {
  display: flex;
  gap: 6px;
  padding: 6px;
  border-top: 1px solid var(--line);
  background: #f2f6fd;
  align-items: center;
}
.ask label { font-size: 12px; color: var(--blue); font-weight: 700; padding-left: 4px; }
.ask input {
  flex: 1;
  font: inherit;
  padding: 5px 7px;
  border: 1px solid #9db6dc;
  border-radius: 4px;
}
.ask input:focus { outline: 2px solid #c6d7f1; }
.ask button { border: 0; background: var(--blue); color: #fff; padding: 5px 12px; border-radius: 4px; }

#tab-debug { overflow-y: auto; padding: 8px 10px 10px; }
.dbg-status { margin: 0 0 8px; font-size: 12.5px; color: var(--ink-soft); }
.dbg-status b { color: #8a6100; }
.dbg-grid { display: grid; grid-template-columns: 1.25fr 1fr; gap: 12px; }
.dbg-grid h3 { margin: 0 0 4px; font-size: 12px; color: var(--ink-soft); font-weight: 700; }

.vars { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.vars th {
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  color: #9a9384;
  border-bottom: 1px solid var(--line);
  padding: 2px 4px;
}
.vars td { padding: 3px 4px; border-bottom: 1px dashed #ece6d8; vertical-align: top; }
.vars td.val { font-family: var(--mono); word-break: break-all; }
.vars td.unset { color: #b5ad9e; }
.vars td.scope { color: #9a9384; font-size: 11.5px; white-space: nowrap; }
.vars tr.local td:first-child { font-weight: 700; color: #6d4f99; }
.vars .empty, .stack .empty { color: #b5ad9e; font-size: 12px; }

.stack { margin: 0; padding: 0; list-style: none; font-family: var(--mono); font-size: 12px; }
.stack li {
  padding: 3px 6px;
  border-left: 3px solid var(--line);
  margin-bottom: 2px;
  word-break: break-all;
}
.stack li.top { border-left-color: var(--amber); background: #fffaea; font-weight: 600; }
.stack li.clickable { cursor: pointer; }
.stack li.clickable:hover { background: var(--paper); }

.dbg-hint {
  margin: 12px 0 0;
  padding-top: 8px;
  border-top: 1px solid #efe9dc;
  font-size: 12px;
  color: #8b8578;
  line-height: 1.55;
}

/* ── 블록 쪽 꾸밈 ── */
.blocklyHighlighted > .blocklyPath {
  stroke: #e89400 !important;
  stroke-width: 4px !important;
  stroke-dasharray: none !important;
  filter: drop-shadow(0 0 5px rgba(240, 160, 0, 0.7));
}
.has-bp > .blocklyPath {
  stroke: var(--red) !important;
  stroke-width: 3px !important;
  stroke-dasharray: 6 3;
}

.blocklyMainBackground { stroke: none !important; }

/* 꾸러미(왼쪽 칸) */
.blocklyToolbox {
  border-right: 1px solid var(--line);
  padding: 6px 0 !important;
  width: 74px;
}
.blocklyToolboxCategoryGroup { display: flex; flex-direction: column; gap: 1px; }
.blocklyToolboxCategoryContainer { outline: none; }
.blocklyToolbox .blocklyToolboxCategory {
  height: auto !important;
  line-height: 1.2 !important;
  padding: 7px 2px 6px !important;
  margin: 0 5px;
  border-radius: 7px;
  cursor: pointer;
  position: relative;
}
.blocklyToolbox .blocklyTreeRowContentContainer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.gb-cat-icon {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background: var(--cat);
  display: grid;
  place-items: center;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.18);
  transition: transform 0.12s;
}
.blocklyToolbox .blocklyToolboxCategoryLabel {
  font-family: var(--font);
  font-size: 11.5px;
  font-weight: 600;
  color: #5d584e !important;
  padding: 0 !important;
  letter-spacing: -0.02em;
}
.blocklyToolbox .blocklyToolboxCategory:hover { background: rgba(0, 0, 0, 0.045); }
.blocklyToolbox .blocklyToolboxCategory.gb-selected {
  background: #fff !important;
  box-shadow: 0 1px 0 var(--line), inset 0 0 0 1px var(--line);
}
.blocklyToolbox .gb-selected .gb-cat-icon { transform: scale(1.08); }
.blocklyToolbox .gb-selected .blocklyToolboxCategoryLabel { color: var(--cat) !important; font-weight: 800; }
.blocklyToolboxCategoryContainer:focus-visible .blocklyToolboxCategory { outline: 2px solid var(--cat); }

/* 블록 목록(플라이아웃) */
.blocklyFlyoutBackground { fill: #e9e5da !important; fill-opacity: 1 !important; }
.blocklyFlyout { border-right: 1px solid var(--line); }
.gb-flyout-label .blocklyFlyoutLabelText,
.gb-flyout-label.blocklyFlyoutLabelText {
  fill: #4a463e !important;
  font-family: var(--font) !important;
  font-weight: 800 !important;
}
.gb-flyout-label .blocklyFlyoutLabelBackground { fill: none !important; }
.blocklyFlyoutButton .blocklyFlyoutButtonBackground,
.blocklyFlyoutButtonBackground {
  fill: #fff !important;
  stroke: var(--line-strong) !important;
  stroke-width: 1px !important;
}
.blocklyFlyoutButton .blocklyText { fill: var(--ink) !important; font-weight: 700; }
.blocklyFlyoutButton:hover .blocklyFlyoutButtonBackground { fill: #fffbea !important; }

/* 블록 글자 */
.blocklyText, .blocklyEditableText > .blocklyText { font-family: var(--font) !important; }
.blocklyNonEditableText > text, .blocklyEditableText > text { font-weight: 700; }
.blocklyDropdownText { font-weight: 700; }
.blocklyPath { transition: filter 0.1s; }
.blocklyDraggable:not(.blocklyDisabled):hover > .blocklyPath { filter: brightness(1.05); }
.blocklyDragging > .blocklyPath { filter: drop-shadow(0 4px 6px rgba(40, 30, 10, 0.28)); }

/* ── 대화상자 ── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(43, 42, 39, 0.32);
  display: grid;
  place-items: center;
}
.modal[hidden] { display: none; }
.modal-box {
  width: min(380px, calc(100% - 32px));
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 18px 18px 14px;
  box-shadow: 0 10px 30px rgba(40, 35, 25, 0.2);
}
.modal-box p { margin: 0 0 12px; line-height: 1.5; }
.modal-box input {
  width: 100%;
  font: inherit;
  padding: 7px 9px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  margin-bottom: 12px;
}
.modal-box input[hidden] { display: none; }
.modal-actions { display: flex; justify-content: flex-end; gap: 6px; }
.modal-actions button {
  border: 1px solid var(--line-strong);
  background: #fff;
  padding: 6px 14px;
  border-radius: 4px;
}
.modal-actions button.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.modal-actions button[hidden] { display: none; }

.help-box { width: min(560px, calc(100% - 32px)); max-height: 85vh; overflow-y: auto; }
.help-box h2 { margin: 0 0 10px; font-size: 17px; }
.help-box dl { margin: 0 0 12px; line-height: 1.6; font-size: 13.5px; }
.help-box dt { font-weight: 700; margin-top: 10px; }
.help-box dd { margin: 2px 0 0; color: #45423b; }
kbd {
  font-family: var(--mono);
  font-size: 11.5px;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 3px;
  padding: 0 4px;
  background: #faf8f2;
}

/* ── 좁은 화면 ── */
.mobile-only, .menu-btn, .mnav { display: none; }

@media (max-width: 1100px) and (min-width: 761px) and (orientation: landscape) {
  .layout { grid-template-columns: minmax(0, 1fr) 380px; }
  .debugbar button { padding: 4px 6px; }
  .speed input { width: 80px; }
  .file-menu > button, .dropdown > button { padding: 6px 6px; }
  .project-name { width: 140px; }
}

@media (max-width: 760px), (max-width: 1100px) and (orientation: portrait) {
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { display: flex; flex-direction: column; height: 100dvh; }

  /* 윗줄 */
  .topbar { flex: none; height: 50px; gap: 4px; padding: 0 4px 0 10px; position: relative; }
  .brand { padding-right: 8px; }
  .brand span { display: none; }
  .project-name { flex: 1; width: auto; min-width: 0; font-size: 16px; }
  .save-state, .help-btn { display: none; }
  .menu-btn {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    border: 0;
    background: none;
    border-radius: 8px;
    fill: var(--ink);
  }
  .menu-btn:active, body.menu-open .menu-btn { background: var(--paper-2); }
  .file-menu {
    display: none;
    position: absolute;
    top: 48px;
    right: 6px;
    z-index: 60;
    flex-direction: column;
    gap: 0;
    min-width: 220px;
    max-height: calc(100dvh - 140px);
    overflow-y: auto;
    padding: 6px;
    background: #fff;
    border: 1px solid var(--line-strong);
    border-radius: 10px;
    box-shadow: 0 10px 28px rgba(40, 35, 25, 0.2);
  }
  body.menu-open .file-menu { display: flex; }
  .file-menu > button,
  .dropdown > button {
    width: 100%;
    text-align: left;
    padding: 12px 12px;
    font-size: 15px;
    border-radius: 6px;
  }
  .mobile-only { display: block; }
  .dropdown-list {
    position: static;
    width: auto;
    box-shadow: none;
    border: 0;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    margin: 2px 0;
    background: var(--paper);
  }
  .dropdown-list button { padding: 10px 12px; }

  /* 몸통: 한 번에 한 화면 */
  .layout { display: block; position: relative; flex: 1; min-height: 0; height: auto; }
  .editor { position: absolute; inset: 0; height: auto; }
  .side {
    position: absolute;
    inset: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    border: 0;
    padding: 10px 10px 16px;
    align-items: stretch;
  }
  .side > * { width: auto; max-width: none; flex: none; }
  body[data-view='code'] .side { visibility: hidden; pointer-events: none; }
  body[data-view='run'] .editor { display: none; }

  .runbar .btn-run, .runbar #btnStop { display: none; }
  .runbar { justify-content: flex-end; }
  .speed { font-size: 13px; }
  .speed input { width: 140px; height: 28px; }

  .panel { flex: none; min-height: 300px; }
  .tab { padding: 10px 16px; font-size: 14px; }
  .link-btn { font-size: 13px; padding: 0 14px; }
  .console { font-size: 13px; }
  .line { padding: 1px 12px; }
  .ask input, .modal-box input { font-size: 16px; }
  .ask input { padding: 8px 9px; }
  .ask button { padding: 8px 14px; }
  .dbg-grid { grid-template-columns: 1fr; }

  /* 디버그 버튼: 가로로 밀어서 보기, 손가락 크기 */
  .debugbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .debugbar::-webkit-scrollbar { display: none; }
  .debugbar button { flex: none; min-height: 38px; padding: 6px 12px; font-size: 14px; white-space: nowrap; }

  /* 멈춰 있을 때는 디버그 버튼을 아래에 띄워서 블록 화면에서도 쓰게 */
  body[data-run='paused'] .debugbar {
    visibility: visible;
    pointer-events: auto;
    position: fixed;
    z-index: 40;
    left: 8px;
    right: 8px;
    bottom: calc(66px + env(safe-area-inset-bottom));
    background: #fffaf0;
    border-color: #c9a13b;
    box-shadow: 0 6px 20px rgba(40, 35, 25, 0.22);
  }
  body[data-run='paused'] #btnStepStart,
  body[data-run='paused'] .debugbar .sep,
  body[data-run='paused'] #btnPause { display: none; }
  body[data-run='paused'] .side { padding-bottom: 72px; }
  body[data-view='code'][data-run='paused'] .debugbar { left: 64px; }

  /* 아래 탭 */
  .mnav {
    display: flex;
    flex: none;
    align-items: center;
    height: calc(60px + env(safe-area-inset-bottom));
    padding: 0 6px env(safe-area-inset-bottom);
    background: #fbfaf6;
    border-top: 1px solid var(--line);
  }
  .mnav-tab {
    flex: 1;
    height: 100%;
    border: 0;
    background: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    font-size: 11.5px;
    color: var(--ink-soft);
    position: relative;
    -webkit-tap-highlight-color: transparent;
  }
  .mnav-tab svg { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; }
  .mnav-tab.active { color: var(--ink); font-weight: 700; }
  .mnav-tab.active svg { stroke-width: 2.1; }
  .mnav-dot {
    position: absolute;
    top: 10px;
    left: calc(50% + 10px);
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--blue);
  }
  .mnav-run {
    flex: none;
    width: 112px;
    height: 44px;
    border: 0;
    border-bottom: 2px solid var(--green-dark);
    border-radius: 22px;
    background: var(--green);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-size: 15px;
    font-weight: 700;
    -webkit-tap-highlight-color: transparent;
  }
  .mnav-run:active { transform: translateY(1px); }
  .mnav-run .i-stop { display: none; }
  body:not([data-run='idle']) .mnav-run { background: var(--red); border-bottom-color: #9e2a22; }
  body:not([data-run='idle']) .mnav-run .i-play { display: none; }
  body:not([data-run='idle']) .mnav-run .i-stop { display: block; }

  /* 꾸러미 칸을 좁게 */
  .blocklyToolbox { width: 58px; padding: 4px 0 !important; }
  .blocklyToolbox .blocklyToolboxCategory { margin: 0 3px; padding: 6px 0 5px !important; }
  .gb-cat-icon { width: 24px; height: 24px; border-radius: 7px; }
  .blocklyToolbox .blocklyToolboxCategoryLabel { font-size: 10.5px; }

  .modal-box { padding: 16px; }
  .modal-actions button { padding: 9px 16px; font-size: 15px; }
  .help-box dl { font-size: 14px; }
}
