:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2c313c;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --teal: #2dd4bf;
  --amber: #fbbf24;
  --pink: #f472b6;
  --red: #f87171;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

body { display: flex; flex-direction: column; }

main { flex: 1; min-height: 0; }

button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
}
button:hover:not(:disabled) { background: #262b36; }
button:disabled { opacity: 0.4; cursor: default; }
button.active { border-color: var(--pink); color: var(--pink); }

button:focus-visible, input:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

input[type="search"], input[type="number"], input[type="file"], input[type="text"] {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
}

/* ---------- topbar ---------- */

#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; margin-right: 8px; }
.dirty { color: var(--amber); margin-left: auto; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 70vw;
  padding: 9px 14px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  z-index: 50;
  white-space: pre-wrap;
}
.toast-warn { border-color: var(--amber); }
.toast-error { border-color: var(--red); }

/* ---------- library ---------- */

#library-view { padding: 18px; max-width: 980px; margin: 0 auto; }
.library-search { display: flex; gap: 8px; margin-bottom: 12px; }
.library-search .search-input { flex: 1; }
.library-filter {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 13px;
  user-select: none;
  width: fit-content;
}
.library-status { color: var(--muted); margin-bottom: 10px; }
.comic-list { display: flex; flex-direction: column; gap: 6px; }
.comic-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.comic-title { flex: 1; }
.chip {
  font-size: 12px;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}
.status-completed { color: var(--teal); border-color: var(--teal); }
.status-pending { color: var(--amber); border-color: var(--amber); }
.status-failed { color: var(--red); border-color: var(--red); }
.chip-manga { color: var(--pink); border-color: var(--pink); }
.pager { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.pager-info { color: var(--muted); font-size: 13px; min-width: 90px; text-align: center; }

/* ---------- import ---------- */

#import-view { padding: 18px; max-width: 980px; margin: 0 auto; }
.import-file-row { margin-bottom: 10px; }
.import-status { color: var(--muted); margin-bottom: 10px; white-space: pre-wrap; }
.import-source { margin-bottom: 10px; color: var(--muted); }
.candidate-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.candidate-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.candidate-score { font-variant-numeric: tabular-nums; color: var(--teal); }
.source-match { color: var(--pink); font-size: 12px; }
.breakdown { color: var(--muted); font-size: 12px; }
.mode-row { display: flex; gap: 16px; margin-bottom: 12px; }
.manual-search { margin-top: 16px; display: flex; flex-wrap: wrap; gap: 8px; }
.manual-results { flex-basis: 100%; display: flex; flex-direction: column; gap: 4px; }
.manual-row { display: flex; align-items: center; gap: 10px; }
.import-result div { margin-bottom: 6px; }
.import-warning { color: var(--amber); }

/* ---------- settings ---------- */

#settings-view { padding: 18px; max-width: 980px; margin: 0 auto; }
.settings-title { font-weight: 600; font-size: 16px; margin-bottom: 6px; }
.settings-intro { color: var(--muted); margin: 0 0 14px; }
.settings-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; margin-bottom: 12px; }
.settings-field { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 320px; }
.settings-field-label { color: var(--muted); font-size: 13px; }
.settings-form .settings-path-input { width: 100%; }
.settings-status { color: var(--muted); margin-bottom: 10px; white-space: pre-wrap; }
.settings-status-ok { color: var(--muted); }
.settings-status-warn { color: var(--amber); }
.settings-status-error { color: var(--red); }
.settings-env-notice { color: var(--amber); margin-bottom: 10px; }
.settings-restart { color: var(--teal); margin-bottom: 10px; }
.settings-resolved {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.settings-row { display: flex; gap: 10px; }
.settings-row-label { width: 150px; color: var(--muted); flex: none; }
.settings-row-value { overflow-wrap: anywhere; }

/* ---------- editor ---------- */

#editor-view { display: flex; flex-direction: column; height: 100%; }
#editor-toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.editor-title { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.mode-group, .action-group { display: flex; gap: 6px; flex-wrap: wrap; }

#editor-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 210px 1fr 250px 210px;
}

.pane { min-height: 0; overflow: auto; }
.page-pane, .order-pane, .inspector-pane {
  background: var(--panel);
  padding: 8px;
}
.page-pane { border-right: 1px solid var(--border); }
.order-pane { border-left: 1px solid var(--border); }
.inspector-pane { border-left: 1px solid var(--border); }
.pane-head { color: var(--muted); margin-bottom: 8px; }

.page-btn {
  display: block;
  width: 100%;
  text-align: left;
  margin-bottom: 3px;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.page-btn.current { border-color: var(--pink); color: var(--pink); }

/* ---------- stage ---------- */

.stage-pane { position: relative; background: #0b0d11; }
/* touch-action: none — on touch screens marquee drawing and select-drag panning
   must win over native scrolling (panning is the select-mode drag). */
.stage-viewport { position: relative; overflow: auto; height: 100%; touch-action: none; }
.stage { position: relative; transform-origin: 0 0; }
.stage-img {
  width: 100%;
  height: 100%;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}
.stage-overlay { position: absolute; inset: 0; }

.box {
  position: absolute;
  border-style: solid;
  /* Screen-constant chrome: --invpx = 1/zoom (set by the stage), so outlines
     and badges stay bold at any zoom — the stage transform scales everything. */
  border-width: calc(3 * var(--invpx, 1px));
  cursor: move;
}
.box-panel { border-color: var(--teal); background: rgba(45, 212, 191, 0.14); }
.box-bubble { border-color: var(--amber); background: rgba(251, 191, 36, 0.14); }
.box.selected {
  border-color: var(--pink);
  border-width: calc(4 * var(--invpx, 1px));
  background: rgba(244, 114, 182, 0.16);
}

/* Free-drawn polygons: the bbox div carries no outline of its own — the SVG
   shape does. */
.box.has-shape { border: none; background: none; }
.shape-svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.shape { fill: rgba(45, 212, 191, 0.14); stroke: var(--teal); stroke-width: 3; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.shape-bubble { fill: rgba(251, 191, 36, 0.14); stroke: var(--amber); }
.box.selected .shape { fill: rgba(244, 114, 182, 0.16); stroke: var(--pink); }

.badge {
  position: absolute;
  top: calc(-14 * var(--invpx, 1px));
  left: calc(-14 * var(--invpx, 1px));
  box-sizing: border-box;
  min-width: calc(28 * var(--invpx, 1px));
  height: calc(28 * var(--invpx, 1px));
  padding: 0 calc(5 * var(--invpx, 1px));
  border-radius: calc(14 * var(--invpx, 1px));
  background: #0b0d11;
  border: calc(2 * var(--invpx, 1px)) solid var(--border);
  color: #fff;
  font-size: calc(16 * var(--invpx, 1px));
  font-weight: 700;
  line-height: calc(24 * var(--invpx, 1px));
  text-align: center;
  cursor: pointer;
}
.badge-input {
  display: block;
  width: 3em;
  height: 100%;
  box-sizing: border-box;
  padding: 0;
  border: none;
  border-radius: inherit;
  background: #fff;
  color: #0b0d11;
  font: inherit;
  text-align: center;
  outline: 2px solid var(--pink);
}

.handle {
  position: absolute;
  width: calc(13 * var(--invpx, 1px));
  height: calc(13 * var(--invpx, 1px));
  background: var(--pink);
  border: calc(1.5 * var(--invpx, 1px)) solid #0b0d11;
  border-radius: calc(3 * var(--invpx, 1px));
}
.handle-nw { left: calc(-6.5 * var(--invpx, 1px)); top: calc(-6.5 * var(--invpx, 1px)); cursor: nwse-resize; }
.handle-n { left: 50%; top: calc(-6.5 * var(--invpx, 1px)); margin-left: calc(-6.5 * var(--invpx, 1px)); cursor: ns-resize; }
.handle-ne { right: calc(-6.5 * var(--invpx, 1px)); top: calc(-6.5 * var(--invpx, 1px)); cursor: nesw-resize; }
.handle-e { right: calc(-6.5 * var(--invpx, 1px)); top: 50%; margin-top: calc(-6.5 * var(--invpx, 1px)); cursor: ew-resize; }
.handle-se { right: calc(-6.5 * var(--invpx, 1px)); bottom: calc(-6.5 * var(--invpx, 1px)); cursor: nwse-resize; }
.handle-s { left: 50%; bottom: calc(-6.5 * var(--invpx, 1px)); margin-left: calc(-6.5 * var(--invpx, 1px)); cursor: ns-resize; }
.handle-sw { left: calc(-6.5 * var(--invpx, 1px)); bottom: calc(-6.5 * var(--invpx, 1px)); cursor: nesw-resize; }
.handle-w { left: calc(-6.5 * var(--invpx, 1px)); top: 50%; margin-top: calc(-6.5 * var(--invpx, 1px)); cursor: ew-resize; }

/* Free-draw polygon preview (inside the stage, so it scales with the page). */
.free-layer { position: absolute; inset: 0; pointer-events: none; }
.free-line { fill: none; stroke: var(--pink); stroke-width: 2; stroke-dasharray: 7 5; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.free-vertex { fill: #fff; stroke: #0b0d11; stroke-width: 2; r: calc(4.5 * var(--invpx, 1px)); }
.free-vertex-start { fill: var(--pink); stroke: #fff; r: calc(7 * var(--invpx, 1px)); }

.marquee {
  position: absolute;
  border: 1px dashed var(--text);
  background: rgba(255, 255, 255, 0.08);
  pointer-events: none;
}

/* ---------- order panel ---------- */

.order-list { display: flex; flex-direction: column; }
.caret-slot {
  height: 7px;
  border-radius: 4px;
  cursor: pointer;
}
.caret-slot:hover { background: rgba(244, 114, 182, 0.25); }
.caret-slot.active { background: var(--pink); }
.caret-slot.drop-slot { background: var(--teal); }
.order-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}
.order-row .badge { position: static; }
.order-row.dragging { opacity: 0.45; }
.order-row .chip-panel { color: var(--teal); border-color: var(--teal); }
.order-row .chip-bubble { color: var(--amber); border-color: var(--amber); }
.order-label { flex: 1; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.order-row .move-up, .order-row .move-down { padding: 0 6px; font-size: 11px; }

/* ---------- inspector ---------- */

.inspector-head { font-weight: 600; margin-bottom: 8px; }
.inspector-empty { color: var(--muted); }
.inspector-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.inspector-row input { width: 90px; }
.inspector-delete { margin-top: 10px; width: 100%; }

/* ---------- mobile ---------- */

@media (max-width: 900px) {
  #editor-body {
    display: flex;
    flex-direction: column;
    overflow-y: auto;
  }
  #editor-toolbar { gap: 8px; padding: 6px 10px; }
  #page-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    max-height: 22vh;
  }
  .page-btn {
    width: auto;
    max-width: 48vw;
    margin: 0;
  }
  .stage-pane {
    flex: 0 0 auto;
    height: 45vh;
    min-height: 240px;
  }
  .order-pane { max-height: 30vh; }
  .inspector-pane { max-height: 40vh; }
  .page-pane {
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .order-pane, .inspector-pane {
    border-left: 0;
    border-top: 1px solid var(--border);
  }
}

@media (max-width: 680px) {
  #topbar {
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 10px;
  }
  #topbar button {
    font-size: 12px;
    padding: 4px 8px;
  }
  .dirty { margin-left: 0; }
}
