:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #5f6672;
  --border: #d9dde3;
  --accent: #62147d;
  --accent-text: #ffffff;
  --accent-soft: #f6ebfa;
  --accent-tap: rgb(98 20 125 / 0.15);
  --error: #b42318;
  --error-soft: #fdecea;
  --code-bg: #f1f3f6;
  color-scheme: light;
  /* Checkboxes and other native controls. */
  accent-color: var(--accent);
  /* The flash on tapped controls in mobile browsers, blue by default. */
  -webkit-tap-highlight-color: var(--accent-tap);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1d2026;
    --text: #e8eaed;
    --muted: #9aa1ac;
    --border: #343842;
    --accent: #d095e4;
    --accent-text: #210a29;
    --accent-soft: #331c3b;
    --accent-tap: rgb(208 149 228 / 0.2);
    --error: #ff8a80;
    --error-soft: #3a1f1f;
    --code-bg: #262a31;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

/* Keyboard focus rings, blue by default. */
:focus-visible {
  outline-color: var(--accent);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

main {
  max-width: 44rem;
  margin: 0 auto;
  padding: 2.5rem 1rem 4rem;
}

/* In the installed app, eases in from the phone's launch screen (the same
   background colour, see manifest.webmanifest) rather than cutting to it. */
@media (display-mode: standalone) and (prefers-reduced-motion: no-preference) {
  main {
    animation: appear 2s ease-out;
  }
}

@keyframes appear {
  from {
    opacity: 0;
  }
}

h1 {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0;
  font-size: 1.75rem;
  letter-spacing: -0.02em;
}

.logo {
  width: 2.5rem;
  height: 2.5rem;
}

.tagline,
.hint {
  margin: 0.25rem 0 0;
  color: var(--muted);
}

/* A non-blocking notice, e.g. that files arrived by a share. */
.notice {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-top: 1.25rem;
  padding: 0.6rem 0.4rem 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--accent-soft);
  overflow-wrap: anywhere;
}

.notice span {
  flex: 1;
  padding-top: 0.25rem;
}

.tabs {
  display: flex;
  gap: 0.25rem;
  margin: 1.75rem 0 0;
  border-bottom: 1px solid var(--border);
  /* On the narrowest phones the tabs scroll rather than wrap. */
  overflow-x: auto;
  scrollbar-width: none;
}

.tabs button {
  padding: 0.6rem 1rem;
  border: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}

@media (max-width: 26rem) {
  .tabs {
    gap: 0;
  }

  .tabs button {
    padding: 0.6rem 0.45rem;
  }
}

.tabs button.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.tool {
  padding-top: 1rem;
}

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  margin-top: 1rem;
  padding: 2.25rem 1rem;
  border: 2px dashed var(--border);
  border-radius: 12px;
  background: var(--surface);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.dropzone:hover,
.dropzone:focus-within,
.dropzone.dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* Keep dragleave from firing as the pointer crosses child elements. */
.dropzone > * {
  pointer-events: none;
}

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

/* Pasting needs a keyboard; touch screens don't get the hint. */
.dropzone .paste-hint {
  display: none;
  font-size: 0.85rem;
}

@media (pointer: fine) {
  .dropzone .paste-hint {
    display: inline;
  }
}

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

.files {
  margin: 1rem 0 0;
  padding: 0;
  list-style: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  counter-reset: page;
}

.files li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-top: 1px solid var(--border);
}

.files li:first-child {
  border-top: 0;
}

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

.files .size {
  color: var(--muted);
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}

button {
  font: inherit;
}

button.icon {
  width: 2rem;
  height: 2rem;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  cursor: pointer;
}

button.icon:hover:not(:disabled) {
  background: var(--accent-soft);
  color: var(--text);
}

button:disabled {
  opacity: 0.4;
  cursor: default;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1rem;
}

.actions .total {
  flex: 1;
  color: var(--muted);
}

.primary,
.secondary {
  padding: 0.55rem 1.1rem;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
}

.primary {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
}

.secondary {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.status {
  margin: 1rem 0 0;
  color: var(--muted);
}

button.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}

.progress {
  margin: 1rem 0 0;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.progress progress {
  display: block;
  width: 100%;
  height: 0.5rem;
  margin-bottom: 0.35rem;
  border: 0;
  border-radius: 999px;
  overflow: hidden;
  background: var(--accent-soft);
  appearance: none;
}

.progress progress::-webkit-progress-bar {
  background: var(--accent-soft);
}

.progress progress::-webkit-progress-value {
  background: var(--accent);
  transition: width 0.15s;
}

.progress progress::-moz-progress-bar {
  background: var(--accent);
}

/* No value yet: a sweeping highlight shows that work has started. */
.progress progress:indeterminate {
  background: linear-gradient(
      90deg,
      var(--accent-soft) 0%,
      var(--accent) 50%,
      var(--accent-soft) 100%
    )
    0 0 / 200% 100%;
  animation: progress-sweep 1.2s linear infinite;
}

.progress progress:indeterminate::-webkit-progress-bar {
  background: transparent;
}

.progress progress:indeterminate::-moz-progress-bar {
  background: transparent;
}

@keyframes progress-sweep {
  from {
    background-position: 100% 0;
  }
  to {
    background-position: -100% 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .progress progress:indeterminate {
    animation: none;
  }
}

.error {
  margin: 1rem 0 0;
  padding: 0.6rem 0.8rem;
  border-radius: 8px;
  background: var(--error-soft);
  color: var(--error);
}

.pdf-settings {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1.25rem;
  margin-top: 1rem;
}

.pdf-settings label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.pdf-settings select {
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.pdf-settings .check {
  cursor: pointer;
}

.pdf-settings .check.disabled {
  color: var(--muted);
  cursor: default;
}

.pdf-settings .sort {
  margin-left: auto;
  padding: 0.35rem 0.75rem;
}

.pages {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr));
  gap: 0.75rem;
  margin: 1rem 0 0;
  padding: 0;
  list-style: none;
  counter-reset: page;
}

.pages li {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  counter-increment: page;
}

.pages .sheet {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 9rem;
  /* Lets a sideways image size itself against the sheet (cqw/cqh units). */
  container-type: size;
}

/* Page numbers follow list order, so they update as pages are reordered. */
.pages li::before {
  content: counter(page);
  position: absolute;
  top: -0.5rem;
  left: -0.5rem;
  z-index: 1;
  min-width: 1.6rem;
  height: 1.6rem;
  padding: 0 0.4rem;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1.6rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.25);
}

.pages img {
  max-width: 100%;
  max-height: 100%;
  border: 1px solid var(--border);
  border-radius: 3px;
  /* PDF viewers draw pages on white, so transparent images show on white. */
  background: #ffffff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.12);
  transition: transform 0.2s;
}

/* Turned a quarter, the image's width runs up the sheet's height. */
.pages img.sideways {
  max-width: 100cqh;
  max-height: 100cqw;
}

@media (prefers-reduced-motion: reduce) {
  .pages img {
    transition: none;
  }
}

/* Opposite the page number badge. */
.pages .remove {
  position: absolute;
  top: -0.5rem;
  right: -0.5rem;
  z-index: 1;
  width: 1.6rem;
  height: 1.6rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.75rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.15);
}

.pages .remove:hover {
  border-color: var(--error);
  background: var(--error-soft);
  color: var(--error);
}

/* An A4 or Letter page: the SVG keeps the page's shape within the sheet. */
.pages .paper {
  height: 100%;
  max-width: 100%;
  overflow: visible;
  filter: drop-shadow(0 1px 2px rgb(0 0 0 / 0.2));
}

.pages .paper rect {
  fill: #ffffff;
  stroke: var(--border);
  stroke-width: 1px;
  vector-effect: non-scaling-stroke;
}

.pages .no-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 70%;
  height: 100%;
  border: 1px dashed var(--border);
  border-radius: 3px;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

.pages .caption {
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
  margin-top: 0.5rem;
  font-size: 0.8rem;
}

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

.pages .size {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.pages .controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.25rem;
}

/* Four controls must fit the narrowest card. */
.pages .controls .icon,
.pages .grip {
  width: 1.75rem;
  height: 1.75rem;
}

.pages .grip {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--muted);
  font-size: 1.1rem;
  cursor: grab;
  /* Lets a touch on the grip drag the card instead of scrolling the page. */
  touch-action: none;
  user-select: none;
}

.pages .grip:hover {
  background: var(--accent-soft);
  color: var(--text);
}

@media (pointer: fine) {
  .pages li {
    cursor: grab;
  }

  .pages li button {
    cursor: pointer;
  }
}

.pages.reordering,
.pages.reordering * {
  cursor: grabbing;
  user-select: none;
}

.pages li.unconvertible {
  border-color: var(--error);
}

.pages .card-warning {
  margin: 0.25rem 0 0;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  background: var(--error-soft);
  color: var(--error);
  font-size: 0.75rem;
  text-align: center;
}

.pages li.dragging {
  z-index: 2;
  border-color: var(--accent);
  box-shadow: 0 6px 18px rgb(0 0 0 / 0.2);
  transform: scale(1.04);
}

/* Format text: the empty state. */
.paste-box {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 1rem;
  padding: 1.25rem;
  border: 2px dashed var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.paste-box > .primary {
  align-self: center;
}

.paste-box .actions {
  margin-top: 0;
}

.paste-box .total {
  font-size: 0.875rem;
}

.text-tool textarea {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: 0.875rem/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  resize: vertical;
}

.text-tool textarea.source {
  margin-top: 1rem;
  background: var(--surface);
}

.actions .spacer {
  flex: 1;
}

/* On phones the toolbar's buttons share each row evenly. */
@media (max-width: 30rem) {
  .text-tool > .actions > button {
    flex: 1 1 auto;
  }

  .text-tool > .actions > .spacer {
    display: none;
  }
}

/* Format text: the formatted answer, laid out like the AI apps show it. */
.formatted {
  margin-top: 1rem;
  padding: 1.25rem 1.5rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-size: 1rem;
  line-height: 1.65;
  overflow-wrap: break-word;
}

@media (max-width: 30rem) {
  .formatted {
    padding: 1rem;
  }
}

.formatted > :first-child {
  margin-top: 0;
}

.formatted > :last-child {
  margin-bottom: 0;
}

.formatted h1,
.formatted h2,
.formatted h3,
.formatted h4,
.formatted h5,
.formatted h6 {
  margin: 1.5em 0 0.5em;
  line-height: 1.3;
  letter-spacing: -0.01em;
}

.formatted h1 {
  font-size: 1.5rem;
}

.formatted h2 {
  font-size: 1.3rem;
}

.formatted h3 {
  font-size: 1.12rem;
}

.formatted h4,
.formatted h5,
.formatted h6 {
  font-size: 1rem;
}

.formatted p,
.formatted ul,
.formatted ol,
.formatted blockquote,
.formatted pre,
.formatted .table-wrap,
.formatted .math-display {
  margin: 0 0 0.85em;
}

.formatted ul,
.formatted ol {
  padding-left: 1.5em;
}

.formatted li {
  margin: 0.3em 0;
}

.formatted li > p {
  margin: 0.3em 0;
}

.formatted li > ul,
.formatted li > ol {
  margin: 0.2em 0;
}

.formatted li::marker {
  color: var(--muted);
}

/* Task lists: the checkbox stands in for the bullet. */
.formatted li:has(> input[type="checkbox"]:first-child) {
  margin-left: -1.25em;
  list-style: none;
}

.formatted li > input[type="checkbox"] {
  margin: 0 0.45em 0 0;
}

.formatted strong {
  font-weight: 650;
}

/* Links that were dropped as unsafe have no href and read as plain text. */
.formatted a[href] {
  color: var(--accent);
}

.formatted hr {
  margin: 1.5em 0;
  border: 0;
  border-top: 1px solid var(--border);
}

.formatted blockquote {
  padding: 0.5em 1em;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: var(--accent-soft);
}

.formatted blockquote > :last-child {
  margin-bottom: 0;
}

.formatted code,
.formatted .math {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: var(--code-bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
}

.formatted pre {
  padding: 0.9em 1em;
  border-radius: 10px;
  background: var(--code-bg);
  line-height: 1.5;
  overflow-x: auto;
}

.formatted pre code {
  padding: 0;
  background: none;
  font-size: 0.85rem;
}

.formatted .code-label {
  padding: 0.4em 1em;
  border-bottom: 1px solid var(--border);
  border-radius: 10px 10px 0 0;
  background: var(--code-bg);
  color: var(--muted);
  font-size: 0.8rem;
}

.formatted .code-label + pre {
  border-radius: 0 0 10px 10px;
}

.formatted .math-display {
  display: block;
  padding: 0.6em 1em;
  overflow-x: auto;
  text-align: center;
  white-space: pre;
}

.formatted .table-wrap {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}

.formatted table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95em;
}

.formatted th,
.formatted td {
  padding: 0.5em 0.75em;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.formatted th + th,
.formatted td + td {
  border-left: 1px solid var(--border);
}

.formatted th {
  background: var(--code-bg);
  font-weight: 600;
}

.formatted tbody tr:last-child td {
  border-bottom: 0;
}

.formatted .align-center {
  text-align: center;
}

.formatted .align-right {
  text-align: right;
}

/* Citations: superscript links to the numbered sources at the end. */
.formatted sup.cite {
  margin-left: 0.1em;
  font-size: 0.7em;
  font-weight: 600;
  line-height: 0;
  white-space: nowrap;
}

.formatted sup.cite a {
  text-decoration: none;
}

.formatted sup.cite a:hover {
  text-decoration: underline;
}

.formatted p:has(> .source-label) {
  margin: 0.2em 0;
  color: var(--muted);
  font-size: 0.9em;
  overflow-wrap: anywhere;
}

.formatted .source-label {
  display: inline-block;
  min-width: 1.5em;
  margin-right: 0.25em;
  padding: 0 0.3em;
  border-radius: 4px;
  background: var(--code-bg);
  color: var(--text);
  font-size: 0.85em;
  font-weight: 600;
  text-align: center;
  scroll-margin-top: 1rem;
}

/* The source a citation was followed to. */
.formatted .source-label:target {
  background: var(--accent);
  color: var(--accent-text);
}

/* Save as PDF prints just the formatted answer, in light colours whatever
   the screen's theme. */
@media print {
  :root {
    --bg: #ffffff;
    --surface: #ffffff;
    --text: #000000;
    --muted: #4a4f57;
    --border: #d0d4da;
    --accent: #62147d;
    --accent-soft: #f6ebfa;
    --code-bg: #f1f3f6;
    color-scheme: light;
  }

  @page {
    margin: 16mm;
  }

  main:has(.text-tool:not([hidden])) {
    max-width: none;
    padding: 0;
    animation: none;
  }

  main:has(.text-tool:not([hidden])) > :not(.text-tool),
  .text-tool > :not(.formatted) {
    display: none !important;
  }

  .text-tool {
    padding: 0;
  }

  .formatted {
    margin: 0;
    padding: 0;
    border: 0;
    background: none;
    font-size: 11pt;
    /* Keep the code and table backgrounds, which browsers drop by default. */
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .formatted h1,
  .formatted h2,
  .formatted h3,
  .formatted h4 {
    break-after: avoid;
  }

  .formatted pre,
  .formatted blockquote,
  .formatted tr,
  .formatted .math-display {
    break-inside: avoid;
  }

  /* Paper can't scroll: wrap long lines and let wide tables shrink. */
  .formatted pre,
  .formatted .math-display {
    white-space: pre-wrap;
  }

  .formatted .table-wrap {
    overflow: visible;
  }
}
