* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
    background: var(--background);
    color: var(--text);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
    min-height: 100vh;
}

.scanner {
    width: 100%;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    background: var(--background);
}

header {
    padding: 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

header h1 {
    margin: 0 0 12px;
    font-size: 20px;
    font-weight: 650;
}

#scannerName {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border-input);
    border-radius: 8px;
    background: var(--surface-secondary);
    color: var(--text-strong);
    font-size: 16px;
    outline: none;
}

#scannerName:focus {
    border-color: var(--text-muted);
}

#scannerName::placeholder {
    color: var(--text-muted);
}

.camera-container {
    position: relative;
    width: 100%;
    max-width: 520px;
    margin: 0 auto;

    aspect-ratio: 3 / 4;

    overflow: hidden;
    border-radius: 16px;
    background: #000;
}

.camera-container video,
.camera-container #frozenFrame {
    width: 100%;
    height: 100%;

    display: block;

    object-fit: cover;
}

.card-guide {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 78%;
    aspect-ratio: 63 / 88;
    transform: translate(-50%, -50%);
    border: 2px solid rgba(255, 255, 255, 0.9);
    border-radius: 12px;
    box-shadow:
        0 0 0 9999px rgba(0, 0, 0, 0.35),
        0 0 20px rgba(255, 255, 255, 0.15);
    pointer-events: none;
}

.status {
    padding: 14px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    color: var(--text-muted-strong);
    text-align: center;
    font-size: 15px;
}

.result {
    display: none;
    padding: 14px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.result.visible {
    display: block;
}

#resultIdentifier {
    display: block;
    font-family: monospace;
    font-size: 19px;
    color: var(--text-strong);
}

#resultName {
    display: block;
    margin-top: 4px;
    color: var(--text-muted);
}

button {
    margin: 12px 16px 16px;
    padding: 14px;
    border: 1px solid var(--border-input);
    border-radius: 8px;
    background: var(--surface-hover);
    color: var(--text-strong);
    font-size: 16px;
}

button:active {
    background: var(--border);
}

@media (min-width: 700px) {
    .scanner {
        max-width: 600px;
        margin: 0 auto;
    }
}


.header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.header-button {
    margin: 0;
    padding: 8px 12px;
    font-size: 13px;
}

.camera-container {
    position: relative;
}

.frozen-frame {
    position: absolute;
    inset: 0;

    width: 100%;
    height: 100%;

    object-fit: cover;

    display: none;

    z-index: 3;
}

.frozen-frame.visible {
    display: block;
}

.freeze-badge {
    position: absolute;
    top: 14px;
    left: 50%;

    transform: translateX(-50%);

    display: none;

    padding: 6px 12px;

    background: rgba(180, 40, 40, 0.9);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 6px;

    color: white;

    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.08em;

    z-index: 10;
}

.freeze-badge.visible {
    display: block;
}

.last-identifier {
    position: absolute;

    left: 50%;
    bottom: 24px;

    transform: translateX(-50%);

    width: calc(100% - 32px);
    max-width: 420px;

    padding: 12px 16px;

    background: rgba(0, 0, 0, 0.82);

    border: 1px solid var(--border-input);
    border-radius: 10px;

    color: var(--text-muted);

    text-align: center;

    font-family: monospace;
    font-size: 20px;
    font-weight: 600;

    letter-spacing: 0.03em;

    backdrop-filter: blur(6px);

    pointer-events: none;

    z-index: 8;
}

.last-identifier.has-value {
    color: var(--text-strong);
    border-color: var(--border-input);
}

.scanner-controls {
    display: flex;
    gap: 8px;

    padding: 10px 16px;

    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.scanner-controls .control-button {
    flex: 1;
    margin: 0;
}

.debug-panel {
    display: none;

    padding: 16px;

    background: var(--background);
    border-top: 1px solid var(--border);
}

.debug-panel.visible {
    display: block;
}

.debug-header {
    display: flex;
    align-items: center;
    justify-content: space-between;

    margin-bottom: 16px;
}

.debug-header h2 {
    margin: 0;
    font-size: 18px;
}

#debugState {
    color: var(--text-muted);
    font-size: 13px;
}

.debug-section {
    margin-bottom: 18px;
}

.debug-section h3 {
    margin: 0 0 8px;

    color: var(--text-muted-strong);

    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
}

.debug-section pre {
    margin: 0;

    max-height: 300px;
    overflow: auto;

    padding: 12px;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 7px;

    color: var(--text-secondary);

    font-family: monospace;
    font-size: 12px;

    white-space: pre-wrap;
    word-break: break-word;
}

.debug-image {
    display: block;

    width: 100%;
    max-width: 500px;
    max-height: 600px;

    object-fit: contain;

    margin: 0 auto;

    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 7px;
}


/*
 * ---------------------------------------------------------------------------
 * Variant selection modal
 * ---------------------------------------------------------------------------
 */

.variant-modal {
    position: fixed;
    inset: 0;

    display: none;

    align-items: center;
    justify-content: center;

    padding: 16px;

    z-index: 100;
}

.variant-modal.visible {
    display: flex;
}

.variant-modal-backdrop {
    position: absolute;
    inset: 0;

    background: rgba(0, 0, 0, 0.78);

    backdrop-filter: blur(6px);
}


.variant-dialog {
    position: relative;

    width: 100%;
    max-width: 520px;
    max-height: calc(100vh - 32px);

    display: flex;
    flex-direction: column;

    overflow: hidden;

    background: var(--surface);

    border: 1px solid var(--border-input);
    border-radius: 14px;

    box-shadow:
        0 20px 60px rgba(0, 0, 0, 0.7);

    z-index: 1;
}


/*
 * Header
 */

.variant-dialog-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;

    gap: 12px;

    padding: 18px 18px 14px;

    border-bottom: 1px solid var(--border);
}

.variant-dialog-header h2 {
    margin: 0;

    color: var(--text-strong);

    font-size: 20px;
    font-weight: 650;
}

.variant-identifier {
    margin-top: 5px;

    color: var(--text-muted-strong);

    font-family: monospace;
    font-size: 14px;
}


.variant-close {
    flex: 0 0 auto;

    width: 40px;
    height: 40px;

    margin: -4px -4px 0 0;

    padding: 0;

    border: 1px solid var(--border-input);
    border-radius: 8px;

    background: var(--surface-hover);

    color: var(--text-muted-strong);

    font-size: 27px;
    line-height: 1;

    cursor: pointer;
}

.variant-close:hover {
    background: var(--border);
    color: var(--text-strong);
}

.variant-close:disabled {
    opacity: 0.4;
    cursor: default;
}


/*
 * Card information
 */

.variant-card-info {
    padding: 14px 18px;

    color: var(--text-secondary);

    font-size: 15px;
    font-weight: 550;

    border-bottom: 1px solid var(--surface-hover);

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}


/*
 * Variant list
 */

.variant-options {
    display: flex;
    flex-direction: column;

    gap: 8px;

    padding: 14px 16px;

    overflow-y: auto;

    overscroll-behavior: contain;
}


.variant-option {
    width: 100%;

    display: flex;
    align-items: center;
    justify-content: space-between;

    gap: 16px;

    margin: 0;
    padding: 14px;

    border: 1px solid var(--border-secondary);
    border-radius: 10px;

    background: var(--surface-secondary);

    color: var(--text-strong);

    text-align: left;

    cursor: pointer;

    transition:
        border-color 0.12s ease,
        background 0.12s ease,
        transform 0.08s ease;
}

.variant-option:hover {
    background: var(--surface-hover);
    border-color: var(--border-input);
}

.variant-option:active {
    transform: scale(0.99);
}

.variant-option.selected {
    background: var(--surface-hover);
    border-color: var(--text-muted-strong);
    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.08);
}


.variant-option-main {
    min-width: 0;

    display: flex;
    flex-direction: column;

    gap: 5px;
}


.variant-option-name {
    color: var(--text-strong);

    font-size: 16px;
    font-weight: 600;
}


.variant-option-id {
    color: var(--text-muted);

    font-family: monospace;
    font-size: 11px;
}


.variant-option-price {
    flex: 0 0 auto;

    color: var(--text-strong);

    font-size: 16px;
    font-weight: 650;

    white-space: nowrap;
}


/*
 * Footer
 */

.variant-dialog-footer {
    display: flex;

    gap: 8px;

    padding: 12px 16px;

    border-top: 1px solid var(--border);

    background: var(--surface);
}

.variant-dialog-footer .control-button {
    flex: 1;

    margin: 0;
}


.control-button {
    cursor: pointer;
}

.control-button.primary {
    background: var(--text);
    border-color: var(--text);

    color: var(--background);

    font-weight: 650;
}

.control-button.primary:hover {
    background: var(--text-secondary);
    border-color: var(--text-secondary);
}

.control-button:disabled {
    opacity: 0.45;
    cursor: default;
}


/*
 * Modal on smaller phones
 */

@media (max-width: 480px) {

    .variant-modal {
        padding: 10px;
    }

    .variant-dialog {
        max-height: calc(100vh - 20px);
        border-radius: 12px;
    }

    .variant-dialog-header {
        padding: 15px;
    }

    .variant-card-info {
        padding: 12px 15px;
    }

    .variant-options {
        padding: 12px;
    }

    .variant-option {
        padding: 13px;
    }

    .variant-dialog-footer {
        padding: 10px 12px;
    }
}



/*
 * ---------------------------------------------------------------------------
 * Opening modal
 * ---------------------------------------------------------------------------
 */

.opening-dialog {
    max-width: 480px;
}


/*
 * Form
 */

.opening-form {
    display: flex;
    flex-direction: column;
    gap: 8px;

    padding: 16px 18px;
}

.opening-form-label {
    color: var(--text-muted);

    font-size: 13px;
    font-weight: 600;
}

.opening-input {
    width: 100%;

    padding: 12px 14px;

    border: 1px solid var(--border-input);
    border-radius: 8px;

    background: var(--surface-secondary);
    color: var(--text-strong);

    font: inherit;
    font-size: 16px;

    outline: none;
}

.opening-input:focus {
    border-color: var(--text-muted);
}

.opening-input::placeholder {
    color: var(--text-muted);
}

select.opening-input {
    appearance: auto;
    cursor: pointer;
}


/*
 * Error
 */

.opening-error {
    margin: 0 18px 14px;
    padding: 10px 12px;

    border: 1px solid #5a2929;
    border-radius: 8px;

    background: #241010;
    color: #ffb0b0;

    font-size: 14px;
}


/*
 * Opening summary
 */

.opening-summary {
    display: flex;
    flex-direction: column;

    padding: 4px 18px 14px;
}

.opening-summary-row {
    display: flex;
    align-items: center;
    justify-content: space-between;

    padding: 9px 0;

    border-bottom: 1px solid var(--surface-hover);

    color: var(--text-muted);
    font-size: 14px;
}

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

.opening-summary-row strong {
    color: var(--text-strong);

    font-size: 16px;
    font-weight: 650;
}


/*
 * Notes
 */

.opening-notes {
    resize: vertical;
    min-height: 80px;
    line-height: 1.4;
}


/*
 * Active opening bar
 * ---------------------------------------------------------------------------
 */

.active-opening-bar,
.no-active-opening-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;

    gap: 12px;

    padding: 12px 16px;

    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.active-opening-info,
.no-active-opening-bar > div {
    min-width: 0;
}

.active-opening-label,
.opening-status-label {
    color: var(--text-muted);

    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.08em;
}

.active-opening-title {
    margin-top: 3px;

    color: var(--text-strong);

    font-size: 15px;
    font-weight: 650;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.active-opening-meta {
    margin-top: 2px;

    color: var(--text-muted-strong);

    font-size: 12px;
}

.opening-status-text {
    margin-top: 3px;

    color: var(--text-muted-strong);

    font-size: 13px;
}

.active-opening-bar .control-button,
.no-active-opening-bar .control-button {
    flex: 0 0 auto;

    margin: 0;

    padding: 10px 12px;

    font-size: 14px;
}


/*
 * Mobile adjustments
 */

@media (max-width: 480px) {

    .opening-form {
        padding: 14px 15px;
    }

    .opening-error {
        margin-left: 15px;
        margin-right: 15px;
    }

    .active-opening-bar,
    .no-active-opening-bar {
        padding: 10px 12px;
    }

    .opening-status-text {
        font-size: 12px;
    }

    .active-opening-bar .control-button,
    .no-active-opening-bar .control-button {
        padding: 9px 10px;
        font-size: 13px;
    }
}


/*
 * ---------------------------------------------------------------------------
 * Opening type
 * ---------------------------------------------------------------------------
 */

.opening-type-options {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;

    margin-bottom: 12px;
}

.opening-type-option {
    position: relative;

    display: flex;
    align-items: center;
    justify-content: center;

    min-height: 52px;

    border: 1px solid var(--border-input);
    border-radius: 10px;

    background: var(--surface-secondary);
    color: var(--text-muted-strong);

    font-size: 15px;
    font-weight: 650;

    cursor: pointer;

    -webkit-tap-highlight-color: transparent;
}

.opening-type-option input {
    position: absolute;

    width: 1px;
    height: 1px;

    opacity: 0;
}

.opening-type-option:has(input:checked) {
    background: var(--surface-hover);
    border-color: var(--text-muted-strong);

    color: var(--text-strong);

    box-shadow:
        0 0 0 1px rgba(255, 255, 255, 0.08);
}

.opening-type-option:active {
    transform: scale(0.98);
}


/*
 * ---------------------------------------------------------------------------
 * Set selector
 * ---------------------------------------------------------------------------
 */

.opening-input {
    width: 100%;

    min-height: 52px;

    padding: 0 14px;

    border: 1px solid var(--border-input);
    border-radius: 10px;

    background: var(--surface-secondary);
    color: var(--text-strong);

    font-family: inherit;
    font-size: 16px;

    outline: none;

    color-scheme: inherit;
}

.opening-input:focus {
    border-color: var(--text-muted);
}

select.opening-input {
    cursor: pointer;
}

.camera-prompt {
    position: absolute;
    inset: 0;
    z-index: 12;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(5, 10, 18, 0.85);
}
.camera-prompt-card {
    width: 100%;
    padding: 24px;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--surface);
    color: var(--text);
    text-align: center;
}
.camera-prompt-card h2 { margin: 0 0 10px; font-size: 22px; }
.camera-prompt-card p { margin: 0 0 20px; color: var(--text-secondary); font-size: 15px; line-height: 1.6; }
.camera-prompt-card button { width: 100%; margin: 0; }

.camera-container #frozenFrame { display: none; }
.camera-container #frozenFrame.visible { display: block; }

.scanner-settings {
    width: min(480px, calc(100% - 32px));
    max-height: calc(100dvh - 32px);
    overflow-y: auto;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 24px 80px rgba(0, 0, 0, .4);
}
.scanner-settings::backdrop { background: rgba(5, 10, 18, .75); backdrop-filter: blur(5px); }
.scanner-settings .opening-error { margin: 0; }
.scanner-settings #debugToggle { align-self: flex-start; color: var(--text-muted); }
.scanner > .scanner-header { padding: 12px 16px; }
.scanner > .scanner-header .header-row { margin-bottom: 0; }
.scanner > .scanner-header h1 { font-size: 18px; }
.scanner .no-active-opening-bar { padding: 8px 16px; }
.scanner .camera-container { margin-top: 0; margin-bottom: 0; border-radius: 0; }
.scanner .status { font-size: 13px; padding: 10px 14px; }
.scanner .scanner-controls { padding: 10px 12px; }
.scanner #scannerName[readonly] { opacity: .7; }

.group-actions { display: flex; gap: 8px; }
.scanner .active-opening-bar { padding: 8px 12px; }
.record-group { color: var(--error); }
.active-opening-label { color: var(--success); }
.recording-paused .active-opening-label { color: var(--text-muted); }
@media (max-width: 760px) {
    main.scanner { width: 100%; margin: 0; border: 0; border-radius: 0; }
    .scanner .camera-container { max-width: 100%; }
}

/* Solid, readable recognition panel; the camera's printed footer stays visible. */
.camera-container .last-identifier {
    top: 10px; bottom: auto; width: calc(100% - 20px); max-width: none;
    padding: 10px 12px; background: var(--surface); color: var(--text);
    border: 1px solid var(--border); border-radius: 12px; text-align: left;
    font-family: inherit; font-size: 15px; letter-spacing: 0; backdrop-filter: none;
    box-shadow: 0 4px 18px rgba(0,0,0,.25);
    display: flex; flex-direction: column; gap: 3px;
}
.recognition-stage { color: var(--accent); font-size: 10px; font-weight: 750; letter-spacing: .08em; text-transform: uppercase; }
.recognition-details { color: var(--text-secondary); font-size: 12px; font-weight: 400; overflow-wrap: anywhere; }
#recognitionTitle { font-size: 15px; color: var(--text); }
.camera-container[data-recognition="reading"] .card-guide { border-color: #ffd479; }
.camera-container[data-recognition="recognized"] .card-guide,
.camera-container[data-recognition="saved"] .card-guide { border-color: #79dda8; }
.camera-container[data-recognition="recognized"] .recognition-stage,
.camera-container[data-recognition="saved"] .recognition-stage { color: var(--success); }

.settings-divider { border: 0; border-top: 1px solid var(--border); width: 100%; margin: 12px 0; }
.camera-settings-title { margin: 0; font-size: 16px; }
.torch-control { display: flex; align-items: center; gap: 10px; padding: 10px 0; font-size: 14px; }
.torch-control input { width: 20px; height: 20px; accent-color: var(--accent); }
.guide-alignment-label { position: absolute; left: 8px; top: 8px; padding: 3px 7px; border-radius: 5px; background: #121b28; color: #edf3fc; font-size: 10px; font-weight: 600; }
.identifier-guide { position: absolute; left: 1%; right: 1%; bottom: 0; height: 14%; border: 2px dashed #ffd479; border-radius: 7px; background: rgba(255,212,121,.06); }
.identifier-guide span { position: absolute; left: 4px; top: -23px; padding: 3px 7px; border-radius: 5px; background: #121b28; color: #ffd479; font-size: 10px; font-weight: 600; white-space: nowrap; }
/* Leave the whole footer visible: identifiers are not always on the left. */
.identifier-guide::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 48%; border-right: 1px dashed #ffd479; background: rgba(255,212,121,.08); }

/* Let the camera show through while maintaining text contrast. */
.camera-container .last-identifier {
    background: color-mix(in srgb, var(--surface) 30%, transparent);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.recognition-evidence { margin: 0; padding: 12px 18px; font-size: 13px; color: var(--text-secondary); border-bottom: 1px solid var(--border); }
