:root {
  color-scheme: light;
  --ink: #0f172a;
  --muted: #64748b;
  --surface: #ffffff;
  --canvas: #f5f3ee;
  --line: #e2e8f0;
  --accent: #0f766e;
  --accent-light: #ccfbf1;
  --danger: #b91c1c;
  --danger-light: #fef2f2;
  --warning: #92400e;
  --warning-light: #fffbeb;
  --shadow: 0 18px 50px rgba(15, 23, 42, .08);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; overflow-x: hidden; }
body { margin: 0; color: var(--ink); background: var(--canvas); min-height: 100vh; overflow-x: hidden; }
button, input, textarea, select { font: inherit; }
button, a, input, textarea, select { touch-action: manipulation; }
button { cursor: pointer; }
[hidden] { display: none !important; }

.shell { width: min(100%, 760px); margin: 0 auto; padding: max(24px, env(safe-area-inset-top)) 18px max(48px, env(safe-area-inset-bottom)); }
.login-card, .capture-card, .receipt-card { background: var(--surface); border: 1px solid rgba(148, 163, 184, .25); box-shadow: var(--shadow); }
.login-card { max-width: 460px; margin: 7vh auto 0; padding: 32px; border-radius: 28px; }
.brand-mark { display: grid; place-items: center; width: 58px; height: 58px; border-radius: 18px; background: var(--ink); color: white; font-weight: 900; letter-spacing: -.04em; margin-bottom: 28px; }
.eyebrow { color: var(--accent); text-transform: uppercase; letter-spacing: .14em; font-size: .72rem; font-weight: 800; margin: 0 0 6px; }
h1, h2, h3 { letter-spacing: -.035em; margin: 0; }
h1 { font-size: clamp(2rem, 7vw, 2.8rem); line-height: 1; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }

form, #receipt-context { display: grid; gap: 18px; margin-top: 28px; }
label { display: grid; gap: 8px; font-weight: 700; font-size: .9rem; }
input, textarea, select { width: 100%; min-width: 0; border: 1px solid #cbd5e1; border-radius: 14px; padding: 13px 14px; background: white; color: var(--ink); outline: none; font-size: 16px; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(15, 118, 110, .12); }
.purpose-suggestion-status { display: block; border-radius: 12px; padding: 10px 12px; color: var(--muted); background: #f8fafc; font-size: .8rem; font-weight: 700; }
.purpose-suggestion-status.working { color: var(--warning); background: var(--warning-light); }
.purpose-suggestion-status.success { color: var(--accent); background: var(--accent-light); }

.primary, .secondary, .camera-button { width: 100%; border-radius: 16px; padding: 14px 18px; font-weight: 800; border: 0; }
.primary { background: var(--ink); color: white; }
.primary:hover { background: #1e293b; }
.secondary { color: var(--ink); background: #f1f5f9; border: 1px solid #cbd5e1; }
.text-button, .icon-button { border: 0; background: transparent; color: var(--accent); font-weight: 800; padding: 8px; }
.icon-button { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%; background: white; font-size: 1.4rem; }
.error { min-height: 1.2em; color: var(--danger); font-weight: 650; }

.app-header { display: flex; justify-content: space-between; align-items: flex-start; margin: 12px 2px 30px; }
.header-actions { display: grid; justify-items: end; gap: 4px; font-size: .85rem; }
.capture-card { border-radius: 28px; padding: 22px; }
#capture-intro { display: flex; align-items: flex-start; gap: 14px; }
.step-number { display: grid; place-items: center; flex: 0 0 34px; height: 34px; border-radius: 12px; color: var(--accent); background: var(--accent-light); font-weight: 900; }
.capture-card p { margin: 7px 0 0; }
.capture-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 22px; }
.camera-button, .album-button { display: flex; justify-content: center; align-items: center; gap: 11px; min-height: 64px; }
.camera-button { color: white; background: var(--accent); }
.camera-button:hover { background: #115e59; }
.camera-icon { width: 22px; height: 16px; border: 2px solid currentColor; border-radius: 4px; position: relative; }
.camera-icon::before { content: ""; position: absolute; width: 8px; height: 8px; border: 2px solid currentColor; border-radius: 50%; left: 5px; top: 2px; }
.camera-icon::after { content: ""; position: absolute; width: 8px; height: 3px; background: currentColor; border-radius: 2px 2px 0 0; left: 5px; top: -5px; }
.album-icon { width: 22px; height: 18px; border: 2px solid currentColor; border-radius: 4px; position: relative; overflow: hidden; }
.album-icon::before { content: ""; position: absolute; width: 5px; height: 5px; border-radius: 50%; background: currentColor; right: 3px; top: 3px; }
.album-icon::after { content: ""; position: absolute; width: 18px; height: 12px; background: currentColor; transform: rotate(38deg); left: -6px; bottom: -8px; box-shadow: 10px -4px 0 -2px currentColor; }
#photo-review { margin-top: 20px; }
.preview-wrap { position: relative; overflow: hidden; border-radius: 20px; background: #e2e8f0; max-height: 440px; }
#photo-preview { width: 100%; max-height: 440px; object-fit: contain; display: block; }
.quality-badge { position: absolute; left: 12px; bottom: 12px; border-radius: 999px; padding: 7px 11px; color: white; background: var(--accent); font-size: .8rem; font-weight: 800; box-shadow: 0 4px 18px rgba(0,0,0,.2); }
.quality-badge.bad { background: var(--danger); }
.quality-message { font-weight: 700; margin: 14px 0 !important; }
.progress { display: flex; align-items: center; gap: 16px; margin-top: 20px; border-radius: 18px; padding: 18px; background: #f8fafc; }
.spinner { width: 28px; height: 28px; border: 3px solid #cbd5e1; border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-panel, .warning-panel { margin-top: 18px; border-radius: 17px; padding: 16px; }
.error-panel { color: var(--danger); background: var(--danger-light); border: 1px solid #fecaca; }
.warning-panel { color: var(--warning); background: var(--warning-light); border: 1px solid #fde68a; }
.button-row { display: flex; gap: 8px; align-items: center; margin-top: 12px; }
.button-row .secondary { width: auto; }

.receipts-section { margin-top: 42px; }
.section-heading { display: flex; justify-content: space-between; align-items: end; margin: 0 2px 16px; }
.receipt-list { display: grid; gap: 12px; }
.receipt-card { display: grid; grid-template-columns: 86px minmax(0, 1fr) auto; gap: 14px; align-items: center; width: 100%; min-width: 0; overflow: hidden; padding: 10px; border-radius: 20px; text-align: left; color: inherit; }
.receipt-card:hover { transform: translateY(-1px); }
.receipt-thumb { width: 86px; height: 96px; border-radius: 13px; object-fit: cover; background: #e2e8f0; }
.receipt-main { min-width: 0; }
.receipt-main h3 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.receipt-meta { color: var(--muted); font-size: .82rem; margin-top: 5px; }
.receipt-amount { font-weight: 900; font-variant-numeric: tabular-nums; }
.status { display: inline-flex; border-radius: 999px; padding: 5px 8px; margin-top: 8px; font-size: .7rem; font-weight: 850; color: #475569; background: #f1f5f9; }
.status.review, .status.needs_retake, .status.processing_failed { color: var(--warning); background: var(--warning-light); }
.status.approved { color: var(--accent); background: var(--accent-light); }
.status.duplicate, .status.rejected { color: var(--danger); background: var(--danger-light); }
.duplicate-dot { color: var(--warning); font-weight: 900; margin-left: 5px; }
.empty-state { text-align: center; color: var(--muted); padding: 38px; }

dialog { width: min(680px, calc(100vw - 24px)); max-width: calc(100vw - 24px); max-height: 92vh; max-height: 92dvh; overflow-x: hidden; border: 0; border-radius: 26px; padding: 0; color: var(--ink); box-shadow: 0 28px 100px rgba(15, 23, 42, .28); }
dialog::backdrop { background: rgba(15, 23, 42, .58); backdrop-filter: blur(4px); }
.dialog-close { position: sticky; float: right; z-index: 2; top: 12px; right: 12px; width: 42px; height: 42px; border: 0; border-radius: 50%; background: rgba(15,23,42,.82); color: white; font-size: 1.5rem; }
#receipt-detail { min-width: 0; padding-bottom: 24px; }
.detail-image { width: 100%; max-width: 100%; max-height: 52vh; object-fit: contain; background: #e2e8f0; display: block; }
.detail-body { min-width: 0; padding: 22px; }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 12px; margin: 18px 0; }
.detail-field { min-width: 0; background: #f8fafc; border-radius: 14px; padding: 12px; }
.detail-field.full { grid-column: 1 / -1; }
.detail-field small { display: block; color: var(--muted); margin-bottom: 5px; }
.detail-field strong, .line-item strong, .line-item small { overflow-wrap: anywhere; }
.line-items { display: grid; gap: 8px; margin: 16px 0; }
.line-review-intro { color: var(--ink); background: #f8fafc; border-radius: 14px; padding: 13px; margin-bottom: 4px; }
.line-review-intro small { color: var(--muted); }
.line-item { display: grid; min-width: 0; gap: 7px; border-bottom: 1px solid var(--line); padding: 12px 0; }
.line-item-heading { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 10px; align-items: start; }
.line-item-heading > * { min-width: 0; }
.line-item-heading > div { display: grid; gap: 5px; }
.line-item small { color: var(--muted); }
.line-type { justify-self: start; display: inline-flex; width: fit-content; padding: 3px 7px; border-radius: 999px; color: #475569; background: #f1f5f9; font-size: .67rem; font-weight: 850; text-transform: uppercase; letter-spacing: .05em; }
.line-type.discount { color: var(--accent); background: var(--accent-light); }
.discount-line { border: 1px solid #99f6e4; background: #f0fdfa; border-radius: 14px; padding: 12px; }
.confirmation { color: var(--warning) !important; }
.item-review { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(0, .75fr) minmax(0, .75fr); gap: 8px; margin-top: 4px; }
.item-review label { min-width: 0; gap: 4px; color: var(--muted); font-size: .72rem; }
.item-review input, .item-review select { min-width: 0; padding: 9px 10px; border-radius: 10px; font-size: 16px; }
.artifact-actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 14px 0 18px; }
.artifact-link { color: var(--accent); background: var(--accent-light); border: 1px solid #99f6e4; border-radius: 12px; padding: 10px 13px; font-weight: 800; text-decoration: none; }
.artifact-pending { color: var(--muted); padding: 10px 0; }
.detail-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 20px; }
.detail-actions .primary { grid-column: 1 / -1; }
.duplicate-warning { color: var(--warning); background: var(--warning-light); border-radius: 14px; padding: 13px; margin: 14px 0; }

@media (max-width: 520px) {
  .shell { padding-left: 12px; padding-right: 12px; }
  .login-card { padding: 26px 20px; border-radius: 24px; }
  .capture-card { padding: 18px; border-radius: 24px; }
  .receipt-card { grid-template-columns: 72px 1fr; }
  .receipt-thumb { width: 72px; height: 82px; }
  .receipt-amount { grid-column: 2; justify-self: start; }
  .detail-grid { grid-template-columns: 1fr; }
  dialog { width: calc(100vw - 16px); max-width: calc(100vw - 16px); border-radius: 20px; }
  .detail-body { padding: 20px 14px; }
  .item-review { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .item-review label:first-child { grid-column: 1 / -1; }
}

@media (max-width: 380px) {
  .capture-actions { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; }
}
