:root {
  --bg: #f4f3ee;
  --panel: #ffffff;
  --panel-2: #faf9f5;
  --ink: #24231f;
  --ink-soft: #6b6a62;
  --line: #dddbd0;
  --accent: #7a5c2e;
  --accent-ink: #fff;
  --shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 6px 18px rgba(0, 0, 0, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #201f1c;
    --panel: #2a2925;
    --panel-2: #262521;
    --ink: #ece9e0;
    --ink-soft: #a6a397;
    --line: #3b3a34;
    --accent: #d7b072;
    --accent-ink: #2a2115;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, a.btn-link {
  font: inherit;
  color: var(--ink);
}
button {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
a.btn-link {
  text-decoration: none;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
}

/* ---------- login ---------- */
.login-wrap {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 28px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card h1 { margin: 0 0 6px; font-size: 22px; font-weight: 700; }
.login-card h1 span { color: var(--accent); }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--ink-soft); }
.login-card input {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: var(--panel-2);
}
.login-card button {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  padding: 9px;
  border-radius: 6px;
  font-weight: 600;
}
.err { color: #b5341f; margin: 0; font-size: 13px; }

/* ---------- app shell ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 16px; }
.brand span { color: var(--accent); }
.topbar .search { flex: 1; }
.topbar .search input {
  width: 100%;
  max-width: 420px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 14px;
  background: var(--panel-2);
}
.topbar .user { display: flex; align-items: center; gap: 8px; }
.whoami { color: var(--ink-soft); font-size: 12px; margin-right: 4px; }
.signout { display: inline; margin: 0; }

.workspace {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: calc(100vh - 53px);
}
.tree {
  overflow: auto;
  background: var(--panel-2);
  border-right: 1px solid var(--line);
  padding: 10px 6px;
}
.tree ul { list-style: none; margin: 0; padding-left: 14px; }
.tree > ul { padding-left: 4px; }
.tnode {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.tnode:hover { background: rgba(122, 92, 46, .1); }
.tnode.active { background: var(--accent); color: var(--accent-ink); }
.tnode .fic { flex: none; }
.ttwisty {
  flex: none;
  width: 14px;
  height: 14px;
  margin-left: -2px;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  color: var(--ink-soft);
  display: grid;
  place-items: center;
}
.ttwisty::before {
  content: "";
  border-style: solid;
  border-width: 4px 0 4px 5px;
  border-color: transparent transparent transparent currentColor;
  transition: transform .12s ease;
}
.ttwisty.is-leaf { cursor: default; }
.ttwisty.is-leaf::before { border-color: transparent; }
li.is-open > .tnode > .ttwisty::before { transform: rotate(90deg); }
.tnode.active .ttwisty { color: var(--accent-ink); }
.tname { overflow: hidden; text-overflow: ellipsis; }
.tcount {
  margin-left: auto;
  font-size: 11px;
  color: var(--ink-soft);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 6px;
}
.tnode.active .tcount { color: var(--ink); }

/* ---------- document grid ---------- */
.grid {
  position: relative;
  overflow: auto;
  padding: 18px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 18px;
  align-content: start;
}
.grid .hint {
  grid-column: 1 / -1;
  color: var(--ink-soft);
  text-align: center;
  margin-top: 40px;
}
.grid.is-dropping {
  outline: 2px dashed var(--accent);
  outline-offset: -10px;
  background: rgba(122, 92, 46, .06);
}
.folder-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 2px;
}
.folder-head strong { font-size: 15px; }
.folder-head input[type="color"] {
  width: 34px;
  height: 24px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: none;
}
.folder-head .move-picker {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.folder-head .move-picker select {
  max-width: 220px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 8px;
  background: var(--panel-2);
}
.doc {
  margin: 0;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.doc .thumb {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: var(--shadow);
  aspect-ratio: 3 / 4;
  display: grid;
  place-items: center;
  overflow: hidden;
  transition: transform .08s ease;
}
.doc:hover .thumb { transform: translateY(-2px); border-color: var(--accent); }
.doc .thumb img { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.doc figcaption {
  font-size: 12px;
  line-height: 1.35;
  max-height: 2.7em;
  overflow: hidden;
}
.doc small { color: var(--ink-soft); font-size: 11px; }

/* ---------- freeform folder canvas ---------- */
.grid.is-free {
  display: block;
  align-content: initial;
}
.grid.is-free .doc.free {
  position: absolute;
  width: 160px;
  user-select: none;
  touch-action: none;
}
.grid.is-free .doc.free .thumb { transition: none; }
.grid.is-free .doc.free img { -webkit-user-drag: none; }
.grid.is-free .doc.free:hover { cursor: grab; }
.grid.is-free .doc.dragging {
  z-index: 10;
  opacity: .9;
  cursor: grabbing;
}

/* ---------- viewer ---------- */
.viewer {
  position: fixed;
  inset: 0;
  background: rgba(20, 19, 17, .82);
  display: flex;
  flex-direction: column;
  z-index: 50;
}
/* `display: flex` above beats the UA `[hidden] { display: none }` rule, so the
   overlay would never hide. Restore the hidden behaviour explicitly. */
.viewer[hidden] { display: none; }
.viewer-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.viewer-bar .spacer { flex: 1; }
.v-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40vw; }
.v-page { min-width: 74px; text-align: center; color: var(--ink-soft); }
.viewer-canvas {
  flex: 1;
  overflow: auto;
  display: grid;
  place-items: start center;
  padding: 24px;
}
#v-canvas {
  background: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .5);
  display: block;
}

/* ---------- annotations ---------- */
.v-tools { display: flex; align-items: center; gap: 4px; margin-right: 8px; }
.v-tools[hidden] { display: none; }
.v-tool { font-size: 12px; padding: 4px 8px; }
.v-tool[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.v-hist { font-size: 12px; padding: 4px 8px; }
.v-hist:disabled { opacity: .4; cursor: default; }
#v-undo { margin-left: 8px; }
#v-color { width: 30px; height: 26px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; }
.v-commit {
  margin-left: 8px;
  font-size: 12px;
  padding: 4px 8px;
  border-color: #b5762a;
  color: #8a5a20;
  font-weight: 600;
}
.v-commit:hover:not(:disabled) { background: #b5762a; color: #fff; border-color: #b5762a; }
.v-commit:disabled { opacity: .55; cursor: progress; }
@media (prefers-color-scheme: dark) {
  .v-commit { border-color: #d7b072; color: #e6c78e; }
  .v-commit:hover:not(:disabled) { background: #d7b072; color: #2a2115; border-color: #d7b072; }
}

.v-stage { position: relative; line-height: 0; }
.v-overlay { position: absolute; left: 0; top: 0; overflow: visible; touch-action: none; }
.v-overlay.is-idle { pointer-events: none; }
.v-overlay.is-idle .an { pointer-events: auto; }
.v-overlay:not(.is-idle) { cursor: crosshair; }
.v-overlay .an { cursor: pointer; }
.v-overlay .an.sel { outline: 1px dashed var(--accent); }

.v-html { position: absolute; inset: 0; pointer-events: none; }
.v-html > * { pointer-events: auto; }
.an-text {
  position: absolute;
  max-width: 48ch;
  font-family: Helvetica, Arial, sans-serif;
  line-height: 1.2;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  cursor: pointer;
  text-shadow: 0 1px 1px rgba(255, 255, 255, .35);
}
.an-text.sel { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 2px; }
.v-html.drawing .an-text { pointer-events: none; }
.an-del {
  position: absolute;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: #c0392b;
  color: #fff;
  font-size: 12px;
  line-height: 20px;
  cursor: pointer;
}
.an-del.an-edit { background: var(--accent); color: var(--accent-ink); }
.doc small .an-flag { color: var(--accent); }

@media (max-width: 720px) {
  .workspace { grid-template-columns: 1fr; }
  .tree { position: absolute; z-index: 5; width: 260px; height: calc(100vh - 53px); }
  .v-title { max-width: 30vw; }
}
