:root {
    --bg: #111418;
    --panel: #1b2027;
    --line: #2b323c;
    --text: #f2f4f7;
    --muted: #8d97a5;
    --ok: #2ecc71;
    --warn: #f5a623;
    --err: #e74c3c;
    --accent: #3b82f6;
}

* {
    box-sizing: border-box;
}

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

button {
    font: inherit;
    color: inherit;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 640px;
    margin: 0 auto;
    padding-top: env(safe-area-inset-top);
}

.batch-header {
    display: flex;
    justify-content: space-between;
    padding: 10px 14px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    font-weight: 700;
}

.batch-status {
    color: var(--ok);
    font-size: 0.85rem;
    align-self: center;
}

.batch-status.closed {
    color: var(--muted);
}

.camera {
    position: relative;
    flex: 0 0 33dvh;
    container-type: size;
    background: #000;
    overflow: hidden;
}

.camera video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.camera-frame {
    position: absolute;
    top: 50%;
    left: 50%;
    width: min(70cqh, 90cqw);
    aspect-ratio: 1;
    transform: translate(-50%, -50%);
    border: 4px solid rgba(234, 9, 9, 0.7);
    border-radius: 10px;
    pointer-events: none;
}

.camera-controls {
    position: absolute;
    right: 8px;
    bottom: 8px;
    display: flex;
    gap: 8px;
}

.chip {
    min-width: 44px;
    min-height: 44px;
    padding: 0 12px;
    border: 0;
    border-radius: 22px;
    background: rgba(0, 0, 0, 0.6);
}

.chip.on {
    background: var(--warn);
    color: #000;
}

.feedback {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    padding: 10px 14px;
    font-weight: 700;
    text-align: center;
}

.feedback.success {
    background: var(--ok);
    color: #04210f;
}

.feedback.duplicate {
    background: var(--warn);
    color: #2b1b00;
}

.feedback.invalid,
.feedback.error {
    background: var(--err);
    color: #fff;
}

.counter {
    text-align: center;
    padding: 8px 0;
    border-bottom: 1px solid var(--line);
}

.counter-value {
    font-size: 3.2rem;
    font-weight: 800;
    line-height: 1;
}

.counter-label {
    color: var(--muted);
    font-size: 0.8rem;
    letter-spacing: 0.08em;
    margin: 4px 0;
}

.counter-total {
    font-size: 1rem;
}

h2 {
    margin: 0;
    padding: 8px 14px 4px;
    font-size: 0.75rem;
    letter-spacing: 0.08em;
    color: var(--muted);
}

.last-scan {
    border-bottom: 1px solid var(--line);
}

.last-scan-body {
    padding: 0 14px 8px;
    min-height: 2.6em;
}

.last-scan-body .sub {
    color: var(--muted);
    font-size: 0.9rem;
}

.scan-list-section {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.scan-list {
    flex: 1;
    overflow-y: auto;
    margin: 0;
    padding: 0;
    list-style: none;
}

.scan-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
    border-bottom: 1px solid var(--line);
}

.scan-number {
    width: 2.4em;
    font-weight: 700;
    color: var(--muted);
}

.scan-info {
    flex: 1;
    min-width: 0;
}

.scan-info .sub {
    color: var(--muted);
    font-size: 0.85rem;
}

.delete-button {
    min-width: 44px;
    min-height: 44px;
    border: 0;
    background: none;
    font-size: 1.2rem;
}

.actions {
    display: flex;
    gap: 10px;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    background: var(--panel);
    border-top: 1px solid var(--line);
}

.btn {
    flex: 1;
    min-height: 48px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--bg);
    font-weight: 700;
}

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
}

.btn-danger {
    background: var(--err);
    border-color: var(--err);
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(0, 0, 0, 0.7);
    z-index: 10;
}

.modal {
    width: 100%;
    max-width: 380px;
    padding: 20px;
    border-radius: 12px;
    background: var(--panel);
    text-align: center;
}

.modal h3 {
    margin: 0 0 12px;
}

.modal p {
    margin: 4px 0;
}

.modal input {
    width: 100%;
    padding: 12px;
    margin-top: 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 1rem;
}

#enable-write-button {
    display: none;
}

.readonly #enable-write-button {
    display: block;
}

.readonly #finish-button,
.readonly .delete-button {
    display: none;
}

.readonly .camera {
    opacity: 0.35;
}

.history {
    position: fixed;
    inset: 0;
    z-index: 5;
    display: flex;
    flex-direction: column;
    max-width: 640px;
    margin: 0 auto;
    padding-top: env(safe-area-inset-top);
    background: var(--bg);
}

.history[hidden] {
    display: none;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    font-weight: 700;
}

.history-footer {
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    background: var(--panel);
    border-top: 1px solid var(--line);
}

.history-footer .btn {
    width: 100%;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    margin: 0;
    padding: 0;
    list-style: none;
}

.history-item {
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
}

.history-title {
    font-weight: 700;
}

.history-item .sub {
    color: var(--muted);
    font-size: 0.85rem;
    margin: 2px 0 8px;
}

.history-actions {
    display: flex;
    gap: 8px;
}

.history-actions .btn {
    min-height: 40px;
    font-size: 0.8rem;
}

.history-empty {
    padding: 24px;
    text-align: center;
    color: var(--muted);
}

/* Móvil en horizontal: cámara a la izquierda, datos a la derecha. */
@media (orientation: landscape) and (max-height: 600px) {
    .app {
        display: grid;
        grid-template-columns: 40% 1fr;
        grid-template-rows: auto auto auto minmax(0, 1fr) auto;
        grid-template-areas:
            "header header"
            "camera counter"
            "camera last"
            "camera list"
            "camera actions";
        max-width: none;
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }

    .batch-header {
        grid-area: header;
    }

    .camera {
        grid-area: camera;
        flex: none;
    }

    .counter {
        grid-area: counter;
        padding: 4px 0;
    }

    .counter-value {
        font-size: 2.4rem;
    }

    .last-scan {
        grid-area: last;
    }

    .scan-list-section {
        grid-area: list;
    }

    .actions {
        grid-area: actions;
        padding-bottom: 10px;
    }
}

.modal-actions {
    display: flex;
    gap: 10px;
    margin-top: 18px;
}