/* =============================================================================
   RustDoc-Lite
   =============================================================================
   Mobile first. Every base rule targets a phone; min-width queries add what a
   larger screen can afford. That order matters — the common case for this
   archive is someone standing somewhere needing a document, not someone at a
   desk.

   Replaces the Tailwind CDN, which was 397 KB of JavaScript compiling CSS in
   the browser on every page load, for 2 KB of actual styles. It also made the
   PWA impossible: a service worker cannot usefully cache an app shell whose
   appearance depends on fetching and running a compiler from a CDN.
   ========================================================================== */

/* --- Tokens --------------------------------------------------------------- */

:root {
  --primary: #B4472A;
  --primary-hover: #9A3B22;
  --secondary: #3D4759;
  --accent: #4A7C9E;
  --success: #3E7C4F;
  --warning: #B8862B;
  --error: #B4342A;

  --bg-canvas: #F3F0E6;
  --bg-surface: #FFFFFF;
  --bg-sunken: #EAE6D9;
  --text-main: #2B2724;
  --text-muted: #5C544F;
  --border: #CBD5E1;

  /* One scale, used everywhere. Fewer decisions, more consistency. */
  --sp-1: 0.25rem;
  --sp-2: 0.5rem;
  --sp-3: 0.75rem;
  --sp-4: 1rem;
  --sp-5: 1.5rem;
  --sp-6: 2rem;

  --radius: 8px;
  --radius-sm: 5px;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.1), 0 1px 2px rgb(0 0 0 / 0.06);
  --shadow-lg: 0 10px 25px rgb(0 0 0 / 0.15);

  /* Anything interactive is at least this tall. Below ~44px touch accuracy
     falls off sharply, and this is a one-handed, on-the-move interface. */
  --tap: 44px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
}

:root.dark {
  --primary: #E86A45;
  --primary-hover: #F08160;
  --secondary: #1E293B;
  --accent: #7CB4D8;
  --success: #5BA672;
  --warning: #D6A845;
  --error: #E8635A;

  --bg-canvas: #0F172A;
  --bg-surface: #1E293B;
  --bg-sunken: #172033;
  --text-main: #E8EAED;
  --text-muted: #94A3B8;
  --border: #334155;
}

/* --- Reset ---------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

* { margin: 0; }

html {
  -webkit-text-size-adjust: 100%;   /* stop iOS inflating text in landscape */
  scroll-behavior: smooth;
}

body {
  min-height: 100dvh;               /* dvh, not vh: accounts for mobile browser chrome */
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  font-size: 16px;                  /* iOS zooms any input below 16px on focus */
  line-height: 1.5;
  color: var(--text-main);
  background: var(--bg-canvas);
  -webkit-font-smoothing: antialiased;
}

img, svg, video { display: block; max-width: 100%; }

/* An <svg> with no intrinsic size expands to fill its container. Tailwind used
   to size every icon with w-5/h-5; without it they ballooned to fill the page.
   Only icons that do not carry their own width attribute are defaulted, so an
   explicit size still wins. */
svg { flex: none; }
svg:not([width]) { width: 1.25rem; height: 1.25rem; }

input, button, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { line-height: 1.25; font-weight: 600; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.125rem; }
h3 { font-size: 1rem; }

/* --- Scrollbars ----------------------------------------------------------- */

* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
::-webkit-scrollbar { width: 12px; height: 12px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background-color: var(--border);
  border-radius: 6px;
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--text-muted); }
::-webkit-scrollbar-corner { background: transparent; }

/* --- Layout --------------------------------------------------------------- */

.app-main { flex: 1; width: 100%; padding: var(--sp-4); }
@media (min-width: 640px) { .app-main { padding: var(--sp-5); } }

.stack { display: flex; flex-direction: column; gap: var(--sp-4); }
.stack-sm { display: flex; flex-direction: column; gap: var(--sp-2); }
.row { display: flex; align-items: center; gap: var(--sp-2); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }   /* min-width:0 so flex children can truncate */
.none { flex: none; }
.hidden { display: none !important; }

.narrow { max-width: 42rem; margin-inline: auto; }
.readable { max-width: 56rem; margin-inline: auto; }

@media (min-width: 768px) { .mobile-only { display: none !important; } }
@media (max-width: 767px) { .desktop-only { display: none !important; } }

/* --- Navigation ----------------------------------------------------------- */

.nav {
  flex: none;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 30;
  /* Notch and home indicator on iOS. */
  padding: var(--sp-3) max(var(--sp-4), env(safe-area-inset-left))
           var(--sp-3) max(var(--sp-4), env(safe-area-inset-right));
}

.nav-brand {
  display: inline-flex; align-items: center; min-height: var(--tap);
  font-size: 1.125rem; font-weight: 700; color: var(--text-main);
}
.nav-brand:hover { text-decoration: none; }
.nav-brand span { color: var(--primary); }

.nav-links { display: none; gap: var(--sp-4); }
@media (min-width: 768px) { .nav-links { display: flex; } }

.nav-link { color: var(--text-muted); }
.nav-link:hover, .nav-link[aria-current="page"] { color: var(--text-main); text-decoration: none; }

.nav-menu { border-top: 1px solid var(--border); margin-top: var(--sp-3); padding-top: var(--sp-3); }
.nav-menu a { display: block; padding: var(--sp-3) 0; min-height: var(--tap); color: var(--text-muted); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: var(--tap); min-height: var(--tap);
  padding: var(--sp-2);
  background: none; border: none; border-radius: var(--radius);
  color: var(--text-muted);
}
.icon-btn:hover { background: color-mix(in srgb, var(--primary) 10%, transparent); color: var(--text-main); }

/* --- Buttons -------------------------------------------------------------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  min-height: var(--tap);
  padding: var(--sp-2) var(--sp-4);
  border: 1px solid transparent; border-radius: var(--radius);
  font-weight: 500;
  background: var(--bg-surface); color: var(--text-main); border-color: var(--border);
  transition: opacity .15s, background-color .15s;
}
.btn:hover { text-decoration: none; background: color-mix(in srgb, var(--primary) 8%, var(--bg-surface)); }
.btn:active { opacity: .8; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--error); color: #fff; border-color: var(--error); }
.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: var(--sp-1) var(--sp-3); font-size: .875rem; }

/* --- Forms ---------------------------------------------------------------- */

.field { display: flex; flex-direction: column; gap: var(--sp-1); }
.label { font-size: .875rem; font-weight: 500; color: var(--text-main); }
.hint { font-size: .75rem; color: var(--text-muted); }

.input, .select, .textarea {
  width: 100%;
  min-height: var(--tap);
  padding: var(--sp-2) var(--sp-3);
  background: var(--bg-surface);
  color: var(--text-main);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;                  /* below this iOS zooms on focus */
}
.input:focus, .select:focus, .textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
  border-color: var(--primary);
}
.textarea { min-height: 5rem; resize: vertical; }
.select { appearance: none; padding-right: var(--sp-6);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke-width='2' stroke='%2394A3B8'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19.5 8.25l-7.5 7.5-7.5-7.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right var(--sp-3) center; background-size: 1.1em;
}

/* Tags as tappable chips. Was a multi-select whose helper text said
   "Hold Ctrl/Cmd" — an instruction no phone can follow. */
.chips { display: flex; flex-wrap: wrap; gap: var(--sp-2); }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; width: 0; height: 0; }
.chip span {
  display: inline-flex; align-items: center; min-height: 36px;
  padding: var(--sp-1) var(--sp-3);
  border: 1px solid var(--border); border-radius: 999px;
  color: var(--text-muted); font-size: .875rem;
}
.chip input:checked + span {
  background: var(--primary); border-color: var(--primary); color: #fff;
}
.chip input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Tag editor: the chips show what is *on* the document, the combobox reaches
   the rest. Rendering the whole vocabulary as chips made the form grow without
   limit as tags accumulated — 107 of them was 1664px of chips. */
.tag-editor { display: flex; flex-direction: column; gap: var(--sp-2); }

.chip-selected {
  display: inline-flex; align-items: center; gap: var(--sp-1);
  min-height: 36px; padding: var(--sp-1) var(--sp-1) var(--sp-1) var(--sp-3);
  background: var(--primary); border-radius: 999px;
  color: #fff; font-size: .875rem;
}
.chip-remove {
  display: inline-flex; align-items: center; justify-content: center;
  /* 28px is below the 44px tap target used elsewhere, but it sits inside a
     chip that is itself the target; anything larger deforms the chip. The
     surrounding padding keeps a stray thumb from hitting the neighbour. */
  width: 28px; height: 28px;
  border: 0; border-radius: 999px;
  background: transparent; color: #fff;
  font-size: 1.125rem; line-height: 1; cursor: pointer;
}
.chip-remove:hover, .chip-remove:focus-visible {
  background: rgb(255 255 255 / 0.25); outline: none;
}

.combo { position: relative; }
.combo-list {
  position: absolute; z-index: 30; left: 0; right: 0; top: calc(100% + 4px);
  max-height: 14rem; overflow-y: auto;
  margin: 0; padding: var(--sp-1); list-style: none;
  background: var(--bg-surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}
.combo-list li {
  display: flex; align-items: center;
  min-height: var(--tap); padding: var(--sp-2) var(--sp-3);
  border-radius: var(--radius-sm);
  font-size: .9375rem; color: var(--text-main); cursor: pointer;
}
.combo-list li[aria-selected="true"], .combo-list li:hover {
  background: color-mix(in srgb, var(--primary) 18%, transparent);
}
.combo-list .combo-create { color: var(--primary); font-weight: 500; }

/* --- Surfaces ------------------------------------------------------------- */

.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--sp-4);
  box-shadow: var(--shadow);
}
.card-tight { padding: var(--sp-3); }

.badge {
  display: inline-flex; align-items: center;
  padding: 2px var(--sp-2);
  border-radius: 999px;
  font-size: .75rem; font-weight: 500;
  background: color-mix(in srgb, var(--success) 20%, transparent);
  color: var(--success);
  white-space: nowrap;
}
.badge-muted { background: color-mix(in srgb, var(--text-muted) 18%, transparent); color: var(--text-muted); }

.note { padding: var(--sp-3); border-radius: var(--radius); font-size: .875rem; border: 1px solid; }
.note-info    { background: color-mix(in srgb, var(--accent) 10%, transparent);  border-color: color-mix(in srgb, var(--accent) 30%, transparent); }
.note-success { background: color-mix(in srgb, var(--success) 10%, transparent); border-color: color-mix(in srgb, var(--success) 30%, transparent); }
.note-warning { background: color-mix(in srgb, var(--warning) 10%, transparent); border-color: color-mix(in srgb, var(--warning) 30%, transparent); }
.note-error   { background: color-mix(in srgb, var(--error) 10%, transparent);   border-color: color-mix(in srgb, var(--error) 30%, transparent); }

/* --- Documents ------------------------------------------------------------ */

/* Cards on a phone, table on a desk. A table on a 375px screen can only ever
   show two of its six columns, which is how the old layout hid the
   correspondent, the category and every action. */
.doc-list { display: flex; flex-direction: column; gap: var(--sp-2); }

.doc-item {
  display: flex; align-items: center; gap: var(--sp-3);
  min-height: var(--tap);
  padding: var(--sp-3);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
}
.doc-item:hover { text-decoration: none; border-color: var(--primary); }
.doc-title { font-weight: 600; color: var(--text-main); overflow-wrap: anywhere; }
.doc-meta { font-size: .8125rem; color: var(--text-muted); }

.doc-grid {
  display: grid;
  gap: var(--sp-3);
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
@media (min-width: 640px) { .doc-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); } }

.doc-card { display: flex; flex-direction: column; background: var(--bg-surface);
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.doc-card:hover { border-color: var(--primary); }
.doc-thumb { aspect-ratio: 3 / 4; width: 100%; object-fit: cover; background: var(--bg-sunken); }
.doc-card-body { padding: var(--sp-3); display: flex; flex-direction: column; gap: var(--sp-1); }

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: var(--sp-3); text-align: left; border-bottom: 1px solid var(--border); }
.table th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); }
.table-wrap { overflow-x: auto; background: var(--bg-surface);
  border: 1px solid var(--border); border-radius: var(--radius); }

/* --- Viewer --------------------------------------------------------------- */

/* Document first on a phone; side by side once there is room. */
.viewer { flex: 1; display: flex; flex-direction: column; min-height: 0; }
@media (min-width: 768px) { .viewer { flex-direction: row; overflow: hidden; } }

/* Flex column rather than a percentage height on the iframe: height:100%
   resolves against the parent's *specified* height, which a flex-grown box does
   not reliably have — the iframe fell back to its intrinsic 150px inside a
   569px pane. Letting it flex:1 sidesteps the resolution problem entirely. */
.viewer-doc {
  flex: 1; min-width: 0; display: flex; flex-direction: column;
  background: var(--secondary); min-height: 60vh;
}
@media (min-width: 768px) { .viewer-doc { min-height: 0; order: 2; } }
.viewer-doc iframe { flex: 1; width: 100%; border: 0; background: #fff; }
.viewer-doc .image-pane { height: 100%; display: flex; align-items: center;
  justify-content: center; padding: var(--sp-4); overflow: auto; }

.viewer-side { flex: none; background: var(--bg-surface); }
@media (min-width: 768px) {
  .viewer-side { width: 24rem; order: 1; border-right: 1px solid var(--border); overflow-y: auto; }
}

/* The metadata form: collapsed behind a toggle on a phone so the document keeps
   the screen, always open on desktop. One element, shown two ways — the two
   copies this replaced had duplicate ids and lost mobile tag edits on save. */
.meta-toggle {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; min-height: var(--tap);
  padding: var(--sp-3) var(--sp-4);
  border: 0; border-top: 1px solid var(--border);
  background: transparent; color: var(--text-main);
  font: inherit; font-weight: 500; text-align: left; cursor: pointer;
}
.meta-toggle svg { transition: transform .2s; }
.meta-toggle.is-open svg { transform: rotate(180deg); }

.viewer-meta { padding: var(--sp-4); }
@media (max-width: 767px) {
  .viewer-meta { display: none; border-top: 1px solid var(--border); }
  .viewer-meta.is-open { display: block; }
}

/* --- Upload --------------------------------------------------------------- */

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: var(--sp-5) var(--sp-4);
  text-align: center;
  color: var(--text-muted);
  background: var(--bg-surface);
}
.dropzone.drag-over { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 8%, var(--bg-surface)); }

.progress { height: 6px; border-radius: 999px; background: var(--bg-sunken); overflow: hidden; }
.progress > div { height: 100%; background: var(--primary); transition: width .2s; }

/* --- Toasts --------------------------------------------------------------- */

.toast-host {
  position: fixed;
  left: var(--sp-4); right: var(--sp-4);
  bottom: max(var(--sp-4), env(safe-area-inset-bottom));
  z-index: 50;
  display: flex; flex-direction: column; gap: var(--sp-2);
  pointer-events: none;
}
@media (min-width: 640px) { .toast-host { left: auto; width: 24rem; } }
.toast {
  pointer-events: auto;
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius);
  color: #fff; background: var(--secondary);
  box-shadow: var(--shadow-lg);
  transition: opacity .3s;
}
.toast-success { background: var(--success); }
.toast-error   { background: var(--error); }
.toast-info    { background: var(--accent); }

/* --- Modal ---------------------------------------------------------------- */

.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center;
  justify-content: center; padding: var(--sp-4); }
.modal-backdrop { position: absolute; inset: 0; background: rgb(0 0 0 / .5); }
.modal-panel { position: relative; z-index: 1; width: 100%; max-width: 24rem;
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: var(--sp-5); box-shadow: var(--shadow-lg); }

/* --- Utilities ------------------------------------------------------------ */

.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.muted { color: var(--text-muted); }
.small { font-size: .875rem; }
.tiny  { font-size: .75rem; }
.center { text-align: center; }
.mono { font-family: var(--mono); font-size: .8125rem; }
.spin { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.htmx-indicator { display: none; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { display: inline-block; }
/* The reprocess button swaps its label for the indicator while the request runs. */
.htmx-request .reprocess-idle { display: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}
