:root {
  --bg: #f6f1e7;
  --paper: #fffdf8;
  --ink: #2b2620;
  --ink-2: #6f665a;
  --line: #e4dccd;
  --accent: #e8573a;
  --accent-ink: #fff;
  --soft: #f0e9db;
  --focus: #2f7de1;
  --radius: 14px;
  --shadow: 0 1px 0 rgba(60, 40, 10, .04), 0 6px 20px rgba(60, 40, 10, .06);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a17; --paper: #262320; --ink: #f1ebe0; --ink-2: #a9a091;
    --line: #3a3530; --soft: #302c27; --shadow: 0 6px 20px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  background-image: radial-gradient(circle, rgba(120, 100, 70, .10) 1.2px, transparent 1.6px);
  background-size: 18px 18px;
  color: var(--ink);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.5;
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* header */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; max-width: 1600px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 20px; letter-spacing: .08em; }
.brand p { margin: 0; color: var(--ink-2); font-size: 12px; }
.logo { display: grid; grid-template-columns: repeat(2, 14px); gap: 3px; }
.logo i {
  width: 14px; height: 14px; border-radius: 50%;
  box-shadow: inset 0 0 0 4px rgba(255,255,255,.0), inset 0 -2px 0 rgba(0,0,0,.15);
  position: relative;
}
.logo i::after { content: ""; position: absolute; inset: 4.5px; border-radius: 50%; background: var(--bg); }
.logo i:nth-child(1) { background: #e8573a; }
.logo i:nth-child(2) { background: #f4c542; }
.logo i:nth-child(3) { background: #3bb3a0; }
.logo i:nth-child(4) { background: #4a78d6; }

main { max-width: 1600px; margin: 0 auto; padding: 0 20px 40px; }

/* buttons */
.btn {
  border: 1px solid var(--line); background: var(--paper); border-radius: 10px;
  padding: 9px 14px; cursor: pointer; transition: background .15s, border-color .15s, transform .05s;
  white-space: nowrap;
}
.btn:hover { border-color: var(--ink-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; text-decoration: underline; text-underline-offset: 3px; }

.panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }

/* upload */
.upload-wrap { max-width: 720px; margin: 6vh auto 0; text-align: center; }
.drop {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 48px 24px; border: 2px dashed var(--line); border-radius: 22px;
  background: var(--paper); cursor: pointer; transition: border-color .15s, background .15s;
}
.drop:hover, .drop.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 5%, var(--paper)); }
.drop strong { font-size: 18px; margin-top: 10px; }
.drop span { color: var(--ink-2); }
.drop .muted { font-size: 12px; margin-top: 6px; }
.drop-art {
  width: 112px; height: 112px;
  background:
    radial-gradient(circle at 50% 50%, var(--paper) 22%, transparent 24%) 0 0 / 16px 16px,
    conic-gradient(from 0deg, #e8573a, #f4c542, #3bb3a0, #4a78d6, #b05ad6, #e8573a);
  -webkit-mask: radial-gradient(circle, #000 46%, transparent 48%) 0 0 / 16px 16px;
          mask: radial-gradient(circle, #000 46%, transparent 48%) 0 0 / 16px 16px;
  border-radius: 18px;
}
.demo-row { margin-top: 14px; color: var(--ink-2); }
.how { list-style: none; padding: 0; margin: 36px 0 0; display: flex; justify-content: center; gap: 28px; flex-wrap: wrap; color: var(--ink-2); }
.how li { display: flex; align-items: center; gap: 8px; }
.how b { width: 24px; height: 24px; border-radius: 50%; background: var(--ink); color: var(--paper); display: grid; place-items: center; font-size: 12px; }

/* workspace */
.workspace {
  display: grid; gap: 16px;
  grid-template-columns: 280px minmax(0, 1fr) 280px;
  align-items: start;
}
.settings, .legend-col { padding: 14px; position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow: auto; }
.src { border-radius: 10px; overflow: hidden; background: var(--soft); display: grid; place-items: center; max-height: 160px; margin-bottom: 6px; }
.src img { max-width: 100%; max-height: 160px; display: block; }
fieldset { border: 0; border-top: 1px solid var(--line); margin: 10px 0 0; padding: 10px 0 0; }
legend { font-weight: 700; font-size: 12px; letter-spacing: .1em; color: var(--ink-2); padding: 0 6px 0 0; }
.field { margin: 8px 0; }
.field label { display: block; margin-bottom: 4px; }
.field output { font-weight: 700; font-variant-numeric: tabular-nums; }
.field.row2 { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.field.row2 label { margin: 0; }
.field.inline { display: flex; align-items: center; gap: 6px; margin: 0; }
.field.inline label { margin: 0; color: var(--ink-2); font-size: 13px; }
select { border: 1px solid var(--line); background: var(--paper); border-radius: 8px; padding: 5px 8px; max-width: 170px; }
input[type=range] { width: 100%; accent-color: var(--accent); }
.check { display: flex; gap: 8px; align-items: flex-start; margin: 8px 0; cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--accent); }
.presets { display: flex; gap: 6px; flex-wrap: wrap; }
.presets button { border: 1px solid var(--line); background: var(--soft); border-radius: 999px; padding: 2px 10px; cursor: pointer; font-size: 12px; }
.presets button.on { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.hint { color: var(--ink-2); font-size: 12px; margin: 6px 0 0; }

.stage-col { min-width: 0; }
.toolbar { display: flex; align-items: center; gap: 10px; padding: 8px 10px; flex-wrap: wrap; margin-bottom: 10px; }
.seg { display: inline-flex; background: var(--soft); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 5px 10px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.seg button.on { background: var(--paper); box-shadow: 0 1px 3px rgba(0,0,0,.12); font-weight: 600; }
.cur { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.cur span { width: 20px; height: 20px; border-radius: 6px; border: 1px solid rgba(0,0,0,.2); background: transparent; }
.grow { flex: 1; }
.zoom { display: flex; gap: 4px; }

.stage {
  position: relative; overflow: auto; height: calc(100vh - 140px); min-height: 420px;
  background: var(--soft);
  touch-action: none;
}
.stage canvas { display: block; margin: 0 auto; image-rendering: pixelated; }
.stage.tool-pen canvas, .stage.tool-erase canvas, .stage.tool-pick canvas { cursor: crosshair; }
.tip {
  position: fixed; pointer-events: none; z-index: 10;
  background: var(--ink); color: var(--paper); padding: 4px 8px; border-radius: 6px; font-size: 12px;
  white-space: nowrap; font-variant-numeric: tabular-nums;
}
.busy { position: absolute; top: 10px; right: 12px; background: var(--ink); color: var(--paper); padding: 3px 10px; border-radius: 999px; font-size: 12px; }
.stage-hint { text-align: center; }

/* legend */
.legend-head { display: flex; align-items: baseline; justify-content: space-between; }
.legend-head h2 { font-size: 16px; margin: 0; }
#totals { color: var(--ink-2); font-size: 12px; font-variant-numeric: tabular-nums; }
.legend-actions { display: flex; gap: 6px; margin: 8px 0; }
.legend { list-style: none; margin: 0; padding: 0; max-height: 46vh; overflow: auto; }
.legend li {
  display: grid; grid-template-columns: 26px 1fr auto; align-items: center; gap: 8px;
  padding: 4px 6px; border-radius: 8px; cursor: pointer;
}
.legend li:hover { background: var(--soft); }
.legend li.on { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.legend .sw { width: 26px; height: 26px; border-radius: 50%; border: 1px solid rgba(0,0,0,.18); position: relative; }
.legend .sw::after { content: ""; position: absolute; inset: 9px; border-radius: 50%; background: rgba(255,255,255,.55); }
.legend .code { font-weight: 700; font-variant-numeric: tabular-nums; }
.legend .hex { color: var(--ink-2); font-size: 11px; margin-left: 4px; font-weight: 400; }
.legend .n { font-variant-numeric: tabular-nums; color: var(--ink-2); }
.all-colors { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 8px; }
.all-colors summary { cursor: pointer; color: var(--ink-2); }
.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(22px, 1fr)); gap: 3px; margin-top: 8px; max-height: 200px; overflow: auto; }
.swatches button { aspect-ratio: 1; border: 1px solid rgba(0,0,0,.15); border-radius: 50%; cursor: pointer; padding: 0; }
.swatches button.on { outline: 2px solid var(--ink); outline-offset: 1px; }
.export { display: grid; gap: 8px; margin-top: 14px; border-top: 1px solid var(--line); padding-top: 14px; }

#printArea { display: none; }

/* responsive */
@media (max-width: 1180px) {
  .workspace { grid-template-columns: 260px minmax(0, 1fr); }
  .legend-col { grid-column: 1 / -1; position: static; max-height: none; }
  .legend { max-height: 300px; }
}
@media (max-width: 760px) {
  .top { padding: 12px 16px; }
  main { padding: 0 16px 32px; }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .settings { position: static; max-height: none; order: 2; }
  .stage-col { order: 1; }
  .legend-col { order: 3; }
  .stage { height: 62vh; min-height: 300px; }
  .how { gap: 14px; }
}

@media print {
  body { background: #fff; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block !important; }
  #printArea img { width: 100%; height: auto; display: block; page-break-after: always; }
  @page { margin: 10mm; }
}

/* ---------- 账号 / 弹窗 / 我的图纸 ---------- */
.brand { color: inherit; text-decoration: none; }
a.btn { display: inline-block; text-decoration: none; color: inherit; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.account { display: flex; align-items: center; gap: 8px; }
.muted-text { color: var(--ink-2); font-size: 13px; }
.btn.danger { color: #c2410c; }
.btn.save { border-color: var(--ink); font-weight: 600; }

.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.menu summary::-webkit-details-marker { display: none; }
.menu-pop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 150px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 6px;
  display: grid;
}
.menu-pop button, .menu-pop a {
  text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 8px; cursor: pointer;
  color: inherit; text-decoration: none; font-size: 14px;
}
.menu-pop button:hover, .menu-pop a:hover { background: var(--soft); }

.dlg {
  border: 1px solid var(--line); border-radius: 18px; padding: 22px; width: min(380px, calc(100vw - 32px));
  background: var(--paper); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.dlg::backdrop { background: rgba(30, 24, 16, .45); }
.dlg h3 { margin: 0 0 12px; font-size: 18px; }
.dlg-tabs { display: flex; gap: 6px; margin-bottom: 14px; background: var(--soft); padding: 4px; border-radius: 12px; }
.dlg-tabs button { flex: 1; border: 0; background: none; padding: 7px; border-radius: 9px; cursor: pointer; font-size: 15px; }
.dlg-tabs button.on { background: var(--paper); font-weight: 700; box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.fld { display: grid; gap: 5px; margin: 10px 0; font-size: 13px; color: var(--ink-2); }
.fld input {
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font-size: 15px;
  background: var(--paper); color: var(--ink);
}
.fld input:focus { outline: 2px solid var(--focus); outline-offset: 0; border-color: transparent; }
.err { color: #c2410c; font-size: 13px; min-height: 1em; margin: 6px 0 0; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: var(--paper); padding: 10px 18px; border-radius: 999px; font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); max-width: calc(100vw - 32px);
}
.pattern-note { background: color-mix(in srgb, var(--accent) 10%, var(--paper)); border-radius: 10px; padding: 8px 10px; font-size: 12px; margin: 6px 0; }
.save-state { margin: -2px 0 2px; text-align: center; }
.gen :disabled { opacity: .5; cursor: not-allowed; }

.works-wrap { max-width: 1200px; margin: 10px auto 0; }
.works-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 14px; }
.works-head h2 { margin: 0; font-size: 22px; }
.works-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; }
.work-card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px; display: grid; gap: 8px; }
.work-thumb {
  border: 0; padding: 0; cursor: pointer; border-radius: 10px; aspect-ratio: 1; display: grid; place-items: center;
  background: repeating-conic-gradient(#f3eee4 0 25%, #fff 0 50%) 0 0 / 16px 16px;
}
.work-thumb img { max-width: 90%; max-height: 90%; image-rendering: pixelated; }
.work-meta { display: grid; gap: 2px; min-width: 0; }
.work-meta b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.work-meta span { color: var(--ink-2); font-size: 12px; }
.work-acts { display: flex; gap: 6px; flex-wrap: wrap; }
.works-empty { text-align: center; color: var(--ink-2); padding: 60px 0; }

/* ---------- 管理后台 ---------- */
.admin { max-width: 1200px; }
.admin-msg { padding: 24px; text-align: center; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { padding: 14px 16px; display: grid; gap: 4px; }
.stat span { color: var(--ink-2); font-size: 13px; }
.stat b { font-size: 26px; font-variant-numeric: tabular-nums; }
.admin-panel { padding: 16px; margin-bottom: 16px; }
.admin-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.admin-head h2 { margin: 0; font-size: 17px; }
.admin-head input { border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; background: var(--paper); min-width: 0; width: 200px; }
.table-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th { text-align: left; color: var(--ink-2); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; white-space: nowrap; }
.tbl td.empty { text-align: center; color: var(--ink-2); padding: 24px; }
.tbl td.acts .btn { margin-right: 4px; }
.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--soft); margin-left: 4px; }
.tag-admin { background: #fde6c8; color: #8a4b08; }
.tag-ok { background: #dcf3e6; color: #17663a; }
.tag-off { background: #f7dcd6; color: #9b2c16; }
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 12px; }
.temp-pw { text-align: center; margin: 14px 0; }
.temp-pw code { font-size: 20px; letter-spacing: .06em; background: var(--soft); padding: 8px 14px; border-radius: 10px; user-select: all; }
@media (max-width: 760px) {
  .account .btn { padding: 7px 10px; }
  #btnMyWorks { display: none; }
  .works-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}
