:root {
  --bg: #0f1722;
  --panel: #162131;
  --panel-2: #1d2b3f;
  --line: #2a3a52;
  --text: #e8eef7;
  --muted: #8fa0b8;
  --accent: #ffd166;
  --logic: #a78bfa;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, 'Malgun Gothic', sans-serif;
  overflow: hidden;
}

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

button, select, input { font: inherit; color: inherit; }

button, select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
}

button:hover, select:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #1b1300; font-weight: 700; }

#bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

#bar h1 { font-size: 16px; margin: 0 8px 0 0; white-space: nowrap; }
#bar label { color: var(--muted); white-space: nowrap; }
.spacer { flex: 1; }

main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 224px minmax(0, 1fr) 236px;
}

#palette, #inspector {
  background: var(--panel);
  overflow-y: auto;
  padding: 10px;
}

#palette {
  border-right: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  align-content: start;
}
#palette h2 { grid-column: 1 / -1; margin: 8px 0 2px; }
#inspector { border-left: 1px solid var(--line); }

#palette h2, #inspector h2 {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin: 12px 0 6px;
}

#palette h2:first-child, #inspector h2:first-child { margin-top: 0; }

.tool {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-width: 0;
  padding: 5px 7px;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
  white-space: nowrap;
  touch-action: pan-y;
  user-select: none;
}

.tool i { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.tool.on { border-color: var(--accent); background: #2b3447; }

body.playing #palette, body.playing #inspector { opacity: 0.45; pointer-events: none; }

#stage { position: relative; min-width: 0; min-height: 0; }
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

#hud {
  position: absolute;
  left: 12px;
  top: 10px;
  padding: 4px 10px;
  background: rgba(15, 23, 34, 0.75);
  border-radius: 6px;
  color: var(--muted);
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}

#hud b { color: var(--text); }

#zoom { position: absolute; right: 12px; top: 10px; display: flex; flex-direction: column; gap: 4px; }
#zoom button { width: 44px; height: 34px; padding: 0; font-size: 18px; }
#zoom #fit, #zoom #wires, #zoom #sound { font-size: 12px; }
#zoom #wires.off, #zoom #sound.off { opacity: 0.45; text-decoration: line-through; }

#playctl { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); display: none; gap: 6px; }
body.playing #playctl { display: flex; }
#playctl button.on { border-color: var(--accent); color: var(--accent); }

#keys { position: absolute; left: 0; right: 0; bottom: 14px; display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; pointer-events: none; }
#keys button { pointer-events: auto; min-width: 56px; height: 48px; font-weight: 700; touch-action: none; user-select: none; }
#keys button.down { background: var(--accent); color: #1b1300; }

#toast {
  position: absolute;
  left: 50%;
  top: 56px;
  transform: translateX(-50%);
  background: var(--accent);
  color: #1b1300;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 6px;
  max-width: 90%;
}

#inspector h3 { margin: 0 0 4px; font-size: 16px; }
#inspector p { margin: 0 0 10px; color: var(--muted); font-size: 13px; }
#inspector .row { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 10px; }
#inspector .row button { flex: 1; min-width: 36px; padding: 5px 4px; }
#inspector .row button.on { border-color: var(--accent); color: var(--accent); }
#inspector input[type='range'] { width: 100%; }
#inspector .val { color: var(--text); font-weight: 600; }
#inspector ul { margin: 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
#inspector li { margin-bottom: 4px; }
#inspector .danger { width: 100%; border-color: #7a3340; color: #ff8fa0; }

@media (max-width: 820px) {
  main { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto auto; }
  #stage { grid-row: 1; }
  #palette { grid-row: 2; display: flex; gap: 4px; overflow-x: auto; border-right: 0; border-top: 1px solid var(--line); padding: 6px; }
  #palette h2 { display: none; }
  #palette .tool { width: auto; margin: 0; flex: none; touch-action: pan-x; }
  #inspector { grid-row: 3; max-height: 26vh; border-left: 0; border-top: 1px solid var(--line); }
  #bar h1 { display: none; }
}

#ghost {
  position: fixed;
  z-index: 10;
  transform: translate(-50%, -140%);
  padding: 4px 10px;
  border-radius: 6px;
  background: var(--accent);
  color: #1b1300;
  font-weight: 600;
  pointer-events: none;
  white-space: nowrap;
}

body.mapedit #mapedit { border-color: var(--accent); color: var(--accent); }
#inspector .wide { width: 100%; margin-bottom: 6px; }

dialog {
  width: min(520px, 92vw);
  max-height: 80vh;
  overflow-y: auto;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px;
}
dialog::backdrop { background: rgba(5, 9, 15, 0.6); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
dialog p { color: var(--muted); }
dialog ul { list-style: none; margin: 0; padding: 0; }
dialog li { display: flex; align-items: center; gap: 6px; padding: 8px 0; border-top: 1px solid var(--line); }
dialog input { flex: 1; min-width: 0; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
.slot-new { display: flex; gap: 6px; margin-bottom: 12px; }
.slot-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.slot-text b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot-text span { color: var(--muted); font-size: 12px; }
.slot-foot { display: flex; justify-content: flex-end; margin-top: 12px; }
button.danger { border-color: #7a3340; color: #ff8fa0; }

@media (max-width: 820px) {
  #palette { display: flex; }
  #playctl { top: 44px; }
}

.share-kinds { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.share-kinds button { display: flex; flex-direction: column; gap: 2px; text-align: left; padding: 8px 10px; }
.share-kinds button span { color: var(--muted); font-size: 12px; }
.share-kinds button.on { border-color: var(--accent); }
.share-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.share-note { font-size: 12px; margin: 0; }

#hud .sub { display: block; font-size: 12px; }

#watch { position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); border-color: var(--accent); color: var(--accent); background: rgba(15, 23, 34, 0.9); }

#guide {
  position: absolute;
  left: 12px;
  bottom: 14px;
  width: min(300px, calc(100% - 24px));
  padding: 12px 14px;
  background: rgba(22, 33, 49, 0.96);
  border: 1px solid var(--accent);
  border-radius: 10px;
}
#guide b { display: block; margin-bottom: 4px; }
#guide p { margin: 0 0 10px; color: var(--muted); font-size: 13px; }
.guide-row { display: flex; align-items: center; gap: 6px; }
.guide-row span { flex: 1; color: var(--muted); font-size: 12px; }
.guide-row button { padding: 3px 10px; }
body.mapedit #guide { display: none; }

.share-run { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; font-size: 13px; }
.share-run[hidden] { display: none; }
.share-run input { flex: none; }
.share-actions { margin-top: 8px; }

/* 달리는 동안에는 안내 카드가 조작 버튼을 가리지 않게 숨긴다 */
body.playing #guide { display: none; }
#playctl button { white-space: nowrap; }

@media (max-width: 820px) {
  #playctl { left: 12px; top: 64px; transform: none; flex-wrap: wrap; right: 64px; }
  #playctl button { padding: 4px 8px; font-size: 13px; }
  #watch { bottom: 70px; }
  #guide { bottom: 8px; }
}

/* ---------- 상단 바: 넓은 화면은 한 줄, 좁은 화면은 "더 보기"로 접기 ---------- */
#bar { position: relative; }
#more { display: contents; }
#morebtn { display: none; order: 12; }
#bar h1 { order: 0; }
#play { order: 1; }
#more > label { order: 2; }
#mapedit { order: 3; }
#example { order: 4; }
#more > .spacer { order: 5; }
#undo { order: 6; }
#redo { order: 7; }
#clear { order: 8; }
#slots { order: 9; }
#help { order: 10; }
#share { order: 11; }

@media (max-width: 1240px) {
  #bar { flex-wrap: nowrap; }
  #undo { margin-left: auto; }
  #morebtn { display: inline-block; }
  #more {
    display: none;
    position: absolute;
    right: 8px;
    top: calc(100% + 4px);
    z-index: 30;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    min-width: 210px;
    padding: 10px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  }
  #more.open { display: flex; }
  #more > .spacer { display: none; }
  #more > label { display: flex; align-items: center; gap: 8px; }
  #more > label select { flex: 1; }
  #more button, #more select { text-align: left; padding: 8px 10px; }
}

/* ---------- 과제 카드와 도착 화면 ---------- */
#task {
  position: absolute;
  left: 12px;
  top: 46px;
  width: min(250px, calc(100% - 84px));
  padding: 8px 12px;
  background: rgba(22, 33, 49, 0.94);
  border: 1px solid #34d399;
  border-radius: 8px;
  font-size: 13px;
}
#task.bad { border-color: #ff8fa0; }
#task > b { color: var(--accent); }
#task p { margin: 2px 0 4px; }
#task ul, #result ul { list-style: none; margin: 0; padding: 0; font-size: 13px; }
#task li.ok, #result li.ok { color: #6ee7b7; }
#task li.bad, #result li.bad { color: #ff8fa0; }
.task-quit { margin-top: 6px; padding: 2px 8px; font-size: 12px; color: var(--muted); }
body.mapedit #task, body.arrived #task { display: none; }
body.playing #task { top: 64px; }
body.playing .task-quit { display: none; }

#result {
  position: absolute;
  left: 50%;
  top: 64px;
  transform: translateX(-50%);
  z-index: 5;
  width: min(340px, calc(100% - 24px));
  padding: 14px 16px;
  background: rgba(22, 33, 49, 0.97);
  border: 1px solid var(--accent);
  border-radius: 10px;
}
#result.bad { border-color: #ff8fa0; }
#result > b { display: block; font-size: 18px; margin-bottom: 4px; padding-right: 24px; }
#result p { margin: 0 0 4px; color: var(--muted); font-size: 13px; }
#result p.result-time { color: var(--text); font-size: 14px; }
#result ul { margin-top: 6px; }
.result-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 12px; }
.result-close { position: absolute; right: 6px; top: 4px; border: 0; background: none; font-size: 20px; padding: 0 8px; }

.tool.banned { opacity: 0.4; text-decoration: line-through; }

.share-task { margin-bottom: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; }
.share-task[hidden] { display: none; }
.share-task h3 { margin: 0 0 8px; font-size: 14px; }
.share-max { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 13px; }
.share-max input { flex: none; width: 76px; }
.share-max span { color: var(--muted); }
.share-bans { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.share-bans button { padding: 2px 8px; font-size: 12px; }
.share-bans button.on { border-color: #ff8fa0; color: #ff8fa0; text-decoration: line-through; }

@media (max-width: 820px) {
  body.playing #task { display: none; }
  #result { top: 108px; }
}

/* 과제 카드 머리줄: 접어 두면 이 줄만 남는다 */
.task-head { display: flex; align-items: center; gap: 8px; }
.task-head span { flex: 1; font-size: 12px; }
.task-head span.ok { color: #6ee7b7; }
.task-head span.bad { color: #ff8fa0; }
.task-fold { padding: 0 8px; font-size: 12px; color: var(--muted); }

/* 화면 위 글자가 오른쪽 확대 버튼이나 가운데 실행 버튼 밑으로 들어가지 않게 */
#hud { max-width: calc(100% - 84px); }
@media (min-width: 821px) {
  body.playing #hud { max-width: calc(50% - 190px); }
}
#hud { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#hud .sub { overflow: hidden; text-overflow: ellipsis; }

/* 받은 제출 카드 (선생님 화면) */
#submission {
  position: absolute;
  left: 50%;
  top: 46px;
  transform: translateX(-50%);
  z-index: 4;
  width: min(360px, calc(100% - 24px));
  padding: 12px 16px;
  background: rgba(22, 33, 49, 0.97);
  border: 1px solid #34d399;
  border-radius: 10px;
}
#submission.bad { border-color: #ff8fa0; }
#submission > b { display: block; font-size: 16px; margin-bottom: 6px; }
#submission ul { list-style: none; margin: 0 0 6px; padding: 0; font-size: 13px; }
#submission li.ok { color: #6ee7b7; }
#submission li.bad { color: #ff8fa0; }
#submission p { margin: 0; color: var(--muted); font-size: 12px; }
body.playing #submission, body.mapedit #submission { display: none; }
/* 제출 카드가 조건을 이미 보여 주므로, 떠 있는 동안에는 과제 카드를 겹치지 않게 숨긴다 */
#submission:not([hidden]) ~ #task { display: none; }
.share-note.bad { color: #ff8fa0; }

/* 수업 과제 목록 */
#lessondlg { width: min(640px, 94vw); }
li.lesson { align-items: flex-start; gap: 10px; }
li.lesson .slot-text b { white-space: normal; }
li.lesson .slot-text span { white-space: normal; }
li.lesson .lesson-task { color: var(--text); font-size: 13px; }
.lesson-tag { display: inline-block; margin-right: 6px; padding: 0 6px; border-radius: 4px; font-size: 11px; font-weight: 600; color: #0f1722; }
.lesson-tag.sci { background: #6ee7b7; }
.lesson-tag.code { background: #c4b5fd; }
.lesson-actions { display: flex; flex-direction: column; gap: 4px; flex: none; }

@media (max-width: 820px) {
  /* 도착 카드가 떠 있는 동안에는 오른쪽 화면 버튼을 잠시 치운다 (카드에 가려 눌리지 않으므로) */
  body.arrived #zoom { display: none; }
  li.lesson { flex-wrap: wrap; }
  .lesson-actions { flex-direction: row; flex-wrap: wrap; flex: 1 1 100%; }
  .lesson-actions button { flex: 1 1 auto; }
}

#gatherdlg { width: min(720px, 94vw); }
dialog textarea { flex: 1; min-width: 0; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; font: inherit; resize: vertical; }
.gather-wrap { overflow-x: auto; margin-bottom: 8px; }
table.gather { width: 100%; border-collapse: collapse; font-size: 13px; }
table.gather th, table.gather td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.gather th { color: var(--muted); font-weight: 600; }
table.gather tbody tr[tabindex] { cursor: pointer; }
table.gather tbody tr[tabindex]:hover, table.gather tbody tr[tabindex]:focus { background: var(--panel-2); outline: none; }
table.gather tr.ok td:nth-child(2) { color: #6ee7b7; font-weight: 600; }
table.gather tr.bad td:nth-child(2) { color: #ff8fa0; font-weight: 600; }

/* 교실 모드: 멀리서도 보이게 키우고, 달리는 동안에는 양옆 패널을 접는다 */
body.present #bar, body.present #hud, body.present #task, body.present #result, body.present #submission,
body.present #playctl, body.present #keys, body.present #toast, body.present #zoom, body.present #watch { zoom: 1.3; }
body.present #inspector, body.present #palette { zoom: 1.15; }
/* 시연할 때 잘 쓰지 않는 버튼은 숨겨 상단 바를 한 줄로 유지한다 */
body.present #redo, body.present #clear, body.present #slots, body.present #help, body.present #mapedit, body.present #example { display: none; }
body.present.playing main { grid-template-columns: minmax(0, 1fr); }
body.present.playing #palette, body.present.playing #inspector { display: none; }
#bar button.on { border-color: var(--accent); color: var(--accent); }

.lesson-done { color: #6ee7b7 !important; font-weight: 600; }
dialog ol.lesson-steps { margin: 0 0 12px; padding-left: 20px; color: var(--muted); font-size: 13px; }
dialog ol.lesson-steps li { display: list-item; border: 0; padding: 2px 0; }

.task-hint-btn { display: block; margin: 6px 0; font-size: 12px; padding: 3px 8px; }
.task-hint { color: var(--accent) !important; }

.task-send { display: block; margin: 6px 0; }

.share-field { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.share-field span { flex: none; width: 32px; color: var(--muted); font-size: 13px; }

.lesson-free-title { margin: 16px 0 4px; font-size: 14px; }

#teacherdlg { width: min(680px, 94vw); }
.slot-foot { gap: 6px; }
.doc h3 { margin: 16px 0 6px; font-size: 14px; color: var(--accent); }
.doc h3:first-child { margin-top: 0; }
.doc ol, .doc ul { list-style: revert; margin: 0; padding-left: 22px; font-size: 13px; }
.doc li { display: list-item; border: 0; padding: 3px 0; color: var(--muted); }
.doc li b, .doc dt { color: var(--text); }
.doc dl { margin: 0; font-size: 13px; }
.doc dt { font-weight: 600; margin-top: 10px; }
.doc dd { margin: 2px 0 0; color: var(--muted); }

/* 태블릿 폭: 양옆 창을 조금 줄여 가운데 작업 공간을 넓힌다 */
@media (min-width: 821px) and (max-width: 1100px) {
  main { grid-template-columns: 204px minmax(0, 1fr) 196px; }
}
/* 폰 폭: 고른 부품이 없으면 도움말뿐인 아래 창을 접는다 */
@media (max-width: 820px) {
  #inspector.idle { display: none; }
}

/* 교실 모드: 커진 글자가 잘리지 않게 양옆 창도 넓힌다 */
@media (min-width: 1101px) {
  body.present main { grid-template-columns: 264px minmax(0, 1fr) 276px; }
  body.present.playing main { grid-template-columns: minmax(0, 1fr); }
}

/* "만드는 법"은 아래 창이 접히는 폰 폭에서만 필요하다 */
#howto { display: none; }
@media (max-width: 820px) {
  #howto { display: inline-block; }
  body.showhelp #inspector.idle { display: block; }
}

.gather-bar { width: 100%; height: 10px; margin-bottom: 6px; accent-color: var(--accent); }
.issued-box { margin: 4px 0 10px; font-size: 13px; }
.issued-box summary { cursor: pointer; color: var(--muted); }

/* 노트북 폭(1280 안팎): 상단 바가 두 줄로 넘어가지 않게 간격을 조금 줄인다 */
@media (min-width: 1241px) and (max-width: 1440px) {
  #bar { gap: 6px; }
  #bar button, #bar select { padding: 5px 8px; }
  #bar h1 { margin-right: 2px; }
}

/* 태블릿 폭: 가운데 실행 버튼이 위쪽 설명 줄을 가리지 않게 둘째 줄로 내린다 */
@media (min-width: 821px) and (max-width: 1100px) {
  body.playing #hud { max-width: calc(100% - 84px); }
  #playctl { left: 12px; right: 64px; top: 64px; transform: none; flex-wrap: wrap; }
  body.playing #task { top: 106px; }
}

/* 한글은 낱말 중간에서 줄이 바뀌면 읽기 어렵다. 낱말 단위로 줄을 바꾸되, 긴 링크는 넘치지 않게 끊는다. */
body { word-break: keep-all; overflow-wrap: break-word; }

button:disabled { opacity: 0.45; cursor: default; }
button:disabled:hover { border-color: var(--line); }

/* 키보드로 옮겨 다닐 때 지금 어디에 있는지 보이게 */
button:focus-visible, select:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* 따라 만들기 카드: 달리는 동안에도 보인다 */
#tutor {
  position: absolute;
  left: 12px;
  bottom: 14px;
  width: min(320px, calc(100% - 24px));
  padding: 12px 14px;
  background: rgba(22, 33, 49, 0.96);
  border: 1px solid var(--accent);
  border-radius: 10px;
  z-index: 5;
}
#tutor b { display: block; margin-bottom: 4px; }
#tutor p { margin: 0 0 10px; color: var(--muted); font-size: 13px; }
#tutor p.tut-ok { color: #6ee7b7; }
body.mapedit #tutor { display: none; }
body.playing #tutor { bottom: 76px; }
@media (max-width: 820px) {
  #tutor { bottom: 8px; }
}
.tut-glow { outline: 2px solid var(--accent); outline-offset: 2px; animation: tutglow 0.9s ease-in-out infinite alternate; }
@keyframes tutglow {
  from { box-shadow: 0 0 0 0 rgba(255, 209, 102, 0.75); }
  to { box-shadow: 0 0 0 7px rgba(255, 209, 102, 0); }
}
a.linkbtn {
  display: block;
  margin-bottom: 6px;
  padding: 6px 10px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
}
a.linkbtn:hover { border-color: var(--muted); }
.slot-foot a.linkbtn { margin: 0 auto 0 0; }
#helpdlg { width: min(420px, 92vw); }
