/* Loaded last: mobile ergonomics without changing the original theme.
   Safe-area tokens may be overridden on :root for device/QA simulation. */
:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --muted: #607167;
  --danger: #9f4e42;
}

/* Readability and minimum hit areas (checkbox labels own their hit area). */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body { font-size: 15px; }
button, summary, .check { min-height: 44px; }
button { min-width: 44px; }
button, summary, .check, a { touch-action: manipulation; }
input:not([type="checkbox"]), textarea, select {
  min-height: 48px;
  font-size: 16px;
}
.quiet, .close { min-width: 44px; min-height: 44px; }
.close { width: 44px; height: 44px; flex: 0 0 44px; }
.primary { min-height: 48px; font-size: 15px; }
.secondary { min-height: 44px; font-size: 15px; }
.quiet { font-size: 13px; white-space: normal; }
/* Preserve touch width despite the min-width:0 flex overflow reset below. */
.top > button.quiet, .section-heading > button.quiet { min-width: 44px; flex-shrink: 0; }
summary { padding: 10px 4px; cursor: pointer; }
.paste-details summary { display: block; }
.check { padding: 8px 0; overflow-wrap: anywhere; }
input[type="checkbox"], .chat-row .row-memory input {
  width: 20px;
  height: 20px;
  min-width: 20px;
  min-height: 20px;
}
.brand small, .hint, footer, .entry-info small, .entry-amount small,
.loading small, .chat-message > span, .chat-row small,
.chat-row .row-memory, .chat-tools .quiet, .chat-modal > .hint,
.chat-modal > .quiet, .memory-row small { font-size: 12px; }
.entry-info strong, .chat-message p, .chat-row strong { font-size: 15px; }
footer, .entry-amount small, .draft-fields label:not(.check) { color: var(--muted); }
.check { color: #526757; }
.status { color: #356b4b; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
button:focus-visible, a:focus-visible, summary:focus-visible { outline-color: #386b50; }
input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 2px solid #386b50; outline-offset: 1px; }
.chat-question > p { font-size: 14px; }

/* Safe-area padding also applies when launched in standalone display mode. */
.page {
  padding: calc(24px + var(--safe-top)) calc(16px + var(--safe-right))
    calc(32px + var(--safe-bottom)) calc(16px + var(--safe-left));
}
.page, .modal, .page section, .section-heading > *, .top > *,
.chat-modal > *, .chat-message, .chat-message p, .memory-row > * {
  min-width: 0;
}
.page, .modal { overflow-wrap: anywhere; }
img { max-width: 100%; }
.section-heading { flex-wrap: wrap; }
.top { gap: 8px; }
.brand { min-height: 44px; }
.balance .section-heading { gap: 8px; }
.balance input { width: 190px; max-width: 100%; font-size: 16px; }
.balance h1 { overflow-wrap: anywhere; }
.balance p { overflow-wrap: anywhere; }
.import-card { padding: 18px; }
.import-card > .import-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.import-card > .import-actions > .primary { grid-column: 1 / -1; }
.import-card > .import-actions > .secondary:last-child:nth-child(2) { grid-column: 1 / -1; }
.import-actions > button { min-width: 0; }
.import-card > .import-actions > button { padding: 11px 8px; }
.category-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.category-tile { min-height: 72px; }
.category-tile strong { max-width: 100%; }
.entry { align-items: flex-start; flex-wrap: wrap; }
.entry-info { flex: 1 1 120px; }
.entry-amount {
  flex: 0 1 auto;
  max-width: 100%;
  min-width: 0;
  margin-left: auto;
  white-space: normal;
  overflow-wrap: anywhere;
}
.memory-row { flex-wrap: wrap; }

/* VisualViewport coordinates come from the application, not a second server.
   The safe area is reserved once here, outside the scrollable dialog. */
.backdrop {
  top: var(--app-viewport-top, 0px);
  height: var(--app-viewport-height, 100dvh);
  bottom: auto;
  padding: calc(12px + var(--safe-top)) calc(12px + var(--safe-right))
    calc(12px + var(--safe-bottom)) calc(12px + var(--safe-left));
  overflow: hidden;
}
.modal {
  max-height: 100%;
  padding: 20px;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
}
.modal > .section-heading { flex-wrap: nowrap; }
.sticky-actions { bottom: -20px; padding-bottom: 12px; }
.toast {
  bottom: calc(20px + var(--safe-bottom));
  max-width: calc(100% - 32px - var(--safe-left) - var(--safe-right));
  overflow-wrap: anywhere;
}

/* Header, log, warnings, rows, review, question, error, composer, tools, hint
   are siblings. Never let flex shrink the composer or clip a later sibling.
   If many warnings/panels exceed a short viewport, the dialog itself scrolls. */
.modal.chat-modal {
  height: min(880px, 100%);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  scroll-padding-block: 12px;
}
.chat-modal > * { flex-shrink: 0; }
.chat-modal .chat-log {
  flex: 1 0 72px;
  min-height: 72px;
  max-height: none;
  overflow-y: auto;
  overflow-x: hidden;
}
.chat-modal > .muted { margin: 0; }
.chat-modal .chat-transactions {
  max-height: 132px;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.chat-row { grid-template-columns: 44px minmax(0, 1fr); gap: 6px; padding: 6px; }
.chat-row > .check { width: 44px; justify-content: center; }
.chat-row > button { min-height: 44px; }
.chat-row .row-memory { width: auto; justify-content: flex-start; }
.chat-modal .chat-question {
  max-height: 150px;
  overflow-y: auto;
  padding: 8px 10px;
  overscroll-behavior: contain;
}
.chat-categories {
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 2px 6px;
  scroll-padding-inline: 2px;
}
.chat-categories .secondary {
  flex: 0 0 auto;
  min-height: 44px;
  font-size: 14px;
  padding: 8px 12px;
  white-space: nowrap;
}
.chat-modal > .notice, .chat-modal > .form-error {
  max-height: 88px;
  overflow-y: auto;
}
.modal .chat-composer {
  flex: 0 0 auto;
  margin: 0;
  grid-template-columns: minmax(0, 1fr) auto;
}
.chat-composer textarea { min-height: 56px; max-height: 96px; font-size: 16px; }
.chat-composer .primary { min-width: 52px; min-height: 48px; padding: 10px; }
.chat-tools { gap: 6px; }
.chat-tools > .primary, .chat-tools > .secondary, .chat-tools > .quiet {
  min-height: 44px;
  font-size: 13px;
  padding: 8px;
}
.chat-tools > .primary { min-height: 48px; }
.chat-modal > .quiet { min-height: 44px; padding: 8px 4px; }
.chat-modal > .hint { margin: 0; }

/* The keyboard changes the visual viewport; selected draft data stays intact. */
body.keyboard-open .chat-modal > .muted,
body.keyboard-open .chat-modal > .hint { display: none; }
body.keyboard-open .chat-modal .chat-transactions { display: none; }
body.keyboard-open .chat-modal { gap: 6px; }
body.keyboard-open .chat-modal .chat-question { max-height: 112px; }
body.keyboard-open .chat-composer textarea { min-height: 48px; max-height: 72px; }

@media (max-width: 640px) {
  .backdrop {
    align-items: stretch;
    padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  }
  .modal { border-radius: 18px 18px 0 0; padding: 16px; }
  .modal.chat-modal { height: 100%; max-width: 600px; border-radius: 0; padding: 12px; }
  .chat-modal > .section-heading h2 { font-size: 17px; }
  .sticky-actions { bottom: -16px; padding-bottom: 12px; }
  .balance { padding: 20px; }
  .balance h1 { font-size: 36px; }
}
@media (min-width: 650px) {
  .page { padding-top: calc(40px + var(--safe-top)); }
  .category-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}
