/* ===== Reset & Base ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary: #1976d2;
    --primary-dark: #1565c0;
    --success: #2e7d32;
    --success-dark: #1b5e20;
    --secondary: #546e7a;
    --bg: #f5f5f5;
    --card-bg: #ffffff;
    --text: #212121;
    --text-light: #757575;
    --radius: 12px;
    --shadow: 0 2px 8px rgba(0,0,0,0.1);
}

html {
    font-size: 16px;
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    -webkit-tap-highlight-color: transparent;
}

#app {
    max-width: 900px;
    margin: 0 auto;
    padding: 16px;
}

/* ===== Header ===== */
header {
    text-align: center;
    padding: 20px 0 12px;
}

header h1 {
    font-size: 1.6rem;
    font-weight: 700;
    color: var(--primary);
}

.subtitle {
    color: var(--text-light);
    font-size: 0.9rem;
    margin-top: 4px;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 14px 28px;
    border: none;
    border-radius: var(--radius);
    font-size: 1.05rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    touch-action: manipulation;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--primary);
    color: white;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-success {
    background: var(--success);
    color: white;
}
.btn-success:hover { background: var(--success-dark); }

.btn-secondary {
    background: var(--secondary);
    color: white;
}

#controls {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
    padding: 16px 0;
}

/* ===== Camera ===== */
#video-container {
    position: relative;
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
    border-radius: var(--radius);
    overflow: hidden;
    background: #000;
    aspect-ratio: 4/3;
}

#video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

#overlay-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

/* Guide frame for card placement */
#guide-frame {
    position: absolute;
    top: 12%;
    left: 8%;
    right: 8%;
    bottom: 12%;
    border: 2px dashed rgba(255, 255, 255, 0.5);
    border-radius: 12px;
    pointer-events: none;
}

.corner {
    position: absolute;
    width: 24px;
    height: 24px;
    border-color: #4caf50;
    border-style: solid;
}
.corner.tl { top: -2px; left: -2px; border-width: 3px 0 0 3px; border-radius: 6px 0 0 0; }
.corner.tr { top: -2px; right: -2px; border-width: 3px 3px 0 0; border-radius: 0 6px 0 0; }
.corner.bl { bottom: -2px; left: -2px; border-width: 0 0 3px 3px; border-radius: 0 0 0 6px; }
.corner.br { bottom: -2px; right: -2px; border-width: 0 3px 3px 0; border-radius: 0 0 6px 0; }

.guide-text {
    position: absolute;
    bottom: -30px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.85rem;
    white-space: nowrap;
    text-shadow: 0 1px 3px rgba(0,0,0,0.7);
}

/* Camera selector */
#camera-selector {
    text-align: center;
    padding: 8px 0;
}

#camera-select {
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid #ccc;
    font-size: 0.9rem;
    margin-left: 8px;
}

/* ===== Processing ===== */
#processing {
    text-align: center;
    padding: 40px 20px;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid #e0e0e0;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 16px;
}

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

#progress-bar {
    width: 80%;
    max-width: 300px;
    height: 6px;
    background: #e0e0e0;
    border-radius: 3px;
    margin: 12px auto 0;
    overflow: hidden;
}

#progress-fill {
    height: 100%;
    width: 0%;
    background: var(--primary);
    border-radius: 3px;
    transition: width 0.3s;
}

/* ===== Preview ===== */
#preview-section {
    margin-top: 20px;
}

#preview-section h2 {
    font-size: 1.1rem;
    margin-bottom: 12px;
    color: var(--text-light);
}

#preview-images {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.preview-item h3 {
    font-size: 0.85rem;
    color: var(--text-light);
    margin-bottom: 6px;
}

.preview-item canvas {
    width: 100%;
    border-radius: 8px;
    border: 1px solid #e0e0e0;
    background: #fff;
}

/* ===== Results ===== */
#results-section {
    margin-top: 24px;
}

#results-section h2 {
    font-size: 1.2rem;
    margin-bottom: 16px;
    color: var(--primary);
}

#result-cards {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.result-card {
    background: var(--card-bg);
    padding: 16px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.result-card label {
    display: block;
    font-size: 0.75rem;
    text-transform: uppercase;
    color: var(--text-light);
    letter-spacing: 0.5px;
    margin-bottom: 4px;
}

.result-card .value {
    font-size: 1.1rem;
    font-weight: 600;
    word-break: break-word;
}

.confidence {
    margin-top: 6px;
    font-size: 0.75rem;
}

.confidence .bar {
    height: 4px;
    border-radius: 2px;
    margin-top: 3px;
    background: #e0e0e0;
    overflow: hidden;
}

.confidence .bar-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.5s;
}

.conf-high { color: #2e7d32; }
.conf-high .bar-fill { background: #4caf50; }
.conf-medium { color: #f57f17; }
.conf-medium .bar-fill { background: #ffc107; }
.conf-low { color: #c62828; }
.conf-low .bar-fill { background: #f44336; }

/* JSON / Raw output */
details {
    margin-top: 16px;
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

summary {
    padding: 12px 16px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.95rem;
    user-select: none;
}

details pre {
    padding: 12px 16px;
    background: #263238;
    color: #a5d6a7;
    font-size: 0.82rem;
    line-height: 1.5;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 400px;
    overflow-y: auto;
}

/* ===== Footer ===== */
footer {
    text-align: center;
    padding: 24px 0 12px;
    color: var(--text-light);
    font-size: 0.8rem;
}

/* ===== Utilities ===== */
.hidden {
    display: none !important;
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
    #result-cards {
        grid-template-columns: 1fr;
    }
    #preview-images {
        grid-template-columns: 1fr;
    }
    .btn {
        padding: 12px 20px;
        font-size: 0.95rem;
    }
}

/* Landscape tablet optimization */
@media (min-width: 700px) and (orientation: landscape) {
    #video-container {
        max-width: 640px;
    }
}
