/* ---------- Fonts (served locally, no CDN) ---------- */
@font-face {
  font-family: "IBM Plex Sans";
  src: url("/static/fonts/IBMPlexSans-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "IBM Plex Sans";
  src: url("/static/fonts/IBMPlexSans-Medium.woff2") format("woff2");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "IBM Plex Sans";
  src: url("/static/fonts/IBMPlexSans-SemiBold.woff2") format("woff2");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "IBM Plex Mono";
  src: url("/static/fonts/IBMPlexMono-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "IBM Plex Mono";
  src: url("/static/fonts/IBMPlexMono-Medium.woff2") format("woff2");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}

/* ---------- Tokens ---------- */
:root {
  color-scheme: light;

  --font-sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, Consolas, monospace;

  /* palette (light) */
  --ground: #eceeea;
  --surface: #f7f8f6;
  --white: #ffffff;
  --ink: #14171a;
  --muted: #5c6560;
  --label: #7a837d;
  --rule: #cfd4ce;
  --rule-soft: #dfe3dd;
  --accent: #17506e;
  --accent-hover: #0f3a52;
  --accent-fg: #ffffff;
  --error: #a1291f;

  /* part-class colours (same tone in both themes) */
  --class-mecanic: #4a5560;
  --class-electric: #b8790a;
  --class-pneumatic: #17506e;
  --class-necunoscut: var(--label);

  /* legacy aliases kept so any un-migrated rule still resolves */
  --bg: var(--surface);
  --fg: var(--ink);
  --fg-muted: var(--muted);

  --radius: 3px;
  --radius-primary: 4px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;

    --ground: #14171a;
    --surface: #1b1f1d;
    --white: #22262a;
    --ink: #e8eae5;
    --muted: #a7b0aa;
    --label: #838c87;
    --rule: #2c322e;
    --rule-soft: #353b37;
    --accent: #7fb2d0;
    --accent-hover: #a6cbe2;
    --accent-fg: #0b1114;
    --error: #e2897d;

    --class-mecanic: #8b98a5;
    --class-electric: #d9a227;
    --class-pneumatic: #7fb2d0;
    --class-necunoscut: var(--label);
  }
}

* {
  box-sizing: border-box;
}

html {
  background: var(--ground);
}

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--ground);
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

a {
  color: var(--accent);
}

h1, h2, h3 {
  font-weight: 600;
  line-height: 1.25;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 38px;
  padding: 2px 12px;
  background: #14171a;
  color: #e8eae5;
  border-bottom: 1px solid #2c322e;
}

.topbar .brand {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  text-decoration: none;
  color: #f2f3f1;
  margin-right: 8px;
  white-space: nowrap;
}

.topbar .brand .mark {
  font-family: var(--font-mono);
  font-weight: 500;
  letter-spacing: 0.02em;
  font-size: 15px;
}

.topbar .brand .tag {
  font-size: 12px;
  color: #9aa39d;
  font-weight: 400;
}

.topbar nav {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
  align-items: center;
}

.topbar nav a {
  display: inline-flex;
  align-items: center;
  height: 30px;
  padding: 0 12px;
  border-radius: var(--radius);
  color: #cdd3cf;
  text-decoration: none;
  font-size: 14px;
}

.topbar nav a:hover {
  background: rgba(255, 255, 255, 0.06);
  color: #f2f3f1;
}

.topbar nav a.active {
  background: #262b2e;
  color: #f2f3f1;
}

.topbar .logout {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.who {
  color: #9aa39d;
  font-size: 13px;
  white-space: nowrap;
}

.topbar .logout button {
  min-height: 30px;
  padding: 0 12px;
  font-size: 13px;
  background: transparent;
  color: #cdd3cf;
  border: 1px solid #363c39;
}

.topbar .logout button:hover {
  background: rgba(255, 255, 255, 0.06);
  color: #f2f3f1;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 20px 16px 48px;
}

/* The document page is the one screen where every pixel goes to the
   drawing, not to prose — let it use the full window width. */
body.page-document main {
  max-width: none;
}

/* Fixed viewport shell (desktop/tablet only — see the ≤1000px reset
   below): the window itself never scrolls on the document page. Only
   two regions do, independently — the PDF viewer and the right-hand
   rail — everything above them (top bar, breadcrumb, heading) stays
   put. Achieved with a chain of `flex: 1; min-height: 0` down to the
   two scrolling regions, rather than any `vh` guess, so it keeps
   working whatever height the heading row actually renders at. */
@media (min-width: 1001px) {
  body.page-document {
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  body.page-document main {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 8px 16px 10px;
    /* `main` carries `margin: 0 auto` for the text pages. On a flex
       container an auto inline margin cancels the stretch, so this page
       collapsed to its content width and gave two thirds of the window
       back to empty margin. Zero it — the width is the whole point here. */
    margin: 0;
  }

  body.page-document .doc {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  /* Every row above the viewer is a row the drawing does not get. On a
     682px window they came to 238px, more than a third of the screen, and
     an A3 sheet had 427px left to live in. Tightened, not removed: the
     technician still needs to know which drawing he is looking at. */
  body.page-document .doc > .crumbs,
  body.page-document .doc-heading,
  body.page-document .doc-subtitle {
    flex: none;
  }

  body.page-document .doc > .crumbs {
    margin: 2px 0 6px;
  }

  body.page-document .doc-heading {
    margin-bottom: 6px;
  }

  body.page-document .doc-subtitle {
    margin: -4px 0 6px;
  }

  body.page-document .doc-layout {
    flex: 1 1 auto;
    min-height: 0;
  }
}

.login {
  max-width: 360px;
  margin: 40px auto;
}

.error {
  color: var(--error);
}

label {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

input, select, button, textarea {
  min-height: 44px;
  font-size: 16px;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--white);
  color: var(--ink);
  font-family: inherit;
}

button {
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-fg);
  border: 1px solid var(--accent);
  width: auto;
  padding: 0 16px;
  font-weight: 500;
  border-radius: var(--radius-primary);
}

button:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

button.link {
  background: none;
  color: var(--accent);
  padding: 0;
  width: auto;
  min-height: 44px;
  min-width: 0;
  border: none;
  text-decoration: underline;
  font-size: 14px;
  font-weight: 400;
}

button.link:hover {
  background: none;
  color: var(--accent-hover);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 18px;
  background: var(--accent);
  color: var(--accent-fg);
  text-decoration: none;
  border-radius: var(--radius-primary);
  border: 1px solid var(--accent);
  font-weight: 500;
}

.btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* Secondary, bordered action — used where the button must not compete
   visually with a primary surface (e.g. the "open PDF" link sitting in
   the metadata card next to the drawing). */
.btn-outline {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  width: 100%;
  margin-top: 10px;
  padding: 0 14px;
  background: transparent;
  color: var(--accent);
  text-decoration: none;
  border-radius: var(--radius-primary);
  border: 1px solid var(--rule);
  font-weight: 500;
}

.btn-outline:hover {
  border-color: var(--accent);
  background: var(--surface);
}

/* ---------- Filters ---------- */
.filters {
  display: flex;
  flex-direction: column;
  gap: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 16px;
}

.filters input[type="search"] {
  width: 100%;
  font-size: 16px;
}

.filter-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px 12px;
}

.filter-row label {
  min-width: 0;
  margin-top: 0;
}

.filter-row select, .filter-row input {
  min-height: 44px;
}

.count {
  color: var(--muted);
}

/* ---------- Results table ---------- */
.tablewrap {
  overflow-x: auto;
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  background: var(--white);
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead th {
  background: var(--surface);
  color: var(--label);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  white-space: nowrap;
}

td, th {
  padding: 10px 12px;
  border-bottom: 1px solid var(--rule-soft);
  text-align: left;
}

tbody tr {
  background: var(--white);
}

tbody tr:last-child td {
  border-bottom: none;
}

.num, .mono {
  font-variant-numeric: tabular-nums;
}

.mono {
  font-family: var(--font-mono);
}

td.mono a {
  color: var(--accent);
  font-weight: 500;
  text-decoration: none;
}

td.mono a:hover {
  text-decoration: underline;
}

.num {
  text-align: right;
}

.empty {
  color: var(--muted);
  padding: 16px 0;
}

.chip {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 9px;
  font-size: 11px;
  font-weight: 500;
  color: var(--accent);
  background: var(--surface);
  border: 1px solid var(--rule-soft);
  border-radius: 999px;
}

.pager {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.doclist {
  list-style: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ---------- Row list (related drawings, family class groups) ---------- */
.row-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--white);
}

.row-list li {
  border-bottom: 1px solid var(--rule-soft);
}

.row-list li:last-child {
  border-bottom: none;
}

.row-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  color: var(--ink);
  text-decoration: none;
}

.row-link:hover {
  background: var(--surface);
}

.row-code {
  flex: 0 0 100px;
  color: var(--accent);
  font-weight: 500;
}

.row-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-year {
  flex: 0 0 auto;
  margin-left: auto;
  color: var(--muted);
}

/* ---------- Revision list ---------- */
.rev-list {
  list-style: none;
  margin: 0 0 8px;
  padding: 0;
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  overflow: hidden;
}

.rev-row {
  border-bottom: 1px solid var(--rule-soft);
  background: var(--white);
}

.rev-row:last-child {
  border-bottom: none;
}

.rev-row a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  color: var(--ink);
  text-decoration: none;
}

.rev-row a:hover {
  background: var(--surface);
}

.rev-letter {
  flex: 0 0 26px;
  color: var(--accent);
  font-weight: 500;
}

.rev-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rev-year {
  flex: 0 0 auto;
  margin-left: auto;
  color: var(--muted);
}

.rev-row--current a {
  background: var(--ink);
  color: var(--ground);
}

.rev-row--current a:hover {
  background: var(--ink);
}

.rev-row--current .rev-letter,
.rev-row--current .rev-name,
.rev-row--current .rev-year {
  color: inherit;
}

.rev-span {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.doc {
  display: block;
}

.crumbs {
  color: var(--muted);
}

.crumbs a {
  color: var(--muted);
}

.code {
  font-size: 18px;
  color: var(--accent);
  font-weight: 500;
}

.meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px 16px;
  margin: 0;
}

.meta div {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.meta dt {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--label);
}

.meta dd {
  margin: 0;
}

/* ---------- The drawing viewer ---------- */
.pdfview {
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--white);
  overflow: hidden;
}

/* The fallback path (no JavaScript, or PDF.js failed to start): the
   browser's own plugin, filling the same box. */
.pdfview object,
.pdfview noscript {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 60vh;
  background: var(--white);
}

.pdf-toolbar {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 16px;
  padding: 4px 10px;
  border-bottom: 1px solid var(--rule-soft);
  background: var(--surface);
}

.pdf-pages,
.pdf-zoomer {
  display: flex;
  align-items: center;
  gap: 4px;
}

.pdf-toolbar button {
  min-width: 34px;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  background: var(--white);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}

.pdf-toolbar button:hover:not(:disabled) {
  background: var(--ground);
}

.pdf-toolbar button:disabled {
  opacity: 0.4;
  cursor: default;
}

.pdf-counter,
.pdf-zoomlevel {
  min-width: 52px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

/* Single-sheet drawings are the majority; hide the page controls rather than
   showing "1 / 1" and two dead buttons. */
.pdfview:not(.pdfview--multipage) .pdf-pages {
  display: none;
}

.pdf-status {
  margin: 0 0 0 auto;
  color: var(--muted);
  font-size: 12px;
}

.pdf-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  /* Reserve the vertical scrollbar's width up front. Without it, fitting to
     width measures a container that has no scrollbar yet, the rendered page
     makes one appear, and the drawing then overflows sideways by exactly
     that width — a horizontal scrollbar on a page that was meant to fit. */
  scrollbar-gutter: stable;
  padding: 8px;
  background: var(--ground);
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.pdf-stage {
  position: relative;
  flex: none;
  box-shadow: 0 1px 4px rgb(0 0 0 / 18%);
  background: #fff;
}

.pdf-canvas {
  display: block;
}

.pdf-hotspots {
  position: absolute;
  inset: 0;
}

/* A code's own box is a few millimetres tall. The outline traces the real
   text so the technician sees exactly which code he is about to follow,
   while the invisible padding underneath makes it hittable — bigger still
   where the pointer is a finger. */
.pdf-hotspot {
  position: absolute;
  display: block;
  border: 1px solid transparent;
  border-radius: 2px;
  box-sizing: border-box;
}

.pdf-hotspot::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: max(100%, 24px);
  height: max(100%, 24px);
}

@media (pointer: coarse) {
  .pdf-hotspot::after {
    width: max(100%, 44px);
    height: max(100%, 44px);
  }
}

a.pdf-hotspot {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  cursor: pointer;
}

a.pdf-hotspot:hover,
a.pdf-hotspot:focus-visible {
  background: color-mix(in srgb, var(--accent) 30%, transparent);
  border-color: var(--accent);
  outline: none;
}

/* A code printed on the sheet whose drawing is not in the catalogue (or not
   visible to this user): marked, so he knows we read it, but not a link. */
.pdf-hotspot--dead {
  border-style: dashed;
  border-color: color-mix(in srgb, var(--muted) 40%, transparent);
}

.card {
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
  background: var(--surface);
}

.small {
  font-size: 12px;
}

.inline {
  display: inline;
}

.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: flex-end;
}

.inline-form label {
  flex: 1 1 140px;
  min-width: 0;
  margin-top: 0;
}

/* ---------- Class badge (dot + word) ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--ink);
  white-space: nowrap;
}

.badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: none;
  background: var(--class-necunoscut);
}

.badge--mecanic .dot { background: var(--class-mecanic); }
.badge--electric .dot { background: var(--class-electric); }
.badge--pneumatic .dot { background: var(--class-pneumatic); }
.badge--necunoscut .dot { background: var(--class-necunoscut); }

.group-count {
  color: var(--label);
  font-weight: 400;
  font-size: 13px;
  margin-left: 6px;
}

/* ---------- Status chip ---------- */
.status-chip {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid var(--rule-soft);
  background: var(--surface);
  color: var(--muted);
}

.status-chip--actual {
  color: var(--accent);
  border-color: var(--accent);
}

/* ---------- Document two-column layout ---------- */
.doc-layout {
  display: grid;
  grid-template-columns: 1fr 372px;
  gap: 20px;
  /* stretch (the grid default), not start: on the fixed desktop shell
     both columns must fill the row's full height so each can scroll
     its own content independently. */
}

.doc-main {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.doc-main .pdfview {
  flex: 1;
  min-height: 0;
}

.doc-aside {
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.doc-aside section {
  background: var(--surface);
  border: 1px solid var(--rule-soft);
  border-radius: var(--radius);
  padding: 14px;
}

.doc-aside h2, .doc-aside h3 {
  margin-top: 0;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--label);
}

.doc-heading {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  margin-bottom: 16px;
}

.doc-heading h1 {
  margin: 0;
  font-size: 22px;
  flex: 1 1 auto;
  min-width: 200px;
}

.doc-subtitle {
  margin: -10px 0 16px;
  font-size: 13px;
  color: var(--muted);
}

@media (max-width: 1000px) {
  /* Below the two-column breakpoint the fixed viewport shell above
     does not apply — the page scrolls normally, single column, drawing
     first, so nothing here needs the flex-fill/height:100% chain. */
  .doc-layout {
    grid-template-columns: 1fr;
  }
  .doc-aside {
    overflow-y: visible;
  }
  .doc-main {
    display: block;
  }
  .pdfview {
    height: auto;
  }
  .doc-main .pdfview object {
    height: auto;
    min-height: 60vh;
  }
  /* On a phone the page does scroll, so the viewer needs a definite height
     of its own — otherwise the canvas area collapses to nothing. */
  .pdf-scroll {
    height: 70vh;
  }
  /* The metadata card's "open PDF" action is the first thing under the
     drawing in the single-column stack — grow it to a proper thumb
     target here instead of the 36px header-adjacent size. */
  .btn-outline {
    min-height: 48px;
  }
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}

/* ---------- Phone ---------- */
@media (max-width: 480px) {
  main {
    padding: 12px 10px 32px;
  }
  .topbar {
    padding: 0 8px;
  }
  .topbar .brand .tag {
    display: none;
  }
  .btn, .login button {
    width: 100%;
    min-height: 48px;
  }
}

/* The verification queue: what the pipeline decided about a document, and on
   what evidence, before the owner corrects it by hand. */
.queue-facts {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 4px 0 8px;
}

.queue-src {
  color: var(--muted);
  font-size: 12px;
}

/* While a sheet is drawing: dim it slightly and take the pointer away, so a
   half-drawn page cannot be mistaken for the finished one or clicked on. */
.pdfview--busy .pdf-stage {
  opacity: 0.55;
}

.pdfview--busy .pdf-hotspots {
  pointer-events: none;
}

/* A filter the search let go of, because it could not live beside the others.
   Said quietly, above the results it changed. */
.filter-dropped {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 13px;
}


/* Renaming a drawing from its page, for the administrator. */
.rename { margin: -4px 0 8px; font-size: 13px; color: var(--muted); }
.rename summary { cursor: pointer; }
.rename-form { display: flex; gap: 8px; align-items: center; margin-top: 6px; max-width: 640px; }
.rename-form input { min-height: 34px; font-size: 14px; }
.rename-form button { min-height: 34px; }

/* The verification queue as a to-do list: filters and what each row lacks. */
.queue-filters { color: var(--muted); font-size: 14px; margin: 0 0 6px; }
.queue-filters a.active { font-weight: 600; color: var(--ink); text-decoration: none; }
.queue-family { margin-bottom: 12px; }
.chip.queue-missing { border-color: var(--error); color: var(--error); }
