:root {
    color-scheme: light;
    --bg: #f7fafc;
    --surface: #ffffff;
    --surface-soft: #f1f5f9;
    --surface-strong: #e8f2f8;
    --border: #d7e2ea;
    --border-strong: #b8c9d6;
    --text: #10202b;
    --muted: #647789;
    --soft-text: #425466;
    --primary: #087ea4;
    --primary-strong: #055f7d;
    --primary-soft: #e3f5fb;
    --success: #16835f;
    --warning: #b7791f;
    --danger: #c2413b;
    --shadow: 0 16px 40px rgba(15, 35, 52, 0.08);
}

* {
    box-sizing: border-box;
}

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

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: "Noto Sans Thai", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.5;
}

button,
input,
select,
textarea {
    font: inherit;
}

header {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    padding: 18px clamp(18px, 4vw, 44px);
    background: rgba(255, 255, 255, 0.95);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 8px 28px rgba(15, 35, 52, 0.06);
    backdrop-filter: blur(14px);
}

.brand {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.brand-logo {
    width: 54px;
    height: 54px;
    flex: 0 0 auto;
    object-fit: contain;
}

.brand-text {
    min-width: 0;
}

.brand-text h1 {
    margin: 0;
    color: var(--text);
    font-size: clamp(22px, 3vw, 30px);
    font-weight: 700;
    line-height: 1.05;
    letter-spacing: 0;
}

.brand-text small {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
}

.header-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 10px;
    color: var(--soft-text);
    font-size: 12px;
    font-weight: 600;
}

.header-meta>span,
.baseline-status {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 32px;
    padding: 6px 10px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 999px;
}

.live-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
    box-shadow: 0 0 0 4px rgba(22, 131, 95, 0.14);
}

.baseline-status.unset {
    color: var(--warning);
    background: #fff8e8;
    border-color: #efd49a;
}

main {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(280px, 360px);
    gap: 22px;
    width: min(1320px, calc(100% - 36px));
    margin: 26px auto 18px;
}

.video-panel,
.side-panel,
.results-section {
    min-width: 0;
}

.video-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
    overflow: hidden;
}

.video-header,
.signal-panel-header,
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 13px 16px;
    color: var(--soft-text);
    background: var(--surface-soft);
    border-bottom: 1px solid var(--border);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.label,
.mono,
.kbd {
    font-variant-numeric: tabular-nums;
}

.mono {
    font-family: "Noto Sans Thai", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.video-container {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 9;
    background: #eef6fa;
    overflow: hidden;
}

#canvas {
    display: block;
    width: 100%;
    height: 100%;
}

.no-signal {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 20px;
    color: var(--muted);
    font-weight: 600;
    text-align: center;
}

.crosshair {
    position: absolute;
    width: 28px;
    height: 28px;
    border-color: rgba(8, 126, 164, 0.5);
    pointer-events: none;
}

.crosshair.tl {
    top: 16px;
    left: 16px;
    border-top: 2px solid;
    border-left: 2px solid;
}

.crosshair.tr {
    top: 16px;
    right: 16px;
    border-top: 2px solid;
    border-right: 2px solid;
}

.crosshair.bl {
    bottom: 16px;
    left: 16px;
    border-bottom: 2px solid;
    border-left: 2px solid;
}

.crosshair.br {
    right: 16px;
    bottom: 16px;
    border-right: 2px solid;
    border-bottom: 2px solid;
}

.video-overlay {
    position: absolute;
    right: 16px;
    bottom: 16px;
    padding: 8px 10px;
    color: var(--primary-strong);
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 8px 20px rgba(15, 35, 52, 0.08);
    font-size: 12px;
    font-weight: 700;
}

.state-strip {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 16px;
    border-top: 1px solid var(--border);
}

.state-text {
    color: var(--text);
    font-size: 18px;
    font-weight: 700;
}

.state-instruction {
    margin-top: 2px;
    color: var(--muted);
    font-size: 13px;
}

.state-badge {
    flex: 0 0 auto;
    padding: 7px 11px;
    border-radius: 999px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    color: var(--soft-text);
    font-size: 12px;
    font-weight: 700;
}

.state-badge.idle {
    color: var(--primary-strong);
    background: var(--primary-soft);
    border-color: #b8e1ed;
}

.progress-track {
    height: 8px;
    background: var(--surface-soft);
    border-top: 1px solid var(--border);
    overflow: hidden;
}

.progress-fill {
    width: 0%;
    height: 100%;
    background: var(--primary);
    transition: width 180ms ease;
}

.signal-panel {
    border-top: 1px solid var(--border);
    background: var(--surface);
}

#sparkline {
    display: block;
    width: 100%;
    height: 96px;
    background: linear-gradient(180deg, #ffffff, #f6fbfd);
}

#sparkline-path {
    stroke: var(--primary);
}

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

.card,
.results-section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
    overflow: hidden;
}

.card-body {
    display: grid;
    gap: 12px;
    padding: 16px;
}

.button,
button {
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    width: 100%;
    min-height: 44px;
    padding: 10px 13px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: 7px;
    cursor: pointer;
    font-weight: 700;
    transition: background 160ms ease, border-color 160ms ease, color 160ms ease, transform 160ms ease;
}

.button:hover:not(:disabled),
button:hover:not(:disabled) {
    background: var(--surface-strong);
    border-color: var(--primary);
}

.button:active:not(:disabled),
button:active:not(:disabled) {
    transform: translateY(1px);
}

.button:disabled,
button:disabled {
    color: #96a7b5;
    background: #f4f7fa;
    border-color: var(--border);
    cursor: not-allowed;
}

.button.primary {
    color: #ffffff;
    background: var(--primary);
    border-color: var(--primary);
}

.button.primary:hover:not(:disabled) {
    background: var(--primary-strong);
    border-color: var(--primary-strong);
}

.kbd {
    flex: 0 0 auto;
    padding: 3px 7px;
    color: var(--soft-text);
    background: rgba(255, 255, 255, 0.75);
    border: 1px solid var(--border);
    border-radius: 5px;
    font-size: 11px;
    font-weight: 700;
}

.button.primary .kbd {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.18);
    border-color: rgba(255, 255, 255, 0.35);
}

.button-group-secondary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.metric-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    min-height: 38px;
    padding: 9px 0;
    border-bottom: 1px solid var(--border);
}

.metric-row:last-child {
    border-bottom: 0;
}

.metric-label {
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}

.metric-value {
    color: var(--text);
    font-weight: 700;
    text-align: right;
}

.results-section {
    grid-column: 1 / -1;
    display: none;
}

.results-header {
    border-radius: 0;
}

.results-body {
    padding: 24px;
    background: var(--surface);
}

.results-grid {
    display: grid;
    grid-template-columns: minmax(220px, 320px) 1fr;
    gap: 22px;
}

.risk-card {
    min-height: 220px;
    padding: 22px;
    background: var(--primary-soft);
    border: 1px solid #b8e1ed;
    border-radius: 8px;
}

.risk-label {
    color: var(--primary-strong);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.risk-level {
    margin-top: 10px;
    color: var(--text);
    font-size: 34px;
    font-weight: 700;
}

.risk-score,
.risk-rec {
    margin-top: 8px;
    color: var(--soft-text);
    font-weight: 600;
}

.results-detail {
    display: grid;
    gap: 12px;
}

.asymmetry-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    margin-top: 14px;
    padding: 14px 16px;
    background: #fff8e8;
    border: 1px solid #efd49a;
    border-radius: 8px;
}

.asymmetry-banner .note {
    display: block;
    margin-top: 3px;
    color: var(--muted);
    font-size: 13px;
}

.asymmetry-banner .value {
    color: var(--warning);
    font-weight: 700;
}

footer {
    width: min(1320px, calc(100% - 36px));
    margin: 0 auto 24px;
    color: var(--muted);
    font-size: 12px;
    text-align: center;
}

@media (max-width: 900px) {
    header {
        position: static;
        align-items: flex-start;
        flex-direction: column;
    }

    .header-meta {
        justify-content: flex-start;
    }

    main,
    .results-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 560px) {

    main,
    footer {
        width: min(100% - 24px, 1320px);
    }

    .brand-logo {
        width: 48px;
        height: 48px;
    }

    .button-group-secondary {
        grid-template-columns: 1fr;
    }

    .state-strip,
    .asymmetry-banner {
        align-items: flex-start;
        flex-direction: column;
    }
}