@font-face {
  font-family: 'JetBrains Mono';
  src: url('fonts/JetBrainsMono-Regular.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --c-bg: #f8f8f7;
  --c-surface: #ffffff;
  --c-border: #e4e3e0;
  --c-text: #1a1a18;
  --c-muted: #8a8a85;
  --c-accent: #1a1a18;
  --c-accent-hover: #3a3a35;
  --c-error: #c0392b;
  --c-json-bg: #1e1e1e;
  --c-json-text: #d4d4d4;
  --c-json-string: #ce9178;
  --c-json-number: #b5cea8;
  --c-json-key: #9cdcfe;
  --c-json-bool: #569cd6;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.06);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--c-bg);
  color: var(--c-text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Layout ─────────────────────────────────────────────────── */
header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--c-border);
  background: var(--c-surface);
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-mark {
  width: 32px; height: 32px;
  background: var(--c-accent);
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: -.5px;
  flex-shrink: 0;
}

header h1 { font-size: 15px; font-weight: 600; letter-spacing: -.2px; }
header p  { font-size: 13px; color: var(--c-muted); margin-top: 1px; }

main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
}

.card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 560px;
  padding: 40px;
}

/* ── Panels ─────────────────────────────────────────────────── */
.panel { display: none; }
.panel.active { display: block; }

/* ── Buttons ─────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
  border: 1px solid transparent;
}

.btn-primary {
  background: var(--c-accent);
  color: #fff;
  width: 100%;
  padding: 12px;
  margin-top: 16px;
  font-size: 15px;
}
.btn-primary:hover { background: var(--c-accent-hover); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-ghost {
  background: transparent;
  color: var(--c-muted);
  border-color: var(--c-border);
  font-size: 13px;
  margin-top: 12px;
  width: 100%;
}
.btn-ghost:hover { color: var(--c-text); border-color: var(--c-text); }

/* ── Email form (state 1) ─────────────────────────────────────── */
.panel-login h2 { font-size: 22px; font-weight: 600; letter-spacing: -.4px; margin-bottom: 8px; }
.panel-login p  { font-size: 14px; color: var(--c-muted); margin-bottom: 24px; line-height: 1.5; }

input[type="email"] {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  font-size: 15px;
  outline: none;
  transition: border-color .15s;
  background: var(--c-bg);
}
input[type="email"]:focus { border-color: var(--c-accent); background: #fff; }

/* ── Sent confirmation (state 2) ─────────────────────────────── */
.panel-sent { text-align: center; padding: 12px 0; }
.panel-sent .icon-sent {
  width: 56px; height: 56px;
  background: #f0fdf4;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 20px;
  font-size: 26px;
}
.panel-sent h2 { font-size: 20px; font-weight: 600; margin-bottom: 8px; }
.panel-sent p  { font-size: 14px; color: var(--c-muted); line-height: 1.5; }

/* ── Upload zone (state 3) ─────────────────────────────────────── */
.panel-upload h2 { font-size: 18px; font-weight: 600; margin-bottom: 4px; }
.panel-upload .subtitle { font-size: 13px; color: var(--c-muted); margin-bottom: 24px; }

#drop-zone {
  border: 2px dashed var(--c-border);
  border-radius: var(--radius);
  padding: 40px 24px;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
  position: relative;
  overflow: hidden;
}
#drop-zone:hover,
#drop-zone.drag-over {
  border-color: var(--c-accent);
  background: #f5f5f3;
  animation: dash-spin 1.2s linear infinite;
}

@keyframes dash-spin {
  to { border-dash-offset: 24px; }
}

/* Animated dashed border on hover using pseudo-element trick */
#drop-zone::before {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: calc(var(--radius) + 2px);
  border: 2px dashed transparent;
  transition: border-color .2s;
  pointer-events: none;
}
#drop-zone:hover::before,
#drop-zone.drag-over::before {
  border-color: var(--c-accent);
  animation: border-dash 1.4s linear infinite;
}

@keyframes border-dash {
  from { stroke-dashoffset: 0; }
  to { stroke-dashoffset: 40px; }
}

.drop-icon { font-size: 36px; margin-bottom: 12px; display: block; }
.drop-label { font-size: 15px; font-weight: 500; margin-bottom: 4px; }
.drop-hint  { font-size: 12px; color: var(--c-muted); }

#file-input { display: none; }

/* ── Scanner (state 3 → processing) ─────────────────────────── */
#scanner {
  display: none;
  position: relative;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  overflow: hidden;
  background: #fafafa;
  min-height: 200px;
  align-items: center;
  justify-content: center;
}
#scanner.active { display: flex; }

.scanner-doc {
  width: 120px;
  height: 160px;
  background: #fff;
  border: 1px solid var(--c-border);
  border-radius: 4px;
  opacity: .85;
  position: relative;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}
/* Lines simulating text */
.scanner-doc::before {
  content: '';
  position: absolute;
  top: 18px; left: 12px; right: 12px;
  height: 100%;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0, transparent 6px,
    #e8e8e6 6px, #e8e8e6 7px,
    transparent 7px, transparent 14px
  );
}

.scanner-beam {
  position: absolute;
  left: 0; right: 0;
  height: 3px;
  background: linear-gradient(to right, transparent, rgba(66,153,225,.6) 30%, rgba(66,153,225,.9) 50%, rgba(66,153,225,.6) 70%, transparent);
  top: 0;
  animation: scan 1.8s ease-in-out infinite;
  box-shadow: 0 0 8px rgba(66,153,225,.5);
}

@keyframes scan {
  0%   { top: 0%; opacity: 1; }
  90%  { top: 100%; opacity: 1; }
  95%  { top: 100%; opacity: 0; }
  100% { top: 0%; opacity: 0; }
}

.scanner-label {
  position: absolute;
  bottom: 12px; left: 0; right: 0;
  text-align: center;
  font-size: 12px;
  color: var(--c-muted);
  letter-spacing: .3px;
}

/* ── Results (state 4) ─────────────────────────────────────────── */
.panel-results { max-width: 720px; }

.results-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}
.results-header h2 { font-size: 18px; font-weight: 600; }

.tab-bar {
  display: inline-flex;
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  overflow: hidden;
}
.tab-btn {
  padding: 6px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--c-muted);
  transition: background .15s, color .15s;
}
.tab-btn.active {
  background: var(--c-accent);
  color: #fff;
}

/* Human view */
#results-table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
@media (max-width: 480px) { #results-table { grid-template-columns: 1fr; } }

.field-card {
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 12px 14px;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .25s ease, transform .25s ease;
}
.field-card.visible { opacity: 1; transform: none; }
.field-card.full-width { grid-column: 1 / -1; }

.field-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .6px;
  color: var(--c-muted);
  margin-bottom: 4px;
}
.field-value {
  font-size: 15px;
  font-weight: 500;
  color: var(--c-text);
}
.field-value.empty { color: var(--c-muted); font-weight: 400; }
.field-value.total { font-size: 20px; font-weight: 700; }

/* Line items table */
.line-items { width: 100%; border-collapse: collapse; font-size: 13px; }
.line-items th {
  text-align: left;
  font-weight: 500;
  color: var(--c-muted);
  padding: 6px 8px;
  border-bottom: 1px solid var(--c-border);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
}
.line-items td { padding: 8px; border-bottom: 1px solid var(--c-border); }
.line-items tr:last-child td { border-bottom: none; }
.line-items td:nth-child(2),
.line-items td:nth-child(3),
.line-items td:nth-child(4) { text-align: right; font-variant-numeric: tabular-nums; }

/* JSON view */
#json-panel {
  display: none;
  background: var(--c-json-bg);
  border-radius: var(--radius);
  padding: 20px;
  font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 13px;
  line-height: 1.6;
  overflow-x: auto;
  position: relative;
  max-height: 480px;
  overflow-y: auto;
}
#json-panel .json-line { display: flex; }
#json-panel .ln { color: #555; min-width: 28px; user-select: none; margin-right: 12px; }
#json-panel .s  { color: var(--c-json-string); }
#json-panel .n  { color: var(--c-json-number); }
#json-panel .k  { color: var(--c-json-key); }
#json-panel .b  { color: var(--c-json-bool); }
#json-panel .p  { color: #d4d4d4; }

.json-copy-btn {
  position: sticky;
  top: 0;
  float: right;
  margin-bottom: -28px;
  padding: 4px 10px;
  background: #3a3a3a;
  color: #ccc;
  border: 1px solid #555;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.json-copy-btn:hover { background: #4a4a4a; color: #fff; }
.json-copy-btn.copied { background: #1a472a; color: #5cb85c; border-color: #5cb85c; }

/* Download button */
#download-btn {
  margin-top: 20px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
#download-btn:hover { border-color: var(--c-accent); background: #fff; }
#download-btn .icon { font-family: monospace; font-size: 16px; }

/* ── Error message ─────────────────────────────────────────── */
#error-msg,
#upload-error-msg {
  display: none;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 14px;
  color: var(--c-error);
  margin-top: 16px;
}
#error-msg.active,
#upload-error-msg.active { display: block; }

/* ── Footer ─────────────────────────────────────────────────── */
footer {
  padding: 16px 24px;
  text-align: center;
  font-size: 12px;
  color: var(--c-muted);
  border-top: 1px solid var(--c-border);
}
footer a { color: var(--c-muted); text-decoration: none; }
footer a:hover { color: var(--c-text); }

/* ── Responsive adjustments ─────────────────────────────────── */
@media (max-width: 600px) {
  .card { padding: 24px 20px; }
  main { padding: 24px 16px; }
  .panel-results { max-width: 100%; }
}
