/* Design tokens: every color is a custom property, redefined
   for the dark theme, and every text color clears WCAG AA (4.5:1) on the
   surfaces it sits on, in both themes. */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1b2330;
  --muted: #545e6f;
  --border: #d3d9e2;
  --accent: #2557a7;
  --accent-hover: #1d4787;
  --accent-text: #ffffff;
  --accent-soft: #e3ecfa;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --success: #1c6e37;
  --success-soft: #e5f4ea;
  --focus: #2557a7;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 12px rgb(16 24 40 / 6%);
  --radius: 12px;
  --radius-small: 8px;
  --gutter: 16px;
  --tap: 44px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #11151b;
  --surface: #1a2029;
  --surface-2: #232b36;
  --text: #e7ecf3;
  --muted: #a3adbd;
  --border: #364152;
  --accent: #8db4ff;
  --accent-hover: #a9c6ff;
  --accent-text: #0d1726;
  --accent-soft: #1f2c42;
  --danger: #ff8a80;
  --danger-soft: #3a1f1f;
  --success: #7ad69a;
  --success-soft: #1b3325;
  --focus: #8db4ff;
  --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 12px rgb(0 0 0 / 30%);
  color-scheme: dark;
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.5;
  padding-bottom: env(safe-area-inset-bottom);
}

h1 {
  font-size: 1.5rem;
  line-height: 1.25;
  margin: 0 0 0.5rem;
}

h2 {
  font-size: 1.125rem;
  margin: 0 0 0.75rem;
}

p {
  margin: 0 0 0.75rem;
}

a {
  color: var(--accent);
}

[hidden] {
  display: none !important;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

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

.skip-link {
  position: absolute;
  left: var(--gutter);
  top: -4rem;
  z-index: 20;
  padding: 0.5rem 1rem;
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-small);
}

.skip-link:focus {
  top: 0.5rem;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.875rem;
}

/* --- header ------------------------------------------------------------------ */

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: calc(0.5rem + env(safe-area-inset-top)) var(--gutter) 0.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-start {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 700;
  white-space: nowrap;
}

.brand-logo {
  display: inline-flex;
}

.version {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  min-height: 32px;
  padding: 0 0.5rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
}

.version svg {
  width: 14px;
  height: 14px;
}

.app-nav {
  display: flex;
  gap: 0.25rem;
  margin-left: auto;
}

.app-nav button {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  min-height: var(--tap);
  padding: 0 0.75rem;
  border: 0;
  border-radius: var(--radius-small);
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.app-nav button:hover {
  color: var(--text);
  background: var(--surface-2);
}

.app-nav button[aria-current="page"] {
  color: var(--accent);
  background: var(--accent-soft);
}

.header-tools {
  display: flex;
  gap: 0.25rem;
  margin-left: auto;
}

.app-nav:not([hidden]) + .header-tools {
  margin-left: 0;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap);
  height: var(--tap);
  border: 0;
  border-radius: var(--radius-small);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.icon-button:hover {
  color: var(--text);
  background: var(--surface-2);
}

:root[data-theme="dark"] .icon-when-light,
:root[data-theme="light"] .icon-when-dark {
  display: none;
}

/* --- layout ------------------------------------------------------------------ */

.shell {
  width: 100%;
  max-width: 60rem;
  margin: 0 auto;
  padding: 1.5rem var(--gutter) 3rem;
}

.shell:focus {
  outline: none;
}

.narrow {
  max-width: 32rem;
  margin: 0 auto;
}

.view-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  margin-bottom: 1rem;
}

.view-head h1 {
  margin: 0;
}

.view-head p {
  margin: 0;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1rem;
}

.status {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-small);
  background: var(--accent-soft);
  color: var(--text);
  border: 1px solid var(--border);
}

.status[data-kind="error"] {
  background: var(--danger-soft);
  border-color: var(--danger);
}

.status[data-kind="success"] {
  background: var(--success-soft);
  border-color: var(--success);
}

.notice {
  margin: 1rem 0 0;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-small);
  background: var(--success-soft);
}

.empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* --- forms ------------------------------------------------------------------- */

label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

input[type="email"],
input[type="text"],
input[type="search"],
input[type="number"] {
  display: block;
  width: 100%;
  min-height: var(--tap);
  padding: 0.5rem 0.75rem;
  margin-bottom: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface);
  color: var(--text);
  /* 16px at least, or iOS zooms the page on focus. */
  font: inherit;
  font-size: max(1rem, 16px);
}

input::placeholder {
  color: var(--muted);
  opacity: 1;
}

.check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 400;
  min-height: var(--tap);
  margin: 0;
}

.check input {
  width: 1.25rem;
  height: 1.25rem;
  accent-color: var(--accent);
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: var(--tap);
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}

.button:hover {
  background: var(--surface-2);
}

.button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.button.primary:hover {
  background: var(--accent-hover);
}

.button.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--bg);
}

.button.quiet {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.button.quiet:hover {
  color: var(--text);
  background: var(--surface-2);
}

.button.quiet.danger-text:hover {
  color: var(--danger);
  background: var(--danger-soft);
}

.button.small {
  min-height: 36px;
  padding: 0.25rem 0.75rem;
  font-size: 0.875rem;
}

.button.block {
  display: flex;
  width: 100%;
}

.button:disabled,
.button[aria-busy="true"] {
  opacity: 0.6;
  cursor: progress;
}

.divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1rem 0;
  color: var(--muted);
  font-size: 0.875rem;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  border-top: 1px solid var(--border);
}

fieldset {
  border: 0;
  margin: 0 0 0.5rem;
  padding: 0;
}

legend {
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.segmented {
  display: flex;
  flex-wrap: wrap;
}

.segmented legend {
  width: 100%;
}

.segmented input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.segmented label {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  margin: 0;
  padding: 0 1rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}

.segmented label + input + label {
  border-left: 0;
}

.segmented label:first-of-type {
  border-radius: var(--radius-small) 0 0 var(--radius-small);
}

.segmented label:last-of-type {
  border-radius: 0 var(--radius-small) var(--radius-small) 0;
}

.segmented input:checked + label {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.segmented input:focus-visible + label {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.facts {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.125rem;
  margin: 1rem 0 0;
}

.facts dt {
  color: var(--muted);
  font-size: 0.875rem;
}

.facts dd {
  margin: 0 0 0.5rem;
  overflow-wrap: anywhere;
}

/* --- documents --------------------------------------------------------------- */

.dropzone {
  margin-bottom: 1rem;
}

.dropzone-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  padding: 1.5rem 1rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: center;
  font-weight: 400;
  cursor: pointer;
}

.dropzone-label:hover,
.dropzone.is-over .dropzone-label {
  border-color: var(--accent);
  background: var(--accent-soft);
}

#file-input:focus-visible + .dropzone-label {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.dropzone-icon {
  display: inline-flex;
  color: var(--accent);
}

.dropzone-icon svg {
  width: 32px;
  height: 32px;
}

.dropzone-title {
  font-weight: 700;
  font-size: 1.0625rem;
}

.upload-list,
.document-list,
.user-list,
.plain-list {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
}

.upload-list li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.9375rem;
}

.upload-list .upload-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-list [data-state="failed"] .upload-state {
  color: var(--danger);
}

.upload-list [data-state="done"] .upload-state {
  color: var(--success);
}

.toolbar {
  margin-bottom: 0.5rem;
}

.search-field {
  position: relative;
  display: block;
}

.search-field > span {
  position: absolute;
  left: 0.75rem;
  top: calc(var(--tap) / 2);
  transform: translateY(-50%);
  display: inline-flex;
  color: var(--muted);
}

.search-field input {
  padding-left: 2.5rem;
}

.document-list {
  display: grid;
  gap: 0.5rem;
}

.document {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.document-kind {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-small);
  background: var(--accent-soft);
  color: var(--accent);
}

.document-text {
  min-width: 0;
}

.document-name {
  margin: 0;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.document-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
}

.document-actions {
  display: flex;
  gap: 0.25rem;
}

.document-actions .button {
  min-width: var(--tap);
  padding: 0.5rem;
}

/* --- processing and inspection ---------------------------------------------- */

.chip {
  display: inline-block;
  padding: 0 0.5rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.chip.status-completed {
  background: var(--success-soft);
  border-color: var(--success);
}

.chip.status-failed,
.chip.status-no_text {
  background: var(--danger-soft);
  border-color: var(--danger);
}

.chip.status-pending,
.chip.status-processing {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.notice.warning {
  background: var(--accent-soft);
}

.notice.error {
  background: var(--danger-soft);
}

.inspect-summary {
  margin-bottom: 1rem;
}

.inspect-summary p {
  margin: 0 0 0.5rem;
}

.facts-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 1rem;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 0.875rem;
}

.tabs {
  display: flex;
  gap: 0.25rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.tabs button {
  min-height: var(--tap);
  padding: 0 1rem;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.tabs button[aria-selected="true"] {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.inspect-grid {
  display: grid;
  grid-template-columns: minmax(14rem, 1fr) 2fr;
  gap: 1rem;
  align-items: start;
}

.section-tree {
  position: sticky;
  top: 4.5rem;
  max-height: calc(100dvh - 6rem);
  overflow-y: auto;
  padding: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.section-tree ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.section-tree ul ul {
  padding-left: 1rem;
  border-left: 1px solid var(--border);
  margin-left: 0.5rem;
}

.section-button {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: var(--tap);
  padding: 0.375rem 0.5rem;
  border: 0;
  border-radius: var(--radius-small);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.section-button:hover {
  background: var(--surface-2);
}

.section-button[aria-pressed="true"] {
  background: var(--accent-soft);
}

.section-title {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.section-meta {
  color: var(--muted);
  font-size: 0.8125rem;
}

.chunk-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
}

.chunk-list,
.block-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.5rem;
}

.chunk,
.block {
  padding: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.chunk-meta,
.block-meta {
  margin: 0 0 0.375rem;
  font-size: 0.8125rem;
  overflow-wrap: anywhere;
}

.chunk-text,
.block-text {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-family: inherit;
  font-size: 0.9375rem;
  line-height: 1.5;
}

.chunk-text.is-table {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 0.8125rem;
}

/* The overlap (or a table's repeated header) is the previous chunk's text: shown, but set apart. */
.overlap {
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 4px;
}

.block.kind-heading .block-text {
  font-weight: 700;
}

.block.kind-table .block-text {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 0.8125rem;
}

.block.kind-header .block-text,
.block.kind-footer .block-text {
  color: var(--muted);
  text-decoration: line-through;
}

/* --- people and passkeys ------------------------------------------------------ */

.user-list {
  display: grid;
  gap: 0.5rem;
}

.user {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.user-text {
  min-width: 0;
}

.user-name {
  margin: 0;
  font-weight: 600;
}

.user-detail {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
  overflow-wrap: anywhere;
}

.badge {
  display: inline-block;
  margin-left: 0.375rem;
  padding: 0 0.5rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 0.75rem;
  font-weight: 600;
  vertical-align: middle;
}

.badge.off {
  background: var(--danger-soft);
}

.plain-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

/* --- modal ------------------------------------------------------------------- */

/* A fixed frame: the dialog never resizes with its content,
   which scrolls inside it; it fits a phone and 200% text zoom. */
.modal {
  width: min(32rem, calc(100vw - 2 * var(--gutter)));
  height: min(30rem, calc(100dvh - 2 * var(--gutter)));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.modal::backdrop {
  background: rgb(10 14 20 / 55%);
}

.modal-frame {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.modal-frame h2 {
  margin: 0;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}

.modal-error {
  margin: 0 1.25rem 0.5rem;
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-small);
  background: var(--danger-soft);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
}

.modal-actions .push-left {
  margin-right: auto;
}

/* --- small screens ------------------------------------------------------------ */

@media (max-width: 40rem) {
  .brand-name {
    display: none;
  }

  /* The version shrinks to its tag; a tap shows the number. */
  .version[aria-expanded="false"] .version-text {
    display: none;
  }

  .app-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    justify-content: space-around;
    margin: 0;
    padding: 0.25rem 0.5rem calc(0.25rem + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
  }

  .app-nav button {
    flex: 1;
    flex-direction: column;
    gap: 0;
    font-size: 0.75rem;
    padding: 0.25rem;
  }

  .app-nav:not([hidden]) + .header-tools {
    margin-left: auto;
  }

  body:has(.app-nav:not([hidden])) .shell {
    padding-bottom: calc(5rem + env(safe-area-inset-bottom));
  }

  .document {
    grid-template-columns: auto 1fr;
  }

  .document-actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
  }

  .document-actions .button {
    flex: 1;
  }

  .modal-actions .button {
    flex: 1;
  }

  .modal-actions .push-left {
    flex-basis: 100%;
    margin-right: 0;
  }

  .inspect-grid {
    grid-template-columns: 1fr;
  }

  .section-tree {
    position: static;
    max-height: 40dvh;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
