@import url("https://fonts.googleapis.com/css2?family=Special+Elite&family=Archivo+Black&family=IBM+Plex+Sans:wght@400;600;700&display=swap");

:root {
  --paper: #12161c;
  --ink: #e7edf6;
  --line: #4b5663;
  --accent: #8ec5ff;
  --acid: #24303a;
  --teal: #46c9cb;
  --app-bg: #0f1319;
  --panel-bg: #1a212a;
  --context-bg: #171e27;
  --stage-shell: #202a35;
  --page-bg: #ffffff;
  --topbar-bg: #151b23;
  --topbar-height: 82px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "IBM Plex Sans", sans-serif;
  min-height: 100vh;
  background:
    linear-gradient(0deg, rgba(255, 255, 255, 0.03), rgba(255, 255, 255, 0.03)),
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.025) 0 1px, transparent 1px 42px),
    var(--app-bg);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.8rem;
  padding: 0.9rem 1rem;
  min-height: var(--topbar-height);
  border-bottom: 2px solid #2b3642;
  background: var(--topbar-bg);
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.05), 0 10px 22px rgba(0, 0, 0, 0.42);
}

.topbar::after {
  content: none;
  position: absolute;
  left: 0;
  right: 0;
  bottom: -4px;
  height: 4px;
  background: repeating-linear-gradient(
    90deg,
    var(--line) 0 12px,
    transparent 12px 16px
  );
}

.topbar h1 {
  margin: 0;
  font-family: "Archivo Black", sans-serif;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: clamp(1rem, 2.3vw, 1.3rem);
}

.brand-title {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
}

.back-home-link {
  display: inline-flex;
  align-items: center;
}

.brand-logo {
  width: 72px;
  height: 72px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
  background: transparent;
  border: none;
  overflow: visible;
}

.brand-logo img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  object-position: center;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.45));
}

.header-actions {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
}

.autosave-status {
  font-size: 0.74rem;
  font-weight: 600;
  color: #8fa3bc;
  white-space: nowrap;
  padding: 0 0.3rem;
  cursor: default;
}

.autosave-status.warn {
  color: #ffcf70;
}

.project-menu {
  position: relative;
}

.project-menu summary {
  list-style: none;
  border: 2px solid var(--line);
  background: #202833;
  color: var(--ink);
  padding: 0.48rem 0.72rem;
  border-radius: 8px;
  cursor: pointer;
  font-family: "IBM Plex Sans", sans-serif;
  font-size: 0.86rem;
  font-weight: 700;
  text-transform: uppercase;
}

.project-menu summary::-webkit-details-marker {
  display: none;
}

.project-menu[open] .project-menu-items {
  display: grid;
}

.project-menu-items {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  min-width: 190px;
  z-index: 30;
  gap: 0.3rem;
  padding: 0.45rem;
  border: 1px solid #3a4757;
  background: #161f29;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.48);
}

.project-filename-row {
  display: grid;
  gap: 0.2rem;
  font-size: 0.72rem;
  color: #8fa3bc;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0 0.15rem 0.35rem;
  border-bottom: 1px solid #2b3847;
  margin-bottom: 0.1rem;
}

.project-filename-input {
  background: #0f1720;
  border: 1px solid #3a4757;
  border-radius: 5px;
  color: var(--ink);
  font-family: "IBM Plex Mono", monospace;
  font-size: 0.8rem;
  padding: 0.28rem 0.45rem;
  width: 100%;
  box-sizing: border-box;
}

button,
.file-upload,
.button-link {
  border: 2px solid var(--line);
  background: #202833;
  color: var(--ink);
  padding: 0.48rem 0.72rem;
  border-radius: 8px;
  cursor: pointer;
  font-family: "IBM Plex Sans", sans-serif;
  font-size: 0.86rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06), 0 3px 9px rgba(0, 0, 0, 0.4);
  transition: transform 90ms ease, box-shadow 90ms ease;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button:hover,
.file-upload:hover,
.button-link:hover {
  transform: translateY(-1px);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.08), 0 6px 14px rgba(0, 0, 0, 0.46);
}

button:active,
.file-upload:active,
.button-link:active {
  transform: translateY(1px);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.05), 0 2px 6px rgba(0, 0, 0, 0.5);
}

button.primary {
  background: var(--accent);
  color: #fff;
}

.layout {
  display: grid;
  grid-template-columns: minmax(270px, 340px) 1fr;
  gap: 1rem;
  padding: 1rem;
}

.sidebar {
  display: grid;
  gap: 0.8rem;
  align-content: start;
}

.mode-panel .mode-switch {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.35rem;
}

.mode-switch button.active {
  background: var(--teal);
  color: #fff;
}

.sidebar section {
  background: var(--panel-bg);
  border: 1px solid #2a3642;
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.38);
  border-radius: 10px;
  padding: 0.76rem;
}

.sidebar-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.35rem;
}

.sidebar-tab {
  font-size: 0.67rem;
  padding: 0.42rem 0.45rem;
}

.sidebar-tab::after {
  content: "+";
  float: right;
  opacity: 0.72;
}

.sidebar-tab.active {
  background: var(--acid);
}

.sidebar-tab.active::after {
  content: "−";
}

.sidebar-panel[hidden] {
  display: none;
}

.context-panel {
  background: var(--context-bg);
}

.context-state[hidden] {
  display: none;
}

.sidebar h2 {
  margin: 0 0 0.55rem;
  font-family: "Archivo Black", sans-serif;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-size: 0.8rem;
}

.controls {
  display: grid;
  gap: 0.45rem;
}

.basic-style-chips {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.35rem;
}

.basic-style-chips button {
  font-size: 0.7rem;
}

.advanced-only {
  display: grid;
  gap: 0.35rem;
}

body.basic-mode .advanced-only {
  display: none !important;
}

body.basic-mode #context-multi,
body.basic-mode #lock-toggle-btn,
body.basic-mode #panel-layout .controls:last-child,
body.basic-mode #panel-layout h2:has(+ .controls:last-child) {
  display: none !important;
}

.controls-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.35rem;
}

.controls label {
  display: grid;
  gap: 0.24rem;
  font-size: 0.84rem;
  font-weight: 600;
}

/* Space between stacked groups inside one sidebar panel. */
.sidebar-panel .controls + h2 {
  margin-top: 1rem;
}

.shape-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.35rem;
}

.align-row {
  display: grid;
  gap: 0.35rem;
  font-size: 0.84rem;
  font-weight: 600;
}

.align-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.35rem;
}

.align-buttons button.active {
  background: var(--teal);
  color: #fff;
}

input[type="text"],
select,
textarea,
input[type="range"],
input[type="color"] {
  border: 2px solid var(--line);
  border-radius: 0;
  background: #111820;
  color: var(--ink);
}

input[type="text"],
select {
  padding: 0.38rem 0.44rem;
  font-family: "IBM Plex Sans", sans-serif;
}

textarea {
  padding: 0.5rem;
  font-family: "IBM Plex Sans", sans-serif;
  resize: vertical;
}

.long-text-toolbar {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.3rem;
}

.long-text-toolbar button {
  min-height: 32px;
  padding: 0.35rem 0.4rem;
  font-size: 0.7rem;
}

.long-text-editor {
  min-height: 170px;
  max-height: 320px;
  overflow: auto;
  border: 2px solid var(--line);
  background: #111820;
  padding: 0.55rem;
  font-family: "IBM Plex Sans", sans-serif;
  line-height: 1.45;
  white-space: pre-wrap;
}

.long-text-editor:empty::before {
  content: "Paste or type long-form book text here.";
  color: #666;
}

.page-break-marker {
  border: 2px dashed var(--accent);
  background: #fff2f2;
  color: #7a0017;
  font-weight: 700;
  font-size: 0.75rem;
  padding: 0.25rem 0.4rem;
  margin: 0.35rem 0;
  display: block;
  user-select: none;
  cursor: grab;
}

.page-break-marker:active {
  cursor: grabbing;
}

input[type="file"] {
  display: none;
}

.file-upload {
  display: inline-block;
  text-align: center;
}

.page-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 0.42rem;
}

.page-list.thumbnail-mode {
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
}

.page-chip {
  border: 2px solid var(--line);
  background: #151d26;
  padding: 0.5rem;
  font-size: 0.78rem;
  display: grid;
  gap: 0.3rem;
  justify-items: start;
  box-shadow: 2px 2px 0 #000;
}

.page-chip-open {
  width: 100%;
  border: 0;
  background: transparent;
  box-shadow: none;
  padding: 0;
  text-transform: none;
  display: grid;
  gap: 0.16rem;
  justify-items: start;
  text-align: left;
}

.page-chip-open:hover,
.page-chip-open:active {
  transform: none;
  box-shadow: none;
}

.chip-actions {
  margin-top: 0.28rem;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.2rem;
}

.chip-actions button {
  width: 100%;
  padding: 0.28rem 0.3rem;
  font-size: 0.62rem;
  text-transform: uppercase;
}

.chip-actions button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.page-chip.active {
  background: var(--acid);
}

.page-chip.dragging {
  opacity: 0.45;
}

.page-chip.drop-target {
  outline: 2px dashed var(--teal);
  outline-offset: 2px;
}

.page-chip.flowed {
  box-shadow: 0 0 0 2px var(--teal), 2px 2px 0 #000;
}

.page-chip.flowed .role::after {
  content: " • Flow";
  color: #075f61;
  font-weight: 700;
}

.page-mode-controls .controls-row button.active {
  background: var(--teal);
  color: #fff;
}

.page-thumb {
  width: 100%;
  aspect-ratio: 11 / 17;
  border: 2px solid #354252;
  background: #10161d;
  background-size: cover;
  background-position: center;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18);
}

.page-thumb.ready {
  background-color: #fff;
}

.overflow-warning {
  color: #b00020;
  font-weight: 700;
}

.text-overflow-marker {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 10px;
  height: 10px;
  background: #d90429;
  border: 1px solid #fff;
  box-shadow: 0 0 0 1px #600;
  pointer-events: none;
}

.font-picker {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0;
}

.font-picker input[type="text"] {
  width: 100%;
  border-right: 0;
}

.font-picker-toggle {
  border: 2px solid var(--line);
  border-radius: 0;
  background: #111820;
  color: var(--ink);
  padding: 0 0.5rem;
  font-size: 0.7rem;
  box-shadow: none;
  text-transform: none;
}

.font-picker-toggle:hover,
.font-picker-toggle:active {
  transform: none;
  box-shadow: none;
}

.font-picker-list {
  position: absolute;
  top: calc(100% + 2px);
  left: 0;
  right: 0;
  z-index: 40;
  max-height: 280px;
  overflow-y: auto;
  background: #111820;
  border: 2px solid var(--line);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.55);
}

.font-picker-list[hidden] {
  display: none;
}

.font-picker-option {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.45rem 0.55rem;
  border: 0;
  border-bottom: 1px solid #1e2935;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  font-size: 1.02rem;
  text-transform: none;
  letter-spacing: 0;
  box-shadow: none;
  cursor: pointer;
}

.font-picker-option:hover,
.font-picker-option:focus,
.font-picker-option.highlighted {
  background: var(--acid);
  transform: none;
  box-shadow: none;
}

.font-picker-option.selected {
  color: var(--teal);
}

.font-picker-empty {
  padding: 0.5rem 0.55rem;
  font-size: 0.78rem;
  color: #8fa3bc;
}

.text-style-presets {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.35rem;
}

.page-chip .num {
  font-weight: 700;
  font-family: "Archivo Black", sans-serif;
  font-size: 0.74rem;
  letter-spacing: 0.03em;
}

.page-chip .role {
  font-size: 0.63rem;
  line-height: 1.14;
  opacity: 0.88;
}

.editor-wrap {
  display: grid;
  gap: 0.55rem;
  justify-items: center;
  position: sticky;
  top: var(--topbar-height);
  align-self: start;
  height: calc(100vh - var(--topbar-height));
  overflow: auto;
  padding-right: 0.2rem;
}

.workspace-toolbar {
  display: grid;
  grid-template-columns: auto 1fr auto auto auto auto auto;
  align-items: center;
  gap: 0.5rem;
  width: min(100%, 760px);
  padding: 0.35rem 0.45rem;
  border: 1px solid #334253;
  background: #1a2430;
}

.work-page-indicator {
  text-align: center;
  font-family: "Special Elite", monospace;
  font-size: 0.84rem;
  font-weight: 700;
  color: #d8e2ef;
}

.page-role-label {
  margin: 0;
  font-family: "Archivo Black", sans-serif;
  font-size: 0.86rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--accent);
  text-shadow: none;
}

.page-frame {
  background: var(--stage-shell);
  border: 1px solid #2c3947;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.42);
  border-radius: 12px;
  padding: clamp(10px, 2vw, 18px);
  width: min(100%, 760px);
  display: grid;
  place-items: center;
}

.page-stage {
  width: min(100%, 550px);
  aspect-ratio: 11 / 17;
  position: relative;
  background: var(--page-bg);
  border: 1px solid #324254;
  box-shadow: 0 0 0 4px #1a212a, 0 10px 24px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

#draw-canvas,
.items-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#draw-canvas {
  z-index: 2;
  touch-action: none;
}

.items-layer {
  pointer-events: auto;
  z-index: 1;
}

.guide-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.guide-box {
  position: absolute;
  pointer-events: none;
}

.guide-box::before {
  position: absolute;
  top: 2px;
  left: 2px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  background: rgba(255, 255, 255, 0.9);
  padding: 1px 4px;
}

.bleed-guide {
  inset: 10px;
  border: 1px dashed rgba(217, 4, 41, 0.72);
}

.bleed-guide::before {
  content: "BLEED";
  color: #b00020;
}

.margin-guide {
  inset: 24px;
  border: 1px dashed rgba(10, 147, 150, 0.78);
}

.margin-guide::before {
  content: "MARGIN";
  color: #075f61;
}

.safe-guide {
  inset: 44px;
  border: 2px solid rgba(34, 34, 34, 0.62);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.75);
}

.safe-guide::before {
  content: "SAFE ZONE";
  color: #0f1721;
}

.page-number-footer {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  z-index: 3;
  pointer-events: none;
  font-family: "Special Elite", monospace;
  font-size: 14px;
  font-weight: 700;
  color: #10151c;
}

.snap-guide {
  position: absolute;
  background: #ff2e63;
  opacity: 0.8;
  pointer-events: none;
  z-index: 9;
}

.snap-guide.v { width: 1px; top: 0; bottom: 0; left: 50%; }
.snap-guide.h { height: 1px; left: 0; right: 0; top: 50%; }

.onboarding-strip {
  width: min(100%, 760px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  background: #14351f;
  border: 1px solid #2c6a44;
  color: #c4ecd2;
  padding: 0.45rem 0.7rem;
  border-radius: 8px;
  font-size: 0.8rem;
}

.onboarding-strip[hidden] { display: none; }

.onboarding-strip button {
  background: transparent;
  border: 0;
  box-shadow: none;
  color: #c4ecd2;
  font-size: 0.9rem;
  padding: 0.2rem 0.4rem;
}

.resize-feedback {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  background: #111;
  color: #fff;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px #000;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  padding: 4px 6px;
  white-space: nowrap;
}

.selection-box {
  position: absolute;
  border: 2px dashed var(--teal);
  background: rgba(10, 147, 150, 0.12);
  pointer-events: none;
  z-index: 8;
}

.item-context-menu {
  position: absolute;
  z-index: 20;
  display: grid;
  gap: 0.2rem;
  min-width: 170px;
  padding: 0.35rem;
  border: 1px solid #3a4757;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.55);
  background: #19222d;
}

.item-context-menu[hidden] {
  display: none;
}

.item-context-menu button {
  text-align: left;
  font-size: 0.68rem;
  padding: 0.32rem 0.45rem;
}

.page-item {
  position: absolute;
  user-select: none;
  touch-action: none;
  -webkit-user-select: none;
}

.page-item[contenteditable="true"] {
  touch-action: auto;
  user-select: text;
  -webkit-user-select: text;
}

.page-item.selected {
  outline: 2px dashed var(--accent);
  outline-offset: 2px;
}

.page-item.locked {
  opacity: 0.97;
  cursor: not-allowed !important;
}

.lock-indicator {
  position: absolute;
  top: -11px;
  left: -9px;
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  background: #e94a6b;
  color: #fff;
  border: 2px solid #fff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.55);
  pointer-events: none;
  z-index: 7;
}

.page-item.text {
  min-width: 80px;
  min-height: 24px;
  padding: 2px 4px;
  white-space: pre-wrap;
  cursor: move;
}

.page-item.text[contenteditable="true"] {
  cursor: text;
  outline: 2px solid var(--teal);
}

.page-item.image,
.page-item.shape {
  cursor: move;
}

.page-item.image {
  overflow: visible;
}

.page-item.image .image-content {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
  user-select: none;
}

.page-item.image.resizing {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.page-item.shape.circle {
  border-radius: 999px;
}

.page-item.shape.triangle {
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}

.resize-handle {
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 0;
  background: #ff2e63;
  border: 2px solid #fff;
  cursor: nwse-resize;
  box-shadow: 0 0 0 2px #000;
  touch-action: none;
  z-index: 6;
}

.resize-handle.nw {
  left: -8px;
  top: -8px;
  cursor: nwse-resize;
}

.resize-handle.ne {
  right: -8px;
  top: -8px;
  cursor: nesw-resize;
}

.resize-handle.sw {
  left: -8px;
  bottom: -8px;
  cursor: nesw-resize;
}

.resize-handle.se {
  right: -8px;
  bottom: -8px;
  cursor: nwse-resize;
}

.small-note {
  font-size: 0.75rem;
  color: #b4c0cf;
}

.pencil-only-toast.palm-indicator {
  top: auto;
  bottom: 1rem;
}

.pencil-only-toast {
  position: fixed;
  top: calc(var(--topbar-height) + 0.5rem);
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  background: #fff3cd;
  color: #7a4e00;
  border: 1px solid #e7cc84;
  padding: 0.35rem 0.85rem;
  border-radius: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
}

.hint {
  margin: 0;
  color: #dfe8f5;
  font-size: 0.84rem;
  font-weight: 700;
  background: var(--acid);
  padding: 0.2rem 0.45rem;
  border: 1px solid #354252;
}

.flow-stepper {
  display: grid;
  gap: 0.22rem;
  border: 1px dashed #3d4f63;
  padding: 0.45rem 0.55rem;
  background: #121a23;
}

.flow-stepper span {
  font-size: 0.72rem;
  color: #cbd7e5;
}

.export-dialog {
  width: min(460px, 94vw);
  border: 1px solid #3a4757;
  border-radius: 10px;
  padding: 0;
  background: #161f29;
  color: var(--ink);
  box-shadow: 0 16px 30px rgba(0, 0, 0, 0.6);
}

.export-dialog::backdrop {
  background: rgba(0, 0, 0, 0.62);
}

.export-shell {
  display: grid;
  gap: 0.7rem;
  padding: 1.1rem;
}

.export-shell h2 {
  margin: 0;
  font-family: "Archivo Black", sans-serif;
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
}

.export-option {
  display: flex;
  gap: 0.65rem;
  align-items: flex-start;
  padding: 0.65rem 0.75rem;
  border: 2px solid #2f3b48;
  border-radius: 8px;
  background: #111820;
  cursor: pointer;
}

.export-option:has(input:checked) {
  border-color: var(--teal);
}

.export-option:has(input:disabled) {
  opacity: 0.5;
  cursor: not-allowed;
}

.export-option input {
  margin-top: 0.2rem;
}

.export-option span {
  display: grid;
  gap: 0.2rem;
}

.export-option strong {
  font-size: 0.88rem;
}

.export-option small {
  font-size: 0.76rem;
  color: #9db5cc;
  line-height: 1.4;
}

.export-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.templates-dialog {
  width: min(720px, 94vw);
  max-height: 88vh;
  border: 1px solid #3a4757;
  border-radius: 10px;
  padding: 0;
  background: #161f29;
  color: var(--ink);
  box-shadow: 0 16px 30px rgba(0, 0, 0, 0.6);
}

.templates-dialog::backdrop {
  background: rgba(0, 0, 0, 0.62);
}

.templates-shell {
  display: grid;
  gap: 0.75rem;
  padding: 1.1rem;
}

.templates-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.templates-head h2 {
  margin: 0;
  font-family: "Archivo Black", sans-serif;
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
}

.templates-save {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.5rem;
}

.templates-save input {
  min-width: 0;
}

.templates-kind {
  grid-column: 1 / -1;
  display: grid;
  gap: 0.3rem;
  margin: 0;
  padding: 0.5rem 0.7rem 0.6rem;
  border: 1px solid #2f3b48;
  border-radius: 8px;
}

.templates-kind legend {
  padding: 0 0.3rem;
  font-size: 0.72rem;
  color: #8fa3bc;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.templates-kind label {
  display: flex;
  align-items: baseline;
  gap: 0.45rem;
  font-size: 0.8rem;
  line-height: 1.35;
  cursor: pointer;
}

.templates-kind input {
  flex: none;
}

.templates-list {
  display: grid;
  gap: 0.6rem;
  max-height: 52vh;
  overflow-y: auto;
}

.templates-empty {
  margin: 0;
  padding: 1rem;
  text-align: center;
  font-size: 0.82rem;
  color: #9db5cc;
  border: 1px dashed #3a4757;
  border-radius: 8px;
}

.template-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-areas:
    "thumbs meta"
    "thumbs actions";
  gap: 0.4rem 0.8rem;
  align-items: start;
  padding: 0.6rem;
  border: 1px solid #2f3b48;
  border-radius: 8px;
  background: #111820;
}

.template-thumbs {
  grid-area: thumbs;
  display: flex;
  gap: 0.3rem;
}

.template-thumbs figure {
  margin: 0;
  display: grid;
  gap: 0.15rem;
  justify-items: center;
}

.template-thumbs {
  flex-wrap: wrap;
  max-width: 220px;
}

.template-thumbs img {
  width: 48px;
  aspect-ratio: 11 / 17;
  object-fit: cover;
  background: #fff;
  border: 1px solid #3a4757;
}

.template-thumbs img.wide {
  width: 100px;
}

.template-thumbs figcaption {
  font-size: 0.56rem;
  color: #8fa3bc;
  white-space: nowrap;
}

.template-meta {
  grid-area: meta;
  display: grid;
  gap: 0.1rem;
}

.template-meta strong {
  font-size: 0.92rem;
  overflow-wrap: anywhere;
}

.template-meta small {
  font-size: 0.72rem;
  color: #8fa3bc;
}

.template-actions {
  grid-area: actions;
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.template-actions button {
  font-size: 0.68rem;
  padding: 0.35rem 0.55rem;
}

.templates-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

@media (max-width: 600px) {
  .template-card {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "meta" "thumbs" "actions";
  }
}

#preview-dialog {
  width: min(780px, 96vw);
  border: 1px solid #3a4757;
  border-radius: 0;
  padding: 0;
  box-shadow: 0 16px 30px rgba(0, 0, 0, 0.6);
}

#preview-dialog::backdrop {
  background: rgba(0, 0, 0, 0.62);
}

.preview-shell {
  padding: 1rem;
  background: #161f29;
  color: var(--ink);
}

.preview-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.flipbook-wrap {
  margin-top: 0.5rem;
  display: grid;
  grid-template-columns: 42px 1fr 42px;
  align-items: center;
  gap: 0.6rem;
}

.flipbook-spread {
  width: min(100%, 700px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
  align-items: center;
}

.flipbook-page {
  width: 100%;
  aspect-ratio: 11 / 17;
  border: 1px solid #3a4757;
  background-color: #fff;
  background-size: cover;
  background-position: center;
  box-shadow: 0 10px 18px rgba(0, 0, 0, 0.45);
  transform-origin: left center;
  transition: transform 230ms ease;
}

.flipbook-page.blank {
  opacity: 0.35;
  background-image: linear-gradient(135deg, #f0f0f0 25%, #fafafa 25%, #fafafa 50%, #f0f0f0 50%, #f0f0f0 75%, #fafafa 75%);
  background-size: 20px 20px;
}

.flipbook-page.turn {
  transform: rotateY(-14deg);
}

.flip-index {
  text-align: center;
  margin: 0.6rem 0 0;
  font-family: "Special Elite", monospace;
  font-size: 0.85rem;
}

@media (max-width: 940px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: flex-start;
  }

  .editor-wrap {
    position: static;
    max-height: none;
    overflow: visible;
    padding-right: 0;
  }

  .workspace-toolbar {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .sidebar-tab {
    font-size: 0.78rem;
    min-height: 44px;
  }

  .project-menu-items {
    position: static;
    margin-top: 0.35rem;
  }
}

body.mobile-simplified #copy-page-btn,
body.mobile-simplified .page-mode-controls,
body.mobile-simplified #context-multi,
body.mobile-simplified #lock-toggle-btn,
body.mobile-simplified #align-selected-left-btn,
body.mobile-simplified #align-selected-center-btn,
body.mobile-simplified #align-selected-right-btn,
body.mobile-simplified #distribute-selected-h-btn,
body.mobile-simplified #distribute-selected-v-btn,
body.mobile-simplified #delete-multi-btn,
body.mobile-simplified .text-style-presets,
body.mobile-simplified #font-status {
  display: none !important;
}

body.mobile-simplified .header-actions {
  display: none;
  gap: 0.3rem;
}

body.mobile-simplified.mobile-header-expanded .header-actions {
  display: flex;
  width: 100%;
  margin-top: 0.35rem;
}

body.mobile-simplified .header-actions button {
  padding: 0.6rem 0.7rem;
  font-size: 0.84rem;
}

.mobile-header-toggle {
  display: none;
}

body.mobile-simplified .topbar {
  flex-wrap: wrap;
  align-items: center;
}

body.mobile-simplified .mobile-header-toggle {
  display: inline-flex;
  margin-left: auto;
}

body.mobile-simplified .sidebar-tabs {
  grid-template-columns: 1fr;
}

body.mobile-simplified .sidebar-tab {
  min-height: 46px;
  font-size: 0.86rem;
}

/* Touch device enhancements (tablets) */
@media (pointer: coarse) {
  .resize-handle {
    width: 26px;
    height: 26px;
  }

  .resize-handle.se {
    right: -13px;
    bottom: -13px;
  }

  button,
  .file-upload,
  .button-link {
    min-height: 42px;
  }

  .sidebar-tab {
    min-height: 44px;
    font-size: 0.74rem;
  }

  .item-context-menu button {
    min-height: 40px;
    font-size: 0.76rem;
  }

  .page-chip-open {
    min-height: 44px;
  }

  input[type="range"] {
    min-height: 30px;
  }

  input[type="color"] {
    min-width: 44px;
    min-height: 36px;
  }
}

/* Tablet layout: keep two columns but tighten the sidebar */
@media (min-width: 941px) and (max-width: 1180px) {
  .layout {
    grid-template-columns: minmax(240px, 280px) 1fr;
    gap: 0.7rem;
    padding: 0.7rem;
  }

  .sidebar-tab {
    font-size: 0.62rem;
    padding: 0.4rem 0.35rem;
  }
}
