/* 自绘取色器：替换 input[type=color] 的显示层。
   原生控件只是被隐藏，值和事件仍然由它承载，业务代码不用改。 */

.cad-color-native {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  border: 0 !important;
  margin: 0 !important;
  opacity: 0 !important;
  pointer-events: none !important;
}

.cad-color-swatch {
  display: inline-block;
  width: 34px;
  height: 22px;
  min-height: 0;
  padding: 0;
  border: 1px solid rgba(0, 0, 0, 0.28);
  border-radius: 4px;
  background: #ffffff;
  cursor: pointer;
  vertical-align: middle;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.cad-color-swatch:focus-visible {
  outline: 2px solid #1677ff;
  outline-offset: 1px;
}

.cad-color-panel {
  position: fixed;
  z-index: 100000;
  width: 248px;
  padding: 10px;
  border: 1px solid rgba(0, 0, 0, 0.16);
  border-radius: 10px;
  background: #ffffff;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22);
  font-size: 13px;
  touch-action: none;
  user-select: none;
}

.cad-color-panel[hidden] {
  display: none;
}

.cad-color-sv {
  position: relative;
  height: 148px;
  border-radius: 6px;
  background: #ff0000;
  overflow: hidden;
  cursor: crosshair;
}

.cad-color-sv-white,
.cad-color-sv-black {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.cad-color-sv-white {
  background: linear-gradient(to right, #ffffff, rgba(255, 255, 255, 0));
}

.cad-color-sv-black {
  background: linear-gradient(to top, #000000, rgba(0, 0, 0, 0));
}

.cad-color-sv-cursor {
  position: absolute;
  width: 12px;
  height: 12px;
  margin: -6px 0 0 -6px;
  border: 2px solid #ffffff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
  pointer-events: none;
}

.cad-color-hue {
  position: relative;
  height: 16px;
  margin-top: 8px;
  border-radius: 8px;
  background: linear-gradient(
    to right,
    #ff0000 0%,
    #ffff00 17%,
    #00ff00 33%,
    #00ffff 50%,
    #0000ff 67%,
    #ff00ff 83%,
    #ff0000 100%
  );
  cursor: pointer;
}

.cad-color-hue-cursor {
  position: absolute;
  top: -2px;
  width: 8px;
  height: 20px;
  margin-left: -4px;
  border: 2px solid #ffffff;
  border-radius: 4px;
  background: transparent;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}

.cad-color-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}

.cad-color-preview {
  width: 32px;
  height: 26px;
  border: 1px solid rgba(0, 0, 0, 0.25);
  border-radius: 4px;
  background: #ffffff;
  flex: none;
}

.cad-color-hex {
  flex: 1;
  min-width: 0;
  padding: 4px 6px;
  border: 1px solid rgba(0, 0, 0, 0.25);
  border-radius: 4px;
  background: #ffffff;
  color: #1f2328;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.cad-color-swatches {
  display: grid;
  /* 一排 6 个正好铺满面板宽度；9 列时全局 button 规则（高度 32px + 内边距）
     会抬高每个格子的最小内容宽度，整排溢出面板。 */
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  margin-top: 8px;
}

.cad-color-swatch-chip {
  width: 100%;
  min-width: 0;
  height: auto;
  aspect-ratio: 1;
  padding: 0;
  border: 1px solid rgba(0, 0, 0, 0.22);
  border-radius: 3px;
  cursor: pointer;
}

.cad-color-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 10px;
}

.cad-color-btn {
  padding: 5px 14px;
  border: 1px solid rgba(0, 0, 0, 0.25) !important;
  border-radius: 6px;
  /* styles.css 的全局 button 规则带 !important，这里必须同样加 important 才不被压。 */
  background: #f4f5f6 !important;
  color: #1f2328 !important;
  font-size: 13px;
  cursor: pointer;
}

.cad-color-btn-primary {
  border-color: #1677ff !important;
  background: #1677ff !important;
  color: #ffffff !important;
}

/* 深色主题 */
body.theme-dark .cad-color-panel {
  border-color: rgba(255, 255, 255, 0.18);
  background: #22262b;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
}

body.theme-dark .cad-color-swatch {
  border-color: rgba(255, 255, 255, 0.35);
}

body.theme-dark .cad-color-hex {
  border-color: rgba(255, 255, 255, 0.25);
  background: #171a1e;
  color: #e8eaed;
}

body.theme-dark .cad-color-btn {
  border-color: rgba(255, 255, 255, 0.25) !important;
  background: #2c3137 !important;
  color: #e8eaed !important;
}

body.theme-dark .cad-color-btn-primary {
  border-color: #3c89f5 !important;
  background: #3c89f5 !important;
  color: #ffffff !important;
}

body.theme-dark .cad-color-swatch-chip {
  border-color: rgba(255, 255, 255, 0.28);
}

/* 手机 / 小程序：触摸目标放大，面板按视口收敛 */
@media (max-width: 640px) {
  .cad-color-swatch {
    width: 46px;
    height: 30px;
  }

  .cad-color-panel {
    width: min(280px, calc(100vw - 24px));
  }

  .cad-color-sv {
    height: 160px;
  }

  .cad-color-hue {
    height: 22px;
  }

  .cad-color-hue-cursor {
    height: 26px;
    top: -3px;
  }

  .cad-color-swatch-chip {
    min-width: 0;
  }

  .cad-color-btn {
    padding: 9px 18px;
    font-size: 14px;
  }
}
