:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --panel: #ffffff;
  --panel-2: #f6f8fa;
  --panel-3: #eaeef3;
  --line: #dde3ea;
  --text: #1e242c;
  --muted: #66707c;
  --accent: #d9861a;
  --accent-hover: #c27612;
  --accent-ink: #ffffff;
  --blue: #2f7fe0;
  --danger: #cf443c;
  --warn: #a9760a;
  --ok: #2e9748;
  --scroll-thumb: #c3cbd5;
  --tint: rgba(15, 23, 42, .025);
  --toast-bg: rgba(255, 255, 255, .98);
  --shadow: 0 14px 34px rgba(15, 23, 42, .12);
}

/* 深色主题：只覆盖颜色变量，布局与浅色完全一致 */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101317;
  --panel: #1b1f26;
  --panel-2: #232a34;
  --panel-3: #2b333f;
  --line: #2e3641;
  --text: #e6e9ee;
  --muted: #8d97a6;
  --accent: #f2a93b;
  --accent-hover: #ffb94f;
  --accent-ink: #1a1305;
  --blue: #4a9eff;
  --danger: #e5534b;
  --warn: #e8b339;
  --ok: #3fb950;
  --scroll-thumb: #3d4757;
  --tint: rgba(255, 255, 255, .015);
  --toast-bg: rgba(28, 36, 50, .96);
  --shadow: 0 14px 34px rgba(0, 0, 0, .35);
}

/* 滚动条：贴合深色面板，默认低调灰蓝，悬停变主题橙 */
/* Firefox 用标准属性；Chromium 走下面的 ::-webkit-scrollbar，避免出现系统箭头按钮 */
@supports (-moz-appearance: none) {
  * {
    scrollbar-width: thin;
    scrollbar-color: var(--scroll-thumb) transparent;
  }
}

*::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

*::-webkit-scrollbar-button {
  display: none;
  width: 0;
  height: 0;
}

*::-webkit-scrollbar-track {
  background: transparent;
}

*::-webkit-scrollbar-thumb {
  background-color: var(--scroll-thumb);
  background-clip: content-box;
  border: 2px solid transparent;
  border-radius: 999px;
}

*::-webkit-scrollbar-thumb:hover {
  background-color: var(--accent);
}

*::-webkit-scrollbar-corner {
  background: transparent;
}

* { box-sizing: border-box; }

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

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.5 "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

.app-header {
  flex: 0 0 auto;
  height: 48px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  letter-spacing: .02em;
  white-space: nowrap;
}

.brand-dot {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: linear-gradient(135deg, var(--accent), #e5534b 60%, var(--blue));
}

.brand-link {
  color: inherit;
  text-decoration: none;
}

.brand-link:hover { color: var(--accent); }

.name-input {
  flex: 0 1 240px;
  height: 30px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
  outline: none;
}

.name-input:focus { border-color: var(--accent); }

.header-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.btn {
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s ease, border-color .12s ease;
}

.btn:hover { background: var(--panel-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

a.btn {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.btn.primary:hover { background: var(--accent-hover); }

.btn-theme { flex: 0 0 auto; min-width: 56px; color: var(--muted); }
.btn-theme:hover { color: var(--text); }

/* 页面内确认框 */
.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, .55);
}
.modal-mask[hidden] { display: none; }
.modal {
  width: min(420px, 100%);
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  box-shadow: 0 18px 48px rgba(0, 0, 0, .45);
}
.modal h3 {
  margin: 0 0 8px;
  font-size: 15px;
}
.modal p {
  margin: 0 0 18px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
.btn.block { display: block; width: 100%; }

.app-main {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 300px minmax(320px, 1fr) 330px;
}

.panel {
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  background: var(--panel);
}

.panel-left { border-right: 1px solid var(--line); }
.panel-right { border-left: 1px solid var(--line); padding: 0; display: flex; flex-direction: column; }

.panel-title {
  margin: 4px 0 8px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .08em;
  color: var(--muted);
  text-transform: uppercase;
}

.panel-title:not(:first-child) { margin-top: 18px; }

.dropzone {
  padding: 10px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--tint);
  text-align: center;
}

.dropzone.dragging { border-color: var(--accent); background: rgba(242, 169, 59, .08); }

.hint {
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
}

.preview-wrap {
  position: relative;
  margin-top: 10px;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 320px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#previewCanvas {
  display: none;
  width: 100%;
  height: 100%;
}

.preview-wrap.has-image #previewCanvas { display: block; }
.preview-wrap.has-image #previewEmpty { display: none; }

.muted { color: var(--muted); }
.pad { padding: 10px; }

.field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.field > span { color: var(--muted); white-space: nowrap; }

.field input[type="number"] {
  width: 84px;
  height: 28px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--panel-2);
  outline: none;
}

.field select {
  width: 150px;
  height: 28px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--panel-2);
  outline: none;
}

.field input:focus, .field select:focus { border-color: var(--accent); }

.field-row { display: flex; gap: 8px; }
.field-row .field { flex: 1; flex-direction: column; align-items: stretch; gap: 4px; }
.field-row .field input { width: 100%; }

.check {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}

.check input { accent-color: var(--accent); }

.workspace {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.tool-group { display: flex; gap: 4px; }
.toggles { flex-wrap: wrap; }
.toolbar .check { margin: 0; font-size: 12px; }

/* 预览模式只读：隐藏所有编辑专用控件，避免看起来可点 */
.toolbar.is-preview .edit-only { display: none; }
.mode-hint { font-size: 12px; white-space: nowrap; line-height: 28px; }
.toolbar:not(.is-preview) .mode-hint { display: none; }

.tool, .tool-group button {
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--panel-2);
  cursor: pointer;
}

.tool:hover, .tool-group button:not(:disabled):hover { background: var(--panel-3); }

.tool:disabled, .tool-group button:disabled { opacity: .4; cursor: not-allowed; }

.tool.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.current-color {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 132px;
  font-size: 12px;
  color: var(--muted);
}

.swatch {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  border: 1px solid rgba(0, 0, 0, .35);
  background: #444;
  flex: 0 0 auto;
}

.zoom {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
}

.zoom input[type="range"] { width: 110px; accent-color: var(--accent); }

.canvas-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#patternCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.canvas-wrap.panning #patternCanvas { cursor: grab; }
.canvas-wrap.readonly #patternCanvas { cursor: grab; }

.preview-actions { padding: 6px 12px 0; }
.preview-actions .btn { width: 100%; height: 26px; font-size: 12px; }

.tool-link {
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--muted);
}

.tool-link a { color: var(--accent); }

/* 高级设置折叠区 */
.advanced {
  margin-top: 16px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.advanced > summary {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.advanced > summary::-webkit-details-marker { display: none; }
.advanced > summary::before { content: '▸'; font-size: 10px; }
.advanced[open] > summary::before { content: '▾'; }
.advanced > *:not(summary) { margin-top: 8px; }

/* 裁剪对话框 */
.crop-modal {
  position: fixed;
  inset: 0;
  z-index: 9000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 10, 14, .8);
}
.crop-dialog {
  display: flex;
  flex-direction: column;
  width: min(760px, 94vw);
  max-height: 92vh;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 18px 48px rgba(0, 0, 0, .45);
}
.crop-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.crop-head span { color: var(--muted); font-size: 12px; }
.crop-stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  background: #0c1015;
  overflow: hidden;
  touch-action: none;
}
.crop-img {
  display: block;
  max-width: 100%;
  max-height: 58vh;
  user-select: none;
  -webkit-user-drag: none;
  cursor: crosshair;
}
.crop-box {
  position: absolute;
  border: 1px solid #fff;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .55);
  cursor: move;
  touch-action: none;
}
.crop-box i {
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 3px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .35);
}
.crop-box i[data-h="nw"] { left: -7px; top: -7px; cursor: nwse-resize; }
.crop-box i[data-h="ne"] { right: -7px; top: -7px; cursor: nesw-resize; }
.crop-box i[data-h="sw"] { left: -7px; bottom: -7px; cursor: nesw-resize; }
.crop-box i[data-h="se"] { right: -7px; bottom: -7px; cursor: nwse-resize; }
.crop-box .cg {
  position: absolute;
  background: rgba(255, 255, 255, .26);
  pointer-events: none;
}
.crop-box .cg.v1 { left: 33.333%; top: 0; bottom: 0; width: 1px; }
.crop-box .cg.v2 { left: 66.666%; top: 0; bottom: 0; width: 1px; }
.crop-box .cg.h1 { top: 33.333%; left: 0; right: 0; height: 1px; }
.crop-box .cg.h2 { top: 66.666%; left: 0; right: 0; height: 1px; }
.crop-tools {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
}
.crop-ratios {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}
.chip {
  height: 24px;
  padding: 0 10px;
  font-size: 12px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
}
.chip:hover { border-color: var(--muted); }
.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.crop-info { margin-left: auto; font-size: 12px; color: var(--muted); }
.crop-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}
.crop-actions .btn { height: 32px; padding: 0 16px; }

@media (max-width: 520px) {
  .crop-dialog { width: 100%; max-height: 94dvh; border-radius: 14px; }
  .crop-stage { padding: 10px; }
  .crop-img { max-height: 52vh; }
  .crop-info { flex: 1 1 100%; margin-left: 0; text-align: right; }
  .crop-actions .btn { flex: 1; height: 38px; border-radius: 9px; }
  .crop-tools { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  pointer-events: none;
}

.tab-panel {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.tab-panel.hidden { display: none; }

.legend-head {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 26px 1fr 56px;
  gap: 8px;
  padding: 7px 12px;
  font-size: 11px;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
}

.legend-head .num, .legend-row .num { text-align: right; }

.legend-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

.legend-row {
  display: grid;
  grid-template-columns: 26px 1fr 56px;
  gap: 8px;
  align-items: center;
  padding: 5px 12px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}

.legend-row:hover { background: var(--panel-2); }
.legend-row.active { background: rgba(242, 169, 59, .14); }

.legend-row .swatch { width: 20px; height: 20px; border-radius: 5px; }

.legend-meta { min-width: 0; }
.legend-code { font-size: 12px; font-weight: 600; }
.legend-name { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-count { font-size: 12px; text-align: right; }

.legend-actions {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
}

.legend-actions .btn { flex: 1 1 calc(50% - 4px); height: 28px; padding: 0 6px; font-size: 12px; }

.legend-actions .menu-wrap { position: relative; flex: 1 1 calc(50% - 4px); }
.legend-actions .menu-wrap .btn { width: 100%; }

.export-menu {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  padding: 4px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .45);
  z-index: 60;
}
.export-menu[hidden] { display: none; }
.export-menu button {
  padding: 7px 10px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text);
  font-size: 12px;
  text-align: left;
  cursor: pointer;
}
.export-menu button:hover { background: var(--panel-3); }

.statusbar {
  flex: 0 0 auto;
  height: 28px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 14px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
}

.statusbar #statusText {
  flex: 0 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
}

.statusbar .stats {
  margin-left: auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--muted);
}

.drop-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(16, 19, 23, .82);
  border: 2px dashed var(--accent);
  color: var(--accent);
  font-size: 16px;
  z-index: 60;
  pointer-events: none;
}

.drop-overlay.active { display: flex; }

/* 操作反馈：按钮忙碌态 + 顶部提示条 */
.btn.busy { opacity: .85; cursor: progress; }

.btn.busy::before {
  content: '';
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px;
  vertical-align: -1px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: pindou-spin .7s linear infinite;
}

@keyframes pindou-spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%;
  top: 14px;
  z-index: 200;
  max-width: min(92vw, 460px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--toast-bg);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .35);
  transform: translate(-50%, -12px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.ok { border-color: rgba(76, 200, 130, .55); }
.toast.error { border-color: rgba(230, 90, 90, .6); }

#print-root { display: none; }

@media (max-width: 1280px) {
  .app-main { grid-template-columns: 268px minmax(280px, 1fr) 300px; }
}

@media (max-width: 1040px) {
  .app-main { grid-template-columns: 250px 1fr; }
  .panel-right { display: none; }
  .header-actions .btn { padding: 0 8px; }
}
