:root {
    --yellow: #f2c500;
    --dark: #111;
    --muted: #6b6b6b;
    --error: #e5484d;
    --ok: #2ecc71;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: #000;
    color: #fff;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

button, input, label { font: inherit; }
button, label.action { cursor: pointer; }

svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.app {
    position: fixed;
    inset: 0;
}

/* ---------- Caméra et cadre de visée ---------- */

.camera {
    position: absolute;
    inset: 0;
    background: #000;
}
.camera video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.mask {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 60px;
}

/* Zone réellement envoyée à l'OCR, aux proportions d'une étiquette */
.frame {
    position: relative;
    width: min(84vw, 520px, 52vh * 2.4);
    aspect-ratio: 2.4 / 1;
    border-radius: 16px;
    box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .55);
    overflow: hidden;
}
.corner {
    position: absolute;
    width: 30px;
    height: 30px;
    border: 4px solid var(--yellow);
    transition: border-color .2s;
}
.corner.tl { top: 0; left: 0; border-right: 0; border-bottom: 0; border-top-left-radius: 16px; }
.corner.tr { top: 0; right: 0; border-left: 0; border-bottom: 0; border-top-right-radius: 16px; }
.corner.bl { bottom: 0; left: 0; border-right: 0; border-top: 0; border-bottom-left-radius: 16px; }
.corner.br { bottom: 0; right: 0; border-left: 0; border-top: 0; border-bottom-right-radius: 16px; }

.frame.scanning::after {
    content: "";
    position: absolute;
    left: 6%;
    right: 6%;
    height: 2px;
    background: var(--yellow);
    box-shadow: 0 0 10px var(--yellow);
    animation: scanline 1.8s ease-in-out infinite alternate;
}
@keyframes scanline {
    from { top: 12%; }
    to { top: 88%; }
}
.frame.detected .corner { border-color: var(--ok); }

/* ---------- Barres haute et basse ---------- */

.topbar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 6;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(var(--safe-top) + 12px) 16px 24px;
    background: linear-gradient(rgba(0, 0, 0, .7), transparent);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-logo { width: 40px; height: 40px; border-radius: 16%; box-shadow: 0 2px 8px rgba(0, 0, 0, .4); }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand strong { font-size: 1.1rem; }
.brand span { font-size: .8rem; opacity: .75; }

.tools { display: flex; gap: 10px; }
.icon-btn {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    border: 1px solid rgba(255, 255, 255, .3);
    background: rgba(0, 0, 0, .45);
    color: #fff;
    font-weight: 700;
    display: grid;
    place-items: center;
    padding: 0;
}
.icon-btn.active { background: var(--yellow); color: var(--dark); border-color: var(--yellow); }
.icon-btn.active svg { fill: currentColor; }

.status {
    position: absolute;
    z-index: 6;
    top: calc(var(--safe-top) + 84px);
    left: 50%;
    transform: translateX(-50%);
    max-width: calc(100% - 32px);
    width: max-content;
    padding: 8px 16px;
    border-radius: 999px;
    background: rgba(0, 0, 0, .65);
    font-size: .95rem;
    text-align: center;
}
.status.error { background: var(--error); }
.status.busy::before {
    content: "";
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-right: 8px;
    vertical-align: -1px;
    border: 2px solid rgba(255, 255, 255, .4);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.bottombar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 6;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    padding: 28px 16px calc(var(--safe-bottom) + 12px);
    background: linear-gradient(transparent, rgba(0, 0, 0, .8));
}
.action {
    min-height: 64px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border: 0;
    border-radius: 14px;
    background: rgba(255, 255, 255, .12);
    color: #fff;
    font-size: .85rem;
}
.action:active { background: rgba(255, 255, 255, .25); }

/* ---------- Écran de démarrage ---------- */

.start {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: calc(var(--safe-top) + 80px) 24px calc(var(--safe-bottom) + 110px);
    background: #111;
    text-align: center;
}
.start[hidden] { display: none; }
.start h1 { margin: 12px 0 0; font-size: 1.5rem; }
.start p { margin: 0; max-width: 340px; opacity: .8; line-height: 1.4; }
.start .btn { max-width: 340px; margin-top: 8px; }
.start-msg { min-height: 1.4em; font-size: .9rem; }

/* ---------- Étiquette (rappel visuel) ---------- */

.tag {
    background: var(--yellow);
    color: var(--dark);
    border-radius: 12px;
    padding: 10px 16px 6px;
    text-align: center;
    line-height: 1.15;
}
.tag small { display: block; font-size: .75rem; }
.tag b { display: block; font-size: .95rem; }
.tag span {
    display: block;
    font-size: 3.4rem;
    font-weight: 800;
    letter-spacing: .04em;
    font-variant-numeric: tabular-nums;
}
/* Le JPEG a un fond plein : les coins arrondis du logo sont recréés en CSS */
.start-logo {
    width: min(180px, 40vw);
    height: auto;
    border-radius: 16%;
    box-shadow: 0 8px 30px rgba(242, 197, 0, .25);
}

/* ---------- Panneaux glissants ---------- */

.backdrop {
    position: fixed;
    inset: 0;
    z-index: 10;
    background: rgba(0, 0, 0, .5);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s;
}
.backdrop.show { opacity: 1; pointer-events: auto; }

.sheet {
    position: fixed;
    left: 0;
    right: 0;
    /* --kb : hauteur du clavier virtuel quand il recouvre la page (calculée en JS) */
    bottom: var(--kb, 0px);
    z-index: 11;
    width: 100%;
    max-width: 560px;
    max-height: calc(85vh - var(--kb, 0px));
    max-height: calc(85dvh - var(--kb, 0px));
    margin: 0 auto;
    overflow-y: auto;
    padding: 10px 20px calc(var(--safe-bottom) + 20px);
    border-radius: 22px 22px 0 0;
    background: #fff;
    color: var(--dark);
    transform: translateY(110%);
    transition: transform .28s cubic-bezier(.2, .8, .2, 1);
    display: grid;
    gap: 12px;
}
.sheet.open { transform: none; }
.sheet.dragging { transition: none; }
.sheet h2 { margin: 0; font-size: 1.2rem; }

.handle {
    width: 42px;
    height: 5px;
    border-radius: 3px;
    background: #d0d0d0;
    margin: 0 auto 6px;
}

.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 54px;
    padding: 0 18px;
    border: 1px solid #d6d6d6;
    border-radius: 14px;
    background: #fff;
    color: var(--dark);
    font-size: 1.05rem;
    font-weight: 600;
    text-decoration: none;
}
.btn:active { filter: brightness(.92); }
.btn.primary { background: var(--dark); border-color: var(--dark); color: #fff; }
.btn.yellow { background: var(--yellow); border-color: var(--yellow); color: var(--dark); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.progress {
    height: 6px;
    border-radius: 3px;
    background: #eee;
    overflow: hidden;
    visibility: hidden;
}
.progress div { height: 100%; width: 0; background: var(--yellow); }
.counting .progress { visibility: visible; }
.counting .progress div { animation: fill var(--duration, 3s) linear forwards; }
@keyframes fill { to { width: 100%; } }
.countdown { margin: -4px 0 0; min-height: 1.2em; text-align: center; color: var(--muted); font-size: .9rem; }

#manualForm { display: grid; gap: 12px; }
#manualInput {
    width: 100%;
    height: 72px;
    border: 2px solid #d6d6d6;
    border-radius: 14px;
    text-align: center;
    font-size: 2.4rem;
    font-weight: 800;
    letter-spacing: .3em;
    font-variant-numeric: tabular-nums;
    color: var(--dark);
    user-select: text;
    -webkit-user-select: text;
}
#manualInput:focus { outline: none; border-color: var(--yellow); }
.error { margin: 0; color: var(--error); font-weight: 600; }
.error[hidden] { display: none; }

.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.link-btn { border: 0; background: none; color: var(--error); font-weight: 600; padding: 8px; }
.link-btn[hidden] { display: none; }

.history { list-style: none; margin: 0; padding: 0; }
.history li + li { border-top: 1px solid #eee; }
.history button {
    width: 100%;
    min-height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border: 0;
    background: none;
    color: var(--dark);
    padding: 0 4px;
}
.history .num {
    font-size: 1.4rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    background: var(--yellow);
    border-radius: 6px;
    padding: 2px 10px;
}
.history .time { color: var(--muted); font-size: .9rem; }
.history .empty { color: var(--muted); padding: 16px 4px; }

.noscript { position: fixed; inset: auto 16px 16px; z-index: 20; display: flex; gap: 8px; }
.noscript input { flex: 1; font-size: 1.4rem; padding: 10px; }

/* ---------- Paysage sur téléphone ---------- */

@media (orientation: landscape) and (max-height: 500px) {
    .mask { padding-bottom: 0; }
    .topbar { padding-top: calc(var(--safe-top) + 6px); }
    .status { top: calc(var(--safe-top) + 64px); }
    .bottombar { left: auto; width: 320px; padding-top: 12px; }
    .action { min-height: 52px; }
    .start { flex-direction: row; flex-wrap: wrap; padding-top: 70px; padding-bottom: 80px; }
    .tag span { font-size: 2.4rem; }
}

@media (prefers-reduced-motion: reduce) {
    .frame.scanning::after { animation: none; top: 50%; }
    .sheet, .backdrop { transition: none; }
}
