* { box-sizing: border-box; margin: 0; padding: 0; }
html, body, #app { height: 100%; width: 100%; }
body { font-family: "Segoe UI", "Malgun Gothic", system-ui, sans-serif; background: #1a1d21; color: #e6e8ea; overflow: hidden; }
/* 상단 메뉴바 레이아웃: 상단바(로고/파일) → 메뉴바(가로) → 메인(뷰포트), 세로 스택 */
#app { display: grid; grid-template-columns: 1fr; grid-template-rows: auto auto 1fr;
  grid-template-areas: "top" "side" "main"; height: 100%; }
#topbar { grid-area: top; }

/* top bar */
#topbar { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: #23272e; border-bottom: 1px solid #333; flex: 0 0 auto; }
/* 햄버거: 상단 가로 메뉴바에서는 필요 없어 항상 숨긴다(레일/오프캔버스 제거) */
#btn-hamburger { display: none; }
#sb-backdrop { display: none; }
.logo { font-weight: 700; font-size: 17px; color: #5db3ff; letter-spacing: .5px; }
.btn { background: #2f6fed; color: #fff; padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; border: none; user-select: none; }
.btn:hover { background: #3d7bf5; }
#filename { font-size: 13px; color: #cbd2d9; }
#modelinfo { font-size: 12px; color: #8b929b; margin-left: auto; }

/* 언어 선택 — 상단바 오른쪽 끝의 한국어/English 토글 */
#langsel { display: flex; gap: 2px; flex: 0 0 auto; margin-left: 10px; padding: 2px;
  background: #1b1f25; border: 1px solid #333a44; border-radius: 6px; }
#langsel button { background: none; border: none; color: #9aa1a9; font: inherit; font-size: 12px;
  padding: 3px 9px; border-radius: 4px; cursor: pointer; white-space: nowrap; }
#langsel button:hover { color: #e6e8ea; }
#langsel button.active { background: #2f6fed; color: #fff; }

/* toolbar — 대분류(.tlabel) 아래 중분류(.titems) 메뉴. 타일 그리드보다 폭을 적게 먹는다.
   기본은 '레일'(아이콘만 56px)이고, 대분류를 누르면 옆으로 서브메뉴가 뜬다.
   햄버거로 펼치면 176px 아코디언 메뉴가 된다. 상태는 localStorage 에 남는다. */
/* toolbar — 상단 가로 메뉴바. 대분류(.tlabel)를 누르면 아래로 서브메뉴(.titems)가 드롭다운으로 뜬다.
   한 번에 하나만 열리고(.flyout), 바깥을 누르거나 항목을 고르면 닫힌다(viewer.js). */
#toolbar { grid-area: side; display: flex; flex-direction: row; align-items: stretch; gap: 2px;
  padding: 3px 10px; width: 100%; background: #1f2329; border-bottom: 1px solid #2c313a;
  overflow: visible; flex: 0 0 auto;
  /* 메뉴는 글자 선택이 되면 안 된다 — 드래그하면 라벨이 파랗게 하이라이트되는 자국이 남는다 */
  user-select: none; -webkit-user-select: none; }
.tgroup { position: relative; }
.tgroup[hidden] { display: none; }

/* 대분류 — 메뉴바 버튼(이름 + ▾). 아이콘은 쓰지 않는다: 뜻이 한눈에 안 들어오고
   한국어/영어로 갈아 끼워야 하는 것도 결국 글자라, 라벨 자체를 글자로 둔다. */
.tlabel { display: flex; align-items: center; gap: 6px; padding: 8px 12px; border: none;
  background: transparent; color: #cfd6de; font-size: 13px; font-weight: 700; line-height: 1.2;
  border-radius: 6px; cursor: pointer; user-select: none; white-space: nowrap; }
.tlabel::after { content: '▾'; font-size: 10px; color: #7d8590; transition: transform .15s; }
.tlabel:hover { background: #272d36; }
.tgroup.flyout > .tlabel { background: #2b3038; color: #fff; }
.tgroup.flyout > .tlabel::after { transform: rotate(180deg); }

/* 중분류 — 대분류 아래로 뜨는 드롭다운 카드 */
.titems { display: none; }
.tgroup.flyout > .titems { display: flex; flex-direction: column; gap: 1px;
  position: absolute; top: calc(100% + 4px); left: 0; min-width: 184px; padding: 6px;
  background: #232830; border: 1px solid #39414c; border-radius: 10px;
  box-shadow: 0 12px 30px #000a; z-index: 60; }
/* 오른쪽 끝 그룹(도구·PLM)은 화면 밖으로 넘치지 않게 오른쪽 정렬로 편다 */
.tgroup:nth-last-child(-n+2) > .titems { left: auto; right: 0; }
/* 중분류 항목도 글자만 — 아이콘 칸 없이 왼쪽 정렬된 텍스트 메뉴다. */
.tbtn { display: flex; align-items: center; gap: 8px; width: 100%; padding: 7px 9px; border-radius: 6px;
  background: transparent; border: 1px solid transparent; color: #c2c9d2; font-size: 12.5px; line-height: 1.2;
  text-align: left; cursor: pointer; word-break: keep-all; white-space: nowrap; }
.tbtn:hover { background: #2b313a; }
.tbtn.active { background: #2f6fed; border-color: #2f6fed; color: #fff; }

/* main */
/* min-width:0 필수 — 그리드 1fr 칸은 기본 min-width:auto라, 트리 패널이 열리면
   칸이 내용(트리+뷰포트)만큼 늘어나 뷰포트가 창 밖으로 넘치고 우상단 컨트롤이 사라진다. */
#main { grid-area: main; display: flex; min-height: 0; min-width: 0; position: relative; }

/* tree panel */
#tree-panel { width: 260px; background: #20242b; border-right: 1px solid #2c313a; display: flex; flex-direction: column; flex: 0 0 auto; }
#tree-panel.hidden { display: none; }
.panel-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px; font-size: 13px; font-weight: 600; border-bottom: 1px solid #2c313a; }
.panel-head .x { background: none; border: none; color: #9aa1a9; font-size: 18px; cursor: pointer; }
.panel-actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px; border-bottom: 1px solid #2c313a; }
/* 부품 검색 */
.tree-search { display: flex; align-items: center; gap: 8px; padding: 8px 12px 0; }
.tree-search input { flex: 1 1 auto; min-width: 0; background: #171a1f; color: #e6e8ea;
  border: 1px solid #343b45; border-radius: 6px; padding: 5px 9px; font-size: 12px; }
.tree-search input:focus { outline: none; border-color: #2f6fed; }
#tree-search-count { font-size: 11px; color: #8b939e; flex: 0 0 auto; }
#tree-list li.match { background: #2c3b26; }
#tree-list li.match .nm { color: #b7f0a0; font-weight: 600; }
.mini { background: #2b3038; color: #d5dae0; border: 1px solid #3a4049; padding: 4px 8px; border-radius: 4px; font-size: 11px; cursor: pointer; }
.mini:hover { background: #353b45; }
.mini.active { background: #2f6fed; border-color: #2f6fed; color: #fff; }
#tree-list { list-style: none; overflow-y: auto; flex: 1 1 auto; padding: 4px 0; }
#tree-list li { display: flex; align-items: center; gap: 8px; padding: 4px 12px; font-size: 12px; cursor: pointer; }
#tree-list li:hover { background: #2a2f37; }
#tree-list li.selected { background: #2f4a7a; }
#tree-list li .swatch { width: 11px; height: 11px; border-radius: 2px; flex: 0 0 auto; border: 1px solid #0004; }
#tree-list li .nm { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#tree-list li input { cursor: pointer; }
#tree-list li .ic { flex: 0 0 auto; font-size: 12px; }
#tree-list li .caret { flex: 0 0 auto; width: 12px; text-align: center; font-size: 9px; color: #8b939e; user-select: none; }
#tree-list li .caret.clickable { cursor: pointer; }
#tree-list li .caret.clickable:hover { color: #fff; }
#tree-list li .dl { color: #5db3ff; text-decoration: none; font-size: 13px; padding: 0 2px; flex: 0 0 auto; }
#tree-list li .dl:hover { color: #8fd0ff; }
#tree-list li .nm { min-width: 0; }
.mini:disabled { opacity: .4; cursor: default; }
.ver { font-size: 10px; color: #6b7480; margin-left: 2px; letter-spacing: .3px; }
/* 2D 도면 치수 라벨 — 흰 배경 위에서 읽히도록 측정 라벨과 다른 색을 쓴다 */
/* 마크업 말풍선 — 인출선(3D 화살표)이 앵커를 가리키므로 여기선 둥근 말풍선만. 드래그 이동 가능. */
.memo-label { position: absolute; transform: translate(-50%, -50%); display: flex; align-items: center; gap: 5px;
  background: #fff7ed; color: #7c2d12; border: 1.5px solid #ff6b35; border-radius: 10px;
  padding: 3px 6px 3px 9px; font-size: 12px; max-width: 280px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; box-shadow: 0 2px 6px #0004; cursor: grab; user-select: none; }
.memo-label:active { cursor: grabbing; }
.memo-text { pointer-events: auto; }
.memo-x { background: none; border: none; color: #9a3412; font-size: 14px; line-height: 1;
  cursor: pointer; padding: 0 2px; }
.memo-x:hover { color: #ef4444; }
.dim-label { position: absolute; transform: translate(-50%, -50%); pointer-events: none;
  background: #fff; color: #12161b; border: 1px solid #9aa3ad; border-radius: 3px;
  padding: 1px 5px; font-size: 11px; font-weight: 600; white-space: nowrap; }

/* viewport */
#viewport { position: relative; flex: 1 1 auto; min-width: 0; }
#viewport canvas { display: block; }
#drop-hint { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: #6b7280; font-size: 15px; pointer-events: none; text-align: center; padding: 20px; }
#drop-hint small { color: #545b64; font-size: 12px; }
#viewport.dragover { outline: 3px dashed #2f6fed; outline-offset: -12px; }
#viewport.has-model #drop-hint { display: none; }

/* progress overlay */
#progress { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(18,20,23,.72); z-index: 20; }
#progress[hidden] { display: none; }   /* ID display:flex would otherwise override the hidden attribute */
.pcard { background: #23272e; border: 1px solid #333; border-radius: 10px; padding: 22px 26px; width: 340px; text-align: center; box-shadow: 0 10px 40px #0007; }
#pstage { font-size: 14px; margin-bottom: 12px; color: #e6e8ea; }
.pbar { height: 8px; background: #333a44; border-radius: 4px; overflow: hidden; margin-bottom: 10px; }
#pfill { height: 100%; width: 0%; background: linear-gradient(90deg, #2f6fed, #5db3ff); transition: width .3s ease; }
#pfill.indeterminate { width: 35% !important; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { 0% { margin-left: -35%; } 100% { margin-left: 100%; } }
#pdetail { font-size: 12px; color: #9aa1a9; margin-bottom: 12px; min-height: 16px; }
#pcancel { margin-top: 2px; }

/* floating controls */
.floatctl { position: absolute; right: 14px; background: rgba(35,39,46,.9); border: 1px solid #3a4049; border-radius: 8px; padding: 10px 12px; z-index: 15; font-size: 12px; }
#section-ctl { top: 14px; width: 210px; }
#explode-ctl { top: 14px; width: 210px; }
#measure-ctl { top: 14px; width: 210px; }
.fc-title { font-weight: 600; margin-bottom: 8px; }
.fc-row { display: flex; gap: 5px; margin-bottom: 8px; }
.floatctl input[type=range] { width: 100%; }
#measure-info { color: #9aa1a9; margin-bottom: 8px; }
#exp-val { color: #9aa1a9; }

/* measurement label */
.measure-label { position: absolute; background: rgba(17,17,17,.85); color: #fff; padding: 2px 6px; border-radius: 4px; font-size: 12px; pointer-events: none; transform: translate(-50%, -130%); white-space: nowrap; z-index: 16; }

.toast { position: absolute; left: 50%; bottom: 20px; transform: translateX(-50%); background: #c0392b; color: #fff; padding: 10px 16px; border-radius: 6px; font-size: 13px; z-index: 30; max-width: 80%; }
/* 3면도 각 분할의 뷰 이름 */
.mv-label { position: absolute; pointer-events: none; background: #ffffffcc; color: #12161b;
  border: 1px solid #9aa3ad; border-radius: 3px; padding: 1px 6px; font-size: 11px; font-weight: 700; }

/* 텍스트/미디어 뷰어 */
#docview .doc-text { padding: 16px 20px; }
#docview .doc-pre { margin: 0; white-space: pre; font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.5px; line-height: 1.5; color: #d7dbe0; }
#docview .doc-table { border-collapse: collapse; font-size: 12.5px; }
#docview .doc-table th, #docview .doc-table td { border: 1px solid #333a44; padding: 3px 8px;
  white-space: nowrap; text-align: left; }
#docview .doc-table th { background: #23272e; color: #fff; position: sticky; top: 0; }
#docview .doc-table tr:nth-child(even) td { background: #1b1e22; }

/* 툴바 색상 피커 타일 — 스와치가 아이콘, 아래 라벨 */
.pickw input[type=color] { width: 22px; height: 14px; padding: 0; border: 1px solid #3a4049;
  border-radius: 4px; background: none; cursor: pointer; }
.pickw input[type=color]::-webkit-color-swatch-wrapper { padding: 1px; }
.pickw input[type=color]::-webkit-color-swatch { border: none; border-radius: 2px; }

/* 낙서 오버레이 캔버스 */
/* canvas#scribble — `#viewport canvas`(1,0,1)와 특정도를 맞춰 display:none 이 지도록 한다.
   안 그러면 낙서 캔버스가 처음부터 보여 뷰포트 입력을 가로챈다(우클릭 회전 먹통). */
canvas#scribble { position: absolute; inset: 0; width: 100%; height: 100%; display: none;
  touch-action: none; cursor: crosshair; z-index: 14; }
#draw-ctl { top: 14px; width: 210px; }
#draw-ctl input[type=color] { width: 26px; height: 20px; padding: 0; border: 1px solid #3a4049; border-radius: 3px; background: none; }
#draw-ctl input[type=range] { flex: 1; }
/* 재질 컨트롤 */
#mtl-ctl { top: 14px; width: 210px; }
.mtl-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 11px; color: #9aa1a9; margin-top: 6px; }
.mtl-row input[type=range] { flex: 1; }

/* 부품트리 색상 피커 */
#tree-list li .partcol { width: 16px; height: 14px; padding: 0; border: 1px solid #3a4049;
  border-radius: 3px; background: none; cursor: pointer; flex: 0 0 auto; }
#tree-list li .partcol::-webkit-color-swatch-wrapper { padding: 1px; }
#tree-list li .partcol::-webkit-color-swatch { border: none; border-radius: 2px; }

/* ===== 모바일 — 상단 메뉴바를 그대로 쓰되 폭을 촘촘히 ===== */
@media (max-width: 820px) {
  #toolbar { padding: 3px 4px; gap: 0; }
  #langsel { margin-left: 6px; }
  #langsel button { padding: 3px 6px; font-size: 11px; }
  .tlabel { padding: 8px 8px; font-size: 12px; gap: 4px; }
  .tlabel::after { display: none; }   /* 좁은 화면에선 캐럿 생략해 폭 절약 */
  /* 트리 패널은 상단바+메뉴바(약 94px) 아래에 화면 위로 겹쳐 표시 */
  #tree-panel { position: fixed; top: 94px; bottom: 0; left: 0; width: 82%; max-width: 320px; z-index: 38; }
}
