:root {
  --bg: #eef0f4;
  --fg: #1d2330;
  --ac: #3b6cf6;
  --bd: #d6dae3;
}
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  font:
    14px/1.4 system-ui,
    -apple-system,
    Segoe UI,
    Roboto,
    sans-serif;
  background: var(--bg);
  color: var(--fg);
}
header,
#opts {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: #fff;
  border-bottom: 1px solid var(--bd);
  flex-wrap: wrap;
}
#bar {
  position: sticky;
  top: 0;
  z-index: 20;
}
#opts {
  gap: 14px;
  padding: 6px 14px;
  background: #f8f9fc;
}
#opts label {
  display: flex;
  align-items: center;
  gap: 5px;
}
#opts input[type='number'] {
  width: 60px;
}
#hint {
  color: #566075;
  margin-left: auto;
}
[hidden] {
  display: none !important;
}
.ic {
  margin-right: 4px;
}
#mhint {
  display: none;
}
.logo {
  font-size: 16px;
  margin-right: 8px;
}
.grow {
  flex: 1;
}
#tools,
#actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
button {
  font: inherit;
  border: 1px solid var(--bd);
  background: #fff;
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
}
button:hover {
  background: #f1f3f8;
}
button.on {
  background: #e4ecff;
  border-color: var(--ac);
  color: #1c3fa8;
}
button.primary {
  background: var(--ac);
  border-color: var(--ac);
  color: #fff;
}
button.primary:hover {
  background: #2f5ae0;
}
button.big {
  font-size: 18px;
  padding: 14px 28px;
  border-radius: 12px;
}
button:disabled {
  opacity: 0.4;
  cursor: default;
}
#welcome {
  text-align: center;
  padding: 12vh 20px;
}
#welcome h1 {
  font-size: 30px;
  margin: 0 0 10px;
}
.small {
  color: #566075;
  font-size: 13px;
}
#pages {
  padding: 18px 10px 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
}
.page {
  position: relative;
}
.ptools {
  position: absolute;
  right: -6px;
  top: 6px;
  transform: translateX(100%);
  display: flex;
  flex-direction: column;
  gap: 3px;
  opacity: 0;
  transition: opacity 0.15s;
}
.page:hover .ptools,
.page:focus-within .ptools {
  opacity: 1;
}
.ptools button {
  padding: 3px 7px;
  font-size: 13px;
}
.pnum {
  position: absolute;
  left: 0;
  top: -18px;
  font-size: 12px;
  color: #566075;
}
.sheet {
  position: relative;
  background: #fff;
  box-shadow: 0 2px 10px #0002;
  touch-action: pan-y pinch-zoom; /* let touch scroll the page */
}
.sheet canvas {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
.sheet canvas.ov {
  pointer-events: none;
}
.run {
  position: absolute;
  display: none;
  cursor: text;
  border-radius: 2px;
}
.tool-edit .run {
  display: block;
}
.tool-edit .run:hover {
  outline: 2px solid var(--ac);
  background: #3b6cf61a;
}
body:not(.tool-edit):not(.tool-none) .sheet {
  cursor: crosshair;
}
.tool-edit .sheet {
  cursor: default;
}
.editor {
  position: absolute;
  transform-origin: 0 0;
  border: 0;
  outline: 2px solid var(--ac);
  padding: 0;
  margin: 0;
  background: #fffc;
  white-space: pre;
  z-index: 5;
}
#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: #1d2330;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  z-index: 50;
  max-width: 90vw;
}
body.drag::after {
  content: 'Drop PDF to open';
  position: fixed;
  inset: 0;
  background: #3b6cf633;
  border: 4px dashed var(--ac);
  display: grid;
  place-items: center;
  font-size: 28px;
  z-index: 60;
}

/* Options row only matters for text tools */
.tool-draw #opts,
.tool-hl #opts,
.tool-white #opts,
.tool-rect #opts,
.tool-img #opts {
  display: none;
}

/* Drawing tools need to own touch gestures */
.tool-draw .sheet,
.tool-hl .sheet,
.tool-white .sheet,
.tool-rect .sheet {
  touch-action: none;
}

/* Phones and small tablets: tools in a bottom bar, within thumb reach */
@media (max-width: 800px) {
  header,
  #opts {
    padding: 6px 10px;
    gap: 6px;
  }
  header {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .logo,
  .grow,
  #hint {
    display: none;
  }
  #actions {
    flex-wrap: nowrap;
    width: 100%;
    justify-content: space-between;
  }
  #actions button {
    flex: none;
    min-height: 38px;
    min-width: 40px;
    white-space: nowrap;
  }
  #zin,
  #zout {
    display: none; /* pinch to zoom instead */
  }
  #save {
    position: sticky;
    right: 0;
    margin-left: auto;
  }
  #opts {
    flex-wrap: wrap;
    row-gap: 4px;
  }
  .long {
    display: none;
  }
  #opts select,
  #opts input[type='number'] {
    font-size: 16px; /* stops iOS zooming in on focus */
    min-height: 34px;
  }

  #tools {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    gap: 0;
    flex-wrap: nowrap;
    overflow-x: auto;
    background: #fff;
    border-top: 1px solid var(--bd);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    scrollbar-width: none;
  }
  #tools button {
    flex: 1 1 0;
    min-width: 0;
    width: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    border: 0;
    background: none;
    padding: 6px 0;
    font-size: 10.5px;
  }
  #tools button.on {
    background: #e4ecff;
    color: #1c3fa8;
  }
  #tools .ic {
    margin: 0;
    font-size: 22px;
    line-height: 1.1;
  }

  #mhint {
    display: block;
    position: fixed;
    left: 50%;
    bottom: calc(76px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    z-index: 25;
    margin: 0;
    padding: 6px 12px;
    max-width: 92vw;
    border-radius: 16px;
    background: #1d2330dd;
    color: #fff;
    font-size: 13px;
    text-align: center;
    pointer-events: none;
  }
  #toast {
    bottom: 130px;
  }

  #pages {
    padding: 10px 4px 150px;
    overflow-x: auto;
    align-items: flex-start;
    gap: 14px;
  }
  /* page label on the left, page buttons on the right, one compact row */
  .pnum {
    position: static;
    float: left;
    line-height: 32px;
    font-size: 13px;
  }
  .ptools {
    position: static;
    transform: none;
    flex-direction: row;
    opacity: 1;
    float: right;
    gap: 4px;
    margin-bottom: 4px;
  }
  .ptools button {
    min-width: 36px;
    min-height: 32px;
    padding: 2px 6px;
  }
  .sheet {
    clear: both;
  }
  #welcome h1 {
    font-size: 24px;
  }
}

#mhint:empty {
  display: none !important;
}

/* Keyboard focus is always visible */
:focus-visible {
  outline: 3px solid var(--ac);
  outline-offset: 2px;
}
.run:focus-visible {
  display: block;
  outline-offset: 0;
}

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

#toast button {
  margin-left: 8px;
  padding: 4px 10px;
  background: #fff;
  color: #1d2330;
  border-color: #fff;
}
