:root {
  --bg: #0f1115;
  --panel: #161922;
  --panel-2: #1d2230;
  --border: #272d3d;
  --text: #e6e8ec;
  --muted: #8a93a6;
  --accent: #5b8def;
  --accent-2: #4979d6;
  --danger: #e6536b;
  --ok: #3fbf7f;
  --warn: #f0b03b;
  --shadow: 0 6px 20px rgba(0,0,0,.35);
  --radius: 8px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); }
.ok    { color: var(--ok); }

button { font: inherit; cursor: pointer; transition: filter .1s, transform .05s; }
/* Press feedback. Without it a slow action reads as a dead button. */
button:not(:disabled):hover  { filter: brightness(1.15); }
button:not(:disabled):active { transform: translateY(1px); filter: brightness(.9); }
button:disabled { opacity: .5; cursor: not-allowed; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Global in-flight indicator, toggled by api.js. Indeterminate on purpose —
   these are small JSON calls, the wait is latency, not bytes. */
body.busy { cursor: progress; }
body.busy::after {
  content: ""; position: fixed; top: 0; left: 0; z-index: 100;
  width: 100%; height: 3px; pointer-events: none;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: busy-slide 1s linear infinite;
}
@keyframes busy-slide {
  from { transform: translateX(-100%); }
  to   { transform: translateX(100%); }
}
@media (prefers-reduced-motion: reduce) {
  body.busy::after { animation: none; background: var(--accent); }
}
.btn-primary {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff; border: 0; border-radius: var(--radius);
  padding: 9px 14px; font-weight: 600;
}
.btn-primary:hover { background: var(--accent-2); }
.btn-ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 7px 12px;
}
.btn-ghost:hover { background: var(--panel-2); }
.btn-danger { background: var(--danger); color: #fff; border: 0; border-radius: var(--radius); padding: 6px 10px; }
input, select, textarea {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 8px 10px; font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }

/* ------- auth ------- */
body.auth { display: grid; place-items: center; }
.auth-card {
  width: 360px; max-width: calc(100vw - 32px);
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0 0 4px; font-size: 22px; letter-spacing: .3px; }
.tabs { display: flex; gap: 4px; margin: 16px 0 14px; border-bottom: 1px solid var(--border); }
.tab {
  background: transparent; border: 0; color: var(--muted);
  padding: 8px 12px; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.auth-form button[type=submit] { background: var(--accent); color: #fff; border: 0; border-radius: var(--radius); padding: 10px; font-weight: 600; }

/* ------- topbar ------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { font-weight: 700; letter-spacing: .3px; }
.user { display: flex; align-items: center; gap: 12px; position: relative; }
.user-search {
  width: 160px; padding: 5px 8px; font-size: 12px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text);
}
.user-search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.user-search-results {
  position: absolute; top: 100%; right: 0; margin-top: 4px;
  width: 220px; max-height: 240px; overflow: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); z-index: 60;
}
.user-search-results .result {
  padding: 8px 10px; cursor: pointer; font-size: 13px;
}
.user-search-results .result:hover { background: var(--panel-2); }
.user-search-results .empty { padding: 8px 10px; color: var(--muted); font-size: 12px; }

/* ------- layout ------- */
.layout { height: calc(100vh - 49px); }

.picker { padding: 24px; max-width: 900px; margin: 0 auto; }
.picker-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.picker-head h2 { margin: 0; }
.upload-btn { cursor: pointer; }

.project-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.project-list li {
  display: grid; grid-template-columns: 1fr auto auto auto; gap: 12px;
  align-items: center;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px;
}
.project-list .name { font-weight: 600; }
.project-list .meta { color: var(--muted); font-size: 12px; }
.project-list .open { background: var(--accent); color: #fff; border: 0; padding: 6px 10px; border-radius: var(--radius); }

/* ------- editor ------- */
.editor { display: grid; grid-template-columns: 280px 1fr; height: 100%; }
.sidebar {
  border-right: 1px solid var(--border); background: var(--panel);
  padding: 14px; overflow: auto; display: flex; flex-direction: column; gap: 10px;
}
.panel-title { font-weight: 600; margin-bottom: 6px; }
.panel { background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.shape-list { list-style: none; padding: 0; margin: 0; }
.shape-list li {
  display: flex; justify-content: space-between; align-items: center;
  gap: 6px; padding: 4px 0; border-bottom: 1px dashed var(--border);
  min-width: 0; overflow: hidden;
}
.shape-list li > * { min-width: 0; }
.shape-list li input[type=text] { min-width: 0; }
.shape-list li:last-child { border-bottom: 0; }

.canvas-wrap { display: flex; flex-direction: column; min-width: 0; }
.toolbar {
  display: flex; align-items: center; gap: 10px;
  border-bottom: 1px solid var(--border); padding: 8px 12px; background: var(--panel);
}
.toolbar .sep { width: 1px; height: 20px; background: var(--border); }
.toolbar .inline { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.toolbar select { padding: 4px 6px; }
.tool {
  background: transparent; color: var(--muted);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 5px 10px;
}
.tool:hover { color: var(--text); }
.tool.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.trace-tools { display: inline-flex; gap: 6px; margin-left: 4px; }
.trace-tools button { padding: 5px 10px; }
.canvas-host {
  flex: 1; position: relative; overflow: hidden;
  background: #0a0c11;
}
.canvas-host canvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
  image-rendering: pixelated;
}
#overlay-canvas { pointer-events: auto; }
.load-progress {
  position: absolute; top: 0; left: 0; height: 3px;
  background: var(--accent); z-index: 2; pointer-events: none;
  transition: width .1s linear;
}

/* ------- modal ------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: grid; place-items: center; z-index: 50;
}
.modal {
  background: var(--panel); color: var(--text);
  width: min(960px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  border: 1px solid var(--border); border-radius: var(--radius);
  display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; }
.modal-body { padding: 16px 18px; overflow: auto; }
.dpi-row { display: flex; align-items: end; gap: 16px; margin-bottom: 14px; }
.dpi-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.page-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
}
.page-card {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px;
  display: flex; flex-direction: column; gap: 6px;
  color: var(--text); cursor: pointer;
}
.page-card:hover:not(:disabled) { border-color: var(--accent); }
.page-card.loading { opacity: .6; cursor: wait; }
.page-card img {
  width: 100%; aspect-ratio: 1 / 1.41; object-fit: contain;
  background: #fff; border-radius: 4px;
}
.page-label { font-size: 13px; }

/* When dim, the overlay backdrop becomes transparent and non-blocking so user
   can interact with the canvas; the inner modal hides. */
.modal-overlay.dim { background: transparent; pointer-events: none; }
.modal-overlay.dim .modal { display: none; }

.modal-narrow { width: min(560px, calc(100vw - 32px)); }
.modal-foot {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 12px 18px; border-top: 1px solid var(--border);
}
.cal-pane p { margin: 4px 0 12px; color: var(--muted); }
.cal-row { display: flex; gap: 12px; align-items: end; margin-bottom: 10px; }
.cal-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.cal-row input, .cal-row select { min-width: 120px; }
#cal-current { margin-bottom: 12px; }

/* ------- measure page ------- */
.measure-page { display: flex; flex-direction: column; height: 100%; }
.measure-head {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-bottom: 1px solid var(--border); background: var(--panel);
}
.measure-name-input {
  font-size: 16px; font-weight: 600; padding: 6px 10px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); min-width: 240px;
}
.measure-body { display: grid; grid-template-columns: 280px 1fr; flex: 1; min-height: 0; }
.measure-side {
  border-right: 1px solid var(--border); background: var(--panel);
  padding: 14px; overflow: auto; display: flex; flex-direction: column; gap: 10px;
}
.measure-canvas-host {
  background: #0a0c11; overflow: hidden; position: relative;
  display: flex; align-items: center; justify-content: center;
}
#measure-svg { width: 100%; height: 100%; cursor: grab; user-select: none; }
#measure-svg.adding { cursor: crosshair; }
#measure-svg.panning { cursor: grabbing; }


.pp-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.pp-row label { font-size: 12px; color: var(--muted); }
.pp-row .pp-check { display: inline-flex; align-items: center; gap: 6px; }
.pp-row input[type=range] { width: 100%; }
.pp-row select { width: 100%; }
