:root {
    --bg: #eef3ea;
    --surface: rgba(255, 255, 255, 0.82);
    --surface-strong: #ffffff;
    --line: rgba(37, 61, 44, 0.12);
    --text: #183222;
    --muted: #587060;
    --accent: #24583e;
    --accent-strong: #163929;
    --warn: #a64834;
    --ok: #2e8b57;
    --shadow: 0 18px 44px rgba(18, 34, 24, 0.08);
    --radius: 20px;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Manrope", sans-serif;
    color: var(--text);
    background:
        radial-gradient(circle at top left, rgba(113, 162, 124, 0.22), transparent 28%),
        radial-gradient(circle at top right, rgba(197, 221, 170, 0.34), transparent 22%),
        linear-gradient(180deg, #f7faf4 0%, var(--bg) 100%);
}

img {
    max-width: 100%;
    display: block;
}

button,
input {
    font: inherit;
}

.hidden {
    display: none !important;
}

.page-shell {
    min-height: 100vh;
    padding: 24px;
}

.topbar {
    display: flex;
    justify-content: space-between;
    gap: 24px;
    align-items: flex-start;
    margin-bottom: 24px;
    padding: 28px;
    border: 1px solid var(--line);
    border-radius: 28px;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.88), rgba(240, 248, 239, 0.92));
    box-shadow: var(--shadow);
}

.eyebrow {
    margin: 0 0 8px;
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--muted);
}

.topbar h1 {
    margin: 0;
    font-size: clamp(28px, 4vw, 42px);
    line-height: 1.05;
}

.subcopy {
    margin: 10px 0 0;
    max-width: 760px;
    color: var(--muted);
}

.health-panel {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 200px;
    padding: 18px 20px;
    border-radius: 18px;
    background: rgba(255, 255, 255, 0.78);
    border: 1px solid var(--line);
}

.health-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #c2c8c4;
    box-shadow: 0 0 0 6px rgba(194, 200, 196, 0.22);
}

.health-dot.ok {
    background: var(--ok);
    box-shadow: 0 0 0 6px rgba(46, 139, 87, 0.18);
}

.health-dot.error {
    background: var(--warn);
    box-shadow: 0 0 0 6px rgba(166, 72, 52, 0.16);
}

.health-label,
.health-value {
    margin: 0;
}

.health-label {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--muted);
}

.health-value {
    margin-top: 4px;
    font-weight: 700;
}

.dashboard {
    display: grid;
    grid-template-columns: minmax(340px, 420px) minmax(0, 1fr);
    gap: 24px;
}

.panel,
.card {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    backdrop-filter: blur(10px);
}

.panel {
    padding: 22px;
}

.panel-header h2,
.card h3 {
    margin: 0;
}

.panel-header p,
.debug-header p {
    margin: 6px 0 0;
    color: var(--muted);
}

.upload-block,
.crop-stage,
.status-card,
.error-box {
    margin-top: 18px;
}

.file-picker {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 16px 18px;
    border-radius: 16px;
    border: 1px dashed rgba(36, 88, 62, 0.34);
    background: rgba(245, 250, 244, 0.92);
    font-weight: 700;
    cursor: pointer;
}

.file-picker input {
    width: 1px;
    height: 1px;
    opacity: 0;
    overflow: hidden;
    position: absolute;
}

.hint,
.image-meta {
    margin: 10px 0 0;
    font-size: 14px;
    color: var(--muted);
}

.crop-stage {
    min-height: 320px;
}

.image-frame {
    width: 100%;
    min-height: 320px;
    border-radius: 18px;
    overflow: hidden;
    background:
        linear-gradient(135deg, rgba(36, 88, 62, 0.06), rgba(36, 88, 62, 0.02)),
        repeating-linear-gradient(
            45deg,
            rgba(36, 88, 62, 0.04),
            rgba(36, 88, 62, 0.04) 14px,
            rgba(255, 255, 255, 0.7) 14px,
            rgba(255, 255, 255, 0.7) 28px
        );
    border: 1px solid var(--line);
}

#previewImage {
    width: 100%;
    min-height: 320px;
    object-fit: contain;
}

.button-row {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 18px;
}

.btn {
    border: 0;
    border-radius: 14px;
    padding: 13px 18px;
    font-weight: 800;
    cursor: pointer;
    transition: transform 0.18s ease, opacity 0.18s ease, background 0.18s ease;
}

.btn:hover:not(:disabled) {
    transform: translateY(-1px);
}

.btn:disabled {
    cursor: not-allowed;
    opacity: 0.55;
}

.btn-primary {
    color: #fff;
    background: linear-gradient(135deg, var(--accent), var(--accent-strong));
}

.btn-secondary {
    color: var(--text);
    background: rgba(255, 255, 255, 0.88);
    border: 1px solid var(--line);
}

.status-card {
    padding: 16px;
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.82);
    border: 1px solid var(--line);
}

.status-line {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    margin: 0;
    padding: 8px 0;
}

.status-line + .status-line {
    border-top: 1px solid var(--line);
}

.error-box {
    padding: 14px 16px;
    border-radius: 16px;
    border: 1px solid rgba(166, 72, 52, 0.22);
    background: rgba(166, 72, 52, 0.08);
    color: #7d2617;
    white-space: pre-wrap;
}

.results-panel {
    display: flex;
    flex-direction: column;
    gap: 18px;
}

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

.card {
    padding: 18px;
}

.empty-state {
    color: var(--muted);
}

.kv-table,
.fields-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
}

.kv-table td,
.fields-table th,
.fields-table td {
    padding: 10px 8px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

.fields-table th {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
}

.confidence-pill {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 999px;
    background: rgba(36, 88, 62, 0.1);
    color: var(--accent-strong);
    font-size: 12px;
    font-weight: 800;
}

.face-card img,
.debug-item img {
    border-radius: 14px;
    border: 1px solid var(--line);
    background: var(--surface-strong);
}

.face-meta,
.debug-meta {
    margin-top: 10px;
    color: var(--muted);
    font-size: 14px;
}

.debug-card {
    padding: 20px;
}

.debug-header {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: flex-start;
    margin-bottom: 14px;
}

.debug-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 14px;
}

.debug-item {
    display: block;
    color: inherit;
    text-decoration: none;
}

.debug-item strong {
    display: block;
    margin-top: 10px;
}

.raw-json-card summary {
    cursor: pointer;
    font-weight: 800;
}

#rawJson {
    margin: 14px 0 0;
    padding: 16px;
    overflow: auto;
    border-radius: 14px;
    background: #10231a;
    color: #d4f3dd;
    font-size: 13px;
    line-height: 1.45;
}

.loading::after {
    content: "";
    display: inline-block;
    width: 14px;
    height: 14px;
    margin-left: 8px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-top-color: #fff;
    animation: spin 0.8s linear infinite;
    vertical-align: -2px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@media (max-width: 1080px) {
    .dashboard {
        grid-template-columns: 1fr;
    }

    .result-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 720px) {
    .page-shell {
        padding: 14px;
    }

    .topbar {
        flex-direction: column;
        padding: 22px;
    }

    .health-panel {
        width: 100%;
    }

    .debug-header {
        flex-direction: column;
    }
}
