:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #dfe2e7;
  --text: #1d2330;
  --muted: #69707d;
  --accent: #2f6fde;
  --accent-soft: #e5eefc;
  --ok: #1f9d55;
  --warn: #c27c0e;
  --canvas-bg: #eceef2;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181e;
    --panel: #1e222a;
    --border: #2e333d;
    --text: #e6e8ec;
    --muted: #9aa1ad;
    --accent: #5b8ff0;
    --accent-soft: #23314b;
    --canvas-bg: #262a33;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}

h1 { font-size: 22px; margin: 0 0 4px; }
h3 { font-size: 15px; margin: 0 0 10px; }
.lead { color: var(--muted); margin: 0 0 16px; max-width: 900px; }
.muted { color: var(--muted); }

/* Верхняя панель */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { font-size: 28px; color: var(--accent); }
.brand-title { font-weight: 700; font-size: 20px; letter-spacing: .01em; }
.brand-sub { font-size: 12px; color: var(--muted); }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.cv-status { font-size: 12px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; margin-left: 8px; }
.cv-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.cv-status.ready .dot { background: var(--ok); }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .5; cursor: default; }

/* Разметка */
:root { --top-h: 58px; --status-h: 28px; }
.layout { display: flex; min-height: calc(100vh - var(--top-h) - var(--status-h)); }

/* Левая панель: дерево разделов сверху, параметры операции снизу */
.sidebar {
  width: 300px; flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--panel);
  display: flex; flex-direction: column;
  position: sticky; top: var(--top-h); height: calc(100vh - var(--top-h) - var(--status-h));
}
.nav-toggle { display: none; }
.nav-tree { flex: 1 1 auto; min-height: 120px; overflow-y: auto; padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; }
.nav-tree button { font: inherit; color: inherit; background: none; border: 0; text-align: left; cursor: pointer; width: 100%; }

.nav-top { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; font-weight: 600 !important; }
.nav-top:hover { background: var(--canvas-bg); }
.nav-num {
  display: inline-grid; place-items: center; flex-shrink: 0;
  width: 22px; height: 22px; border-radius: 6px;
  background: var(--canvas-bg); color: var(--muted); font-size: 12px; font-weight: 600;
}
.nav-title { flex: 1; }
.nav-group.active > .nav-top { color: var(--accent); }
.nav-group.active > .nav-top .nav-num { background: var(--accent); color: #fff; }
.nav-top.active { background: var(--accent-soft); }

.chev { width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(-45deg); transition: transform .15s; flex-shrink: 0; margin-right: 4px; }
.nav-group.open > .nav-top .chev, .nav-cat.open > .nav-cat-head .chev { transform: rotate(45deg); }

.nav-children { display: none; padding: 2px 0 8px 14px; margin-left: 11px; border-left: 1px solid var(--border); }
.nav-group.open > .nav-children { display: flex; flex-direction: column; gap: 4px; }

.nav-cat-head {
  display: flex; align-items: center; gap: 8px; padding: 8px 8px 6px;
  font-size: 11px !important; font-weight: 700 !important; letter-spacing: .06em; text-transform: uppercase; color: var(--muted) !important;
  border-radius: 6px;
}
.nav-cat-head > span:first-child { flex: 1; }
.nav-cat-head:hover { color: var(--text) !important; }
.nav-count { font-size: 11px; font-weight: 600; letter-spacing: 0; padding: 0 6px; border-radius: 10px; background: var(--canvas-bg); }
.nav-items { display: none; flex-direction: column; gap: 5px; padding: 2px 0 6px; }
.nav-cat.open > .nav-items { display: flex; }

.nav-item {
  display: flex; flex-direction: column; gap: 2px;
  padding: 8px 11px !important; border: 1px solid var(--border) !important; border-radius: 8px;
  background: var(--panel) !important;
}
.nav-item:hover { border-color: var(--accent) !important; }
.nav-item-title { font-weight: 600; font-size: 13.5px; line-height: 1.3; }
.nav-item-desc { font-size: 12px; color: var(--muted); line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.nav-item.active { background: var(--accent-soft) !important; border-color: var(--accent) !important; box-shadow: 0 0 0 1px var(--accent) inset; }
.nav-item.active .nav-item-title { color: var(--accent); }

/* Параметры выбранной операции — вверху раздела «Редактор» */
.op-params { padding: 14px 16px; }
.params-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 12px; }
.params-titles { min-width: 0; }
.eyebrow { font-size: 11px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.link-btn { border: 0; background: none; color: var(--accent); font: inherit; font-size: 13px; cursor: pointer; padding: 2px 0; white-space: nowrap; }
.link-btn:hover { text-decoration: underline; }
.link-btn:disabled { color: var(--muted); opacity: .5; cursor: default; text-decoration: none; }
.params-title { font-weight: 700; font-size: 17px; margin-top: 2px; }
.params-desc { font-size: 13px; color: var(--muted); margin-top: 2px; }
.params-body { display: flex; gap: 18px; align-items: flex-start; }
.params-grid { flex: 1; min-width: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(min(230px, 100%), 1fr)); gap: 10px 22px; }
.params-none { grid-column: 1 / -1; margin: 0; }
.param { min-width: 0; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.param-label { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.param-label output {
  font-weight: 500; font-variant-numeric: tabular-nums; min-width: 46px; text-align: center;
  border: 1px solid var(--border); border-radius: 6px; padding: 0 6px; background: var(--panel); font-size: 12px; line-height: 20px; white-space: nowrap;
}
.param input[type=range] { width: 100%; accent-color: var(--accent); margin: 0; }
.param select { width: 100%; }
.param-range { display: flex; justify-content: space-between; font-size: 10.5px; color: var(--muted); }
.param-hint { font-size: 11.5px; color: var(--muted); margin-top: 4px; line-height: 1.35; }
.param-check { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; cursor: pointer; }
.curve-box {
  flex: 0 0 auto; display: grid; grid-template-columns: 120px auto; grid-template-rows: auto 1fr; column-gap: 12px; align-items: start;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); max-width: 330px;
}
.curve-box .param-label { grid-column: 2; margin: 0; }
.curve-box canvas { grid-column: 1; grid-row: 1 / span 2; display: block; width: 120px; height: 120px; background: var(--canvas-bg); border-radius: 6px; }
.curve-box .formula { grid-column: 2; font-size: 13px; text-align: left; margin-top: 4px; }

/* Строка состояния */
.statusbar {
  position: sticky; bottom: 0; z-index: 9; height: var(--status-h);
  display: flex; align-items: center; gap: 18px; padding: 0 16px;
  background: var(--panel); border-top: 1px solid var(--border);
  font: 12px Consolas, "Cascadia Mono", monospace; color: var(--muted);
}
.statusbar span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.statusbar-right { margin-left: auto; }

/* Редактор: «до / после» */
.editor-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.editor-toggles { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 13px; }
.editor-toggles label.inline { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.editor-toggles .sep { width: 1px; height: 16px; background: var(--border); }
.ba { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.ba.single { grid-template-columns: minmax(0, 1fr); }
.ba.single .ba-before { display: none; }
.ba-panel { margin: 0 !important; display: flex; flex-direction: column; min-width: 0; }
.ba-head { margin-bottom: 8px; display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 10px; align-items: start; }
.ba-head .canvas-actions { grid-column: 2; grid-row: 1 / span 2; margin: 0 !important; }
.ba-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.ba-view {
  position: relative; border-radius: 6px; min-height: 340px;
  height: calc(100vh - var(--top-h) - var(--status-h) - 330px - var(--params-h, 160px));
  background-color: var(--canvas-bg);
  background-image: conic-gradient(rgba(128,128,128,.13) 25%, transparent 0 50%, rgba(128,128,128,.13) 0 75%, transparent 0);
  background-size: 22px 22px;
  overflow: hidden;
}
/* Изображение вписывается в область целиком при любых пропорциях (поля — «шахматка» прозрачности) */
.ba-canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; cursor: zoom-in; }
.ba-hist-head { display: flex; justify-content: space-between; font-size: 13px; margin-top: 10px; }
.ba-panel .canvas-actions { margin-top: 6px; }

.content { flex: 1; min-width: 0; padding: 22px 24px 60px; position: relative; }
.section { display: none; }
.section.active { display: block; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin: 0 0 14px;
}
figure.panel { margin: 0 0 14px; }
figcaption { font-weight: 600; margin-bottom: 8px; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.grid-2 > .panel, .grid-3 > .panel { margin: 0; }

.empty {
  padding: 40px; text-align: center; color: var(--muted);
  border: 2px dashed var(--border); border-radius: var(--radius);
}
.empty.small { padding: 24px; }
body.has-image [data-empty] { display: none; }
body:not(.has-image) [data-needs-image] { display: none !important; }
/* Файл не декодирован браузером: в разделе «Информация» показываем таблицу заголовка */
body.header-only #sec-info [data-needs-image] { display: grid !important; }
body.header-only #sec-info [data-empty] { display: none; }

.canvas-actions { display: flex; justify-content: flex-end; gap: 6px; margin-top: 6px; }
.btn.small { padding: 3px 10px; font-size: 12px; border-radius: 6px; }
.lightbox-bar { position: fixed; top: 14px; right: 14px; display: flex; gap: 8px; z-index: 71; }

canvas.preview {
  display: block; width: 100%; height: auto;
  max-height: 520px; object-fit: contain;
  background: var(--canvas-bg);
  border-radius: 6px;
}
canvas.pick { cursor: crosshair; }
canvas.hist { display: block; width: 100%; height: auto; margin-top: 10px; background: var(--canvas-bg); border-radius: 6px; }
canvas.curve { display: block; width: 100%; max-width: 300px; height: auto; margin: 0 auto; background: var(--canvas-bg); border-radius: 6px; }

/* Элементы управления */
.controls { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 14px 22px; }
.controls label, .params label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.controls label.inline, .params label.inline, .hist-toggles label.inline { flex-direction: row; align-items: center; gap: 6px; font-size: 13px; color: var(--text); }
.controls label.btn { color: #fff; font-size: 14px; flex-direction: row; }
.controls output, .params output { font-size: 12px; color: var(--text); font-variant-numeric: tabular-nums; }
.controls input[type=range], .params input[type=range] { width: 160px; accent-color: var(--accent); }
select, input[type=number] {
  padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--panel); color: var(--text); font: inherit;
}
.params { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: flex-end; }
.hist-toggles { display: flex; flex-wrap: wrap; align-items: center; gap: 16px; }

/* Таблица свойств */
table.kv { width: 100%; border-collapse: collapse; }
table.kv th, table.kv td { padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: top; text-align: left; }
table.kv th { width: 42%; color: var(--muted); font-weight: 500; }
table.kv tr.group th { color: var(--accent); font-weight: 600; padding-top: 14px; }

/* Цвета */
.swatch-row { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.swatch { width: 72px; height: 72px; border-radius: 10px; border: 1px solid var(--border); }
.swatch-hex { font-size: 20px; font-weight: 600; font-family: Consolas, monospace; }
.color-cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.color-card { border: 1px solid var(--border); border-radius: 8px; padding: 10px; }
.color-card.source { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.color-card h4 { margin: 0 0 8px; font-size: 13px; }
.color-card .fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); gap: 6px; }
.color-card label { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); gap: 2px; }
.color-card input { width: 100%; }
.pick-info { margin-top: 8px; font-size: 13px; color: var(--muted); }

.stats { margin-top: 8px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.formula { margin-top: 10px; text-align: center; font-family: "Cambria Math", Cambria, serif; font-size: 16px; }

/* Плитки результатов */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(280px, 100%), 1fr)); gap: 14px; }
.tiles.wide { grid-template-columns: repeat(auto-fill, minmax(min(380px, 100%), 1fr)); }
.tile { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.tile h3 { margin: 0; }
.tile .desc { font-size: 12px; color: var(--muted); margin: 0; }
.tile .meta { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tile canvas { cursor: zoom-in; }

/* Сравнение */
.compare-drop {
  margin: 0 0 14px; padding: 26px 20px;
  border: 2px dashed var(--border); border-radius: var(--radius);
  text-align: center; color: var(--muted); cursor: pointer;
  transition: border-color .15s, background .15s;
}
.compare-drop b { color: var(--text); font-weight: 600; white-space: nowrap; }
.compare-drop:hover, .compare-drop:focus-visible { border-color: var(--accent); outline: none; }
body.dragging-compare .compare-drop { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.compare-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr)); gap: 10px; margin-bottom: 14px; }
.cmp-card {
  display: flex; align-items: center; gap: 12px; min-width: 0;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px;
}
.cmp-card.best { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.cmp-card canvas { width: 52px; height: 52px; object-fit: cover; border-radius: 6px; background: var(--canvas-bg); flex-shrink: 0; }
.cmp-body { flex: 1; min-width: 0; }
.cmp-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cmp-meta { font-size: 12px; color: var(--muted); font-family: Consolas, monospace; margin-top: 3px; }
.cmp-del { border: 0; background: none; color: var(--accent); font: inherit; font-size: 13px; cursor: pointer; padding: 4px; flex-shrink: 0; }
.cmp-del:hover { text-decoration: underline; }
.matrix-wrap { overflow-x: auto; }
table.matrix { border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
table.matrix th, table.matrix td { padding: 6px 8px; text-align: center; border: 1px solid var(--border); }
table.matrix td.best { outline: 2px solid var(--ok); outline-offset: -2px; font-weight: 700; }

/* Прочее */
.dropzone-hint {
  display: none; position: fixed; inset: 0; z-index: 50;
  background: rgba(47, 111, 222, .15); border: 3px dashed var(--accent);
  place-items: center; font-size: 22px; font-weight: 600; color: var(--accent);
}
body.dragging .dropzone-hint { display: grid; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 8px;
  opacity: 0; transition: .25s; pointer-events: none; z-index: 60; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.lightbox { position: fixed; inset: 0; z-index: 70; background: rgba(0,0,0,.8); display: grid; place-items: center; cursor: zoom-out; }
.lightbox canvas { max-width: 95vw; max-height: 92vh; }

@media (max-width: 1100px) {
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 800px) {
  .layout { flex-direction: column; }
  /* Блоки панели становятся элементами общей колонки: меню → картинки → параметры */
  .sidebar { display: contents; }
  .nav-toggle { order: 0; background: var(--panel); border-bottom: 1px solid var(--border) !important; }
  .nav-tree { order: 1; background: var(--panel); border-bottom: 1px solid var(--border); }
  .content { order: 2; }
  .params-body { flex-direction: column; }
  .curve-box { flex: none; width: 100%; max-width: 280px; }
  .nav-toggle {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 12px 16px; border: 0; background: none; color: var(--text); font: inherit; font-weight: 600; text-align: left; cursor: pointer;
  }
  .nav-toggle-icon { font-size: 18px; color: var(--accent); }
  .nav-tree { display: none; max-height: none; overflow: visible; border-top: 1px solid var(--border); }
  body.nav-open .nav-tree { display: flex; }
  .grid-2, .grid-3, .color-cards, .ba { grid-template-columns: minmax(0, 1fr); }
  .ba-view { height: 60vh; min-height: 260px; }
  .content { padding: 16px; }
  .topbar { flex-direction: column; align-items: flex-start; position: static; }
  .statusbar-right { display: none; }
}

.warn-text { color: var(--warn); }
