:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #e3e6ec;
  --ink: #22252b;
  --muted: #767d8c;
  --accent: #ff5c8a;
  --accent-ink: #d93a6c;
  --ok: #1c9c4f;
  --bad: #d94a3d;
  --radius: 12px;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must win over component rules like `.row{display:flex}`
 * and `#adminUsers{display:flex}`, or those panels show up un-invited. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px 16px;
  background: #2b2f3a;
  color: #fff;
  flex-wrap: wrap;
}

.brand { font-weight: 700; letter-spacing: .5px; }
.brand .ver {
  margin-left: 8px;
  font-size: 11px;
  font-weight: 500;
  padding: 2px 7px;
  border-radius: 20px;
  background: rgba(255, 255, 255, .16);
  vertical-align: 2px;
}

.tabs { display: flex; gap: 6px; }

.tab {
  background: transparent;
  color: #cfd3dd;
  border: 0;
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}

.tab:hover { background: rgba(255, 255, 255, .1); color: #fff; }
.tab.active { background: var(--accent); color: #fff; font-weight: 600; }

.topbar .hint { margin-left: auto; font-size: 12px; color: #9aa1b1; }
.sync-status { font-weight: 600; }
.sync-status.ok { color: var(--ok); }
.sync-status.warn { color: var(--accent-ink); }
.sync-status.sync { color: #3a7bd5; }
.sync-status.local { color: #9aa1b1; }

main { flex: 1; min-height: 0; overflow: auto; padding: 14px; }

.view { display: none; }
.view.active { display: block; }

#view-studio.active {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 250px;
  gap: 14px;
  height: 100%;
  min-height: 0;
}

.col { display: flex; flex-direction: column; gap: 12px; overflow: auto; min-height: 0; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}

.card.wide { max-width: 1100px; margin: 0 auto; }

.card h2 { margin: 0 0 10px; font-size: 13px; letter-spacing: .4px; color: #4b5160; text-transform: none; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
details.adv { margin: 6px 0; border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; }
details.adv > summary { cursor: pointer; font-size: 12px; color: var(--muted); padding: 4px 0; user-select: none; }
details.adv[open] > summary { margin-bottom: 8px; border-bottom: 1px solid var(--line); }
details.adv > *:not(summary) { margin-top: 8px; }.row.space { justify-content: space-between; }
.row.triple { flex-direction: column; align-items: stretch; gap: 4px; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: #5a6070; flex: 1; min-width: 0; }
.field.inline { flex-direction: row; align-items: center; gap: 6px; flex: 0 0 auto; }

input[type=text], input[type=number], select {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  min-width: 0;
}

input[type=number] { width: 76px; }
.field > input[type=text] { width: 100%; }

.slider { display: flex; align-items: center; gap: 8px; }
.slider input[type=range] { flex: 1; accent-color: var(--accent); }
.slider b { font-size: 12px; color: #5a6070; min-width: 52px; text-align: right; font-variant-numeric: tabular-nums; }

.check { display: flex; align-items: center; gap: 6px; font-size: 12px; color: #5a6070; }
.check input { accent-color: var(--accent); }

button {
  font: inherit;
  padding: 7px 12px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  cursor: pointer;
  color: var(--ink);
}

button:hover:not(:disabled) { border-color: #c6cbd6; background: #fafbfc; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent-ink); color: #fff; font-weight: 600; }
button.primary:hover:not(:disabled) { background: #ff6e96; }
button.big { width: 100%; padding: 11px; margin-top: 8px; font-size: 15px; }
button.danger { color: var(--bad); border-color: #f2c6c2; }

.filebtn {
  display: inline-flex;
  align-items: center;
  padding: 7px 12px;
  border: 1px dashed #c6cbd6;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: #5a6070;
}

.filebtn:hover { border-color: var(--accent); color: var(--accent-ink); }

.source-box { display: flex; gap: 10px; align-items: center; margin: 10px 0; }
.thumb { width: 74px; height: 74px; border-radius: 8px; border: 1px solid var(--line); background: #fafbfc; overflow: hidden; display: grid; place-items: center; flex: 0 0 auto; }
.thumb canvas, .thumb img { max-width: 100%; max-height: 100%; display: block; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.warn { color: var(--bad); font-size: 12px; margin: 6px 0 0; }

#dropZone.dragging { outline: 2px dashed var(--accent); outline-offset: -4px; }

.stage-wrap { display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; }

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
}

.tool-group { display: flex; gap: 4px; align-items: center; }
.tool-group + .tool-group { border-left: 1px solid var(--line); padding-left: 10px; }

.tool { padding: 6px 10px; }
.tool.active { background: #2b2f3a; color: #fff; border-color: #2b2f3a; }

.stage {
  position: relative;
  flex: 1;
  min-height: 260px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: #eef0f4;
}

.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #98a0b0;
  font-size: 15px;
  pointer-events: none;
}

.stage.has-grid .empty { display: none; }

.busy {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(43, 47, 58, .92);
  color: #fff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
}

.statusbar {
  font-size: 12px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-variant-numeric: tabular-nums;
}

.legend { display: flex; flex-direction: column; gap: 4px; max-height: 60vh; overflow: auto; }

.legend-row {
  display: grid;
  grid-template-columns: 22px 1fr auto auto;
  gap: 8px;
  align-items: center;
  padding: 5px 8px;
  text-align: left;
  border: 1px solid transparent;
}

.legend-row:hover { border-color: var(--line); }
.legend-row.active { border-color: var(--accent); background: #fff5f8; }

.swatch { width: 18px; height: 18px; border-radius: 4px; border: 1px solid rgba(0, 0, 0, .18); display: inline-block; vertical-align: middle; }

.legend-code { font-weight: 600; font-size: 13px; }
.legend-need { color: var(--muted); font-size: 12px; }
.legend-gap { color: var(--bad); font-size: 12px; font-weight: 600; }
.legend-ok { color: var(--ok); font-size: 12px; }

.library-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-top: 12px; }

.pat-card { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; background: #fff; }
.pat-card h3 { margin: 8px 0 2px; font-size: 14px; }
.pat-card p { margin: 0 0 8px; font-size: 11px; }

.pat-thumb { height: 120px; display: grid; place-items: center; background: #fafbfc; border-radius: 8px; border: 1px solid var(--line); }
.pat-thumb img { max-width: 100%; max-height: 120px; image-rendering: pixelated; }

.pat-actions { display: flex; gap: 6px; }
.pat-actions button { flex: 1; padding: 5px 8px; font-size: 12px; }

table.stock { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 13px; }
table.stock th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; border-bottom: 1px solid var(--line); padding: 6px; }
table.stock td { border-bottom: 1px solid #f1f2f5; padding: 5px 6px; }
table.stock td.num { text-align: left; width: 120px; white-space: nowrap; }
table.stock td.num button { padding: 3px 7px; font-size: 11px; margin-right: 4px; }
table.stock tbody tr:hover { background: #fafbfc; }

#startGuide { width: 100%; }

.guide-bar {
  height: 8px;
  background: #eceef3;
  border-radius: 6px;
  overflow: hidden;
  margin: 12px 0 6px;
}

.guide-bar i { display: block; height: 100%; width: 0; background: var(--ok); transition: width .2s; }

#guideText { margin: 0 0 8px; color: #4b5160; }
#guideText b { color: var(--accent-ink); }

.palette-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 6px;
  margin-top: 12px;
}

.palette-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 5px 2px;
  border: 1px solid transparent;
}

.palette-cell:hover { border-color: #c6cbd6; }
.palette-cell.active { border-color: var(--accent); background: #fff5f8; }

.palette-dot { width: 26px; height: 26px; border-radius: 50%; border: 1px solid rgba(0, 0, 0, .18); }
.palette-code { font-size: 11px; font-weight: 600; }
.palette-stock { font-size: 10px; color: var(--muted); }

.preview-grid { width: 100%; border: 1px solid var(--line); border-radius: 8px; margin-top: 8px; background: #fafbfc; }

.legend-low { color: #b8860b; font-size: 12px; font-weight: 600; }
table.stock tr.low td { background: #fff8f2; }
.logs { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.log-row { display: grid; grid-template-columns: 1.2fr 1.1fr .7fr 2fr; gap: 8px; font-size: 12px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; }
.log-row b { font-weight: 600; }

#adminUsers { margin-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.admin-user-row { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 12px; }
.admin-user-email { font-weight: 600; word-break: break-all; }
.admin-user-row button { padding: 2px 8px; font-size: 12px; line-height: 1.4; }

.login-gate { position: fixed; inset: 0; z-index: 9999; display: flex; align-items: center; justify-content: center; padding: 16px; background: #eef0f4; }
.login-gate[hidden] { display: none; }
.gate-card { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 22px; width: 100%; max-width: 460px; box-shadow: 0 10px 34px rgba(20, 24, 40, .12); display: flex; flex-direction: column; gap: 12px; }
.gate-card h2 { margin: 0; font-size: 20px; }
.gate-card .brand { font-size: 18px; }
body.gated header.topbar, body.gated main { pointer-events: none; }

.layers { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.layer-row { display: grid; grid-template-columns: 24px 1fr repeat(4, 26px); gap: 4px; align-items: center; padding: 4px 6px; border: 1px solid var(--line); border-radius: 8px; font-size: 12px; }
.layer-row.active { border-color: var(--accent); background: #fff5f8; }
.layer-row button { padding: 2px 4px; font-size: 12px; line-height: 1.3; }
.layer-pick { border: 0; background: none; font-size: 14px; }
.layer-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translate(-50%, 20px);
  background: #2b2f3a;
  color: #fff;
  padding: 10px 18px;
  border-radius: 22px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  max-width: 84vw;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: #b23127; }

@media (max-width: 1180px) {
  #view-studio.active { grid-template-columns: 270px minmax(0, 1fr); }
  .col-right { grid-column: 1 / -1; }
  .legend { flex-direction: row; flex-wrap: wrap; max-height: none; }
  .legend-row { grid-template-columns: 20px auto auto auto; width: 200px; }
}

@media (max-width: 820px) {
  main { padding: 10px; }
  #view-studio.active { grid-template-columns: minmax(0, 1fr); height: auto; }
  .col { overflow: visible; }
  .stage { height: 62vh; }
  .topbar .hint { font-size: 11px; max-width: 44vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .tabs { order: 3; width: 100%; justify-content: space-around; }
}
