/* ========== 主题变量 ========== */
:root {
  --paper: #F2EBDD;
  --ui-bg: rgba(251, 248, 241, 0.86);
  --ui-border: rgba(60, 45, 30, 0.13);
  --ui-shadow: 0 10px 30px -12px rgba(60, 40, 20, 0.28), 0 2px 6px rgba(60, 40, 20, 0.06);
  --text: #2A2420;
  --muted: #6B6058;
  --accent: #A8352A;
  --accent-ink: #FFF8F0;
  --hover: rgba(60, 40, 20, 0.07);
  --active: rgba(168, 53, 42, 0.12);
  --track: rgba(60, 40, 20, 0.14);
  --font-display: "Songti SC", "STSong", "Noto Serif SC", "Source Han Serif SC", "SimSun", serif;
  --grain-blend: multiply;
  --grain-opacity: 0.55;
  --stage-bg: radial-gradient(120% 90% at 50% 40%, #F6F0E4 0%, #EFE6D5 70%, #E6DAC4 100%);
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}
[data-theme="night"] {
  --paper: #0C0C12;
  --ui-bg: rgba(22, 22, 32, 0.72);
  --ui-border: rgba(255, 255, 255, 0.10);
  --ui-shadow: 0 12px 34px -12px rgba(0, 0, 0, 0.7), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  --text: #EDEBF5;
  --muted: #A3A0B5;
  --accent: #6FF0FF;
  --accent-ink: #04161B;
  --hover: rgba(255, 255, 255, 0.07);
  --active: rgba(111, 240, 255, 0.14);
  --track: rgba(255, 255, 255, 0.14);
  --font-display: "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  --grain-blend: overlay;
  --grain-opacity: 0.22;
  --stage-bg: radial-gradient(110% 80% at 50% 35%, #15151F 0%, #0C0C12 60%, #07070A 100%);
}
[data-theme="mist"] {
  --paper: #EAE6E1;
  --ui-bg: rgba(248, 246, 243, 0.82);
  --ui-border: rgba(70, 64, 60, 0.12);
  --ui-shadow: 0 10px 30px -14px rgba(70, 64, 60, 0.30), 0 2px 6px rgba(70, 64, 60, 0.05);
  --text: #3B3735;
  --muted: #6E6763;
  --accent: #5F7567;
  --accent-ink: #FFFFFF;
  --hover: rgba(70, 64, 60, 0.07);
  --active: rgba(95, 117, 103, 0.14);
  --track: rgba(70, 64, 60, 0.14);
  --font-display: "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  --grain-blend: multiply;
  --grain-opacity: 0.35;
  --stage-bg: radial-gradient(120% 90% at 30% 20%, #EFEBE6 0%, #E8E3DD 55%, #DFD9D2 100%);
}
/* ========== 基础 ========== */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--paper);
  color: var(--text);
  font-family: "PingFang SC", "HarmonyOS Sans SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  transition: background-color 0.4s ease;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; opacity: 0.38; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ========== 画布 ========== */
#surface { position: fixed; inset: 0; overflow: hidden; touch-action: none; cursor: crosshair; }
#stage {
  position: absolute; left: 50%; top: 50%;
  transform: translate(-50%, -50%) rotate(var(--rot, 0deg));
  background: var(--stage-bg);
  transition: background 0.4s ease;
}
#stage canvas, #grain { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#stage #glow { display: none; mix-blend-mode: screen; filter: blur(9px) saturate(1.4); opacity: 0.85; pointer-events: none; }
[data-theme="night"] #stage #glow { display: block; }
#wet { pointer-events: none; }
#grain {
  pointer-events: none;
  background-image: var(--grain-url, none);
  background-size: 180px 180px;
  mix-blend-mode: var(--grain-blend);
  opacity: var(--grain-opacity);
}

/* ========== 玻璃面板通用 ========== */
.glass {
  background: var(--ui-bg);
  -webkit-backdrop-filter: blur(18px) saturate(1.3);
  backdrop-filter: blur(18px) saturate(1.3);
  border: 1px solid var(--ui-border);
  box-shadow: var(--ui-shadow);
  transition: background-color 0.4s ease, border-color 0.4s ease, color 0.4s ease;
}
/* ========== 顶栏 ========== */
.topbar {
  position: fixed; z-index: 20;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.brand { display: flex; align-items: center; gap: 10px; height: 48px; padding: 0 16px 0 12px; border-radius: 16px; }
.brand-mark { width: 24px; height: 24px; fill: var(--accent); stroke: none; transition: fill 0.4s ease; }
.brand-text h1 { font-family: var(--font-display); font-size: 18px; font-weight: 600; letter-spacing: 0.12em; line-height: 1.1; }
.brand-text p { font-size: 11px; color: var(--muted); letter-spacing: 0.08em; margin-top: 2px; }
[data-theme="night"] .brand-text h1 { letter-spacing: 0.2em; font-weight: 500; }
.top-actions { display: flex; align-items: center; gap: 8px; }

.theme-switch { display: flex; gap: 2px; padding: 2px; border-radius: 16px; height: 48px; align-items: center; }
.theme-switch button { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 14px; transition: background-color 0.2s ease; }
.theme-switch button:hover { background: var(--hover); }
.theme-switch span {
  width: 24px; height: 24px; border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
  transition: transform 0.25s var(--ease-out), box-shadow 0.25s ease;
}
.theme-switch [data-theme="xuan"] span { background: linear-gradient(135deg, #F2EBDD 0 50%, #1C1917 50% 78%, #A8352A 78%); }
.theme-switch [data-theme="night"] span { background: linear-gradient(135deg, #0C0C12 0 50%, #3EF2FF 50% 78%, #FF3DAA 78%); }
.theme-switch [data-theme="mist"] span { background: linear-gradient(135deg, #EAE6E1 0 50%, #7E93A6 50% 78%, #C49A9A 78%); }
.theme-switch [aria-checked="true"] span { box-shadow: 0 0 0 2px var(--ui-bg), 0 0 0 4px var(--text); transform: scale(0.92); }

.btn-primary {
  display: inline-flex; align-items: center; gap: 6px;
  height: 48px; padding: 0 18px; border-radius: 16px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 15px; font-weight: 600; letter-spacing: 0.04em;
  box-shadow: var(--ui-shadow);
  transition: background-color 0.4s ease, color 0.4s ease, transform 0.15s ease, filter 0.2s ease;
}
.btn-primary:hover { filter: brightness(1.06); }
.btn-primary:active { transform: scale(0.97); }
.btn-primary svg { width: 20px; height: 20px; stroke-width: 2; }
/* ========== 底部工具栏 ========== */
.dock {
  position: fixed; z-index: 20; left: 50%;
  bottom: max(14px, calc(env(safe-area-inset-bottom) + 8px));
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 2px;
  padding: 6px; border-radius: 22px;
}
.tool {
  width: 50px; height: 50px; border-radius: 16px;
  display: grid; place-items: center; color: var(--text);
  transition: background-color 0.2s ease, transform 0.15s ease;
}
.tool:hover:not(:disabled) { background: var(--hover); }
.tool:active:not(:disabled) { transform: scale(0.94); }
.tool[aria-expanded="true"] { background: var(--active); color: var(--accent); }
[data-theme="night"] .tool[aria-expanded="true"] { color: var(--accent); }
/* 普通 / 流淌 切换 */
.brush-mode { display: flex; gap: 2px; padding: 3px; border-radius: 16px; background: var(--hover); }
.brush-mode button {
  width: 46px; height: 44px; border-radius: 13px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  color: var(--muted); font-size: 11px; line-height: 1; letter-spacing: 0.04em;
  transition: background-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}
.brush-mode svg { width: 20px; height: 20px; }
.brush-mode button:hover { color: var(--text); }
.brush-mode [aria-checked="true"] {
  background: var(--ui-bg); color: var(--accent); font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.14);
}
.dock-sep { width: 1px; height: 26px; background: var(--ui-border); margin: 0 4px; }

.swatch-now {
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--brush, #1C1917);
  box-shadow: 0 0 0 2px var(--ui-bg), 0 0 0 3px var(--ui-border), inset 0 -3px 6px rgba(0, 0, 0, 0.18);
  transition: background-color 0.2s ease;
}
.size-now {
  width: var(--dot, 12px); height: var(--dot, 12px); border-radius: 50%;
  background: currentColor; transition: width 0.15s ease, height 0.15s ease;
}
.grav-dial {
  position: relative; width: 28px; height: 28px; border-radius: 50%;
  border: 1.8px solid currentColor; opacity: 0.95;
}
.grav-arrow {
  position: absolute; left: 50%; top: 50%; width: 2px; height: 11px; margin-left: -1px;
  background: currentColor; border-radius: 2px; transform-origin: 50% 0;
  transform: rotate(var(--ga, 0deg)) scaleY(var(--gm, 0.6));
}
.grav-arrow::after {
  content: ""; position: absolute; left: 50%; bottom: -3px; width: 7px; height: 7px; margin-left: -3.5px;
  border-right: 2px solid currentColor; border-bottom: 2px solid currentColor;
  transform: rotate(45deg); border-radius: 1px;
}
.grav-dial.flat .grav-arrow { opacity: 0; }
.grav-dial.flat::after {
  content: ""; position: absolute; inset: 9px; border-radius: 50%; background: currentColor;
}
/* ========== 弹出面板 ========== */
.panel {
  position: fixed; z-index: 30; left: 50%;
  bottom: calc(max(14px, calc(env(safe-area-inset-bottom) + 8px)) + 76px);
  width: min(344px, calc(100vw - 24px));
  padding: 16px; border-radius: 22px;
  transform: translateX(-50%);
  transform-origin: 50% 100%;
  animation: panel-in 0.22s var(--ease-out);
}
@keyframes panel-in {
  from { opacity: 0; transform: translateX(-50%) translateY(8px) scale(0.97); }
  to { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
}
.panel-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 14px; }
.panel h2 { font-size: 15px; font-weight: 600; letter-spacing: 0.04em; }
.panel output, .panel-note { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.hint { font-size: 13px; line-height: 1.6; color: var(--muted); margin-top: 12px; }

.swatches { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.swatch {
  aspect-ratio: 1; width: 100%; max-width: 64px; justify-self: center; border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 -4px 8px rgba(0, 0, 0, 0.18), inset 0 0 0 1px rgba(0, 0, 0, 0.08);
  transition: transform 0.18s var(--ease-out), box-shadow 0.2s ease;
}
.swatch:hover { transform: translateY(-1px); }
.swatch[aria-pressed="true"] {
  box-shadow: 0 0 0 3px var(--ui-bg), 0 0 0 5px var(--text), inset 0 -4px 8px rgba(0, 0, 0, 0.18);
}
.custom-color {
  display: flex; align-items: center; gap: 10px; margin-top: 14px; height: 44px; padding: 0 12px;
  border-radius: 14px; background: var(--hover); cursor: pointer; font-size: 14px;
}
.custom-color input {
  width: 26px; height: 26px; border: 0; padding: 0; background: none; cursor: pointer;
  border-radius: 50%; overflow: hidden; -webkit-appearance: none; appearance: none;
}
.custom-color input::-webkit-color-swatch-wrapper { padding: 0; }
.custom-color input::-webkit-color-swatch { border: 0; border-radius: 50%; }
.custom-color input::-moz-color-swatch { border: 0; border-radius: 50%; }

/* 滑块 */
input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 32px; background: transparent; cursor: pointer;
  --pct: 50%;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(to right, var(--accent) var(--pct), var(--track) var(--pct));
}
input[type="range"]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--track); }
input[type="range"]::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--accent); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 26px; height: 26px; margin-top: -10px; border-radius: 50%;
  background: #fff; border: 0; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25), 0 0 0 1px rgba(0, 0, 0, 0.06);
}
input[type="range"]::-moz-range-thumb {
  width: 26px; height: 26px; border-radius: 50%; background: #fff; border: 0;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}
.size-preview { height: 64px; display: grid; place-items: center; margin-top: 6px; }
.size-preview span {
  width: var(--sz, 12px); height: var(--sz, 12px); border-radius: 50%;
  background: var(--brush, #1C1917); transition: width 0.12s ease, height 0.12s ease;
}

.btn-ghost {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; height: 44px; margin-top: 14px; border-radius: 14px;
  background: var(--hover); font-size: 14px; font-weight: 500;
  transition: background-color 0.2s ease, transform 0.15s ease;
}
.btn-ghost:hover { background: var(--active); }
.btn-ghost:active { transform: scale(0.98); }
.btn-ghost svg { width: 18px; height: 18px; }

/* 分段切换 */
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 14px; background: var(--hover); }
.seg button { height: 38px; border-radius: 10px; font-size: 14px; color: var(--muted); transition: background-color 0.2s ease, color 0.2s ease; }
.seg [aria-checked="true"] { background: var(--ui-bg); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }

/* 手动重力圆盘 */
.dial {
  position: relative; width: 188px; height: 188px; margin: 16px auto 0; border-radius: 50%;
  background: radial-gradient(circle, var(--hover) 0 30%, transparent 31%), var(--hover);
  border: 1px solid var(--ui-border); touch-action: none; cursor: grab;
}
.dial:active { cursor: grabbing; }
.dial-cross {
  position: absolute; inset: 12px; border-radius: 50%; pointer-events: none;
  background:
    linear-gradient(var(--ui-border), var(--ui-border)) center / 1px 100% no-repeat,
    linear-gradient(var(--ui-border), var(--ui-border)) center / 100% 1px no-repeat;
}
.dial-knob {
  position: absolute; left: 50%; top: 50%; width: 30px; height: 30px; margin: -15px 0 0 -15px; border-radius: 50%;
  background: var(--accent); box-shadow: 0 0 0 4px var(--ui-bg), 0 3px 10px rgba(0, 0, 0, 0.25);
  transform: translate(var(--kx, 0px), var(--ky, 0px));
}

/* ========== 提示 & 预览 ========== */
.toast {
  position: fixed; z-index: 40; left: 50%; top: calc(max(12px, env(safe-area-inset-top)) + 60px);
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 14px;
  font-size: 14px; line-height: 1.5; text-align: center;
  transform: translateX(-50%) translateY(-6px); opacity: 0; pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s var(--ease-out);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.preview {
  position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; padding: 24px; background: rgba(0, 0, 0, 0.72); color: #fff; font-size: 14px;
}
.preview img { max-width: 92vw; max-height: 70vh; border-radius: 12px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); -webkit-touch-callout: default; -webkit-user-select: auto; user-select: auto; }

/* ========== 响应式 ========== */
@media (max-width: 480px) {
  .brand-text p { display: none; }
  .brand { padding: 0 14px 0 10px; }
  .btn-primary span { display: none; }
  .btn-primary { padding: 0; width: 48px; justify-content: center; }
  .tool { width: 42px; height: 46px; }
  .dock-sep { margin: 0 2px; }
  .dock { padding: 5px; }
  .brush-mode button { width: 40px; height: 42px; }
  .brush-mode span { font-size: 10px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .panel { bottom: auto; top: 50%; left: auto; right: max(12px, env(safe-area-inset-right)); transform: translateY(-50%); animation: none; max-height: calc(100vh - 24px); overflow: auto; }
  .dial { width: 150px; height: 150px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
