:root {
  color-scheme: light;
  --bg: #f4f1ea;
  --card-bg: #ffffff;
  --text: #1c1a17;
  --muted: #6b675f;
  --accent: #b5502f;
  --accent-hover: #9a3f22;
  --success: #2f7a3d;
  --success-hover: #256330;
  --border: #e4dfd3;
  --owner-bubble: #f0ece2;
  --scanner-bubble: #b5502f;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  overflow-x: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.page {
  max-width: 480px;
  margin: 0 auto;
  padding: 24px 16px 40px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.brand-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  margin-bottom: 16px;
}

.brand-logo {
  display: block;
  max-width: 160px;
}

.header-link {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.header-link:hover { color: var(--accent); }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  margin-top: 24px;
}

h1 { font-size: 1.5rem; margin: 0 0 8px; }
.subtitle { color: var(--muted); line-height: 1.5; margin: 0 0 20px; }

.stack { display: flex; flex-direction: column; gap: 6px; }
.form-error { color: var(--accent); font-size: 0.85rem; margin: 0 0 12px; }
.stack label { font-size: 0.85rem; font-weight: 600; margin-top: 12px; }
.stack input, .stack textarea {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  resize: vertical;
}

button {
  font: inherit;
  font-weight: 600;
  padding: 12px 16px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  margin-top: 16px;
}
button:hover { background: var(--accent-hover); }
button:disabled { background: var(--muted); cursor: not-allowed; }

.form-hint { color: var(--muted); font-size: 0.85rem; margin: 0 0 12px; }

.chat-card { display: flex; flex-direction: column; gap: 12px; }

.app-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--owner-bubble);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.85rem;
}
.app-banner-link {
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 55vh;
  overflow-y: auto;
  padding: 4px 0;
}

/* Time separator between messages sent meaningfully far apart (see chat.js) — the
   scanner-side equivalent of iMessage's date/time dividers. */
.chat-divider {
  align-self: center;
  margin: 6px 0 2px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
}

/* Reactions: the pill overlaps the bubble's bottom-outer corner (iMessage-style), so a
   reacted-to bubble gets extra bottom margin to make room. `.reactions--mine` marks a
   pill that includes this scanner's own reaction. The tray is the six-button picker
   that a tap on a bubble toggles (a long-press would fight Safari's selection callout). */
.message {
  max-width: 80%;
  position: relative;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  /* Column flex so the quote block hugs its own text instead of stretching to the
     full 80%, which left it visibly wider than the bubble it belongs to. */
  display: flex;
  flex-direction: column;
}
.message-owner { align-items: flex-start; }
.message-scanner { align-items: flex-end; }
.message--reacted { margin-bottom: 14px; }
.reactions {
  position: absolute;
  bottom: -14px;
  display: flex;
  gap: 2px;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  font-size: 0.9rem;
  line-height: 1.3;
  z-index: 1;
}
.reactions:empty { display: none; }
.reactions--mine { border-color: var(--accent); }
.message-owner .reactions { right: -6px; }
.message-scanner .reactions { left: -6px; }
.reaction-tray {
  display: flex;
  gap: 2px;
  margin-top: 6px;
  padding: 4px;
  border-radius: 999px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.14);
  width: max-content;
}
.message-scanner .reaction-tray { margin-left: auto; }
.reaction-tray button {
  margin: 0;
  padding: 4px 6px;
  min-width: 36px;
  background: transparent;
  color: inherit;
  border: 0;
  border-radius: 999px;
  font-size: 1.25rem;
  line-height: 1;
}
.reaction-tray button:hover, .reaction-tray button:active { background: var(--owner-bubble); }
.reaction-tray button[aria-pressed="true"] { background: var(--owner-bubble); box-shadow: inset 0 0 0 2px var(--accent); }
.message-body {
  padding: 10px 14px;
  border-radius: 16px;
  line-height: 1.4;
  word-break: break-word;
}
/* pre-wrap lives on the text itself, not the bubble: the bubble now also contains the
   quote panel, and pre-wrap there would turn the markup's own whitespace into a
   visible gap before the message. */
.message-text { display: block; white-space: pre-wrap; }
/* The quoted-message block on a reply, rendered as the bubble's first child on both
   sides (see scan_chat.html and appendMessage in chat.js). It sits inside `.message`,
   which is itself a tap target for the reaction tray, so chat.js intercepts clicks on
   it to jump to the original instead.

   It normally sits INSIDE `.message-body` (the bubble), as an inset panel above the
   text — that adjacency is what makes it read as one message answering another rather
   than as two stacked bubbles. A photo-only reply has no bubble, so there it's a
   direct child of `.message` and needs its own opaque background instead; the two
   cases are told apart below by `.message-body .message-quote` vs `.message > .message-quote`. */
.message-quote {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 5px 10px;
  border-left: 3px solid currentColor;
  border-radius: 4px;
  font-size: 0.85rem;
  line-height: 1.3;
  white-space: pre-wrap;
  word-break: break-word;
}
.message-quote-author { font-weight: 600; font-size: 0.8rem; }
.message-quote-text {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  opacity: 0.85;
}
/* Inside the bubble: a translucent darkening of whatever the bubble already is, so it
   reads as a recessed panel rather than a second colour. The negative side margins pull
   it out to the bubble's padding edge, which is what makes it look built-in. */
.message-body .message-quote {
  margin: -2px -8px 7px -10px;
  border-radius: 6px;
}
.message-owner .message-body .message-quote {
  background: rgba(0, 0, 0, 0.06);
  border-left-color: var(--accent);
}
.message-scanner .message-body .message-quote {
  background: rgba(0, 0, 0, 0.2);
  border-left-color: rgba(255, 255, 255, 0.9);
}
/* Standalone (photo-only reply): nothing behind it, so it needs a real background. */
.message-owner > .message-quote {
  margin-bottom: 3px;
  background: var(--owner-bubble);
  color: var(--text);
  border-left-color: var(--accent);
}
.message-scanner > .message-quote {
  margin-bottom: 3px;
  background: var(--scanner-bubble);
  color: #fff;
  border-left-color: rgba(255, 255, 255, 0.9);
}
/* The author line is the "replying to X" signal, so it stays crisp; the quoted text
   itself recedes. */
.message-quote-author { opacity: 0.95; }
/* Flashed for a moment after tapping a quote jumps to the quoted message. */
.message--highlight .message-body { animation: quote-flash 1.2s ease-out; }
@keyframes quote-flash {
  0%, 55% { box-shadow: 0 0 0 3px var(--accent); }
  100% { box-shadow: 0 0 0 3px transparent; }
}

.message-scanner { align-self: flex-end; }
.message-scanner .message-body { background: var(--scanner-bubble); color: white; border-bottom-right-radius: 4px; }
.message-owner { align-self: flex-start; }
.message-owner .message-body { background: var(--owner-bubble); border-bottom-left-radius: 4px; }

.message-image {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  margin-bottom: 4px;
}

.attachment-preview {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--owner-bubble);
  border-radius: 10px;
  padding: 8px;
}
.attachment-preview img { width: 48px; height: 48px; object-fit: cover; border-radius: 8px; }
.attachment-preview button {
  margin: 0 0 0 auto;
  padding: 4px 10px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.flag-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--owner-bubble);
  border-radius: 10px;
  padding: 12px;
}
.flag-card-text { margin: 0; line-height: 1.4; white-space: pre-wrap; word-break: break-word; }
.flag-card-actions { display: flex; gap: 8px; }
.flag-card-actions button { margin-top: 0; }

.btn-secondary {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }

/* `.attachment-preview`/`.flag-card` both set `display: flex` unconditionally, and an
   author stylesheet's normal-priority rule always wins over the browser's built-in
   `[hidden] { display: none }` regardless of selector specificity — so without this,
   the `hidden` attribute on those elements is silently ignored and they show
   permanently. Re-assert `display: none` at higher specificity when `hidden` is set. */
.attachment-preview[hidden],
.flag-card[hidden],
.reply-banner[hidden] {
  display: none;
}

/* Sits between the message list and the composer while a reply is being written —
   modeled on `.attachment-preview`, including its `[hidden]` caveat above. */
.reply-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  border-left: 3px solid var(--accent);
  background: var(--owner-bubble);
  font-size: 0.9rem;
}
.reply-banner-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reply-banner button {
  margin: 0;
  padding: 4px 10px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
.reaction-tray-reply { font-size: 1.1rem; }

.intro {
  margin-bottom: 20px;
}
.intro-image {
  display: block;
  width: 100%;
  height: 220px;
  object-fit: contain;
  background: var(--owner-bubble);
  border-radius: 12px;
  margin-bottom: 12px;
}
/* A portrait photo (see intro_image_portrait in web/scan.py) reads better beside the
   text than stacked full-width above it — but only when there's actually room for a
   readable side column; below 430px (roughly every iPhone's portrait viewport width)
   it falls back to the same stacked layout as a landscape photo. */
.intro--portrait {
  display: flex;
  align-items: flex-start;
  gap: 14px;
}
.intro--portrait .intro-image {
  flex: 0 0 38%;
  width: 38%;
  margin-bottom: 0;
}
.intro--portrait .intro-text {
  flex: 1;
  min-width: 0;
}
@media (max-width: 430px) {
  .intro--portrait {
    flex-direction: column;
  }
  .intro--portrait .intro-image {
    width: 100%;
    flex-basis: auto;
    margin-bottom: 12px;
  }
}
.intro h2 { font-size: 1.1rem; margin: 0 0 6px; }
.intro p { margin: 0 0 10px; line-height: 1.5; }
.intro-list { margin: 0 0 10px; padding-left: 20px; line-height: 1.5; }
.intro :last-child { margin-bottom: 0; }

.category-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}
.category-icon { width: 28px; height: 28px; object-fit: contain; }

.response-choices { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
.response-choice {
  margin-top: 0;
  background: var(--card-bg);
  color: var(--text);
  border: 1px solid var(--border);
  text-align: left;
  font-weight: 500;
}
.response-choice:hover { background: var(--owner-bubble); }

.chat-form { display: flex; align-items: center; gap: 8px; }
.chat-form input[type="text"]:disabled { color: var(--muted); background: var(--bg); }
.attach-button {
  font-size: 1.3rem;
  cursor: pointer;
  padding: 8px;
  line-height: 1;
  border: none;
  border-radius: 8px;
  background: none;
}
.attach-button:disabled { cursor: default; opacity: 0.5; }
/* Deliberate state color, not the generic `button` background — red while the scanner
   doesn't have scanner_photos_allowed yet (tap to request), green once the owner grants
   it (tap to attach). The two-class-selector specificity here beats the plain
   `button:hover`/`button` rules above without needing source-order luck. */
.attach-button--pending { background: var(--accent); }
.attach-button--pending:hover, .attach-button--pending:active { background: var(--accent-hover); }
.attach-button--allowed { background: var(--success); }
.attach-button--allowed:hover, .attach-button--allowed:active { background: var(--success-hover); }
.chat-form input[type="text"] {
  flex: 1;
  font: inherit;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.chat-form button { margin-top: 0; }

.connection-status { font-size: 0.8rem; color: var(--muted); text-align: center; min-height: 1.2em; }

.app-store-link {
  display: inline-block;
  background: var(--accent);
  color: white;
  text-decoration: none;
  font-weight: 700;
  padding: 12px 20px;
  border-radius: 10px;
}

/* The inline Terms disclosure sits on the left of one row, with links out to the full
   Terms and the Privacy Policy on the right. flex-start (not center) keeps the link pinned to the
   summary's line rather than drifting down the page when the terms are expanded. */
.legal-note {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-top: 16px;
}
.legal-note .terms-note {
  flex: 1 1 auto;
  min-width: 0;
  margin-top: 0;
}
.legal-note-links {
  flex: 0 0 auto;
  display: flex;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}
.legal-note-links a {
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
}
.legal-note-links a:hover { color: var(--accent); text-decoration: underline; }

.terms-note {
  margin-top: 16px;
  font-size: 0.85rem;
  color: var(--muted);
}
.terms-note summary {
  cursor: pointer;
  list-style: none;
  font-weight: 600;
}
.terms-note summary::-webkit-details-marker { display: none; }
.terms-note summary::before {
  content: "▸";
  display: inline-block;
  margin-right: 4px;
  transition: transform 0.15s ease;
}
.terms-note[open] summary::before { transform: rotate(90deg); }
.terms-note .terms-body {
  margin-top: 8px;
  max-height: 260px;
  overflow-y: auto;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
}
.terms-note .terms-body p { margin: 0 0 8px; }
.terms-note .terms-body ol { margin: 0 0 8px; padding-left: 1.2em; }
.terms-note .terms-body li { margin-bottom: 6px; }
