:root {
  --bg: #f7f7f8;
  --fg: #222;
  --muted: #666;
  --border: #ddd;
  --panel: #fff;
  --accent: #2563eb;
  --accent-fg: #fff;
  --c1: #000000; /* preto */
  --c2: #ffeb3b; /* amarelo */
  --c3: #ff5252; /* vermelho */
  --c4: #4caf50; /* verde */
  --c5: #2196f3; /* azul */
}

@font-face {
  font-family: "Open Sans";
  src: url("../fonts/OpenSans-Regular.ttf") format("truetype");
  font-style: normal;
  font-weight: 400;
  font-display: block;
}
@font-face {
  font-family: "Open Sans";
  src: url("../fonts/OpenSans-Semibold.ttf") format("truetype");
  font-style: normal;
  font-weight: 600;
  font-display: block;
}
@font-face {
  font-family: "Open Sans";
  src: url("../fonts/OpenSans-Italic.ttf") format("truetype");
  font-style: italic;
  font-weight: 400;
  font-display: block;
}
@font-face {
  font-family: "Open Sans";
  src: url("../fonts/OpenSans-SemiboldItalic.ttf") format("truetype");
  font-style: italic;
  font-weight: 600;
  font-display: block;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0; left: 0; right: 0;
  width: 100%;
  z-index: 100;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  flex-wrap: wrap;
}
.brand { font-weight: 700; text-decoration: none; color: var(--fg);
  font-size: 1.15rem;
  display: inline-flex; align-items: center; gap: 8px; }
.brand-icon { font-size: 1.4rem; line-height: 1; }
.crumbs a { text-decoration: none; color: var(--accent); }
.crumbs span { color: var(--muted); margin: 0 2px; }
main { flex: 1; padding: 16px; }

/* No viewer, a barra fica fora da região que rola. Isso evita a instabilidade
   de position: sticky do Safari iOS em documentos altos com canvas. */
body.viewer-page,
body.note-editor {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}
body.viewer-page main,
body.note-editor main {
  min-height: 0;
  padding: 0;
  overflow: hidden;
}

/* Index */
.panel { max-width: 900px; margin: 0 auto; background: var(--panel);
  border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
.panel-head { display: flex; justify-content: space-between; align-items: center;
  gap: 12px; flex-wrap: wrap; }
.panel-head h2 { margin: 0; font-size: 1.1rem; }
.actions { display: flex; gap: 8px; align-items: center; }
button, .btn-upload {
  background: var(--accent); color: var(--accent-fg); border: 0;
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 0.9rem;
}
button:hover, .btn-upload:hover { filter: brightness(1.1); }
.btn-upload { display: inline-block; }

.list { list-style: none; padding: 0; margin: 8px 0 16px; }
.row { display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  border-bottom: 1px solid var(--border); }
.row .icon { flex: 0 0 auto; width: 20px; text-align: center; }
.row .name { color: var(--fg); text-decoration: none; flex: 1 1 auto;
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .name:hover { color: var(--accent); }
.row .action { flex: 0 0 auto; color: var(--accent); text-decoration: none;
  font-size: 0.9rem; padding: 2px 6px; }
.row .action:first-of-type { margin-left: auto; }
.row .action:hover { text-decoration: underline; }
.row .del { flex: 0 0 auto; background: transparent;
  color: #888; font-size: 1rem; padding: 2px 8px; }
.row .del:hover { color: #d33; }
.empty { color: var(--muted); padding: 8px 10px; font-style: italic; }

/* Viewer */
/* A toolbar é um container flex único dentro da topbar. Os itens agrupados
   em .toolbar-group.tools e .toolbar-group.controls quebram para a próxima
   linha inteiros — nunca no meio. Assim conseguimos os 3 níveis:
     1. tudo cabe em 1 linha (brand + tools + controls)
     2. controls quebra para a 2ª linha
     3. tools também quebra → 3 linhas
*/
.viewer-toolbar { display: contents; }
.toolbar-group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.viewer-toolbar button { background: #eee; color: #222; padding: 4px 10px; }
.viewer-toolbar button.active { background: var(--accent); color: var(--accent-fg); }
.viewer-toolbar .tool { min-width: 34px; }
.viewer-toolbar .tool-icon { vertical-align: -2px; }
.page-info { min-width: 3.5em; text-align: center; }
.font-size-picker { display: inline-flex; align-items: center; gap: 4px;
  font-size: 0.9rem; color: var(--muted); }
.font-size-picker select { padding: 2px 4px; border: 1px solid var(--border);
  border-radius: 4px; background: #fff; font-size: 0.85rem; }
.status { color: var(--muted); font-size: 0.85rem; margin-left: 8px; }

.palette { display: flex; gap: 4px; }
.palette .swatch { width: 22px; height: 22px; border-radius: 50%;
  border: 2px solid transparent; cursor: pointer; padding: 0; flex: 0 0 auto; }
.palette .swatch.active { border-color: #222; }

#viewer-wrapper {
  height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  text-align: center;
}
#pdf-container { display: inline-flex; flex-direction: column; gap: 12px; padding: 16px; text-align: left; }
.page-wrap { position: relative; box-shadow: 0 2px 10px rgba(0,0,0,0.15);
  background: #fff; }
.page-wrap canvas.pdf-canvas { display: block; }
.page-wrap .ann-layer { position: absolute; inset: 0; pointer-events: none; }
.page-wrap .ann-layer > * { pointer-events: auto; }
.page-wrap canvas.draw-canvas { position: absolute; inset: 0; pointer-events: none; }
.page-wrap.tool-draw canvas.draw-canvas { pointer-events: auto; cursor: crosshair; touch-action: none; }
.page-wrap.tool-draw, .page-wrap.tool-arrow, .page-wrap.tool-highlight { touch-action: none; }
.page-wrap.tool-arrow { cursor: crosshair; }
.page-wrap.tool-highlight { cursor: crosshair; }
.page-wrap.tool-text { cursor: text; }

.ann-text {
  position: absolute; min-width: 60px; padding: 2px 4px 2px 14px;
  background: rgba(255,255,255,0.85); border: 1px dashed #999;
  font-size: 14px; white-space: pre-wrap; word-wrap: break-word;
  overflow-wrap: break-word; font-family: "Open Sans", sans-serif;
  font-weight: 400; font-synthesis: none; letter-spacing: 0;
}
.ann-text.selected { border-style: solid; }
.ann-text-body {
  display: block;
  width: 100%;
  min-width: 50px;
  min-height: 1em;
  margin: 0;
  padding: 0;
  border: 0;
  outline: none;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: inherit;
  font: inherit;
  line-height: inherit;
  letter-spacing: inherit;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}
.ann-handle {
  position: absolute; left: 1px; top: 0; bottom: 0;
  width: 12px; display: flex; align-items: center; justify-content: center;
  color: #888; cursor: move; user-select: none; font-size: 12px;
  opacity: 0; touch-action: none;
}
.ann-resize-r {
  position: absolute; right: -4px; top: 0; bottom: 0; width: 8px;
  cursor: ew-resize; touch-action: none; opacity: 0;
  background: rgba(37,99,235,0.35);
}
.ann-text:hover .ann-handle,
.ann-text.selected .ann-handle,
.ann-text:focus-within .ann-handle,
.ann-text:hover .ann-resize-r,
.ann-text.selected .ann-resize-r,
.ann-text:focus-within .ann-resize-r { opacity: 1; }
.ann-highlight {
  position: absolute; opacity: 0.35; pointer-events: none;
  border-radius: 2px;
}
.ann-delete-btn {
  position: absolute; top: -10px; right: -10px; width: 18px; height: 18px;
  background: transparent; color: #111; font-size: 20px;
  line-height: 1; text-align: center; cursor: pointer; padding: 0;
  display: none; align-items: center; justify-content: center;
  border: 0;
}
.ann-text:hover .ann-delete-btn,
.ann-text:focus-within .ann-delete-btn { display: flex; }

.ann-image {
  position: absolute;
  background: #fff;
  cursor: default;
  touch-action: none;
  user-select: none;
}
.ann-image.selected { cursor: move; }
.ann-image img { display: block; width: 100%; height: 100%; object-fit: fill; pointer-events: none; }
.ann-image-resize {
  display: none;
  position: absolute;
  right: -7px;
  bottom: -7px;
  width: 14px;
  height: 14px;
  border: 2px solid #fff;
  background: var(--accent);
  cursor: nwse-resize;
  touch-action: none;
}
.ann-image.selected .ann-image-resize { display: block; }
.ann-arrow { position: absolute; cursor: move; touch-action: none; }
.ann-arrow svg { display: block; width: 100%; height: 100%; overflow: visible; }
.ann-arrow line { pointer-events: stroke; }
.ann-arrow-draft { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

.page-wrap .ann-layer > .selection-box,
.selection-box {
  position: absolute; border: 2px dashed var(--accent);
  pointer-events: none; z-index: 5;
  background: rgba(37,99,235,0.05);
}
.sel-del {
  position: absolute; top: -20px; right: -20px;
  width: 22px; height: 22px;
  background: transparent; color: #111; padding: 0;
  font-size: 24px; line-height: 1; text-align: center;
  cursor: pointer; pointer-events: auto;
  border: 0;
  display: inline-flex; align-items: center; justify-content: center;
}

/* Caderno de anotações */
.note-toolbar { display: contents; }
.text-style-picker select {
  height: 28px;
  padding: 2px 4px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #fff;
}
.format-command.active { background: var(--accent); color: var(--accent-fg); }

#note-viewport {
  width: 100%;
  height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-x pan-y;
  background: #e5e7eb;
}
#note-document {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: max-content;
  min-width: 100%;
  min-height: 100%;
  padding: 16px;
  outline: none;
}
.note-document-frame {
  position: relative;
  flex: 0 0 auto;
}
.note-document-sheet {
  position: absolute;
  inset: 0 auto auto 0;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0,0,0,0.16);
  transform-origin: top left;
  touch-action: pan-x pan-y;
  cursor: default;
}
#note-document[data-tool="draw"] .note-document-sheet,
#note-document[data-tool="arrow"] .note-document-sheet,
#note-document[data-tool="highlight"] .note-document-sheet {
  cursor: crosshair;
  touch-action: none;
}
#note-document[data-tool="select"] .note-document-sheet { cursor: grab; }
#note-document[data-tool="draw"] .note-item,
#note-document[data-tool="arrow"] .note-item,
#note-document[data-tool="highlight"] .note-item { pointer-events: none; }

.note-document-text {
  --note-block-spacing: 8px;
  position: absolute;
  top: 44px;
  right: 48px;
  left: 48px;
  min-height: 1035px;
  z-index: 1;
  overflow: visible;
  outline: none;
  color: #111;
  font-family: "Open Sans", sans-serif;
  font-size: 12pt;
  font-style: normal;
  font-weight: 400;
  font-synthesis: none;
  font-kerning: normal;
  line-height: 1.35;
  letter-spacing: 0;
  overflow-wrap: break-word;
  white-space: normal;
  user-select: text;
  cursor: text;
}
.note-document-text p { margin: 0 0 var(--note-block-spacing); }
.note-document-text h1,
.note-document-text h2,
.note-document-text h3 { letter-spacing: 0; }
.note-document-text h1 { margin: 0 0 var(--note-block-spacing); font-size: 22pt; line-height: 1.2; }
.note-document-text h2 { margin: 0 0 var(--note-block-spacing); font-size: 18pt; line-height: 1.22; }
.note-document-text h3 { margin: 0 0 var(--note-block-spacing); font-size: 14pt; line-height: 1.25; }
.note-document-text strong { font-weight: 600; }
.note-document-text em { font-style: italic; }
.note-document-text .katex {
  cursor: text;
  font-size: 1.20em;
}
.note-document-text .katex * { pointer-events: none; }
.note-document-text .note-escaped-dollar { cursor: text; }
.note-document-text .note-empty-math { cursor: text; }
.note-document-text .note-caret-marker {
  display: inline-block;
  width: 0;
  overflow: hidden;
}
#note-document[data-tool="draw"] .note-document-text,
#note-document[data-tool="arrow"] .note-document-text,
#note-document[data-tool="highlight"] .note-document-text { pointer-events: none; }

.note-item {
  position: absolute;
  z-index: 2;
  touch-action: none;
  user-select: none;
}
.note-item svg,
.note-item img { display: block; width: 100%; height: 100%; }
.note-item img { object-fit: fill; pointer-events: none; }
.note-draw,
.note-arrow { cursor: move; }
.note-highlight { opacity: 0.35; border-radius: 2px; pointer-events: auto; }
.note-image { background: #fff; cursor: default; }
.note-image.selected { cursor: move; }
.note-resize-handle {
  display: none;
  position: absolute;
  right: -7px;
  bottom: -7px;
  width: 14px;
  height: 14px;
  border: 2px solid #fff;
  background: var(--accent);
  cursor: nwse-resize;
  touch-action: none;
}
.note-item.selected .note-resize-handle { display: block; }
.note-draft { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.note-document-sheet > .selection-box { z-index: 30; }

/* ------------------ responsivo (mobile) ------------------ */
@media (max-width: 720px) {
  main { padding: 10px; }

  .panel { padding: 10px; border-radius: 6px; }
  .panel-head { gap: 8px; }
  .panel-head h2 { font-size: 1rem; }
  .actions { gap: 6px; flex-wrap: wrap; }
  .row { padding: 10px 8px; gap: 6px; }
  .row .del { font-size: 1.1rem; padding: 6px 10px; }

  #note-document { padding: 12px; }
  .note-editor .toolbar-group.tools,
  .note-editor .toolbar-group.controls {
    flex: 1 1 100%;
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .note-editor .toolbar-group.tools::-webkit-scrollbar,
  .note-editor .toolbar-group.controls::-webkit-scrollbar { display: none; }
}

@media (max-width: 520px) {
  .note-editor .toolbar-group.tools { gap: 4px; }
  .note-editor .viewer-toolbar .tool { min-width: 26px; padding: 5px 3px; }
  .note-editor .text-style-picker select { width: 72px; padding: 4px 1px; }
  .note-editor .palette { gap: 3px; }
  .note-editor .palette .swatch {
    width: 18px;
    height: 18px;
    min-width: 18px;
    padding: 0;
  }
}

@media (max-width: 420px) {
  .viewer-toolbar button { min-width: 30px; padding: 6px; }
  .font-size-picker select { padding: 4px 2px; }
}
