/* Docs — the same tokens the hub tile page uses, so the app looks like it
   belongs to the hub rather than like a third-party thing bolted on. */
:root {
  --bg: #f6f7fb; --card: #ffffff; --text: #16181d; --muted: #6b7280;
  --line: #e5e7eb; --accent: #4f6df5; --ok: #10b981; --err: #dc2626;
  --warn: #b45309; --sunk: #eef0f6;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 8px 24px rgba(16,24,40,.06);
  --sheet: 0 -8px 40px rgba(16,24,40,.18);
  --bar: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216; --card: #1a1d23; --text: #eceef2; --muted: #9aa1ad;
    --line: #2a2e36; --sunk: #14171c;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
    --sheet: 0 -8px 40px rgba(0,0,0,.55);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden, [hidden] { display: none !important; }

/* ------------------------------------------------------------------ chrome */

.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--bar);
  display: flex; align-items: center; gap: 8px; padding: 0 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; margin: 0; letter-spacing: -.01em; flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iconbtn {
  border: 0; background: none; color: var(--text); width: 38px; height: 38px;
  border-radius: 10px; display: grid; place-items: center; font-size: 17px;
}
.iconbtn:hover { background: var(--sunk); }
.iconbtn[aria-pressed="true"] { background: var(--accent); color: #fff; }
.iconbtn:disabled { opacity: .35; cursor: default; }

main { padding: 14px 14px calc(96px + var(--safe-b)); max-width: 1100px; margin: 0 auto; }

/* ----------------------------------------------------------------- library */

.searchrow { display: flex; gap: 8px; margin-bottom: 12px; }
.search {
  flex: 1; display: flex; align-items: center; gap: 8px; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: 0 12px;
}
.search input { flex: 1; border: 0; background: none; outline: none; padding: 11px 0; min-width: 0; }
/* type=search brings its own clear button; the row already has one. */
.search input::-webkit-search-cancel-button, .search input::-webkit-search-decoration { display: none; }
.search .clear { border: 0; background: none; color: var(--muted); font-size: 18px; padding: 0 2px; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 12px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--card); color: var(--muted);
  border-radius: 999px; padding: 6px 14px; font-size: 13.5px; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.doc {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  overflow: hidden; box-shadow: var(--shadow); position: relative; text-align: left;
  padding: 0; display: flex; flex-direction: column; color: inherit;
}
.doc:active { transform: scale(.985); }
.doc .thumb {
  aspect-ratio: 3 / 4; background: var(--sunk); display: grid; place-items: center;
  overflow: hidden; border-bottom: 1px solid var(--line);
}
.doc .thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; }
.doc .thumb .ph { font-size: 30px; opacity: .35; }
.doc .meta { padding: 9px 10px 11px; }
.doc .name { font-size: 13.5px; font-weight: 600; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.doc .sub { font-size: 11.5px; color: var(--muted); margin-top: 3px; }
.doc .snip { font-size: 11.5px; color: var(--muted); margin-top: 6px; border-top: 1px dashed var(--line);
  padding-top: 6px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.doc .snip mark { background: color-mix(in srgb, var(--accent) 26%, transparent); color: inherit; border-radius: 3px; }
.doc .pick {
  position: absolute; top: 8px; left: 8px; width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid #fff; background: rgba(0,0,0,.28); display: none; place-items: center;
  color: #fff; font-size: 14px; box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
body.picking .doc .pick { display: grid; }
.doc[aria-selected="true"] .pick { background: var(--accent); }
.doc[aria-selected="true"] { outline: 2px solid var(--accent); outline-offset: -1px; }
.doc .more {
  position: absolute; top: 6px; right: 6px; width: 28px; height: 28px; border-radius: 8px;
  border: 0; background: color-mix(in srgb, var(--card) 80%, transparent); color: var(--muted);
  backdrop-filter: blur(6px);
}
body.picking .doc .more { display: none; }

.section-title { font-size: 12.5px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); margin: 18px 2px 10px; font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 56px 20px; }
.empty .big { font-size: 40px; margin-bottom: 10px; }
.empty p { margin: 6px 0; font-size: 14.5px; }

.usage { display: flex; align-items: center; gap: 10px; margin: 20px 2px 0; font-size: 12.5px; color: var(--muted); }
.usage .track { flex: 1; height: 5px; background: var(--sunk); border-radius: 999px; overflow: hidden; }
.usage .fill { height: 100%; background: var(--accent); }

/* --------------------------------------------------------------------- fab */

.fab {
  position: fixed; right: 18px; bottom: calc(20px + var(--safe-b)); z-index: 40;
  width: 58px; height: 58px; border-radius: 20px; border: 0; background: var(--accent);
  color: #fff; font-size: 28px; line-height: 1; box-shadow: 0 6px 22px rgba(79,109,245,.42);
}

/* ------------------------------------------------------------ sheet/dialog */

.scrim { position: fixed; inset: 0; background: rgba(10,12,16,.45); z-index: 50; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  background: var(--card); border-radius: 20px 20px 0 0; box-shadow: var(--sheet);
  padding: 10px 12px calc(14px + var(--safe-b)); max-height: 86vh; overflow: auto;
  max-width: 560px; margin: 0 auto;
}
.sheet .grab { width: 38px; height: 4px; border-radius: 999px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { font-size: 16px; margin: 2px 6px 12px; }
.opt {
  display: flex; align-items: center; gap: 14px; width: 100%; border: 0; background: none;
  padding: 13px 8px; border-radius: 12px; text-align: left; color: inherit;
}
.opt:hover { background: var(--sunk); }
.opt .ic { width: 40px; height: 40px; border-radius: 12px; background: var(--sunk); display: grid;
  place-items: center; font-size: 19px; flex: none; }
.opt .t { font-size: 15px; font-weight: 600; }
.opt .d { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.opt.danger .t { color: var(--err); }

.field { margin: 10px 4px 14px; }
.field label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.field input, .field textarea, .field select {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 11px;
  background: var(--bg); outline: none;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field textarea { min-height: 180px; resize: vertical; line-height: 1.55; }
.row { display: flex; gap: 8px; }
.btn {
  flex: 1; padding: 12px; border: 0; border-radius: 12px; background: var(--accent);
  color: #fff; font-weight: 600; font-size: 15px;
}
.btn.ghost { background: var(--sunk); color: var(--text); }
.btn.danger { background: var(--err); }
.btn:disabled { opacity: .55; }

/* ------------------------------------------------------------------ viewer */

#viewer { display: flex; flex-direction: column; height: 100dvh; }
#pages {
  flex: 1; overflow: auto; padding: 12px 10px calc(80px + var(--safe-b));
  background: var(--sunk); scroll-behavior: smooth;
}
.pagewrap {
  position: relative; margin: 0 auto 14px; background: #fff; box-shadow: var(--shadow);
  width: fit-content; border-radius: 3px; overflow: hidden;
}
.pagewrap canvas { display: block; max-width: 100%; height: auto; }
/* An invisible copy of the page's text, positioned over the picture of it, so
   selection and copy work the way they do in any other PDF reader. */
.textlayer {
  position: absolute; inset: 0; overflow: hidden; opacity: .001; line-height: 1;
  user-select: text; -webkit-user-select: text;
}
.textlayer span { position: absolute; white-space: pre; transform-origin: 0 0; color: transparent; }
.textlayer.visible { opacity: 1; }
.hl { position: absolute; background: color-mix(in srgb, #fbbf24 55%, transparent);
  border-radius: 2px; pointer-events: none; }
.hl.on { background: color-mix(in srgb, var(--accent) 55%, transparent); }
.pagenum { position: absolute; right: 8px; bottom: 6px; font-size: 11px; color: var(--muted);
  background: color-mix(in srgb, var(--card) 82%, transparent); padding: 1px 7px; border-radius: 7px; }

.toolbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; justify-content: space-around; align-items: center; gap: 2px;
  padding: 6px 6px calc(6px + var(--safe-b));
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px); border-top: 1px solid var(--line);
}
.tool { border: 0; background: none; color: var(--muted); border-radius: 12px;
  padding: 6px 10px; display: grid; justify-items: center; gap: 2px; font-size: 10.5px; min-width: 56px; }
.tool .ic { font-size: 19px; }
.tool[aria-pressed="true"], .tool:hover { color: var(--accent); }
.tool:disabled { opacity: .35; }

/* Region capture: drag a box on the page to copy or lift it out. */
.capture { position: absolute; inset: 0; cursor: crosshair; background: rgba(10,12,16,.28); }
.capture .box { position: absolute; border: 2px solid var(--accent); background: rgba(255,255,255,.12);
  box-shadow: 0 0 0 9999px rgba(10,12,16,.28); }

/* Signature pad */
.padwrap { border: 1px dashed var(--line); border-radius: 12px; background: var(--bg);
  position: relative; overflow: hidden; }
.padwrap canvas { display: block; width: 100%; height: 190px; touch-action: none; }
.padwrap .hintline { position: absolute; left: 8%; right: 8%; bottom: 46px; border-bottom: 1px solid var(--line); }
.padwrap .hint { position: absolute; left: 0; right: 0; bottom: 16px; text-align: center;
  font-size: 12.5px; color: var(--muted); pointer-events: none; }

/* A stamp being positioned before it is committed to the page. */
.placing { position: absolute; z-index: 5; border: 1.5px dashed var(--accent);
  background: rgba(79,109,245,.06); touch-action: none; }
.placing img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.placing .grip { position: absolute; right: -11px; bottom: -11px; width: 24px; height: 24px;
  border-radius: 50%; background: var(--accent); border: 2px solid var(--card); touch-action: none; }
.placing .txt { padding: 2px 4px; white-space: pre; font-family: Helvetica, Arial, sans-serif; }

/* ---------------------------------------------------------------- ai panel */

.chat { display: flex; flex-direction: column; gap: 10px; max-height: 52vh; overflow: auto; padding: 4px; }
.msg { padding: 10px 13px; border-radius: 14px; font-size: 14.5px; max-width: 88%; white-space: pre-wrap; }
.msg.me { background: var(--accent); color: #fff; align-self: flex-end; border-bottom-right-radius: 5px; }
.msg.ai { background: var(--sunk); align-self: flex-start; border-bottom-left-radius: 5px; }
.msg.err { background: color-mix(in srgb, var(--err) 12%, transparent); color: var(--err); align-self: flex-start; }
.asking { display: flex; gap: 5px; padding: 12px; align-self: flex-start; }
.asking i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bob 1.1s infinite; }
.asking i:nth-child(2) { animation-delay: .15s; }
.asking i:nth-child(3) { animation-delay: .3s; }
@keyframes bob { 0%,60%,100% { opacity: .25; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-4px); } }

.suggest { display: flex; gap: 7px; flex-wrap: wrap; margin: 4px 0 10px; }
.suggest button { border: 1px solid var(--line); background: var(--card); color: var(--muted);
  border-radius: 999px; padding: 6px 12px; font-size: 12.5px; }

/* -------------------------------------------------------------- feedback --- */

.toast {
  position: fixed; left: 50%; bottom: calc(86px + var(--safe-b)); transform: translateX(-50%);
  background: #16181d; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 13.5px;
  z-index: 90; box-shadow: 0 8px 28px rgba(0,0,0,.3); max-width: 88vw; text-align: center;
}
.toast.err { background: var(--err); }
.progress { position: fixed; top: 0; left: 0; height: 3px; background: var(--accent); z-index: 99;
  transition: width .18s ease; }
.busy { position: fixed; inset: 0; z-index: 80; display: grid; place-items: center;
  background: color-mix(in srgb, var(--bg) 72%, transparent); backdrop-filter: blur(3px); }
.busy .box { background: var(--card); border-radius: 16px; padding: 20px 26px; box-shadow: var(--shadow);
  text-align: center; font-size: 14px; }
.spin { width: 26px; height: 26px; margin: 0 auto 10px; border: 3px solid var(--line);
  border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.gate { display: grid; place-items: center; min-height: 70vh; text-align: center; padding: 20px; }
.note { font-size: 12.5px; color: var(--muted); background: var(--sunk); border-radius: 10px;
  padding: 9px 12px; margin: 10px 4px; }
.note.warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 11%, transparent); }

@media (min-width: 720px) {
  .sheet { border-radius: 18px; bottom: auto; top: 50%; transform: translateY(-50%); max-height: 82vh; }
}
