/* =========================================================
 * style.css — Material Design 3（暗色）主题
 * 零第三方依赖，自绘 MD3 组件：应用栏 / 分段按钮 / 开关 / 滑块 / 卡片
 * ========================================================= */

/* ---------------- Design Tokens (MD3 dark) ---------------- */
:root {
  --md-primary: #8ab4ff;
  --md-on-primary: #00315d;
  --md-primary-container: #00468f;
  --md-on-primary-container: #d6e3ff;
  --md-secondary-container: #3d4c63;
  --md-on-secondary-container: #d5e3ff;
  --md-surface: #121318;
  --md-surface-1: #1b1c21;
  --md-surface-2: #1f2026;
  --md-surface-3: #292a31;
  --md-on-surface: #e3e1e8;
  --md-on-surface-variant: #c4c5cf;
  --md-outline: #8e8f99;
  --md-error: #ffb4ab;
  --md-on-error: #690005;
  --md-error-container: #93000a;

  --md-elev-1: 0 1px 3px rgba(0, 0, 0, .5), 0 1px 2px rgba(0, 0, 0, .4);
  --md-elev-2: 0 2px 6px rgba(0, 0, 0, .42), 0 2px 4px rgba(0, 0, 0, .32);
  --md-elev-3: 0 4px 14px rgba(0, 0, 0, .46), 0 3px 8px rgba(0, 0, 0, .36);

  --md-radius-lg: 16px;
  --md-radius-md: 12px;
  --md-radius-sm: 10px;

  --md-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
}

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

html, body { height: 100%; }

body {
  font-family: var(--md-font);
  background: var(--md-surface);
  color: var(--md-on-surface);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  touch-action: manipulation; /* 消除移动端点击/手势的误缩放 */
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

/* ---------------- 布局（保证右侧栏始终可见） ---------------- */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* 移动端浏览器地址栏伸缩时更稳 */
  min-width: 0;
  overflow: auto;
}

/* 抽屉面板：样式见下方“右侧面板 → 滑入抽屉” */

/* 顶部应用栏 */
.appbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 16px;
  height: 64px;
  flex-shrink: 0;
  background: var(--md-surface-2);
  box-shadow: var(--md-elev-2);
  z-index: 10;
  overflow-x: auto;
  scrollbar-width: none;
}
.appbar::-webkit-scrollbar { display: none; }

.appbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
}
.appbar-icon { color: var(--md-primary); flex-shrink: 0; }
.appbar-title {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: .3px;
}

.segmented {
  display: flex;
  background: var(--md-surface-3);
  border-radius: 28px;
  padding: 4px;
  gap: 4px;
  margin: 0 auto;
}
.segmented-btn {
  padding: 0 18px;
  height: 40px;
  border-radius: 22px;
  font-size: 14px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--md-on-surface-variant);
  transition: background .18s ease, color .18s ease, box-shadow .18s ease, transform .06s ease;
}
.segmented-btn svg { flex-shrink: 0; }
.segmented-btn:active { transform: scale(.96); }
.segmented-btn:hover { color: var(--md-on-surface); }
.segmented-btn.active {
  background: var(--md-primary-container);
  color: var(--md-on-primary-container);
  box-shadow: var(--md-elev-1);
}
/* 鼠标（选择）工具：图标按钮更紧凑 */
.segmented-btn.select-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
}
/* 面板内紧凑分段选择器 */
.seg-sm { margin: 0 0 12px; }
.seg-sm .segmented-btn { height: 32px; padding: 0 13px; font-size: 13px; }
/* 预设按钮较多：允许换行，参考 Material segmented 弹性宽度 */
.seg-wrap { flex-wrap: wrap; justify-content: flex-start; margin: 0 0 12px; }
.seg-wrap .segmented-btn { flex: 1 1 auto; text-align: center; padding: 0 10px; }

.appbar-actions { display: flex; gap: 10px; white-space: nowrap; }

/* 主舞台：画布弹性伸缩，面板固定宽度不消失 */
#stage {
  flex: 1;
  display: flex;
  min-height: 0;
}

#scene {
  flex: 1;
  min-width: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  background: #10151d;
}

/* 右侧面板 → 滑入抽屉（所有尺寸统一）。
   默认收起（translateX 移出屏幕），由 body.panel-open 控制滑入。
   top 从应用栏下方开始，保证工具栏（含“☰ 面板”）始终可点。 */
#panel {
  position: fixed;
  top: 64px;
  right: 0;
  bottom: 0;
  height: calc(100vh - 64px);
  width: 320px;
  z-index: 40;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  background: var(--md-surface-1);
  border-left: 1px solid rgba(255, 255, 255, .08);
  box-shadow: var(--md-elev-3);
  transform: translateX(103%);
  transition: transform .3s cubic-bezier(.2, .8, .25, 1);
}
body.panel-open #panel { transform: translateX(0); }

/* 抽屉背板：打开时显示，点击关闭；同样避开应用栏 */
#panel-backdrop {
  display: none;
  position: fixed;
  top: 64px;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 35;
  background: rgba(0, 0, 0, .45);
}
body.panel-open #panel-backdrop { display: block; }

/* ---------------- 人眼视图悬浮按钮（FAB） ---------------- */
#stage { position: relative; }

.fab-eye {
  position: absolute;
  left: 20px;
  bottom: 20px;
  z-index: 20;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--md-primary-container);
  color: var(--md-on-primary-container);
  box-shadow: var(--md-elev-3);
  transition: background .18s ease, transform .1s ease, box-shadow .18s ease;
  user-select: none;
}
.fab-eye:hover { box-shadow: var(--md-elev-2); transform: translateY(-2px); filter: brightness(1.08); }
.fab-eye:active { transform: scale(.95); }
.fab-eye.active {
  background: var(--md-primary);
  color: var(--md-on-primary);
  box-shadow: 0 0 0 4px rgba(138, 180, 255, .35), var(--md-elev-3);
}

/* 底部状态栏 */
.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 16px;
  height: 40px;
  flex-shrink: 0;
  background: var(--md-surface-2);
  border-top: 1px solid rgba(255, 255, 255, .06);
  font-size: 12.5px;
  color: var(--md-on-surface-variant);
}
#status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#info { color: var(--md-error); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------------- 卡片 ---------------- */
.m3-card {
  background: var(--md-surface-2);
  border-radius: var(--md-radius-lg);
  box-shadow: var(--md-elev-1);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border: 1px solid rgba(255, 255, 255, .05);
}
.m3-card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--md-on-surface);
  margin-bottom: 4px;
}
.sel-info {
  font-size: 12.5px;
  color: var(--md-on-surface-variant);
  padding: 4px 0 8px;
}

/* ---------------- 按钮 ---------------- */
.m3-btn {
  height: 40px;
  padding: 0 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .2px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  transition: box-shadow .18s ease, filter .18s ease, transform .06s ease, background .18s ease;
  user-select: none;
}
.m3-btn svg { flex-shrink: 0; }
.m3-btn .caret { transition: transform .2s ease; }
.m3-btn.caret-flip .caret { transform: rotate(180deg); }
.m3-btn:active { transform: scale(.97); }
.m3-btn-filled { background: var(--md-primary); color: var(--md-on-primary); box-shadow: var(--md-elev-1); }
.m3-btn-filled:hover { box-shadow: var(--md-elev-2); filter: brightness(1.05); }
.m3-btn-tonal { background: var(--md-secondary-container); color: var(--md-on-secondary-container); }
.m3-btn-tonal:hover { box-shadow: var(--md-elev-1); filter: brightness(1.08); }
.m3-btn-tonal.active { background: var(--md-primary-container); color: var(--md-on-primary-container); box-shadow: var(--md-elev-1); }
.m3-btn-error { background: var(--md-error-container); color: var(--md-error); width: 100%; }
.m3-btn-error:hover { box-shadow: var(--md-elev-1); filter: brightness(1.12); }

/* ---------------- 开关（MD3 Switch，保留真实 checkbox 供 JS 读取） ---------------- */
.m3-switch {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 5px 0;
  cursor: pointer;
  user-select: none;
}
.m3-switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.m3-switch .track {
  position: relative;
  flex-shrink: 0;
  width: 52px;
  height: 32px;
  border-radius: 16px;
  background: var(--md-surface-3);
  border: 2px solid var(--md-outline);
  transition: background .2s ease, border-color .2s ease;
}
.m3-switch .thumb {
  position: absolute;
  top: 50%;
  left: 2px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--md-on-surface-variant);
  transform: translateY(-50%);
  transition: left .2s ease, background .2s ease;
}
.m3-switch input:checked + .track {
  background: var(--md-primary);
  border-color: var(--md-primary);
}
.m3-switch input:checked + .track .thumb { left: 26px; background: #ffffff; }
.m3-switch input:disabled + .track { opacity: .38; }
.m3-switch input:disabled ~ .label { opacity: .38; }
.m3-switch .label { font-size: 14px; line-height: 1.35; color: var(--md-on-surface); }

/* ---------------- 滑块（MD3 Slider） ---------------- */
.m3-slider-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0 4px;
  min-width: 0;
}
.slider-label { font-size: 14px; color: var(--md-on-surface-variant); white-space: nowrap; flex-shrink: 0; }
.slider-val { font-size: 14px; font-weight: 600; color: var(--md-primary); min-width: 42px; text-align: right; flex-shrink: 0; }
.slider-val.dim { color: var(--md-on-surface-variant); opacity: .38; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1 1 auto;
  min-width: 0;          /* 关键：允许滑块在行内收缩，避免超出卡片/屏幕右侧 */
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--md-surface-3);
  outline: none;
  cursor: pointer;
}
input[type="range"]:disabled { opacity: .38; cursor: not-allowed; }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--md-primary);
  border: none;
  box-shadow: var(--md-elev-1);
  transition: transform .1s ease, box-shadow .1s ease;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.15); box-shadow: var(--md-elev-2); }
input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--md-primary);
  border: none;
  box-shadow: var(--md-elev-1);
}

/* ---------------- 工具/动作按钮内的文字不换行 ---------------- */
.tool-btn { white-space: nowrap; }
.appbar-actions .btn-txt, .tool-btn .tool-txt { white-space: nowrap; }

/* ---------------- 重置/透视预设下拉菜单（MD3 Menu） ---------------- */
.md3-menu {
  position: fixed;
  z-index: 60;
  min-width: 216px;
  max-width: min(320px, 90vw);
  padding: 6px;
  background: var(--md-surface-2);
  border-radius: 12px;
  box-shadow: var(--md-elev-3);
  border: 1px solid rgba(255, 255, 255, .07);
  transform-origin: top right;
  animation: menuPop .16s ease-out;
}
.md3-menu-title {
  padding: 8px 14px 6px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .4px;
  color: var(--md-on-surface-variant);
  text-transform: uppercase;
}
.md3-menu-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--md-on-surface);
  text-align: left;
  transition: background .15s ease;
}
.md3-menu-item:hover { background: var(--md-surface-3); }
.md3-menu-item:active { background: var(--md-secondary-container); }
.md3-menu-item .ck {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  color: var(--md-primary);
  opacity: 0;
  transform: scale(.6);
  transition: opacity .15s ease, transform .15s ease;
}
.md3-menu-item.active .ck { opacity: 1; transform: scale(1); }
.md3-menu-item span { flex: 1; }

/* ---------------- 动画关键帧 ---------------- */
@keyframes menuPop {
  from { opacity: 0; transform: translateY(-6px) scale(.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
/* 卡片进入动画（抽屉内元素） */
@keyframes cardIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}
#panel .m3-card { animation: cardIn .35s ease both; }
#panel .m3-card:nth-child(1) { animation-delay: .03s; }
#panel .m3-card:nth-child(2) { animation-delay: .08s; }
#panel .m3-card:nth-child(3) { animation-delay: .13s; }

/* ---------------- 灭点图例 ---------------- */
.legend { display: flex; gap: 10px; padding: 6px 0 2px; }
.lg {
  font-size: 12.5px;
  color: var(--md-on-surface-variant);
  display: flex;
  align-items: center;
  gap: 6px;
}
.lg::before {
  content: "";
  width: 12px;
  height: 4px;
  border-radius: 2px;
  background: var(--c);
}

/* ---------------- 滚动条 ---------------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--md-surface-3); border-radius: 6px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: var(--md-outline); background-clip: content-box; }

/* ---------------- 响应式 ---------------- */
@media (max-width: 900px) {
  .appbar-title { display: none; }
  #panel { width: 288px; }
}

/* ==================== 移动端优化 ==================== */
@media (max-width: 768px) {
  /* 全屏布局：用 100dvh 适配浏览器 UI 伸缩 */
  #app {
    overflow: hidden;
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* 顶部栏：压缩间距，隐藏品牌文字 */
  .appbar {
    gap: 8px;
    padding: 0 env(safe-area-inset-right);
    padding-left: 8px;
    height: 56px;
  }
  .appbar-brand { display: none; }
  .appbar .segmented { margin: 0; flex-shrink: 0; }
  .appbar-actions { flex-shrink: 0; gap: 6px; }
  .appbar-actions .m3-btn { padding: 0 12px; height: 38px; }
  /* 动作按钮（添加/重置/面板）只保留图标，节省空间 */
  .appbar-actions .btn-txt { display: none; }
  /* 工具按钮也仅保留图标 */
  .tool-btn .tool-txt { display: none; }
  .tool-btn { padding: 0 12px; }

  /* 抽屉：更宽的滑动区域，从 56px 应用栏下方开始（基座样式已统一，仅调尺寸） */
  #panel {
    top: 56px;
    height: calc(100dvh - 56px);
    width: min(84vw, 340px);
  }
  body.panel-open #panel-backdrop {
    top: 56px;
    background: rgba(0, 0, 0, .5);
  }

  /* 画布铺满剩余空间，触控手势交给 JS */
  #scene { touch-action: none; }

  /* 人眼视图 FAB 移到更易点按的位置 */
  .fab-eye { left: 16px; bottom: 16px; width: 54px; height: 54px; }

  /* 状态栏：压缩、只保留单行提示 */
  .statusbar { height: 34px; font-size: 11.5px; padding: 0 10px; }

  /* 面板内控件触控友好 */
  .m3-switch { padding: 6px 0; }
  .m3-switch .label { font-size: 15px; }
  .seg-sm .segmented-btn { min-height: 36px; }
  .m3-btn { min-height: 40px; }
}
