:root {
    --ink: #172933;
    --ink-soft: #42545d;
    --muted: #74848c;
    --line: #dce3e7;
    --line-strong: #cbd5da;
    --surface: #ffffff;
    --canvas: #f3f6f8;
    --canvas-deep: #eaf0f2;
    --mint: #26b98c;
    --mint-dark: #148167;
    --mint-soft: #e5f8f2;
    --amber: #e39930;
    --amber-soft: #fff6e7;
    --danger: #d65959;
    --shadow: 0 18px 45px rgba(24, 46, 57, .09);
    --font: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
    margin: 0;
    color: var(--ink);
    background: var(--canvas);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
    -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; }
button, select { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible {
    outline: 3px solid rgba(38, 185, 140, .25);
    outline-offset: 2px;
}

svg {
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

[hidden] { display: none !important; }

.app-shell { height: 100vh; height: 100dvh; min-height: 620px; display: flex; flex-direction: column; overflow: hidden; }

.topbar {
    height: 72px;
    flex-shrink: 0;
    padding: 0 28px;
    display: flex;
    align-items: center;
    gap: 24px;
    background: rgba(255,255,255,.94);
    border-bottom: 1px solid var(--line);
    position: relative;
    z-index: 20;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 11px;
    color: var(--ink);
    text-decoration: none;
    font-weight: 800;
    font-size: 1.5rem;
    letter-spacing: -.035em;
}

.brand-name { white-space: nowrap; }
.brand-accent { color: var(--mint-dark); }

.brand-mark {
    width: 48px;
    height: 48px;
    flex-shrink: 0;
    display: block;
    background-color: var(--mint-dark);
    -webkit-mask: url('../images/buba-mark.png') center / contain no-repeat;
    mask: url('../images/buba-mark.png') center / contain no-repeat;
}

.toolbar-button {
    border: 0;
    background: transparent;
    color: var(--ink-soft);
}

.workspace {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1.08fr) minmax(390px, .92fr);
    overflow: hidden;
}

.document-panel {
    min-width: 0;
    min-height: 0;
    padding: clamp(18px, 2.4vw, 36px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel-heading, .data-heading {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 20px;
}

.eyebrow {
    margin: 0 0 5px;
    color: var(--muted);
    font-size: .72rem;
    font-weight: 760;
    letter-spacing: .09em;
    text-transform: uppercase;
}

h1, h2 { margin: 0; letter-spacing: -.035em; line-height: 1.15; }
h1 { font-size: clamp(1.45rem, 2vw, 2rem); }
h2 { font-size: 1.45rem; }

.file-state {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: .78rem;
    font-weight: 650;
    white-space: nowrap;
}

.state-dot { width: 7px; height: 7px; border-radius: 50%; background: #a9b5bb; }
.file-state.ready { color: var(--mint-dark); }
.file-state.ready .state-dot { background: var(--mint); }

.upload-stage {
    position: relative;
    flex: 1;
    min-height: 0;
    margin-top: 18px;
    border: 1px dashed #b9c6cc;
    border-radius: 22px;
    overflow: hidden;
    background:
        radial-gradient(circle at 15% 20%, rgba(255,255,255,.85), transparent 35%),
        var(--canvas-deep);
    transition: border-color .2s, background .2s;
}

.upload-stage.dragging { border-color: var(--mint); background: var(--mint-soft); }

.empty-state {
    height: 100%;
    min-height: 0;
    padding: 40px 24px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
}

.empty-state h2 { margin-top: 24px; font-size: 1.25rem; }
.empty-state > p { margin: 8px 0 24px; color: var(--muted); font-size: .92rem; }

.scan-illustration {
    width: 190px;
    height: 150px;
    position: relative;
}

.doc-sheet {
    position: absolute;
    inset: 13px 22px;
    background: #fff;
    border: 1px solid #d7e0e4;
    border-radius: 8px;
    box-shadow: 0 14px 28px rgba(38, 61, 72, .1);
}

.doc-photo { position: absolute; left: 17px; top: 22px; width: 42px; height: 53px; border-radius: 5px; background: #dce7e9; }
.doc-photo::before { content: ""; position: absolute; left: 13px; top: 9px; width: 16px; height: 16px; border-radius: 50%; background: #aebfc5; }
.doc-photo::after { content: ""; position: absolute; left: 8px; bottom: 6px; width: 26px; height: 17px; border-radius: 50% 50% 4px 4px; background: #aebfc5; }
.doc-line { position: absolute; left: 74px; height: 6px; border-radius: 4px; background: #dce7e9; }
.line-one { width: 52px; top: 28px; }
.line-two { width: 42px; top: 45px; }
.line-three { width: 58px; top: 62px; }

.corner, .preview-corner { position: absolute; width: 25px; height: 25px; border-color: var(--mint); border-style: solid; }
.corner-tl { left: 0; top: 0; border-width: 3px 0 0 3px; border-radius: 8px 0 0; }
.corner-tr { right: 0; top: 0; border-width: 3px 3px 0 0; border-radius: 0 8px 0 0; }
.corner-bl { left: 0; bottom: 0; border-width: 0 0 3px 3px; border-radius: 0 0 0 8px; }
.corner-br { right: 0; bottom: 0; border-width: 0 3px 3px 0; border-radius: 0 0 8px; }

.scan-line {
    position: absolute;
    left: 8px;
    right: 8px;
    top: 50%;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--mint), transparent);
    box-shadow: 0 0 12px rgba(38, 185, 140, .5);
    animation: scan 2.8s ease-in-out infinite;
}

@keyframes scan { 0%,100% { transform: translateY(-48px); opacity: .25; } 50% { transform: translateY(48px); opacity: 1; } }

.upload-actions { display: flex; gap: 10px; }

.button {
    min-height: 43px;
    padding: 0 16px;
    border-radius: 11px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 1px solid transparent;
    font-size: .88rem;
    font-weight: 720;
    transition: transform .15s, background .15s, border-color .15s, color .15s;
}

.button:active { transform: translateY(1px); }
.button svg { width: 19px; height: 19px; }
.button-primary { background: var(--ink); color: #fff; box-shadow: 0 8px 20px rgba(23, 41, 51, .13); }
.button-primary:hover { background: #223b48; }
.button-primary:disabled { cursor: not-allowed; background: #aeb9be; box-shadow: none; }
.button-secondary { background: #fff; color: var(--ink); border-color: var(--line-strong); }
.button-secondary:hover { border-color: #9eafb7; }

.capture-tip {
    margin-top: 24px;
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: .78rem;
}
.capture-tip svg { width: 16px; height: 16px; }

.preview-state { height: 100%; min-height: 0; padding: 18px; display: flex; flex-direction: column; }
.preview-viewport { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; overflow: hidden; cursor: grab; touch-action: none; }
.preview-viewport:focus-visible { outline: 3px solid rgba(38,185,140,.25); outline-offset: -3px; border-radius: 12px; }
.preview-viewport.is-panning { cursor: grabbing; }
.preview-viewport img { max-width: calc(100% - 36px); max-height: calc(100% - 36px); object-fit: contain; transform-origin: center; filter: drop-shadow(0 15px 25px rgba(30,48,56,.18)); transition: transform .18s; pointer-events: none; user-select: none; }
.preview-viewport.is-panning img { transition: none; }
.preview-corner { width: 34px; height: 34px; }
.preview-corner.corner-tl { left: 4px; top: 4px; }
.preview-corner.corner-tr { right: 4px; top: 4px; }
.preview-corner.corner-bl { left: 4px; bottom: 4px; }
.preview-corner.corner-br { right: 4px; bottom: 4px; }

.preview-toolbar {
    height: 48px;
    flex: 0 0 auto;
    margin: 14px auto 0;
    padding: 5px;
    display: flex;
    align-items: center;
    gap: 2px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: rgba(255,255,255,.95);
    box-shadow: 0 10px 25px rgba(29,50,60,.1);
}
.toolbar-button { width: 36px; height: 36px; padding: 8px; border-radius: 9px; }
.toolbar-button:hover { background: var(--canvas); color: var(--ink); }
.toolbar-button-danger:hover { background: #fff0f0; color: var(--danger); }
.toolbar-button svg { width: 20px; height: 20px; }
.zoom-value { min-width: 47px; text-align: center; font-size: .74rem; font-weight: 720; color: var(--muted); }
.toolbar-divider { width: 1px; height: 24px; background: var(--line); margin: 0 3px; }
.preview-pan-hint { margin-top: 7px; color: var(--muted); font-size: .69rem; text-align: center; }

.drop-overlay, .processing-overlay {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: grid;
    place-items: center;
    background: rgba(229, 248, 242, .94);
}
.drop-overlay { opacity: 0; visibility: hidden; transition: opacity .15s; }
.upload-stage.dragging .drop-overlay { opacity: 1; visibility: visible; }
.drop-overlay > div { text-align: center; color: var(--mint-dark); }
.drop-overlay svg { width: 48px; height: 48px; display: block; margin: 0 auto 10px; }

.processing-overlay { z-index: 8; background: rgba(235, 241, 243, .9); backdrop-filter: blur(7px); }
.processing-card { width: min(320px, calc(100% - 40px)); padding: 28px; text-align: center; border-radius: 18px; background: #fff; box-shadow: var(--shadow); }
.processing-card strong { display: block; margin-top: 15px; }
.processing-card p { margin: 4px 0 18px; color: var(--muted); font-size: .82rem; }
.processing-orbit { width: 48px; height: 48px; margin: 0 auto; border: 3px solid var(--mint-soft); border-top-color: var(--mint); border-radius: 50%; animation: spin .85s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress-track { height: 5px; overflow: hidden; background: var(--canvas-deep); border-radius: 99px; }
.progress-track span { display: block; height: 100%; width: 0; background: var(--mint); transition: width .25s; }
.progress-value { display: block; margin-top: 8px; color: var(--mint-dark); font-size: .76rem; font-weight: 750; }

.document-footer { flex: 0 0 auto; padding-top: 12px; display: flex; align-items: center; justify-content: space-between; gap: 16px; color: var(--muted); font-size: .75rem; }
.document-footer span { display: inline-flex; align-items: center; gap: 7px; }
.document-footer svg { width: 16px; height: 16px; color: var(--mint-dark); }

.data-panel {
    min-width: 0;
    min-height: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--surface);
    border-left: 1px solid var(--line);
    box-shadow: -20px 0 45px rgba(31,53,63,.035);
}

.data-scroll { flex: 1 1 auto; min-height: 0; padding: clamp(20px, 2.4vw, 34px); padding-bottom: 28px; overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable; }
.data-heading { align-items: center; }
.button-copy-all { min-height: 38px; padding: 0 12px; color: var(--ink-soft); background: #fff; border-color: var(--line); }
.button-copy-all:hover:not(:disabled) { color: var(--mint-dark); border-color: #aedecf; }
.button-copy-all:disabled { opacity: .45; cursor: not-allowed; }

.field-group { margin-top: 25px; }
.field-group > label, .field-label {
    display: block;
    margin-bottom: 8px;
    color: var(--ink-soft);
    font-size: .78rem;
    font-weight: 720;
}

.select-wrap { position: relative; }
.select-wrap select {
    width: 100%;
    height: 48px;
    appearance: none;
    padding: 0 44px 0 14px;
    border: 1px solid var(--line-strong);
    border-radius: 11px;
    background: #fff;
    color: var(--ink);
    font-weight: 650;
}
.select-wrap svg { pointer-events: none; position: absolute; right: 14px; top: 50%; width: 18px; height: 18px; transform: translateY(-50%); color: var(--muted); }

.select-hint {
    display: block;
    margin-top: 6px;
    color: var(--muted);
    font-size: .72rem;
}

.display-options {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.compact-field-group { margin-top: 14px; }
.compact-field-group .select-wrap select { height: 44px; }

.auto-field-state {
    margin: 4px 0 28px;
    padding: 18px;
    color: var(--muted);
    background: #f8fafb;
    border: 1px dashed var(--line-strong);
    border-radius: 12px;
    font-size: .84rem;
    text-align: center;
}

.verification-card {
    margin: 18px 0 26px;
    padding: 14px;
    display: flex;
    gap: 12px;
    border: 1px solid var(--line);
    border-radius: 13px;
    background: #f8fafb;
}
.verification-icon { width: 34px; height: 34px; flex: 0 0 auto; display: grid; place-items: center; color: var(--muted); background: #edf1f3; border-radius: 10px; }
.verification-icon svg { width: 19px; height: 19px; }
.verification-card strong { font-size: .83rem; }
.verification-card p { margin: 2px 0 0; color: var(--muted); font-size: .76rem; line-height: 1.4; }
.verification-card.success { border-color: #c6eadd; background: #f1fbf7; }
.verification-card.success .verification-icon { color: var(--mint-dark); background: #ddf5ed; }
.verification-card.warning { border-color: #f1d49f; background: var(--amber-soft); }
.verification-card.warning .verification-icon { color: #a86613; background: #ffebc7; }

.form-section { margin: 0 0 26px; padding: 0; border: 0; }
.form-section legend { width: 100%; padding: 0 0 11px; margin-bottom: 14px; border-bottom: 1px solid var(--line); color: var(--muted); font-size: .7rem; font-weight: 780; letter-spacing: .085em; text-transform: uppercase; }
.fields-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 12px; }
.form-field.span-2 { grid-column: 1 / -1; }
.field-input-wrap { position: relative; }
.field-input-wrap input {
    width: 100%;
    height: 46px;
    padding: 0 46px 0 13px;
    border: 1px solid var(--line);
    border-radius: 10px;
    color: var(--ink);
    background: #fafcfc;
    font-weight: 560;
    transition: border-color .15s, box-shadow .15s, background .15s;
}
.field-input-wrap input:hover { border-color: var(--line-strong); }
.field-input-wrap input:focus { outline: none; border-color: var(--mint); background: #fff; box-shadow: 0 0 0 3px rgba(38,185,140,.12); }
.field-input-wrap input::placeholder { color: #a5b1b6; font-weight: 450; }
.form-field.needs-review input { border-color: #e8bc72; background: #fffaf1; }
.copy-field-button { position: absolute; top: 5px; right: 5px; width: 36px; height: 36px; padding: 8px; border: 0; border-radius: 8px; background: transparent; color: #8a9aa1; }
.copy-field-button:hover:not(:disabled) { color: var(--mint-dark); background: var(--mint-soft); }
.copy-field-button:disabled { opacity: .35; cursor: not-allowed; }
.copy-field-button svg { width: 18px; height: 18px; }
.review-note { display: none; margin-top: 5px; color: #9b650f; font-size: .69rem; }
.form-field.needs-review .review-note { display: block; }

.data-footer { flex: 0 0 auto; padding: 14px clamp(20px, 2.4vw, 34px); border-top: 1px solid var(--line); background: rgba(255,255,255,.98); box-shadow: 0 -8px 24px rgba(31,53,63,.04); }
.recognize-button { width: 100%; }
.data-footer p { margin: 8px 0 0; text-align: center; color: var(--muted); font-size: .7rem; }

.toast {
    position: fixed;
    left: 50%;
    bottom: 28px;
    z-index: 50;
    padding: 11px 15px;
    display: flex;
    align-items: center;
    gap: 8px;
    color: #fff;
    background: var(--ink);
    border-radius: 11px;
    box-shadow: 0 13px 30px rgba(17, 32, 40, .24);
    font-size: .82rem;
    font-weight: 650;
    opacity: 0;
    transform: translate(-50%, 14px);
    pointer-events: none;
    transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast svg { width: 18px; height: 18px; color: #67e0ba; }

@media (max-width: 780px) {
    .app-shell { height: auto; min-height: 100vh; overflow: visible; }
    .workspace { height: auto; grid-template-columns: 1fr; overflow: visible; }
    .document-panel { min-height: calc(100vh - 72px); }
    .data-panel { border-left: 0; border-top: 1px solid var(--line); }
    .data-scroll { overflow: visible; }
    .upload-stage, .empty-state, .preview-state { min-height: 470px; }
}

@media (max-width: 640px) {
    .topbar { height: 64px; padding: 0 16px; }
    .brand-mark { width: 44px; height: 44px; }
    .workspace { min-height: calc(100vh - 64px); }
    .document-panel, .data-scroll { padding: 22px 16px; }
    .panel-heading { align-items: flex-start; }
    .file-state { max-width: 125px; overflow: hidden; text-overflow: ellipsis; }
    .upload-stage { margin-top: 16px; }
    .upload-stage, .empty-state, .preview-state { min-height: 440px; }
    .empty-state { padding: 30px 16px; }
    .scan-illustration { transform: scale(.88); margin: -8px 0; }
    .upload-actions { width: 100%; flex-direction: column; }
    .upload-actions .button { width: 100%; }
    .document-footer { align-items: flex-start; }
    .document-footer span:last-child { display: none; }
    .data-heading { align-items: flex-start; }
    .display-options { grid-template-columns: 1fr; gap: 0; }
    .button-copy-all { font-size: 0; width: 40px; padding: 0; }
    .button-copy-all svg { margin: 0; }
    .fields-grid { grid-template-columns: 1fr; }
    .form-field.span-2 { grid-column: auto; }
    .data-footer { padding: 15px 16px; }
}

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