/* ── Reset ──────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --bg:      #f0f2f5;
  --surf:    #ffffff;
  --bord:    #dde1e8;
  --text:    #1c1e26;
  --muted:   #6b7280;
  --acc:     #4a6cf7;
  --acc-dk:  #3452d4;
  --hdr:     46px;
  --radius:  5px;
}

body {
  font: 12.5px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--text);
  background: var(--bg);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
body.resizing { cursor: col-resize !important; user-select: none; }

/* ── Header ─────────────────────── */
header {
  height: var(--hdr);
  background: var(--surf);
  border-bottom: 1px solid var(--bord);
  display: flex;
  align-items: center;
  padding: 0 14px;
  gap: 16px;
  flex-shrink: 0;
  z-index: 100;
}
.logo { font-weight: 700; font-size: 13.5px; letter-spacing: -.3px; }
nav   { display: flex; gap: 3px; }
.nav-btn {
  padding: 5px 12px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: none;
  cursor: pointer;
  font-size: 12px;
  color: var(--muted);
  transition: .1s;
}
.nav-btn:hover  { background: var(--bg); color: var(--text); }
.nav-btn.active { background: var(--acc); color: #fff; }

/* ── Workspace ───────────────────── */
.workspace {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100vh - var(--hdr));
}

/* ── Panels ──────────────────────── */
.side-panel {
  flex-shrink: 0;
  width: 220px;
  display: flex;
  flex-direction: column;
  background: var(--surf);
  overflow: hidden;
  border-right: 1px solid var(--bord);
  transition: width 0s; /* no transition, instant */
}
/* Right side panel: border on left */
#panel-palette { border-right: none; border-left: 1px solid var(--bord); }

.main-panel {
  flex: 1;
  min-width: 180px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  font-size: 11.5px;
  font-weight: 600;
  border-bottom: 1px solid var(--bord);
  cursor: grab;
  user-select: none;
  background: var(--surf);
  flex-shrink: 0;
  color: var(--text);
}
.panel-header:active { cursor: grabbing; }
.drag-dots { font-size: 16px; color: var(--muted); opacity: .6; line-height: 1; }

.side-panel.is-dragging { opacity: .5; }
.side-panel.drop-target { box-shadow: inset 0 0 0 2px var(--acc); }

.panel-body {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* ── Resize handles ──────────────── */
.resize-handle {
  width: 4px;
  background: var(--bord);
  cursor: col-resize;
  flex-shrink: 0;
  transition: background .1s;
}
.resize-handle:hover, .resize-handle.active { background: var(--acc); }

/* ── Settings panel sections ─────── */
.p-section { padding: 10px 11px; border-bottom: 1px solid var(--bord); display: flex; flex-direction: column; gap: 6px; }

.upload-area {
  border: 1.5px dashed var(--bord);
  border-radius: var(--radius);
  padding: 11px;
  text-align: center;
  cursor: pointer;
  color: var(--muted);
  background: var(--bg);
  transition: .12s;
  display: flex; flex-direction: column; gap: 2px;
}
.upload-area:hover, .upload-area.dragover { border-color: var(--acc); background: #eef1fd; color: var(--acc); }
.upload-area small { font-size: 10px; opacity: .7; }

.url-row { display: flex; gap: 5px; }
.err-msg { font-size: 10.5px; color: #dc2626; }

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 11px; font-weight: 500; color: var(--muted); }
.field span { text-transform: uppercase; letter-spacing: .3px; font-size: 10.5px; }

.dim-info { font-size: 11px; color: var(--acc); font-weight: 600; text-align: center; }

.slider-row { display: grid; grid-template-columns: 54px 1fr 30px; align-items: center; gap: 6px; }
.slider-label { font-size: 11px; color: var(--muted); font-weight: 500; }
.slider-val { font-size: 11px; font-weight: 600; color: var(--acc); text-align: right; }
.slider { accent-color: var(--acc); cursor: pointer; width: 100%; }

/* ── Inputs ──────────────────────── */
.inp {
  padding: 5px 7px;
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  font-size: 12px;
  background: var(--surf);
  color: var(--text);
  outline: none;
  width: 100%;
}
.inp:focus { border-color: var(--acc); }
.inp-sm {
  padding: 4px 6px;
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  font-size: 11.5px;
  background: var(--surf);
  outline: none;
}
.inp-search {
  display: block;
  width: calc(100% - 16px);
  margin: 6px 8px;
  padding: 5px 8px;
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  font-size: 12px;
  outline: none;
}
.inp-search:focus { border-color: var(--acc); }
.inp-lg { flex: 1; padding: 7px 10px; border: 1px solid var(--bord); border-radius: var(--radius); font-size: 13px; outline: none; }
.inp-lg:focus { border-color: var(--acc); }

/* ── Buttons ─────────────────────── */
.btn-primary {
  padding: 6px 13px;
  background: var(--acc);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  transition: .1s;
  white-space: nowrap;
}
.btn-primary:hover    { background: var(--acc-dk); }
.btn-primary:disabled { opacity: .4; cursor: not-allowed; }
.btn-sec {
  padding: 6px 12px;
  background: var(--surf);
  color: var(--text);
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  transition: .1s;
}
.btn-sec:hover { background: var(--bg); border-color: var(--acc); }
.btn-sm {
  padding: 4px 9px;
  background: var(--bg);
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 11px;
  white-space: nowrap;
}
.btn-sm:hover { background: #e0e3ea; }
.btn-icon {
  width: 26px; height: 26px;
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  background: var(--surf);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  flex-shrink: 0;
}
.btn-icon:hover { background: var(--bg); }
.btn-cat {
  padding: 3px 9px;
  background: var(--bg);
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  transition: .1s;
}
.btn-cat:hover { border-color: var(--acc); color: var(--acc); background: #eef1fd; }

/* ── Editor toolbar ──────────────── */
.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  background: var(--surf);
  border-bottom: 1px solid var(--bord);
  flex-shrink: 0;
}
.chk { display: flex; align-items: center; gap: 4px; cursor: pointer; user-select: none; }
.tool-sep { width: 1px; height: 18px; background: var(--bord); }
.spacer { flex: 1; }
.badge-hidden {
  font-size: 10.5px;
  color: #b91c1c;
  background: #fee2e2;
  border: 1px solid #fca5a5;
  padding: 2px 7px;
  border-radius: 10px;
}

/* ── Canvas area ─────────────────── */
.canvas-scroll {
  flex: 1;
  overflow: auto;
  padding: 18px;
  background: #dde0e8;
  cursor: crosshair;
  position: relative;
}
.canvas-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--muted);
  pointer-events: none;
}
#editor-canvas { display: block; box-shadow: 0 2px 16px rgba(0,0,0,.14); }
.canvas-hint {
  padding: 4px 11px;
  font-size: 10px;
  color: var(--muted);
  background: var(--surf);
  border-top: 1px solid var(--bord);
  flex-shrink: 0;
}

/* ── Palette panel ───────────────── */
.pal-tabs {
  display: flex;
  border-bottom: 1px solid var(--bord);
  flex-shrink: 0;
}
.pal-tab {
  flex: 1;
  padding: 7px 4px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  transition: .1s;
}
.pal-tab:hover  { color: var(--text); }
.pal-tab.active { color: var(--acc); border-bottom-color: var(--acc); }
.pal-pane { flex: 1; overflow-y: auto; display: flex; flex-direction: column; }

.cat-switch { display: flex; gap: 5px; padding: 7px 8px; border-bottom: 1px solid var(--bord); }
.pal-list   { padding: 4px 6px; display: flex; flex-direction: column; gap: 1px; }

/* Palette items */
.pal-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 6px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1.5px solid transparent;
  transition: .08s;
}
.pal-item:hover { background: var(--bg); }
.pal-item.sel   { border-color: var(--acc); background: #eef1fd; }
.swatch {
  width: 24px; height: 24px;
  border-radius: 3px;
  border: 1px solid rgba(0,0,0,.12);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; flex-shrink: 0;
}
.pal-info { flex: 1; min-width: 0; }
.sym-btn {
  display: inline-block;
  padding: 1px 4px;
  background: var(--bg);
  border: 1px solid var(--bord);
  border-radius: 3px;
  font-size: 12px;
  cursor: pointer;
  margin-right: 3px;
}
.sym-btn:hover { border-color: var(--acc); }
.thr-btn { font-size: 11px; font-weight: 600; color: var(--acc); cursor: pointer; }
.thr-btn:hover { text-decoration: underline; }
.thr-sub { display: block; font-size: 10px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.pal-count { display: block; font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Catalog thread list */
.cat-row { padding: 7px 8px; border-bottom: 1px solid var(--bord); }
.thread-list { list-style: none; flex: 1; }
.thread-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 11px;
}
.thread-list li:hover { background: var(--bg); }
.sw-xs { width: 12px; height: 12px; border-radius: 2px; border: 1px solid rgba(0,0,0,.12); flex-shrink: 0; }

/* Symbol popup */
.sym-popup {
  position: fixed; z-index: 500;
  background: var(--surf);
  border: 1px solid var(--bord);
  border-radius: 8px;
  padding: 6px;
  display: grid;
  grid-template-columns: repeat(8, 28px);
  gap: 2px;
  box-shadow: 0 4px 20px rgba(0,0,0,.14);
}
.sym-popup button {
  width: 28px; height: 28px;
  border: 1px solid var(--bord);
  border-radius: 3px;
  background: var(--surf);
  cursor: pointer;
  font-size: 13px;
}
.sym-popup button:hover { background: #eef1fd; border-color: var(--acc); }
.sym-popup button.cur   { background: var(--acc); color: #fff; border-color: var(--acc); }
.sym-popup button.used  { opacity: .3; }

/* ── History ─────────────────────── */
.history-full {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  height: calc(100vh - var(--hdr));
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.h-toolbar { display: flex; gap: 10px; }
.h-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(195px, 1fr)); gap: 11px; }
.h-card {
  background: var(--surf);
  border: 1px solid var(--bord);
  border-radius: 8px;
  overflow: hidden;
  transition: box-shadow .12s;
}
.h-card:hover { box-shadow: 0 3px 14px rgba(0,0,0,.1); }
.h-thumb { width: 100%; height: 108px; object-fit: contain; background: var(--bg); display: block; }
.h-info  { padding: 8px 10px; }
.h-name  { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.h-meta  { font-size: 10.5px; color: var(--muted); margin-top: 2px; }
.h-actions { display: flex; gap: 5px; padding: 7px 9px; border-top: 1px solid var(--bord); }
.btn-open:hover { border-color: var(--acc); color: var(--acc); background: #eef1fd; }
.btn-del:hover  { border-color: #dc2626; color: #dc2626; background: #fff5f5; }
.empty-msg { color: var(--muted); text-align: center; padding: 40px; }

/* ── Modals ──────────────────────── */
.modal-bg {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.36);
  z-index: 400;
  align-items: center;
  justify-content: center;
}
.modal-bg.open { display: flex; }
.modal {
  background: var(--surf);
  border-radius: 10px;
  min-width: 310px;
  max-width: 420px;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
  overflow: hidden;
}
.modal-lg { min-width: 460px; max-width: 560px; max-height: 80vh; overflow-y: auto; }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 15px;
  border-bottom: 1px solid var(--bord);
  font-size: 13.5px;
}
.modal-head-l { display: flex; align-items: center; gap: 10px; }
.modal-close { background: none; border: none; font-size: 16px; cursor: pointer; color: var(--muted); }
.modal-close:hover { color: var(--text); }
.modal-body { padding: 13px 15px; display: flex; flex-direction: column; gap: 10px; }
.modal-foot { padding: 11px 15px; border-top: 1px solid var(--bord); display: flex; gap: 7px; justify-content: flex-end; }

/* Thread modal */
.tm-swatch { width: 30px; height: 30px; border-radius: 5px; border: 1px solid rgba(0,0,0,.12); flex-shrink: 0; }
.tm-ctrls  { display: flex; gap: 7px; }
.tm-list { list-style: none; max-height: 320px; overflow-y: auto; border: 1px solid var(--bord); border-radius: var(--radius); }
.tm-list li {
  display: flex; align-items: center; gap: 7px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--bord);
  cursor: pointer; font-size: 11.5px;
}
.tm-list li:last-child { border-bottom: none; }
.tm-list li:hover   { background: var(--bg); }
.tm-list li.current { background: #eef1fd; }
.tm-swatch { width: 15px; height: 15px; border-radius: 2px; border: 1px solid rgba(0,0,0,.12); flex-shrink: 0; }
.tm-id   { font-weight: 600; width: 46px; flex-shrink: 0; }
.tm-name { flex: 1; color: var(--muted); }
.tm-dist { font-size: 10px; color: var(--muted); width: 38px; text-align: right; }

/* ── Tool buttons ────────────────── */
.tool-btn {
  width: 28px; height: 26px;
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  background: var(--surf);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  flex-shrink: 0;
  transition: .1s;
}
.tool-btn:hover  { background: var(--bg); }
.tool-btn.active { background: var(--acc); color: #fff; border-color: var(--acc); }

.tool-label-sm { font-size: 10px; color: var(--muted); white-space: nowrap; }

/* Line type dropdown */
.line-type-sel {
  height: 26px;
  font-size: 11px;
  padding: 0 4px;
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  background: var(--surf);
  cursor: pointer;
}

/* Backstitch thread selector bar */
.bs-thread-bar {
  display: flex;
  align-items: center;
  gap: 5px;
}
.bs-swatch {
  width: 18px; height: 18px;
  border-radius: 3px;
  border: 1px solid rgba(0,0,0,.18);
  flex-shrink: 0;
}
.bs-code {
  font-size: 10px;
  color: var(--muted);
  font-family: monospace;
  white-space: nowrap;
  max-width: 58px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Thread modal separator */
.tm-sep {
  padding: 3px 10px;
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .3px;
  background: var(--bg);
  border-bottom: 1px solid var(--bord);
  pointer-events: none;
}

/* ── Auto-contour section ────────── */
.contour-header { display: flex; align-items: center; }
.contour-hint   { font-size: 10px; color: var(--muted); }

/* ── Cell hover tooltip ──────────── */
.cell-tooltip {
  position: fixed;
  background: #1e2028;
  color: #f0f2f5;
  border-radius: 7px;
  padding: 7px 10px;
  font-size: 11.5px;
  pointer-events: none;
  z-index: 999;
  display: flex;
  align-items: center;
  gap: 9px;
  box-shadow: 0 4px 18px rgba(0,0,0,.28);
  min-width: 140px;
}
.cell-tooltip[hidden] { display: none; }
.ct-swatch { width: 24px; height: 24px; border-radius: 4px; flex-shrink: 0; }
.ct-info   { display: flex; flex-direction: column; gap: 1px; }
.ct-thread { font-weight: 700; font-size: 12px; }
.ct-name   { color: rgba(255,255,255,.6); font-size: 10.5px; }
.ct-sym    { font-size: 10px; opacity: .45; margin-top: 2px; }

/* ── Reducer modal ───────────────── */
.modal-reducer {
  min-width: 360px;
  max-width: 440px;
}
.reducer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}
.reducer-label { color: var(--muted); font-weight: 500; }
.reducer-val   { font-weight: 600; }
.reducer-val.acc   { color: var(--acc); }
.reducer-val.muted { color: var(--muted); }
.reducer-sep  { height: 1px; background: var(--bord); margin: 4px 0; }
.reducer-info-box {
  background: var(--bg);
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.reducer-note {
  font-size: 10.5px;
  color: var(--muted);
  line-height: 1.5;
}

/* ── Pattern mode ────────────────── */
.pattern-chk { margin-bottom: 6px; font-weight: 500; }
.pattern-cell-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.inp-num {
  width: 58px;
  padding: 4px 6px;
  border: 1px solid var(--bord);
  border-radius: var(--radius);
  font-size: 12px;
  text-align: center;
}
