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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #f0f2f5;
    color: #1a1a2e;
    line-height: 1.6;
}

header {
    background: #1a1a2e;
    color: #fff;
    padding: 1.5rem 2rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

header h1 {
    font-size: 1.5rem;
    font-weight: 700;
}

header .subtitle {
    color: #a0a0c0;
    font-size: 0.95rem;
}

.header-link {
    color: #dbeafe;
    font-size: 0.85rem;
    font-weight: 600;
    text-decoration: none;
    border: 1px solid #475569;
    border-radius: 4px;
    padding: 0.3rem 0.65rem;
}

.header-link:hover {
    background: #334155;
}

.version-info {
    color: #c7c7dd;
    font-size: 0.8rem;
    white-space: nowrap;
}

.health-badge {
    margin-left: auto;
    padding: 0.25rem 0.75rem;
    border-radius: 12px;
    font-size: 0.8rem;
    font-weight: 600;
    background: #555;
    color: #fff;
}
.health-badge.ok { background: #27ae60; }
.health-badge.building { background: #f59e0b; animation: pulse 1.5s ease-in-out infinite; }
.health-badge.degraded { background: #e67e22; }
.health-badge.error { background: #c0392b; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }

.status-banner {
    padding: 0.75rem 1rem;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.status-banner.building {
    background: #fef3c7;
    color: #92400e;
    border: 1px solid #fde68a;
}
.status-banner .spinner {
    border-color: #92400e;
    border-top-color: transparent;
}

main {
    max-width: 860px;
    margin: 2rem auto;
    padding: 0 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.profile-summary {
    display: flex;
    align-items: stretch;
    gap: 1rem;
    padding: 0.8rem 1rem;
    color: #334155;
    background: #e8eef8;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
}

.profile-summary-heading {
    min-width: 150px;
    padding-right: 1rem;
    border-right: 1px solid #cbd5e1;
    display: flex;
    flex-direction: column;
}

.profile-summary-heading span,
.profile-summary-grid span {
    color: #64748b;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.profile-summary-grid {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(4, minmax(90px, 1fr));
    gap: 1rem;
}

.profile-summary-grid div {
    display: flex;
    flex-direction: column;
}

@media (max-width: 700px) {
    .profile-summary { flex-direction: column; }
    .profile-summary-heading { border-right: 0; border-bottom: 1px solid #cbd5e1; padding-bottom: 0.5rem; }
    .profile-summary-grid { grid-template-columns: repeat(2, 1fr); }
}

.card {
    background: #fff;
    border-radius: 8px;
    padding: 1.5rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.card h2 {
    font-size: 1.2rem;
    margin-bottom: 0.5rem;
    color: #1a1a2e;
}

.card p {
    color: #555;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

/* Forms */
.form-group {
    margin-bottom: 1rem;
}

.form-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
}

.form-row label {
    min-width: 160px;
    font-weight: 500;
    font-size: 0.95rem;
}

input[type="number"],
input[type="text"] {
    padding: 0.5rem 0.75rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 1rem;
    width: 140px;
}

input[type="text"].wide-input {
    width: 100%;
    max-width: 500px;
}

input[type="file"] {
    font-size: 0.95rem;
    padding: 0.25rem;
}

button {
    padding: 0.5rem 1.25rem;
    border: none;
    border-radius: 4px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s;
}

#btn-setup, #btn-advise {
    background: #2563eb;
    color: #fff;
}
#btn-setup:hover, #btn-advise:hover {
    background: #1d4ed8;
}
#btn-setup:disabled, #btn-advise:disabled {
    background: #93c5fd;
    cursor: wait;
}

/* Image input toggle */
.input-toggle {
    display: flex;
    gap: 0;
    margin-bottom: 0.75rem;
}

.tab {
    background: #e5e7eb;
    color: #555;
    border-radius: 4px 4px 0 0;
    padding: 0.4rem 1rem;
    font-size: 0.85rem;
}
.tab.active {
    background: #2563eb;
    color: #fff;
}

.image-input {
    padding: 0.5rem 0;
}

/* Results */
.result-box {
    margin-top: 1rem;
    padding: 1rem;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
}

.result-box h3 {
    font-size: 1rem;
    margin-bottom: 0.75rem;
}

.param-grid {
    display: flex;
    gap: 2rem;
    margin-bottom: 0.75rem;
}

.param {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.param-label {
    font-size: 0.8rem;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

/* Preserve the literal micro sign in units — uppercasing mangles µ into an M-like glyph */
.param-label .unit {
    text-transform: none;
}

.param-value {
    font-size: 2rem;
    font-weight: 700;
    color: #1a1a2e;
}

.detail-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.5rem;
    font-size: 0.85rem;
    color: #555;
}

.tolerance-badge {
    display: inline-block;
    padding: 0.25rem 0.75rem;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
}
.tolerance-badge.good { background: #d1fae5; color: #065f46; }
.tolerance-badge.close { background: #fef3c7; color: #92400e; }
.tolerance-badge.far { background: #fee2e2; color: #991b1b; }

/* History table */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

thead th {
    background: #f1f5f9;
    padding: 0.5rem;
    text-align: left;
    font-weight: 600;
    border-bottom: 2px solid #e2e8f0;
}

tbody td {
    padding: 0.5rem;
    border-bottom: 1px solid #f1f5f9;
}

tbody tr:hover {
    background: #f8fafc;
}

/* History thumbnails */
.history-thumb {
    width: 48px;
    height: 48px;
    object-fit: cover;
    border-radius: 4px;
    border: 1px solid #e2e8f0;
    cursor: pointer;
    transition: transform 0.15s;
}
.history-thumb:hover {
    transform: scale(3);
    z-index: 10;
    position: relative;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.thumb-cell {
    width: 112px;
    text-align: center;
}

.muted {
    color: #999;
    font-size: 0.85rem;
    font-style: italic;
}

.hidden {
    display: none;
}

/* Loading spinner */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid #fff;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    vertical-align: middle;
    margin-right: 0.5rem;
}
@keyframes spin {
    to { transform: rotate(360deg); }
}

footer {
    text-align: center;
    padding: 2rem 1rem;
    color: #999;
    font-size: 0.8rem;
}

/* Vision configuration */
.header-link-right { margin-left: auto; }
.header-nav { margin-left: auto; display: flex; gap: 0.6rem; flex-wrap: wrap; }
.config-main { max-width: 980px; }
.profile-manager { display: grid; gap: 0.75rem; }
.profile-toolbar { display: flex; flex-wrap: wrap; gap: 0.6rem; }
.profile-toolbar select { min-width: 220px; padding: 0.5rem; border: 1px solid #cbd5e1; border-radius: 4px; }
.active-profile-line { margin: 0 !important; }
.primary-button { background: #2563eb; color: #fff; }
.primary-button:hover { background: #1d4ed8; }
.secondary-button { background: #e2e8f0; color: #334155; }
.secondary-button:hover { background: #cbd5e1; }
.danger-button { background: #fee2e2; color: #991b1b; }
.danger-button:hover { background: #fecaca; }
button:disabled { opacity: 0.55; cursor: not-allowed; }
.config-heading-row { display: flex; justify-content: space-between; gap: 1rem; align-items: flex-start; }
.protected-badge { white-space: nowrap; padding: 0.25rem 0.65rem; border-radius: 12px; background: #e2e8f0; color: #475569; font-size: 0.75rem; font-weight: 700; }
.config-identity-grid { display: grid; grid-template-columns: minmax(180px, 1fr) 2fr; gap: 1rem; margin-bottom: 1.5rem; }
.config-identity-grid label, .setting-field { display: flex; flex-direction: column; gap: 0.35rem; font-weight: 600; }
.config-identity-grid input { width: 100%; max-width: none; }
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(240px, 1fr)); gap: 1rem; }
.setting-field { padding: 1rem; border: 1px solid #e2e8f0; border-radius: 6px; background: #f8fafc; }
.setting-field input { width: 100%; max-width: 180px; }
.setting-field small { color: #64748b; font-weight: 400; line-height: 1.4; }
.calibration-note { margin-top: 1.25rem; padding: 0.8rem 1rem; border-left: 4px solid #f59e0b; background: #fffbeb; color: #78350f; font-size: 0.85rem; }
.form-actions { display: flex; align-items: center; gap: 1rem; margin-top: 1.25rem; }
.config-message { padding: 0.75rem 1rem; border-radius: 6px; font-weight: 600; }
.config-message.success { background: #d1fae5; color: #065f46; }
.config-message.error { background: #fee2e2; color: #991b1b; }
.config-message.info { background: #dbeafe; color: #1e40af; }
@media (max-width: 700px) {
    .config-identity-grid, .settings-grid { grid-template-columns: 1fr; }
    .config-heading-row { flex-direction: column; }
}

/* Vision tuner */
.tuner-main { max-width: 1280px; }
.tuner-controls { display: grid; gap: 1rem; }
.tuner-settings-grid { display: grid; grid-template-columns: repeat(3, minmax(170px, 1fr)); gap: 0.8rem; }
.tuner-settings-grid label { display: flex; flex-direction: column; gap: 0.3rem; padding: 0.75rem; border: 1px solid #e2e8f0; border-radius: 6px; background: #f8fafc; font-weight: 600; font-size: 0.85rem; }
.tuner-settings-grid input { width: 100%; }
.text-link { color: #2563eb; font-size: 0.9rem; font-weight: 600; text-decoration: none; }
.text-link:hover { text-decoration: underline; }
.tuner-metrics { display: grid; grid-template-columns: repeat(5, 1fr); gap: 0.75rem; margin-bottom: 1.5rem; }
.tuner-metrics div { padding: 1rem; border-radius: 8px; background: #1e293b; color: #fff; display: flex; flex-direction: column; }
.tuner-metrics span { color: #94a3b8; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; }
.tuner-metrics strong { font-size: 1.25rem; }
.threshold-card { margin-bottom: 1.5rem; }
.threshold-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem 1.5rem; font-size: 0.9rem; }
.threshold-grid span { color: #64748b; display: flex; justify-content: space-between; gap: 1rem; border-bottom: 1px solid #f1f5f9; padding: 0.35rem 0; }
.threshold-grid strong { color: #1e293b; }
.diagnostic-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.diagnostic-panel { background: #fff; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.12); }
.diagnostic-panel-wide { grid-column: 1 / -1; }
.diagnostic-panel img { width: 100%; max-height: 520px; object-fit: contain; display: block; background: #020617; image-rendering: auto; }
.diagnostic-panel figcaption { padding: 0.8rem 1rem; display: flex; flex-direction: column; }
.diagnostic-panel figcaption span { color: #64748b; font-size: 0.82rem; }
@media (max-width: 800px) {
    .tuner-settings-grid, .threshold-grid { grid-template-columns: repeat(2, 1fr); }
    .tuner-metrics { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
    .tuner-settings-grid, .threshold-grid, .diagnostic-grid { grid-template-columns: 1fr; }
    .diagnostic-panel-wide { grid-column: auto; }
}

/* Batch extraction */
.batch-main { max-width: 1280px; }
.batch-controls { display: grid; gap: 0.75rem; }
.batch-data-card, .batch-thumbnail-card { display: grid; gap: 1rem; }
.table-scroll { overflow-x: auto; }
.batch-data-card table { min-width: 850px; }
.batch-thumbnail-card table { min-width: 680px; }
.batch-thumbnail-cell { width: 280px; text-align: center; }
.batch-thumbnail { display: block; width: 250px; max-height: 180px; object-fit: contain; margin: 0 auto; border: 1px solid #e2e8f0; border-radius: 4px; background: #f8fafc; }
.batch-advice-card { display: grid; gap: 1rem; }
.batch-filter-grid { display: flex; align-items: end; gap: 1rem; flex-wrap: wrap; }
.batch-filter-grid label, .batch-advice-form label { display: flex; flex-direction: column; gap: 0.35rem; font-weight: 600; }
.batch-filter-grid input { width: min(340px, 75vw); }
.batch-filter-grid select { padding: 0.5rem 0.75rem; border: 1px solid #ccc; border-radius: 4px; font-size: 1rem; width: 210px; }
.batch-advice-form { display: flex; align-items: end; gap: 0.75rem; flex-wrap: wrap; }
.batch-advice-form input, .batch-advice-form select { padding: 0.5rem 0.75rem; border: 1px solid #ccc; border-radius: 4px; font-size: 1rem; width: 150px; }
.batch-advice-form select { width: 190px; }
.batch-row-included { background: #dcfce7; }
.batch-row-excluded { background: #e5e7eb; color: #6b7280; }
.include-cell { width: 72px; text-align: center; }
.include-checkbox { width: 16px; height: 16px; cursor: pointer; }
.batch-row-toggleable { cursor: pointer; }
.batch-row-toggleable:focus-visible { outline: 2px solid #2563eb; outline-offset: -2px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.batch-composites { display: grid; grid-template-columns: repeat(2, minmax(260px, 1fr)); gap: 1rem; }
.batch-composites div { display: flex; flex-direction: column; gap: 0.25rem; padding: 1rem; border-radius: 6px; background: #eff6ff; border: 1px solid #bfdbfe; }
.batch-composites span { font-size: 0.85rem; color: #475569; }
.batch-composites .batch-handoff-row { grid-column: 1 / -1; background: none; border: none; padding: 0.25rem 0 0; }
.batch-handoff-btn { padding: 0.55rem 1.25rem; }
@media (max-width: 700px) { .batch-composites { grid-template-columns: 1fr; } }
