:root {
  font: 16px/1.4 system-ui, sans-serif;
  color: #191b31;
  background: #eef0f7;
  --purple: #4625d3;
  --purple-dark: #3720a4;
  --tint: #f1edfc;
  --line: #d4d8e6;
  --muted: #525a71;
}

/* Keep the document, active field, and navigation visible together. */
* { box-sizing: border-box; }
body { margin: 0; display: flex; flex-direction: column; min-height: 100dvh; }
h1, h2, p { margin: 0; }
.top, main, .tray-inner { width: min(100%, 1460px); margin: 0 auto; padding-inline: clamp(16px, 3vw, 36px); }
.app-header, .tray { flex: none; background: #fff; }
.app-header { border-bottom: 1px solid var(--line); }
.top, .top-controls, .page-controls, .actions {
  display: flex; align-items: center; gap: 10px;
}
.top { justify-content: space-between; gap: 12px; overflow-x: auto; padding-block: 8px; scrollbar-width: thin; }
.top > * { flex: none; white-space: nowrap; }
.top-controls { gap: 6px; }
.top-controls :is(button, select, .file-picker) { min-height: 34px; padding: 5px 8px; }
.top-controls #samples { max-width: 132px; }
.actions { flex-wrap: wrap; }
.brand {
  display: flex; align-items: center; gap: 9px; font-size: 1.25rem; font-weight: 770;
  letter-spacing: -.035em;
}
.brand-mark { width: 27px; height: 23px; color: var(--purple); }
.brand small { color: var(--muted); font-size: .68rem; font-weight: 620; letter-spacing: 0; }

/* Use ordinary controls for file upload, review, and keyboard navigation. */
button, select, input, textarea { font: inherit; }
button, select, .file-picker {
  min-height: 38px; padding: 8px 12px; border: 1px solid var(--line);
  border-radius: 8px; color: inherit; background: #fff; font-size: .87rem; font-weight: 635;
}
button:not(:disabled), select:not(:disabled), .file-picker { cursor: pointer; }
button:hover:not(:disabled), .file-picker:hover { background: var(--tint); }
button:disabled { opacity: .51; }
button.primary { background: var(--purple); border-color: var(--purple); color: white; }
button.primary:hover:not(:disabled) { background: var(--purple-dark); }
.file-picker { display: inline-flex; align-items: center; position: relative; }
.file-picker input { position: absolute; inset: 0; opacity: 0; width: 100%; cursor: pointer; }
.file-picker:has(input:disabled) { opacity: .51; }
:focus-visible, .file-picker:focus-within { outline: 3px solid #825ff5; outline-offset: 2px; }

/* Place scrollable preview and assistance panes side by side. */
main { flex: 1; padding-block: 12px; }
.status { min-height: 20px; margin-bottom: 10px; color: var(--muted); font-size: .85rem; overflow-wrap: anywhere; }
.status:empty { display: none; }
.error { color: #a02334; }
.workspace {
  display: grid; position: relative; grid-template-columns: minmax(0, 1fr); gap: 13px;
}
#viewer, .panel { min-width: 0; }
.page-controls { gap: 4px; font-size: .83rem; color: #353c55; }
.close-button, .page-controls button, .field-navigation > button {
  min-width: 32px; padding: 4px 7px; font-size: 1.16rem; line-height: 1;
}
#focus[aria-pressed=true] { background: var(--tint); color: var(--purple-dark); border-color: #dcd2fc; }
.pdf-scroll {
  position: relative; height: min(56dvh, 520px); overflow: auto; border-radius: 12px;
  background: #dfe3ef; box-shadow: 0 10px 26px #23234b15; scrollbar-gutter: stable;
  touch-action: pan-x pan-y pinch-zoom; cursor: grab;
}
.pdf-scroll.is-dragging, .pdf-scroll.is-dragging * { cursor: grabbing; user-select: none; }
.paper { position: relative; isolation: isolate; overflow: hidden; margin-inline: auto; background: #fff; }
canvas { display: block; width: 100%; height: 100%; }
.hotspots { position: absolute; inset: 0; pointer-events: none; }
.hotspot {
  position: absolute; pointer-events: auto; border-radius: 2px; border: 1px solid #6543df88;
  padding: 0; background: #7249ed15; min-width: 0; min-height: 0;
}
.hotspot:hover:not(:disabled) { background: #7249ed30; }
.hotspot[aria-pressed=true] { z-index: 1; outline: 2px solid var(--purple); background: transparent; }
.paper.focus .hotspot[aria-pressed=true] { box-shadow: 0 0 0 1600px #242c4848; }
/* Frame the region being scanned without obscuring its printed text. */
.paper[data-ocr-active="true"]::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  border: 4px solid #007d69;
  box-shadow: inset 0 0 0 2px #c4f5df;
  pointer-events: none;
}
/* Distinguish Nano's fields from the ordinary purple outlines. */
.hotspot[data-nano-active="true"] {
  z-index: 2;
  outline: 2px dashed #884600;
  outline-offset: -1px;
  background: #ffd77a58;
}
.paper:has(.hotspot[data-nano-active="true"]) .hotspot[aria-pressed="true"] { box-shadow: none; }
.connector { display: none; position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }

/* Keep the reviewed draft and its source in the same editing panel. */
.panel {
  grid-row: 1;
  padding: clamp(10px, 1.5vw, 17px); border: 1px solid #e0e2ef;
  background: #fff; border-radius: 15px; box-shadow: 0 12px 26px #23234b12;
  font-size: .84rem;
}
.review-heading h2 { font-size: clamp(1.2rem, 2.1vw, 1.6rem); letter-spacing: -.035em; line-height: 1.18; }
.panel h2 { font-size: 1.08rem; line-height: 1.2; }
.field-heading { display: flex; align-items: center; gap: 7px; }
.info-button { flex: none; min-width: 32px; min-height: 32px; padding: 0; border-radius: 50%; color: var(--purple-dark); }
#heading-nano-mark { --nano-icon-size: 26px; }
.panel p { color: #494f67; margin: 7px 0; }
.panel .field-help { margin-top: 10px; padding: 9px 11px; background: var(--tint); border-radius: 8px; }
.panel p:empty { display: none; }
.field-help-dialog { font-size: .84rem; }
.field-help-dialog h2 { font-size: 1.08rem; }
.field-help-dialog p { margin-block: 8px; }
.field-help-dialog p:empty { display: none; }
#requester-help { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.form-context .field-reference { font-family: ui-monospace, monospace; font-size: .74rem; overflow-wrap: anywhere; }
#field-help { color: #292242; font-weight: 560; }
.field-tools { margin-top: 16px; border-top: 1px solid var(--line); }
#show-active-nano-group { min-height: 30px; padding: 4px 9px; border-color: #c18e4a; }
.field-picker { margin-top: 12px; font-size: .86rem; color: var(--muted); }
.field-picker summary { cursor: pointer; width: fit-content; font-weight: 630; }
.form-context p { white-space: pre-line; overflow-wrap: anywhere; }
.form-context-title, .form-context-summary { font-weight: 650; }
.panel form, .control { display: grid; gap: 6px; margin-top: 10px; }
.panel select, .panel textarea, .panel input[type=text] { width: 100%; min-width: 0; }
.panel textarea, .panel input[type=text] {
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px;
  color: inherit; background: #fff;
}
.panel textarea { resize: vertical; display: block; }
.control { font-size: .88rem; font-weight: 670; }
.control textarea, .control input { font-weight: 400; }
.assistance { min-width: 0; margin: 13px 0; padding: 10px 12px; border: 1px solid #e1d8fa; border-radius: 10px; background: var(--tint); }
.assistance legend { font-size: .88rem; font-weight: 720; padding-inline: 4px; }
.assistance .control:first-of-type { margin-top: 4px; }
.assistance .actions { gap: 7px; margin-top: 10px; }
.model-status, #field-status { font-size: .83rem; }
.assistance .model-status { margin-bottom: 0; }
.panel :is(.control, button, .field-picker, .model-status, #field-status) { font-size: .78rem; }
.panel textarea { padding: 6px 8px; }
.editor-fields { min-inline-size: 0; margin: 0; padding: 0; border: 0; }
.editor-row { display: grid; gap: 6px; }
.editor-row .control { min-width: 0; margin: 0; }
.editor-row .translation-input, .editor-row .translation-actions { display: none; }
#editor:has(#declaration-panel:not([hidden])) .editor-row {
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: end;
}
#editor:has(#declaration-panel:not([hidden])) :is(.translation-input, .translation-actions) { display: grid; }
.translation-actions { justify-items: stretch; }
.translation-actions button { min-height: 32px; padding-inline: 6px; }
.translation-actions:has(#declaration-cloud:not([hidden])) #declaration-translate:disabled { display: none; }
#declaration-source { height: 4.5em; }
#editor:has(#declaration-panel:not([hidden])) #answer { height: 4.5em; }
.suggestion, #declaration-review { margin-top: 9px; padding: 9px 11px; border-radius: 8px; background: #fff; }
.spanish-review { margin-block: 3px; }
.spanish-review .suggestion, .back-result { padding: 9px 11px; border-radius: 8px; background: var(--tint); }
.back-result { margin-top: 8px; }
.back-result p { white-space: pre-wrap; overflow-wrap: anywhere; }
#address-candidate { white-space: pre-wrap; overflow-wrap: anywhere; }
#declaration-review { margin-top: 0; border: 1px solid #e1be76; background: #fff6e3; }
#declaration-issues { white-space: pre-line; overflow-wrap: anywhere; }
#answer[aria-invalid=true] { border-color: #9d5c00; }

/* Anchor the nearby field cards and keep review accessible from the header. */
.tray { position: sticky; bottom: 0; z-index: 5; border-top: 1px solid var(--line); box-shadow: 0 -8px 24px #23234b08; }
.tray-inner { padding-block: 7px; }
#field-progress { position: absolute; inset: 0 0 auto; width: 100%; height: 3px; accent-color: var(--purple); pointer-events: none; }
.field-navigation { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: stretch; gap: 7px; }
.field-navigation > button { touch-action: none; user-select: none; -webkit-touch-callout: none; }
.cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.field-card, .review-entry {
  min-width: 0; display: flex; align-items: center; gap: 8px; text-align: left;
  padding: 7px 10px; min-height: 50px;
}
.field-card[aria-current="step"] { color: var(--purple-dark); background: var(--tint); border-color: #7254e8; }
.field-card strong, .field-card small, .review-entry strong, .review-entry small { display: block; }
.field-card .nano-icon, .review-entry .nano-icon { --nano-icon-size: 18px; margin-inline-end: 5px; }
.field-card strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.field-card > span:last-child, .review-entry > span:last-child { min-width: 0; }
.field-card small, .review-entry small { color: var(--muted); font-size: .74rem; font-weight: 520; }
.field-mark { align-self: center; color: var(--purple); font-weight: 790; }
.field-mark.filled { color: #137151; }
.field-mark.draft { color: #9d5c00; }

/* Use a native dialog so Escape and focus restoration follow browser behavior. */
.review-dialog {
  width: min(620px, calc(100vw - 28px)); max-height: min(680px, 88dvh); padding: 22px;
  color: inherit; background: #fff; border: 1px solid var(--line); border-radius: 15px;
  box-shadow: 0 20px 54px #17182d34;
}
.review-dialog::backdrop { background: #20243e73; }
.review-heading { display: flex; gap: 15px; justify-content: space-between; align-items: start; }
.review-summary { color: var(--muted); font-size: .88rem; margin-top: 8px; }
.review-items { list-style: none; padding: 0; margin: 14px 0; display: grid; gap: 8px; }
.review-entry { width: 100%; }
.review-entry .preview { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 3px; }
.review-entry .preview-caption { margin-top: 5px; }
.review-validation { margin-top: 14px; padding: 12px; border: 1px solid var(--line); border-radius: 10px; }
.review-validation h3 { margin: 0; font-size: .95rem; }
.review-validation button { margin-top: 8px; }
.review-validation .validation-warnings { margin: 4px 0; gap: 0; }
.review-validation .review-entry { display: block; text-align: start; }
.review-validation .review-entry small { margin-top: 3px; color: #854b00; white-space: normal; }
.review-validation :is(p, ul):empty { display: none; }
.review-actions {
  position: sticky; bottom: -22px; justify-content: flex-end;
  margin: 0 -22px -22px; padding: 10px 22px 14px;
  border-top: 1px solid var(--line); background: #fff;
}
[hidden] { display: none !important; }

/* Size both panes from the space left by the actual header and navigation tray. */
@media (min-width: 700px) and (min-height: 400px) {
  body { height: 100dvh; }
  main { display: flex; flex-direction: column; min-height: 0; }
  .status { flex: none; }
  .workspace {
    flex: 1; min-height: 0; grid-template-columns: minmax(265px, 1fr) minmax(0, 1.35fr);
    grid-template-rows: minmax(0, 1fr); gap: clamp(12px, 2vw, 27px);
  }
  #viewer { display: flex; flex-direction: column; min-height: 0; }
  .pdf-scroll { flex: 1; height: auto; min-height: 0; }
  .panel { grid-row: auto; min-height: 0; overflow: auto; }
  .connector { display: block; }
}

/* Keep the entire editor usable in short landscape windows, including on tablets. */
@media (min-width: 700px) and (min-height: 400px) and (max-height: 650px) {
  .top { padding-block: 8px; }
  main { padding-block: 8px; }
  .panel { padding: 12px; }
  .panel #editor { margin-top: 4px; }
  #editor .control { margin-top: 0; }
  #answer { max-height: clamp(52px, 13dvh, 84px); }
  .tray-inner { padding-block: 6px 8px; }
  .field-card { min-height: 44px; }
}
@media (min-width: 700px) and (min-height: 400px) and (max-height: 480px) {
  .panel { padding-block: 9px; }
  #editor:has(#declaration-panel[hidden]) { grid-template-columns: minmax(0, 1fr) auto; align-items: end; }
  #editor:has(#declaration-panel[hidden]) > button[type=submit] { grid-column: 2; grid-row: 1; }
  #editor :is(#declaration-review, #spanish-review) { grid-column: 1 / -1; }
}
@media (max-width: 900px) {
  .top { padding-inline: 12px; }
  .top-controls { gap: 4px; }
  .top-controls :is(button, select, .file-picker) { padding-inline: 6px; }
  .brand { gap: 5px; font-size: 1.06rem; }
  .brand small { display: none; }
  .brand-mark { width: 23px; }
}
@media (max-width: 520px) {
  .field-navigation { gap: 4px; }
  .cards { gap: 4px; }
  .field-card { min-height: 47px; padding: 5px; gap: 4px; font-size: .75rem; }
  .field-card small { font-size: .67rem; }
}
