:root {
  color-scheme: dark;
  --bg: #101012;
  --panel: #18181b;
  --panel-2: #212125;
  --line: #2c2c31;
  --line-strong: #45454d;
  --text: #e8e8ec;
  --muted: #8d8d97;
  --accent: #f2c94c;
  --accent-ink: #1b1605;
  --danger: #ef6b5e;
  --sheet: #1b1b1e;
  --mark: #b9b9c2;
  --radius: 7px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }
body { height: 100dvh; }

body {
  margin: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr); /* never wider than the screen */
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 var(--font);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

body[data-mode="editor"] .preview-only,
body[data-mode="preview"] .editor-only { display: none !important; }

/* Wording and controls differ between a mouse and a touch screen. */
.for-touch { display: none !important; }
@media (hover: none) {
  .for-mouse { display: none !important; }
  .for-touch { display: revert !important; }
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Toolbar ---------- */

/* Small screens: three compact rows.
     1. brand ............ Preview, Export
     2. page tabs (scroll) ..... Add page
     3. grid size ......... crop marks
   From 900px it is the single row of the desktop layout. */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 8px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.toolbar-break { flex-basis: 100%; height: 0; }
.toolbar > .brand { order: 1; }
.toolbar > .spacer { order: 2; }
.toolbar > #preview, .toolbar > #back { order: 3; }
.toolbar > #export { order: 4; }
.toolbar > .toolbar-break { order: 5; }
.toolbar > .tabs { order: 6; flex: 1 1 0; }
.toolbar > #add-page { order: 7; }
.toolbar > .toolbar-break ~ .toolbar-break { order: 8; }
.toolbar > .segmented { order: 9; }
.toolbar > .switch { order: 10; margin-left: auto; }
/* Preview has no tabs or grid: drop the empty row. */
body[data-mode="preview"] .toolbar-break ~ .toolbar-break { display: none; }

@media (min-width: 900px) {
  .toolbar { flex-wrap: nowrap; gap: 10px; padding: 8px 14px; }
  .toolbar > * { order: 0 !important; }
  .toolbar > .toolbar-break { display: none; }
  .toolbar > .tabs { flex: 0 1 auto; }
  .toolbar > .switch { margin-left: 0; }
}

.brand { display: flex; align-items: center; gap: 8px; margin-right: 8px; }
.brand-mark {
  width: 15px; height: 20px; border-radius: 3px;
  border: 2px solid var(--accent);
  box-shadow: inset 0 0 0 2px var(--panel), inset 0 0 0 10px color-mix(in srgb, var(--accent) 30%, transparent);
}
.brand-name { display: none; font-weight: 650; letter-spacing: 0.01em; white-space: nowrap; }
@media (min-width: 360px) { .brand-name { display: inline; } }
.wide-only { display: none; }
@media (min-width: 900px) { .wide-only { display: inline; } }

.spacer { flex: 1; }

.btn {
  appearance: none;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover:not(:disabled) { background: #2b2b30; border-color: #5a5a63; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-ghost { background: transparent; border-style: dashed; color: var(--muted); }
.btn-ghost:hover:not(:disabled) { color: var(--text); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: #f7d66e; border-color: #f7d66e; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #230a07; }
.btn-danger:hover:not(:disabled) { background: #f58377; border-color: #f58377; }

/* Grid size */
.segmented { display: flex; height: 32px; border: 1px solid var(--line-strong); border-radius: var(--radius); overflow: hidden; }
.segmented button {
  appearance: none; border: 0; padding: 0 10px;
  background: transparent; color: var(--muted);
  font: inherit; font-weight: 550; cursor: pointer;
}
.segmented button + button { border-left: 1px solid var(--line-strong); }
.segmented button:hover { color: var(--text); background: var(--panel-2); }
.segmented button[aria-pressed="true"] { background: var(--panel-2); color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.segmented button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* Page tabs */
.tabs { display: flex; gap: 4px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { flex: none; }
.tab {
  display: flex;
  align-items: center;
  height: 32px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--muted);
}
.tab:hover { background: var(--panel-2); color: var(--text); }
.tab.active { background: var(--panel-2); border-color: var(--line-strong); color: var(--text); }
.tab.active .tab-select { box-shadow: inset 0 -2px 0 var(--accent); }
.tab button {
  appearance: none; border: 0; background: none; color: inherit; font: inherit; cursor: pointer;
  height: 100%;
}
.tab-select { padding: 0 10px; font-weight: 550; white-space: nowrap; border-radius: var(--radius); }
.tab-delete {
  width: 22px; margin-right: 3px; padding: 0;
  display: grid; place-items: center;
  border-radius: 4px; opacity: 0.55;
}
.tab-delete:hover { opacity: 1; color: var(--danger); }
.tab-select:only-child { padding-right: 10px; }

/* Crop marks switch */
.switch { display: flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; white-space: nowrap; margin-right: 4px; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track {
  position: relative; width: 30px; height: 18px; border-radius: 9px;
  background: #3a3a41; transition: background 0.15s;
}
.switch-track::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px;
  border-radius: 50%; background: #c9c9d1; transition: transform 0.15s, background 0.15s;
}
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(12px); background: var(--accent-ink); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Stage ---------- */

.stage { min-height: 0; padding: 10px; display: grid; }
@media (min-width: 900px) { .stage { padding: 22px; } }
.sheet-holder { min-width: 0; min-height: 0; display: grid; place-items: center; overflow: hidden; }

.sheet { position: relative; flex: none; }
.sheet-editor {
  background: var(--sheet);
  box-shadow: 0 0 0 1px var(--line), 0 18px 50px rgba(0, 0, 0, 0.5);
}
.sheet-preview {
  background: #fff;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
}

.sheet-strip { display: flex; gap: 16px; max-width: 100%; overflow-x: auto; padding: 0 4px 6px; scroll-snap-type: x proximity; }
.sheet-figure { scroll-snap-align: center; }
@media (min-width: 900px) { .sheet-strip { gap: 28px; } }
.sheet-figure { margin: 0; flex: none; display: grid; gap: 8px; justify-items: center; }
.sheet-figure figcaption { font-size: 12px; color: var(--muted); }

.tick { position: absolute; background: var(--mark); pointer-events: none; }
.sheet-preview .tick { background: #000; }

.slot { position: absolute; overflow: hidden; }
.slot canvas { display: block; width: 100%; height: 100%; }

.sheet-editor .slot.empty {
  border: 1.5px dashed var(--line-strong);
  display: grid; place-items: center;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}
.sheet-editor .slot.empty:hover,
.sheet-editor .slot.empty:focus-visible { border-color: var(--muted); background: rgba(255, 255, 255, 0.025); }
.sheet-editor .slot.drag-over { border-color: var(--accent) !important; background: color-mix(in srgb, var(--accent) 9%, transparent) !important; }
.sheet-editor .slot.filled.drag-over::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  box-shadow: inset 0 0 0 3px var(--accent);
}
.slot-hint { display: grid; gap: 2px; text-align: center; color: var(--muted); pointer-events: none; padding: 4px; font-size: clamp(10px, 2.9vw, 13px); }
.slot-hint strong { font-weight: 600; color: #b4b4bd; }
.slot-hint span { font-size: 11px; }

.sheet-editor .slot.filled { cursor: grab; touch-action: none; }
.sheet-editor .slot.filled.panning { cursor: grabbing; }

.slot-btn, .seam-btn {
  appearance: none; padding: 0;
  display: grid; place-items: center;
  width: 24px; height: 24px; border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(16, 16, 18, 0.86);
  color: var(--text);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s, background 0.12s, color 0.12s;
}
.slot-btn { position: absolute; top: 6px; }
.slot-remove { right: 6px; }
.slot-move { left: 6px; cursor: grab; touch-action: none; }
.slot-move:hover { background: var(--accent); color: var(--accent-ink); }
.sheet-editor .slot.empty { touch-action: none; }

/* Carrying a pocket to a new place */
body.moving, body.moving * { cursor: grabbing !important; user-select: none; }
body.moving .seam, body.moving .slot-btn { display: none; }
.sheet-editor .slot.move-source {
  border: 0;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
}
.slot.move-source > * { visibility: hidden; }
.move-ghost {
  position: fixed; z-index: 10; pointer-events: none;
  background: var(--sheet);
  transform: scale(1.04) rotate(-1.5deg);
  box-shadow: 0 0 0 2px var(--accent), 0 18px 40px rgba(0, 0, 0, 0.65);
  opacity: 0.94;
}
.slot-split { left: 50%; margin-left: -12px; }
.slot:hover .slot-btn, .slot-btn:focus-visible { opacity: 1; }
.slot-remove:hover { background: var(--danger); color: #230a07; }
.slot-split:hover, .seam-btn:hover { background: var(--accent); color: var(--accent-ink); }

.seam { position: absolute; display: grid; place-items: center; z-index: 2; }
.seam:hover .seam-btn, .seam-btn:focus-visible { opacity: 1; }

/* ---------- Status bar, dialog, toast ---------- */

.statusbar {
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: 2px 16px;
  padding: 7px 10px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
}
.print-hint { color: #b4b4bd; }
/* Small screens: the print hint shows with the Preview, where it matters. */
body[data-mode="editor"] .print-hint { display: none; }
@media (min-width: 900px) {
  .statusbar { flex-wrap: nowrap; white-space: nowrap; gap: 16px; padding: 7px 14px; }
  body[data-mode="editor"] .print-hint { display: inline; }
  .print-hint { margin-left: auto; }
}

.dialog {
  border: 1px solid var(--line-strong); border-radius: 10px;
  background: var(--panel); color: var(--text);
  padding: 18px; max-width: min(340px, calc(100vw - 32px));
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.6);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.dialog p { margin: 0 0 16px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.toast {
  position: fixed; left: 50%; bottom: 46px;
  transform: translate(-50%, 8px);
  max-width: min(520px, 90vw);
  padding: 9px 14px;
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--panel-2); color: var(--text);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  opacity: 0; pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* Keep the print hint visible; the tips truncate first. */
.statusbar > span:not(.print-hint) { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.print-hint { flex: none; }

.slot { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }

/* ---------- Touch screens ---------- */

/* Nothing can be hovered, so the pocket controls are always there, and
   everything tappable gets a finger-sized target. */
@media (hover: none) {
  .slot-btn, .seam-btn { opacity: 1; }
  .seam-btn { opacity: 0.8; position: relative; }
  .slot-btn::before, .seam-btn::before, .tab-delete::before { content: ""; position: absolute; inset: -8px; }
  .tab-delete { position: relative; }
}
@media (pointer: coarse) {
  .btn, .segmented, .tab { height: 40px; }
  .btn { padding: 0 14px; }
  .segmented button { padding: 0 14px; }
  .tab-select { padding: 0 14px; }
  .tab-delete { width: 28px; }
  .switch { min-height: 40px; }
  .slot-btn { width: 28px; height: 28px; top: 4px; }
  .slot-remove { right: 4px; }
  .slot-move { left: 4px; }
  .slot-split { margin-left: -14px; }
}
