:root {
  color-scheme: dark;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body,
#app {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #1e1e1e;
  color: #eee;
}

#app {
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  background: #262626;
  border-bottom: 1px solid #333;
  gap: 0.5rem;
  flex-wrap: wrap;
  z-index: 20;
}

.topbar h1 {
  font-size: 1rem;
  font-weight: 600;
}

.actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

select,
.upload-btn,
.tool-btn {
  background: #333;
  color: #eee;
  border: 1px solid #444;
  border-radius: 6px;
  padding: 0.4rem 0.8rem;
  font-size: 0.9rem;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.upload-btn:hover,
select:hover,
.tool-btn:hover {
  background: #3d3d3d;
}

.tool-btn.active {
  background: #0a84ff;
  border-color: #0a84ff;
  color: #fff;
}

.main {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

#viewer-container {
  position: relative;
  flex: 1;
  overflow: hidden;
}

#viewer-container canvas {
  display: block;
  touch-action: none;
}

#props-panel {
  width: 280px;
  background: #232323;
  border-left: 1px solid #333;
  padding: 0.75rem;
  overflow-y: auto;
  font-size: 0.85rem;
  position: relative;
}

#props-panel .panel-close {
  position: absolute;
  top: 0;
  right: 0;
  background: transparent;
  color: #aaa;
  border: none;
  font-size: 1.8rem;
  cursor: pointer;
  padding: 0.5rem 0.7rem;
  border-radius: 0;
  display: none;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
}

#props-panel .panel-close:hover {
  background: #333;
  color: #fff;
}

#props-panel .panel-close:active {
  background: #444;
}

#props-panel .hint {
  color: #888;
  text-align: center;
  margin-top: 1rem;
}

#props-panel .kv {
  display: flex;
  justify-content: space-between;
  padding: 0.4rem 0;
  border-bottom: 1px solid #2e2e2e;
  gap: 0.5rem;
}

#props-panel .kv .k {
  color: #999;
  font-size: 0.78rem;
  flex-shrink: 0;
}

#props-panel .kv .v {
  color: #eee;
  word-break: break-all;
  text-align: right;
}

#status {
  position: fixed;
  bottom: 0.5rem;
  left: 0.5rem;
  right: 0.5rem;
  padding: 0.5rem 0.8rem;
  background: rgba(0, 0, 0, 0.65);
  border-radius: 6px;
  font-size: 0.85rem;
  pointer-events: none;
  text-align: center;
  backdrop-filter: blur(6px);
  max-width: 800px;
  margin: 0 auto;
  z-index: 5;
}

/* tablet */
@media (max-width: 900px) {
  #props-panel {
    width: 240px;
  }
}

/* mobile: panel becomes a slide-out drawer, hidden by default */
@media (max-width: 700px) {
  .topbar {
    padding: 0.5rem 0.6rem;
  }
  .topbar h1 {
    font-size: 0.9rem;
  }
  select,
  .upload-btn,
  .tool-btn {
    font-size: 0.78rem;
    padding: 0.35rem 0.55rem;
  }

  #props-panel {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 78%;
    max-width: 320px;
    z-index: 30;
    box-shadow: -4px 0 16px rgba(0, 0, 0, 0.6);
    transform: translateX(100%);
    transition: transform 0.22s ease-out;
    padding: 0;
    padding-top: 3.5rem;
    display: flex;
    flex-direction: column;
  }
  #props-panel.open {
    transform: translateX(0);
  }
  #props-panel .panel-close {
    display: flex;
    position: fixed;
    right: 0;
    top: 0;
    width: 44px;
    height: 44px;
  }

  /* 內容可滾動 */
  #props-panel .hint,
  #props-panel .kv {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }
  #props-panel .hint {
    padding-top: 0.75rem;
  }
}

/* 測量面板 */
.measure-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(20, 20, 20, 0.92);
  backdrop-filter: blur(8px);
  border-top: 1px solid #333;
  z-index: 10;
}

.measure-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 0.8rem;
}

#measure-summary {
  font-size: 0.85rem;
  color: #aaa;
}

.measure-panel-actions {
  display: flex;
  gap: 0.4rem;
}

.measure-list {
  display: none;
  list-style: none;
  max-height: 160px;
  overflow-y: auto;
  padding: 0 0.5rem 0.5rem;
}

.measure-list.visible {
  display: block;
}

.measure-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.35rem 0.3rem;
  border-bottom: 1px solid #2a2a2a;
  font-size: 0.83rem;
}

.measure-item .value {
  color: #00d1ff;
  font-variant-numeric: tabular-nums;
  margin: 0 0.5rem;
}

.measure-item .del-btn {
  background: transparent;
  border: none;
  color: #666;
  cursor: pointer;
  padding: 0 0.3rem;
  min-width: 28px;
  min-height: 28px;
  font-size: 1rem;
}

.measure-item .del-btn:hover {
  color: #fff;
}

/* 面板開啟時，狀態列上移避免遮擋 */
body.has-measure #status {
  bottom: 2.8rem;
}

@media (max-width: 700px) {
  .measure-list {
    max-height: 120px;
  }
}

/* 圖層面板 */
.layer-panel {
  position: fixed;
  top: 3.5rem;
  right: 0;
  width: 200px;
  max-height: calc(100vh - 4rem);
  background: rgba(20, 20, 20, 0.95);
  backdrop-filter: blur(8px);
  border-left: 1px solid #333;
  border-bottom: 1px solid #333;
  z-index: 25;
  display: flex;
  flex-direction: column;
}
.layer-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid #333;
  font-size: 0.85rem;
  font-weight: 600;
  flex-shrink: 0;
}
.layer-list {
  overflow-y: auto;
  flex: 1;
  padding: 0.4rem 0;
}
.layer-hint {
  color: #666;
  font-size: 0.8rem;
  padding: 0.5rem 0.75rem;
}
.layer-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0.75rem;
  cursor: pointer;
  font-size: 0.82rem;
  user-select: none;
}
.layer-item:hover {
  background: #2a2a2a;
}
.layer-item input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: #0a84ff;
}
.layer-item.hidden-layer {
  opacity: 0.45;
}

@media (max-width: 700px) {
  .layer-panel {
    width: 160px;
    top: auto;
    bottom: 3rem;
    right: 0;
    max-height: 50vh;
  }
}
