:root {
  --bg: #0f1115;
  --fg: #e6e6e6;
  --muted: #9aa1a9;
  --accent: #7aa2f7;
  --danger: #f7768e;
  --ok: #9ece6a;
  --warn: #e0af68;
  --text-box: #ff4d4d;
  --image-box: #4da6ff;
  --topbar-h: 48px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  padding-top: var(--topbar-h);
  max-width: 100vw;
  overflow-x: hidden;
}

.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1000; height: var(--topbar-h);
  display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: 10px;
  padding: 8px 12px; background: #11151b; border-bottom: 1px solid #1d2330;
}
.brand { font-weight: 600; letter-spacing: 0.3px; }
.controls { display: flex; gap: 12px; align-items: center; justify-content: center; }
.controls label { color: var(--muted); font-size: 14px; }
.controls[hidden] { display: none; }

/* Mobile menu overlay */
.menu-btn { appearance: none; border: 1px solid #2c3444; background: #1b2330; color: var(--fg);
  padding: 6px 10px; border-radius: 6px; font-size: 18px; line-height: 1; justify-self: end; }
.menu-panel {
  position: fixed; top: var(--topbar-h); right: 0; bottom: 0; width: min(320px, 86%);
  background: #0d1219; border-left: 1px solid #1d2330; box-shadow: -2px 0 14px rgba(0,0,0,0.35);
  transform: translateX(100%); transition: transform 0.2s ease; z-index: 1200; overflow-y: auto;
  padding: 14px;
}
body.menu-open .menu-panel { transform: translateX(0); }
.menu-panel .controls { display: grid; grid-auto-rows: min-content; gap: 10px; justify-content: start; }
.menu-panel .control-group, .menu-panel .solo { display: block; }
.menu-panel .control-group label { display: block; margin: 6px 0; }
.menu-panel select { width: 100%; background: #0f141c; border: 1px solid #242c3a; color: var(--fg); border-radius: 8px; padding: 8px; }
.menu-panel .nav-link { display: inline-block; margin-top: 8px; }
.autosave-status {
  justify-self: end;
  background: #1b2330;
  color: var(--fg);
  border: 1px solid #2c3444;
  padding: 6px 14px;
  border-radius: 6px;
  pointer-events: none;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  font-size: 13px;
  font-weight: 500;
  width: 200px;
  transition: background 0.2s ease, border-color 0.2s ease;
}
.autosave-status.warn { border-color: var(--warn); color: var(--warn); }
.autosave-status.err { border-color: var(--danger); color: var(--danger); }
.autosave-status.ok { border-color: #2f415a; color: var(--fg); }

.control-group { display: inline-flex; gap: 8px; align-items: center; padding: 2px 8px; border: 1px solid #242c3a; border-radius: 8px; background: #151b24; }
.control-group .child { opacity: 0.85; }
.control-group .child.disabled { opacity: 0.4; }

.pages { width: 100%; margin: 16px auto; padding: 0 12px 60px; }

.page { margin: 26px 0 32px; padding: 12px; background: #0c0f14; border: 1px solid #1b2230; border-radius: 10px; box-shadow: 0 2px 12px rgba(0,0,0,0.24); }
.page-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 10px; }
.page-title { font-weight: 600; }
.page-meta { color: var(--muted); font-size: 12px; }

.canvas-wrap { position: relative; display: inline-block; }
.page-image { width: 100%; max-width: 800px; height: auto; display: block; user-select: none; }
.overlay { position: absolute; left: 0; top: 0; right: 0; bottom: 0; pointer-events: auto; }

/* Hover-hide behaviour: dim overlays off-image but keep outlines visible */
body.hover-mode .overlay .box { opacity: 0.2; transition: opacity 0.15s ease; }
body.hover-mode .overlay .box .handle,
body.hover-mode .overlay .box .grip { display: none; }
body.hover-mode .overlay .box.active { opacity: 1; }
body.hover-mode .overlay .box.active .handle,
body.hover-mode .overlay .box.active .grip { display: block; }

.box { position: absolute; border: 2px solid var(--image-box); background: transparent; pointer-events: auto; }
.box.text { border-color: var(--text-box); }
.box.hidden { display: none; }

.box .handle { position: absolute; width: 12px; height: 12px; background: currentColor; border-radius: 2px; box-shadow: 0 1px 2px rgba(0,0,0,0.4); pointer-events: auto; }
.box .grip { position: absolute; left: -7px; top: -7px; width: 12px; height: 12px; border-radius: 50%; background: #ddd; border: 2px solid #000; cursor: move; pointer-events: auto; }
.box.text .handle { color: var(--text-box); }
.box.image .handle { color: var(--image-box); }
.box .handle.nw { left: -7px; top: -7px; cursor: nwse-resize; }
.box .handle.ne { right: -7px; top: -7px; cursor: nesw-resize; }
.box .handle.sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.box .handle.se { right: -7px; bottom: -7px; cursor: nwse-resize; }
.box .handle.n { left: calc(50% - 6px); top: -7px; cursor: ns-resize; }
.box .handle.s { left: calc(50% - 6px); bottom: -7px; cursor: ns-resize; }
.box .handle.w { left: -7px; top: calc(50% - 6px); cursor: ew-resize; }
.box .handle.e { right: -7px; top: calc(50% - 6px); cursor: ew-resize; }

.box .label { position: absolute; left: 0; top: -20px; background: rgba(0,0,0,0.55); color: #eee; font-size: 11px; padding: 2px 4px; border-radius: 4px; pointer-events: none; }
.box.selected { outline: 2px dashed #fff; outline-offset: 2px; z-index: 999; }

/* Label toggles */
body.labels-text-off .box.text .label { display: none; }
body.labels-image-off .box.image .label { display: none; }

/* Ctrl preview: full-text overlay with backdrop blur inside the box */
.box.text .ctrl-layer {
  position: absolute;
  left: 0; top: 0; right: 0; bottom: 0;
  display: none;
  pointer-events: none;
  color: #fff;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(8px) saturate(1.05);
  -webkit-backdrop-filter: blur(8px) saturate(1.05);
  mix-blend-mode: normal;
  z-index: 1000;
  padding: 6px 8px;
}
.box.text.ctrl-preview .ctrl-layer { display: block; }
.box.text .ctrl-text {
  position: absolute; left: 0; top: 0;
  transform-origin: 0 0;
  white-space: pre-wrap;
  word-break: break-word;
  text-shadow: 0 1px 2px rgba(0,0,0,0.9);
  line-height: 1.2;
}

.hint { color: var(--muted); font-size: 12px; margin-top: 6px; }
