:root {
  --page-w: 140px;
  --bg: #eef0f3;
  --panel: #ffffff;
  --line: #d7dbe2;
  --text: #1f2937;
  --muted: #616975;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --danger: #b91c1c;
  --radius: 8px;
}

* { box-sizing: border-box; }

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

body { display: flex; flex-direction: column; min-height: 100vh; }

h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 15px; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; line-height: 1.4; }
.hint { color: var(--muted); font-size: 13px; line-height: 1.45; }
.hint-top { margin-bottom: 16px; max-width: 900px; }
.grip-inline { color: #888; }

button {
  font: inherit;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 12px;
  cursor: pointer;
  line-height: 1.3;
  min-height: 34px;
}
button:hover { background: #f5f7fa; }
button:active { background: #e9edf2; }
button:focus-visible, input:focus-visible, select:focus-visible, [tabindex]:focus-visible, .thumb:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #1d4ed8; }
button.big { font-size: 16px; padding: 12px 20px; width: 100%; margin-top: 8px; }
button.wide { width: 100%; margin-top: 10px; }
button.danger-text { color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
select { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: #fff; max-width: 100%; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.topbar h1 { font-size: 18px; }
.views { display: flex; gap: 2px; background: #eef0f3; padding: 3px; border-radius: 8px; }
.views .tab { border: 0; background: transparent; padding: 5px 14px; border-radius: 6px; color: var(--muted); min-height: 30px; }
.views .tab.active { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr) 330px;
  flex: 1;
  min-height: 0;
}
.layout.print-view { grid-template-columns: minmax(0, 1fr); }
.layout.print-view .tray, .layout.print-view .inspector { display: none; }
.tray, .inspector { background: var(--panel); padding: 14px; overflow: auto; }
.tray { border-right: 1px solid var(--line); }
.inspector { border-left: 1px solid var(--line); }
.workspace { overflow: auto; padding: 24px; }

/* ---------- tray ---------- */
.tray-head { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.tray-empty {
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 18px 12px;
  color: var(--muted); font-size: 13px; text-align: center; line-height: 1.45;
}
.thumbs { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.thumb {
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  background: #e5e7eb;
  cursor: pointer;
  aspect-ratio: 1;
  border: 2px solid transparent;
}
.thumb:hover { border-color: var(--accent); }
.thumb img { display: block; width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.thumb .badge {
  position: absolute; left: 4px; bottom: 4px;
  background: rgba(37, 99, 235, .92); color: #fff;
  font-size: 11px; padding: 1px 6px; border-radius: 10px;
}
.thumb .del {
  position: absolute; top: 2px; right: 2px;
  width: 24px; height: 24px; min-height: 0; padding: 0; border-radius: 50%;
  background: rgba(255,255,255,.92); border: 0; font-size: 13px; line-height: 24px;
  opacity: 0;
}
.thumb:hover .del, .thumb:focus-within .del { opacity: 1; }
.thumb.dragging { opacity: .5; }
.tray.over { background: var(--accent-soft); }

/* ---------- booklet ---------- */
.view { display: none; }
.view.active { display: block; }

.booklet {
  display: flex;
  flex-wrap: wrap;
  gap: 34px 28px;
  align-items: flex-start;
  justify-content: center;
  padding-top: 26px;
}
.spread {
  display: flex;
  box-shadow: 0 6px 18px rgba(0,0,0,.14);
  border-radius: 2px;
  background: #fff;
}
.page {
  position: relative;
  width: var(--page-w);
  background: #fff;
  outline: 3px solid transparent;
  outline-offset: 2px;
  transition: outline-color .1s;
}
.page + .page { border-left: 1px dashed #c3c7cf; }
.page.selected { outline-color: var(--accent); z-index: 1; }
.page.over { outline-color: var(--accent); }
.page.over::after {
  content: ""; position: absolute; inset: 0; background: rgba(37,99,235,.18); pointer-events: none;
}
.page-head {
  position: absolute; top: -24px; left: 0; right: 0; height: 22px;
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted);
  cursor: grab; user-select: none; -webkit-user-select: none;
}
.page-head:active { cursor: grabbing; }
.page-head .grip { font-size: 15px; color: #9aa0a9; }
.page.selected .page-head { color: var(--accent); font-weight: 600; }
.page-canvas { display: block; width: 100%; touch-action: pan-y; cursor: grab; user-select: none; -webkit-user-select: none; }
.page-canvas:active { cursor: grabbing; }
.page.empty .page-canvas { cursor: pointer; }
.page-empty {
  position: absolute; inset: 0; display: none;
  align-items: center; justify-content: center; text-align: center;
  font-size: 12px; color: var(--muted); padding: 12px;
  pointer-events: none; border: 2px dashed var(--line); margin: 6px;
}
.page.empty .page-empty { display: flex; }
.page.dragging { opacity: .4; }
body.file-over .workspace, body.file-over .tray { outline: 3px dashed var(--accent); outline-offset: -6px; }

/* ---------- inspector ---------- */
.inspector h2 { margin-bottom: 8px; }
.editor-wrap { position: relative; background: #d9dde3; border-radius: var(--radius); overflow: hidden; }
#editor-canvas { display: block; width: 100%; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
#editor-canvas:active { cursor: grabbing; }
.editor-empty {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  text-align: center; padding: 24px; color: var(--muted); font-size: 13px; pointer-events: none;
}
.editor-wrap.empty .editor-empty { display: flex; }
.editor-wrap.empty #editor-canvas { cursor: default; }
#editor-hint { margin: 6px 0 10px; }
.field { margin: 12px 0; }
.field label { display: flex; justify-content: space-between; font-weight: 600; margin-bottom: 6px; }
.field output { font-weight: 400; color: var(--muted); }
input[type=range] { width: 100%; margin: 4px 0; }
.row { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }
.row button { flex: 1 1 auto; }
.tip { color: var(--muted); font-size: 12px; line-height: 1.45; margin-top: 18px; }
kbd { font-family: inherit; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; background: #f8f9fb; }
#insp-controls.disabled { opacity: .45; pointer-events: none; }
.notice { background: #f1f5ff; border: 1px solid #c7d7fb; border-radius: var(--radius); padding: 10px 12px; margin: 12px 0; font-size: 13px; line-height: 1.45; }
.notice .row { margin-bottom: 0; }
.warning { color: #92400e; background: #fef3c7; border-radius: 6px; padding: 8px 10px; font-size: 13px; line-height: 1.4; margin: 10px 0; }
.warning.strong { color: #991b1b; background: #fee2e2; }

/* ---------- print view ---------- */
.print-grid {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
  max-width: 1400px;
  margin: 0 auto;
}
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; }
.card h2 { margin-bottom: 12px; }
.settings-card .field { margin: 14px 0 6px; }
.settings-card .field label { display: block; }
.settings-card select { width: 100%; margin-bottom: 6px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 12px; margin: 0; }
legend { font-weight: 600; padding: 0 4px; }
fieldset label { display: block; margin: 6px 0; }
.check { display: block; margin: 12px 0; }
.sheet-wrap { background: #cfd3da; padding: 18px; border-radius: var(--radius); display: flex; justify-content: center; }
#sheet-canvas { display: block; max-width: 100%; box-shadow: 0 6px 18px rgba(0,0,0,.25); background: #fff; }
.legend { margin: 12px 0 0; color: var(--muted); font-size: 13px; line-height: 1.45; }
.legend-cut { color: #c33; }
.legend-fold { color: #5577bb; }
.plain { margin: 0; padding-left: 20px; line-height: 1.5; }
.plain li, .steps li { margin: 6px 0; }
.steps ol { margin: 0; padding-left: 22px; line-height: 1.5; }
.status { min-height: 1.3em; color: var(--accent); margin-top: 8px; }

/* ---------- dialogs ---------- */
dialog {
  border: 0; border-radius: 12px; padding: 22px 24px;
  width: min(600px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(17, 24, 39, .45); }
dialog h2 { margin-bottom: 12px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 16px 0 0; padding: 0; }
.help ol { padding-left: 22px; line-height: 1.5; }
.help li { margin: 6px 0; }
.help h3 { margin: 16px 0 8px; font-size: 14px; }
.help-sheet { width: 100%; height: auto; display: block; margin-bottom: 8px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,.3); max-width: calc(100vw - 32px); z-index: 50;
  font-size: 13px; line-height: 1.4;
}

/* ---------- responsive ---------- */
@media (max-width: 1240px) {
  :root { --page-w: 120px; }
  .layout { grid-template-columns: 220px minmax(0, 1fr) 300px; }
}
@media (max-width: 1000px) {
  .print-grid { grid-template-columns: 1fr; }
}
@media (max-width: 960px) {
  :root { --page-w: 112px; }
  /* Stack everything and let the whole page scroll (no height-constrained grid). */
  .layout { display: flex; flex-direction: column; min-height: auto; }
  .layout.print-view { display: flex; }
  .workspace, .tray, .inspector { overflow: visible; }
  .tray { border-right: 0; border-bottom: 1px solid var(--line); }
  .tray-head { flex-direction: row; align-items: center; justify-content: space-between; flex-wrap: wrap; }
  .thumbs { display: flex; overflow-x: auto; padding-bottom: 4px; }
  .thumb { flex: 0 0 76px; }
  .inspector { border-left: 0; border-top: 1px solid var(--line); }
  .editor-wrap { max-width: 420px; }
  .workspace { padding: 16px; }
}
@media (max-width: 480px) {
  :root { --page-w: 100px; }
  .booklet { gap: 28px 14px; }
  .topbar { gap: 10px; }
}
