@font-face { font-family: 'UI'; src: url(../fonts/ibm-plex-mono-r.ttf); font-weight: 400; }
@font-face { font-family: 'UI'; src: url(../fonts/ibm-plex-mono-b.ttf); font-weight: 700; }
@font-face { font-family: 'Pix'; src: url(../fonts/pixelify-sans-b.ttf); font-weight: 700; }

:root {
  --ink: #2b2b2b;
  --ink2: #6b6b6b;
  --mute: #a3a3a3;
  --line: #d6d6d6;
  --soft: #f3f3f3;
  --desk: #e9e9e9;
  --bg: #ffffff;
  --err: #d23a2a;
  --u: 4px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 12px/1.45 'UI', ui-monospace, monospace;
  color: var(--ink); background: var(--bg);
  display: grid; grid-template-rows: 48px 1fr; grid-template-columns: minmax(0, 1fr); overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; border-radius: 0; }
button {
  background: var(--bg); border: 1px solid var(--ink); height: 28px; padding: 0 10px; cursor: pointer; white-space: nowrap;
}
button:hover:not(:disabled) { background: var(--ink); color: var(--bg); }
button:disabled { border-color: var(--line); color: var(--mute); cursor: default; }
button.primary { background: var(--ink); color: var(--bg); font-weight: 700; }
button.primary:hover { background: #000; }
input[type=text], input:not([type]), textarea, select {
  border: 1px solid var(--line); background: var(--bg); padding: 6px 8px; width: 100%; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--ink); }
textarea { resize: vertical; min-height: 60px; line-height: 1.5; }
input[type=range] { width: 100%; accent-color: var(--ink); }
input[type=checkbox] { accent-color: var(--ink); width: 14px; height: 14px; margin: 0; }

/* ---------- верх ---------- */
.top {
  display: flex; align-items: center; gap: 12px; padding: 0 14px;
  border-bottom: 1px solid var(--ink); min-width: 0; overflow: hidden;
}
.logo { font: 700 17px 'Pix', 'UI'; display: flex; align-items: center; gap: 8px; letter-spacing: .02em; color: inherit; text-decoration: none; white-space: nowrap; }
.logo i { width: 14px; height: 14px; background: var(--ink); box-shadow: 4px 4px 0 var(--mute); display: block; }
.title { width: 240px !important; min-width: 80px; flex-shrink: 1; border-color: transparent !important; font-weight: 700; }
.title:hover { border-color: var(--line) !important; }
.title:focus { border-color: var(--ink) !important; }
.grow { flex: 1; }
.saved { color: var(--mute); }
.seg { display: inline-flex; }
.seg button { margin-left: -1px; }
.seg button:first-child { margin-left: 0; }
.seg button.on { background: var(--ink); color: var(--bg); }
.seg.sm button { height: 24px; padding: 0 8px; }
.seg.full { display: flex; }
.seg.full button { flex: 1; padding: 0 4px; }

/* ---------- раскладка ---------- */
main { display: grid; grid-template-columns: 232px minmax(0, 1fr) 296px; min-height: 0; }
aside { overflow: auto; min-height: 0; }
.left { border-right: 1px solid var(--ink); padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.right { border-left: 1px solid var(--ink); display: flex; flex-direction: column; overflow: hidden; }
#inspector { flex: 1; overflow: auto; min-height: 0; }
.filebar { display: flex; border-bottom: 1px solid var(--ink); }
.filebar button { flex: 1; border: 0; border-right: 1px solid var(--line); height: 34px; padding: 0 4px; font-size: 11px; }
.filebar button:last-child { border-right: 0; }
.cap { font-size: 10px; text-transform: uppercase; letter-spacing: .12em; color: var(--ink2); }
.tip { margin-top: auto; color: var(--ink2); font-size: 11px; border-top: 1px dashed var(--line); padding-top: 10px; }
.tip b { color: var(--ink); }

/* палитра */
#palette { display: flex; flex-direction: column; gap: 6px; }
.tile {
  display: flex; gap: 10px; align-items: center;
  border: 1px solid var(--line); padding: 8px; cursor: grab; user-select: none; touch-action: none; background: var(--bg);
}
.tile:hover { border-color: var(--ink); }
.tile b { font-weight: 700; }
.tile small { color: var(--ink2); font-size: 10px; }
.ic { width: 24px; height: 24px; display: block; }
.ic rect { fill: currentColor; }

/* ---------- сцена ---------- */
.stage { display: grid; grid-template-rows: 1fr auto; min-height: 0; background: var(--desk); position: relative; }
#canvas {
  position: relative; overflow: hidden; min-height: 0; display: flex; align-items: center; justify-content: center;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Crect width='2' height='2' fill='%23d4d4d4'/%3E%3C/svg%3E");
}
.spread { display: flex; align-items: flex-start; }
.pagewrap { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.plabel { font-size: 10px; letter-spacing: .1em; text-transform: uppercase; color: var(--ink2); }
.page { position: relative; background: #fff; outline: 1px solid var(--ink); }
.page.active { z-index: 1; }
.page svg.pg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.spread .pagewrap + .pagewrap .page { outline-offset: 0; }
.spread.two .pagewrap:first-child .page::after {
  content: ''; position: absolute; top: 0; bottom: 0; right: 0; border-right: 1px dashed var(--mute); z-index: 3; pointer-events: none;
}
svg .spine { stroke: #9a9a9a; stroke-width: .2; stroke-dasharray: .8 .8; }
svg .ph { fill: url(#zf-hatch); stroke: #bdbdbd; stroke-width: .2; stroke-dasharray: .8 .8; }
svg .ph.faint { fill: none; stroke: #e2e2e2; }
svg .phl { font-family: 'UI'; fill: #8f8f8f; }
/* ячейки сетки: скрыты, видны при наведении на блок и во время перетаскивания/ресайза */
svg .row { fill: rgba(0, 0, 0, .025); stroke: #b8b8b8; stroke-width: .15; stroke-dasharray: .6 .6; opacity: 0; transition: opacity .12s; }
.page:has(.blk:hover) svg .row, body.dnd .page svg .row { opacity: 1; }
.swatches { display: flex; flex-wrap: wrap; gap: 4px; }
.sw { width: 26px; height: 26px; padding: 0; border: 1px solid var(--line); cursor: pointer; position: relative; display: block; }
.sw:hover { border-color: var(--ink); background-blend-mode: normal; }
.sw.on { outline: 2px solid var(--ink); outline-offset: 1px; }
.sw.custom input { opacity: 0; position: absolute; inset: 0; width: 100%; height: 100%; cursor: pointer; }
.sw.custom::after { content: '+'; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 13px; color: var(--ink); pointer-events: none; }

.blk { position: absolute; cursor: pointer; touch-action: none; }
.blk:hover { outline: 1px dashed var(--mute); }
.blk.sel { outline: 1px solid var(--ink); }
.blk.sel.pan { cursor: move; }
.blk.over { outline: 1px dashed var(--err); }
.blk .grip, .blk .rs { position: absolute; background: var(--ink); display: none; }
.blk:hover .grip, .blk.sel .grip { display: block; }
.blk .grip { left: -1px; top: -13px; height: 12px; padding: 0 5px; color: #fff; font-size: 9px; line-height: 12px; cursor: grab; width: auto; }
.blk .rs { width: 12px; height: 12px; right: -6px; bottom: -6px; cursor: nwse-resize; z-index: 2; }
.blk.sel .rs { display: block; }
.blk .badge {
  position: absolute; right: -1px; bottom: -15px; font-size: 9px; line-height: 13px; padding: 0 4px;
  background: #fff; border: 1px solid var(--line); color: var(--ink2); white-space: nowrap; display: none;
}
.blk:hover .badge, .blk.sel .badge, .blk.over .badge { display: block; }
.blk.over .badge { border-color: var(--err); color: var(--err); display: block; }
.drop-hint {
  position: absolute; inset: 12% 10%; display: flex; align-items: center; justify-content: center; text-align: center;
  color: var(--mute); font-size: 11px; border: 1px dashed var(--line); pointer-events: none;
}
/* превью места при перетаскивании; текст — размер в ячейках */
.ins {
  position: absolute; z-index: 5; pointer-events: none;
  outline: 2px solid var(--ink); background: rgba(43, 43, 43, .1);
  display: flex; align-items: flex-end; justify-content: flex-end;
}
.ins b { background: var(--ink); color: var(--bg); font-size: 10px; line-height: 14px; padding: 0 4px; }
.page.dropbad { outline: 2px solid var(--err); }

.ghost {
  position: fixed; z-index: 100; pointer-events: none; background: var(--ink); color: #fff; padding: 4px 8px;
  font-size: 11px; transform: translate(10px, 10px); box-shadow: 3px 3px 0 var(--mute);
}
.ghost.bad { background: var(--err); }

/* лист */
.sheetwrap { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.sheet { position: relative; background: #fff; outline: 1px solid var(--ink); }
.sheet svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.slot { position: absolute; cursor: pointer; }
.slot:hover { outline: 2px solid var(--ink); outline-offset: -2px; }
.slot span {
  position: absolute; left: 4px; top: 4px; background: var(--ink); color: #fff; font-size: 10px; padding: 0 4px; line-height: 14px;
}
.slot.rot span { left: auto; top: auto; right: 4px; bottom: 4px; }
/* подпись под листом: условные обозначения строкой, пояснение — отдельной строкой, не шире листа */
.legend { font-size: 11px; color: var(--ink2); display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; }
.legend .keys { display: flex; gap: 16px; }
.legend .keys span { white-space: nowrap; }
.legend .note { text-wrap: balance; }
.legend i { display: inline-block; width: 18px; border-top: 1px dashed var(--ink); vertical-align: middle; margin-right: 6px; }
.legend i.d { border-top-style: dotted; border-color: var(--ink2); }
.empty { text-align: center; display: flex; flex-direction: column; gap: 12px; align-items: center; color: var(--ink2); }

/* навигация по полосам */
#nav { border-top: 1px solid var(--ink); background: var(--bg); padding: 10px 14px; display: flex; gap: 14px; align-items: flex-end; overflow-x: auto; }
.nsp { display: flex; }
.thumb { position: relative; cursor: pointer; outline: 1px solid var(--line); background: #fff; }
.thumb + .thumb { margin-left: 1px; }
.thumb svg { display: block; }
.thumb:hover { outline-color: var(--ink2); }
.thumb.on { outline: 2px solid var(--ink); z-index: 1; }
.thumb.drop { outline: 2px dashed var(--ink); }
.thumb b { position: absolute; left: 0; bottom: 0; font-size: 9px; background: var(--ink); color: #fff; padding: 0 3px; line-height: 12px; font-weight: 400; }
.thumb.over b { background: var(--err); }
.navhint { color: var(--mute); font-size: 10px; margin-left: auto; align-self: center; white-space: nowrap; }

/* ---------- инспектор ---------- */
.sec { padding: 14px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.sec:last-child { border-bottom: 0; }
.ihead { display: flex; align-items: center; gap: 8px; }
.ihead b { font-size: 13px; }
.ihead button { width: 28px; padding: 0; }
.fld { display: flex; flex-direction: column; gap: 4px; }
.fld > label, .lbl { font-size: 10px; text-transform: uppercase; letter-spacing: .1em; color: var(--ink2); }
.sec .hint { font-size: 11px; line-height: 1.4; color: var(--ink2); }
.row2 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px; }
.chk { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.note { color: var(--ink2); font-size: 11px; }
.note code { background: var(--soft); padding: 0 3px; }
.meter { display: flex; flex-direction: column; gap: 4px; }
.cells { display: grid; grid-template-columns: repeat(24, 1fr); gap: 2px; }
.cells i { height: 8px; background: var(--soft); outline: 1px solid var(--line); outline-offset: -1px; }
.cells i.f { background: var(--ink); outline-color: var(--ink); }
.meter.over .cells i.f { background: var(--err); outline-color: var(--err); }
.meter.over .mtxt { color: var(--err); }
.mtxt { font-size: 11px; }
.thumbimg { width: 100%; height: 90px; object-fit: contain; border: 1px solid var(--line); display: block; filter: grayscale(1);
  /* шахматка под картинкой: видно, где у PNG прозрачность */
  background: repeating-conic-gradient(#eee 0 25%, #fff 0 50%) 0 0 / 12px 12px; }
.btns { display: flex; gap: 6px; flex-wrap: wrap; }
.kbd { color: var(--ink2); font-size: 11px; line-height: 1.7; }
.kbd b { color: var(--ink); font-weight: 400; background: var(--soft); padding: 0 4px; }

/* модалка */
#modal { position: fixed; inset: 0; background: rgba(255,255,255,.75); display: flex; align-items: center; justify-content: center; z-index: 50; }
#modal[hidden] { display: none; }
.dlg [hidden] { display: none; }
.dlg { background: #fff; border: 1px solid var(--ink); box-shadow: 6px 6px 0 var(--ink); width: 520px; max-width: calc(100vw - 32px); }
.dlg h2 { margin: 0; font: 700 16px 'Pix', 'UI'; padding: 12px 16px; border-bottom: 1px solid var(--ink); display: flex; justify-content: space-between; align-items: center; }
.dlg h2 button { border: 0; width: 28px; padding: 0; }
.dlg .body { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.opt { display: grid; grid-template-columns: 18px 1fr; gap: 2px 8px; cursor: pointer; border: 1px solid var(--line); padding: 10px; }
.opt:has(input:checked) { border-color: var(--ink); }
.opt input { margin-top: 2px; }
.opt small { grid-column: 2; color: var(--ink2); }
.warn { border: 1px solid var(--err); color: var(--err); padding: 8px 10px; font-size: 11px; }
.dlg .foot { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-top: 1px solid var(--line); }
.fold { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--ink); font-size: 11px; text-align: center; }
.fold span { border: 1px solid var(--line); padding: 6px 0; }
.fold span.r { transform: rotate(180deg); color: var(--ink2); }
.fold span.cut { border-bottom: 1px dashed var(--ink); }
.fold span.cut2 { border-top: 1px dashed var(--ink); }

#toast {
  position: fixed; left: 50%; bottom: 120px; transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: 8px 14px; font-size: 12px; z-index: 60; opacity: 0; transition: opacity .15s; pointer-events: none; box-shadow: 3px 3px 0 var(--mute);
}
#toast.show { opacity: 1; }
#toast.err { background: var(--err); }

@keyframes shake { 0%,100% { transform: translateX(0) } 25% { transform: translateX(-4px) } 75% { transform: translateX(4px) } }
.shake { animation: shake .2s 2; }

.colmeter { text-transform: none; letter-spacing: 0; color: var(--mute); }
.colmeter.bad { color: var(--err); }

.chip { display: inline-block; width: 9px; height: 9px; margin-right: 6px; vertical-align: -1px; outline: 1px solid rgba(0,0,0,.15); }

@media (max-width: 1100px) { .saved { display: none; } }
.seg .ic { width: 12px; height: 12px; display: inline-block; vertical-align: -2px; }
.seg.lang button { padding: 0 8px; font-size: 11px; }
#undo, #redo { width: 32px; min-width: 32px; padding: 0; display: inline-flex; align-items: center; justify-content: center; }
#undo .hist, #redo .hist { width: 12px; height: 12px; display: block; }
/* шапка на узком экране: кнопки не сжимаются, от логотипа остаётся квадратик, лишнее — прокруткой */
.top > *, .top .seg { flex-shrink: 0; }
.top > .title { flex-shrink: 1; }
.top { overflow-x: auto; scrollbar-width: none; }
.top::-webkit-scrollbar { display: none; }
@media (max-width: 1000px) {
  .logo { font-size: 0; gap: 0; }
  .logo i { margin-right: 4px; }
}
@media (max-width: 760px) {
  .top { gap: 8px; padding: 0 10px; }
  .top .seg button { padding: 0 6px; }
  .top > .title { min-width: 48px; }
}

/* ---------- телефон: страница на весь экран, палитра и свойства — нижними листами ---------- */
.mbar { display: none; }
@media (max-width: 760px) {
  body { grid-template-rows: 44px minmax(0, 1fr) auto; overscroll-behavior: none; }
  .top { height: 44px; }
  main {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: 'stage' 'sheet';
  }
  .stage { grid-area: stage; }
  .left, .right { grid-area: sheet; display: none; border: 0; border-top: 1px solid var(--ink); height: 52vh; height: 52dvh; }
  body.m-palette .left { display: flex; }
  body.m-inspector .right { display: flex; }
  .left { padding: 12px; }
  #palette { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .tile { padding: 10px 8px; gap: 8px; font-size: 11px; }
  .tile .ic { width: 20px; height: 20px; flex-shrink: 0; }

  #nav { padding: 6px 10px; gap: 8px; }
  #nav .thumb b { font-size: 8px; }

  .mbar { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--ink); background: var(--bg); padding-bottom: env(safe-area-inset-bottom); }
  .mbar button { border: 0; height: 48px; font-size: 13px; font-weight: 700; position: relative; }
  .mbar button + button { border-left: 1px solid var(--line); }
  .mbar button.on { background: var(--ink); color: var(--bg); }
  .mb-plus { font-size: 18px; line-height: 0; vertical-align: -2px; }
  .mb-dot { display: none; position: absolute; top: 12px; margin-left: 6px; width: 6px; height: 6px; background: currentColor; }
  body.has-sel .mb-dot { display: inline-block; }

  /* поля ввода 16px — иначе iPhone приближает страницу при фокусе */
  input, textarea, select { font-size: 16px; }

  .plabel { font-size: 9px; }
}
/* палец вместо мыши: крупнее ручки, сетка видна у выделенного блока (наведения нет) */
@media (pointer: coarse) {
  .blk .rs { width: 22px; height: 22px; right: -11px; bottom: -11px; }
  .blk .grip { height: 20px; line-height: 20px; top: -21px; font-size: 11px; padding: 0 8px; }
  .page:has(.blk.sel) svg .row { opacity: 1; }
}

@media (max-width: 760px) {
  .top > .title, #undo, #redo, #exportBtn { display: none; }
  .mbar { grid-template-columns: 1fr 1fr 44px 44px auto; }
  .mbar [data-proxy] { border-left: 1px solid var(--line); }
  .mbar [data-proxy="exportBtn"] { padding: 0 14px; }
  .mbar .hist { width: 14px; height: 14px; display: block; margin: auto; }
}
@media (max-width: 760px) {
  /* пока открыт нижний лист, миниатюры прячем — больше места странице */
  body.m-palette #nav, body.m-inspector #nav { display: none; }
}
