:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #d9dce1;
  --text: #1f2328;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #dbe7ff;
  --hover: #eef1f5;
  font-family: "Malgun Gothic", "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
  font-size: 14px;
  color: var(--text);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181c;
    --panel: #1d2126;
    --border: #2f353d;
    --text: #e6e8eb;
    --muted: #8b95a1;
    --accent: #5b8def;
    --accent-soft: #233656;
    --hover: #262b31;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

button {
  font: inherit;
  color: inherit;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
}
button:hover { background: var(--hover); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 16px; margin: 0 8px 0 0; }
.topbar .hint { margin-left: auto; color: var(--muted); font-size: 12px; }

.layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ── 좌측: 폴더 트리 ── */
.sidebar {
  width: 300px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.folder-head { padding: 10px 12px 6px; }
.folder-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-stats { color: var(--muted); font-size: 12px; margin-top: 2px; }
.list-tools { display: flex; gap: 6px; margin: 4px 12px 8px; }
.view-toggle { display: flex; }
.view-toggle button { padding: 4px 8px; font-size: 12px; border-radius: 0; }
.view-toggle button:first-child { border-radius: 6px 0 0 6px; }
.view-toggle button:last-child { border-radius: 0 6px 6px 0; border-left: 0; }
.filter {
  flex: 1;
  min-width: 0;
  padding: 6px 8px;
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.tree { flex: 1; overflow: auto; padding-bottom: 12px; outline: none; }
.tree details > summary {
  list-style: none;
  cursor: pointer;
  padding: 3px 10px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
.tree details > summary::before { content: "▸ "; }
.tree details[open] > summary::before { content: "▾ "; }
.tree .dir { padding-left: 12px; }
.tree .file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 4px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.tree .file:hover { background: var(--hover); }
.tree .file.active { background: var(--accent-soft); }
.tree .file .name { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.tree .file .size { color: var(--muted); font-size: 11px; }
.badge {
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  color: #fff;
  background: #0f9d58;
}
.badge.dwg { background: #c2410c; }

/* 썸네일 카드 보기 */
.tree .grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 8px;
  padding: 4px 10px 8px 14px;
}
.tree .file.card {
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
  padding: 5px;
  border: 1px solid var(--border);
  border-radius: 6px;
  white-space: normal;
}
.tree .file.card.active { border-color: var(--accent); }
.card .thumb {
  position: relative;
  aspect-ratio: 24 / 17;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1b1f24;
  border-radius: 4px;
  overflow: hidden;
}
.card .thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.card .thumb .badge { position: absolute; top: 4px; left: 4px; z-index: 1; }
.card .thumb .ph { color: #8b95a1; font-size: 11px; }
.card .thumb.working::after {
  content: "";
  width: 18px; height: 18px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.card .meta { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
.card .meta .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.card .meta .size { flex: none; }

/* ── 가운데: 뷰어 ── */
.stage { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.toolbar label { display: flex; align-items: center; gap: 4px; cursor: pointer; }
.toolbar .sep { width: 1px; height: 20px; background: var(--border); }

.viewport { position: relative; flex: 1; min-height: 0; background: #1b1f24; }
.viewport canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.viewport.measuring canvas { cursor: crosshair; }

.empty, .loading, .drop-zone, .dwg-panel {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: #cfd5dc;
}
.empty .muted { color: #8b95a1; font-size: 13px; }
.loading { background: rgba(0, 0, 0, 0.35); gap: 10px; }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid rgba(255, 255, 255, 0.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.drop-zone {
  background: rgba(37, 99, 235, 0.25);
  border: 3px dashed #5b8def;
  font-size: 18px;
  font-weight: 600;
}
.dwg-panel { background: #1b1f24; gap: 14px; padding: 20px; }
.dwg-panel img {
  max-width: min(90%, 900px);
  max-height: 70%;
  image-rendering: pixelated;
  background: #000;
  border: 1px solid #333;
}
.dwg-info { max-width: 640px; line-height: 1.6; font-size: 13px; }
.dwg-info .muted { color: #8b95a1; }

[hidden] { display: none !important; }

.statusbar {
  display: flex;
  gap: 16px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border-top: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
}
.statusbar .cursor { margin-left: auto; font-variant-numeric: tabular-nums; }

/* ── 우측: 레이어 ── */
.layer-panel {
  width: 240px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
  min-height: 0;
}
.layer-panel.collapsed { display: none; }
.layer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.layer-head button { padding: 2px 6px; font-size: 12px; }
.layer-list { list-style: none; margin: 0; padding: 4px 0; overflow: auto; flex: 1; }
.layer-list label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 10px;
  cursor: pointer;
}
.layer-list label:hover { background: var(--hover); }
.layer-list .swatch { width: 12px; height: 12px; border-radius: 2px; border: 1px solid var(--border); flex: none; }
.layer-list .lname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-list .count { color: var(--muted); font-size: 11px; }

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; max-height: 35vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .layer-panel { width: auto; max-height: 25vh; border-left: 0; border-top: 1px solid var(--border); }
  .stage { min-height: 50vh; }
  .topbar .hint { display: none; }
}
