:root {
  --accent: #4c6ef5;
  --accent-dark: #3b5bdb;
  --ink: #212529;
  --sub: #5f6b7a;
  --line: #dde2e8;
  --bar: #ffffff;
  --soft: #f3f5f8;
  --ui: "Noto Sans KR", "Malgun Gothic", "Apple SD Gothic Neo", system-ui, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column; overflow: hidden;
  height: var(--vvh, 100%);       /* 태블릿 화면 자판이 올라오면 그만큼 줄어든다 */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  font-family: var(--ui); font-size: 14px; color: var(--ink); background: var(--bar);
  -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none;
  overscroll-behavior: none;      /* 끌어내려 새로고침 막기 */
}
body.notfound { align-items: center; justify-content: center; text-align: center; background: var(--soft); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ── 위 도구 줄 */
.topbar { background: var(--bar); border-bottom: 1px solid var(--line); box-shadow: 0 1px 4px rgba(20, 30, 50, .06); z-index: 5; flex: none; }
.topbar .row { display: flex; align-items: center; gap: 8px; padding: 7px 12px; flex-wrap: wrap; }
.topbar .tools { border-top: 1px solid #eef1f4; padding-top: 6px; padding-bottom: 6px; gap: 6px 14px; }
.brand {
  display: flex; align-items: center; gap: 7px; flex: none; white-space: nowrap;
  font-size: 18px; font-weight: 800; letter-spacing: -.01em; color: var(--accent-dark);
}
.brand img { display: block; border-radius: 6px; }
.title {
  width: 230px; max-width: 34vw; padding: 6px 9px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 15px; font-weight: 700; background: #fff;
}
.title::placeholder { font-weight: 400; color: #9aa4b1; }
.title:hover { border-color: #c5ccd6; }
.title:focus { border-color: var(--accent); outline: none; background: #fff; }
.status { font-size: 12px; color: var(--sub); white-space: nowrap; }
.status.dirty { color: #d9480f; }
.spacer { flex: 1; }
.field { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--sub); white-space: nowrap; }
select {
  padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
}
.topbar button, .dlgbtns button, .zoom button, #hud button {
  padding: 6px 11px; border: 1px solid var(--line); border-radius: 8px; background: #fff; white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.topbar button:hover, .dlgbtns button:hover, .zoom button:hover { background: var(--soft); border-color: #c5ccd6; }
.topbar button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
button.primary:hover { background: var(--accent-dark) !important; border-color: var(--accent-dark) !important; }
button.on { background: #e7ecff; border-color: var(--accent); color: var(--accent-dark); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.group { display: flex; gap: 4px; }
.topbar button.round { width: 34px; min-width: 34px; padding: 6px 0; font-weight: 700; border-radius: 50%; }
.copy { font-weight: 700; }
.help a { color: var(--accent-dark); }

.menuwrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 230px; padding: 6px;
  background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 30px rgba(20, 30, 50, .18); z-index: 20;
}
.menu button { display: block; width: 100%; text-align: left; border: 0; padding: 9px 10px; border-radius: 8px; }
.menu button small { display: block; margin-left: 24px; font-size: 11.5px; color: var(--sub); }
.menu hr { border: 0; border-top: 1px solid #eef1f4; margin: 5px 0; }

/* ── 작업 화면 */
.work { flex: 1; display: flex; min-height: 0; }
#stage { position: relative; flex: 1; min-width: 0; overflow: hidden; background: #fff; }
#svg { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
#svg.panning { cursor: grabbing; }
#svg.dragging { cursor: grabbing; }
#svg text { pointer-events: none; white-space: pre; }
#svg image { pointer-events: none; }
.node .hit { cursor: pointer; }
.selring { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-dasharray: 6 4; display: none; pointer-events: none; }
.node.sel .selring { display: block; }
.node.dim { opacity: .3; }
.joint { cursor: pointer; }
.joint:hover circle:nth-child(2) { stroke-width: 3; }

/* 글 쓰는 칸: 상자 없이 생각의 글 자리에 투명하게 겹친다 (커서와 쓰는 글만 보인다) */
#editor {
  position: absolute; z-index: 3; margin: 0; padding: 0; resize: none; overflow: hidden;
  border: 0; border-radius: 0; background: transparent; box-shadow: none; outline: none;
  text-align: center; white-space: pre-wrap; word-break: keep-all; overflow-wrap: anywhere;
}
#hint {
  position: absolute; left: 50%; bottom: 64px; transform: translateX(-50%); max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 999px; background: rgba(33, 37, 41, .86); color: #fff; font-size: 14px;
  pointer-events: none; text-align: center;
}
.zoom {
  position: absolute; right: 12px; bottom: 12px; display: flex; align-items: center; gap: 4px; padding: 4px;
  background: rgba(255, 255, 255, .94); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 2px 10px rgba(20, 30, 50, .1);
}
.zoom button { border: 0; padding: 5px 10px; min-width: 32px; }
#zoomLabel { min-width: 44px; text-align: center; font-size: 12px; color: var(--sub); font-variant-numeric: tabular-nums; }

#dragChip {
  position: fixed; z-index: 30; pointer-events: none; padding: 4px 10px; border-radius: 999px;
  background: var(--accent); color: #fff; font-size: 13px; font-weight: 700; box-shadow: 0 4px 12px rgba(20, 30, 50, .25);
}
#toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 60; max-width: calc(100vw - 32px);
  padding: 10px 18px; border-radius: 10px; background: rgba(33, 37, 41, .92); color: #fff; box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}

/* ── 디자인 칸 */
#panel {
  width: 286px; flex: none; overflow-y: auto; padding: 4px 14px 24px; background: var(--bar); border-left: 1px solid var(--line);
}
#panel h3 { margin: 16px 0 4px; font-size: 14px; }
#panel .note { margin: 0 0 6px; font-size: 12px; color: var(--sub); }
#panel .prow { margin: 10px 0; }
#panel .prow > span { display: block; margin-bottom: 5px; font-size: 12.5px; color: var(--sub); }
#panel select { width: 100%; }
#panel input[type=range] { width: 100%; accent-color: var(--accent); }
.seg { display: flex; flex-wrap: wrap; gap: 4px; }
.seg button {
  flex: 1 1 auto; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: 13px; white-space: nowrap;
}
.seg button:hover { background: var(--soft); }
.seg button.on { background: #e7ecff; border-color: var(--accent); color: var(--accent-dark); font-weight: 700; }
.seg.pal button { flex: 1 1 100%; display: flex; align-items: center; gap: 8px; text-align: left; }
.strip { display: inline-flex; border-radius: 5px; overflow: hidden; flex: none; }
.strip i { width: 13px; height: 16px; display: block; }
.seg.sw button { flex: 0 0 30px; height: 30px; padding: 0; border-radius: 50%; }
.seg.sw button.on { box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--accent); }
.seg.sw input[type=color] { flex: 0 0 34px; width: 34px; height: 30px; padding: 0; border: 1px solid var(--line); border-radius: 8px; background: #fff; cursor: pointer; }

.linkbadge { cursor: pointer; }
.linkbadge:hover circle { fill: #e7ecff; }

/* ── 원형 도구 (오른쪽 클릭) */
#radial { position: fixed; z-index: 40; width: 0; height: 0; }
.rdisc {
  position: absolute; border-radius: 50%; background: rgba(255, 255, 255, .93);
  box-shadow: 0 14px 44px rgba(20, 30, 50, .3), 0 0 0 1px var(--line);
}
.ritem, .rcenter {
  position: absolute; display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 0; border-radius: 50%; border: 1px solid var(--line); background: #fff; color: var(--ink);
  font-size: 11px; line-height: 1.15; white-space: pre-line; text-align: center;
  transition: transform .1s, background .1s, border-color .1s;
}
.ritem:hover { background: var(--soft); border-color: var(--accent); transform: scale(1.1); z-index: 1; }
.ritem.on { background: #e7ecff; border-color: var(--accent); color: var(--accent-dark); font-weight: 700; }
.ritem.sw { border: 2px solid #fff; box-shadow: 0 0 0 1px #c5ccd6; }
.ritem.sw.on { box-shadow: 0 0 0 3px var(--accent); }
/* 도형 버튼: 버튼 자체가 그 도형 모양 */
.ritem.rshape { border: 0; border-radius: 8px; background: transparent; font-size: 10.5px; }
.ritem.rshape:hover { background: transparent; }
.ritem.rshape svg { position: absolute; left: 0; top: 0; width: 100%; height: 100%; overflow: visible; }
.ritem.rshape span { position: relative; }
.ricon { font-size: 16px; line-height: 1.15; }
.rcenter {
  width: 50px; height: 50px; left: -25px; top: -25px; background: var(--accent); border-color: var(--accent);
  color: #fff; font-weight: 700; font-size: 10.5px;
}
.rcenter:hover { background: var(--accent-dark); }
.rcolor { position: absolute; left: -8px; top: 20px; width: 16px; height: 8px; opacity: 0; pointer-events: none; border: 0; padding: 0; }

/* ── 창 */
dialog {
  border: 0; border-radius: 14px; padding: 20px; max-width: min(560px, calc(100vw - 32px)); color: var(--ink);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(20, 25, 35, .55); }
dialog p { margin: 0 0 14px; white-space: pre-line; line-height: 1.55; }
dialog h2 { margin: 0 0 8px; font-size: 18px; }
.dlgbtns { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 14px; }
.dlgbtns a { margin-right: auto; color: var(--accent-dark); }
#dlgInput input { width: min(420px, 80vw); padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; }
#dlgPlayer { max-width: min(900px, calc(100vw - 24px)); width: 900px; padding: 14px; }
.player { position: relative; width: 100%; aspect-ratio: 16 / 9; background: #000; border-radius: 10px; overflow: hidden; }
.player iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
#dlgImage { padding: 0; max-width: 96vw; background: transparent; box-shadow: none; }
#dlgImage img { display: block; max-width: 96vw; max-height: 92vh; border-radius: 10px; background: #fff; cursor: zoom-out; }
#dlgHelp, #dlgInfo { max-width: min(640px, calc(100vw - 32px)); }
.help { max-height: 62vh; overflow-y: auto; padding-right: 6px; }
.help h3 { margin: 14px 0 4px; font-size: 14px; color: var(--accent-dark); }
.help ul { margin: 0; padding-left: 20px; line-height: 1.7; }
kbd {
  display: inline-block; padding: 0 6px; border: 1px solid #c5ccd6; border-bottom-width: 2px; border-radius: 5px;
  background: #f8f9fb; font: inherit; font-size: 12px;
}

/* ── 발표 모드 */
#hud {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); display: flex; align-items: center; gap: 8px;
  padding: 6px 8px 6px 14px; border-radius: 999px; background: rgba(33, 37, 41, .82); color: #fff; max-width: calc(100% - 24px);
  bottom: calc(16px + env(safe-area-inset-bottom));
}
#hud button { background: rgba(255, 255, 255, .14); border-color: transparent; color: #fff; border-radius: 999px; }
#hud button:hover { background: rgba(255, 255, 255, .28); }
#hud button:disabled { opacity: .35; cursor: default; }
#pInfo { padding: 0 6px 0 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
body.presenting .topbar, body.presenting #panel, body.presenting .zoom, body.presenting #hint { display: none !important; }
body.presenting .selring { display: none !important; }

/* ── 태블릿 · 좁은 화면 */
@media (max-width: 1000px) {
  .topbar button { padding: 5px 8px; font-size: 13px; }
  .group { gap: 3px; }
  /* 도구 줄은 한 줄로 두고 옆으로 민다 (화면을 넓게 쓰려고) */
  .topbar .tools { flex-wrap: nowrap; overflow-x: auto; gap: 10px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .topbar .tools::-webkit-scrollbar { display: none; }
  .topbar .tools .group { flex: none; }
  .work { position: relative; }
  #panel { position: absolute; right: 0; top: 0; bottom: 0; z-index: 4; box-shadow: -8px 0 24px rgba(20, 30, 50, .15); }
}
@media (max-width: 760px) {
  .topbar .row { padding: 6px 8px; gap: 6px; }
  .title { width: 120px; }
  .brand { font-size: 15px; }
  .brand img { width: 22px; height: 22px; }
  .status { display: none; }
  .field { font-size: 0; }             /* '발상법' 글자는 숨기고 고르는 칸만 */
  .field select { font-size: 13px; }
}
/* 손가락으로 누르는 기기: 버튼을 크게 */
@media (pointer: coarse) {
  .topbar button, .dlgbtns button, #hud button { min-height: 40px; padding-left: 12px; padding-right: 12px; }
  .topbar button.round { width: 40px; min-width: 40px; padding: 0; }
  select { min-height: 40px; }
  .zoom button { min-width: 42px; min-height: 40px; font-size: 16px; }
  .menu button { padding: 12px 10px; }
  .seg button { min-height: 40px; }
  .seg.sw button { flex-basis: 38px; height: 38px; }
  #panel input[type=range] { height: 32px; }
  .joint circle:first-child { r: 20px; }
  #dlgInput input { font-size: 16px; }
}

/* ── 인쇄: 맵만 종이 한 장에 */
@media print {
  .topbar, #panel, .zoom, #hint, #hud, #toast, #dragChip, #radial, #editor, dialog { display: none !important; }
  html, body { height: auto; overflow: visible; background: #fff; }
  .work { display: block; }
  #stage { width: 100%; height: 96vh; background: none !important; }
  .selring, .joint.open { display: none !important; }
}
