/* --- VARIABLES & CORE --- */
:root {
    --bg-color: #0d1117;
    --surface-color: #161b22;
    --primary-color: #8a2be2;
    --primary-hover: #9c40fa;
    --accent-color: #00e5ff;
    --text-primary: #f0f6fc;
    --text-secondary: #8b949e;
    --error-color: #ff5252;
    --success-color: #2ea043;

    --border-radius-lg: 24px;
    --border-radius-md: 16px;
    --transition-fast: 0.2s ease;
    --transition-smooth: 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    height: 100dvh;
}

#app-container {
    width: 100%;
    max-width: 500px;
    height: 100%;
    position: relative;
    background: radial-gradient(circle at top, #1c2135 0%, var(--bg-color) 60%);
    overflow: hidden;
}

/* --- SCREENS --- */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    opacity: 0;
    pointer-events: none;
    transform: scale(0.98);
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.screen.view-active {
    opacity: 1;
    pointer-events: auto;
    transform: scale(1);
    z-index: 10;
    display: flex;
    /* Ensures it takes up space correctly */
}

.hidden {
    display: none !important;
    opacity: 0 !important;
    pointer-events: none !important;
}

/* --- BUTTONS --- */
.primary-btn,
.secondary-btn {
    font-family: inherit;
    font-weight: 600;
    font-size: 1.1rem;
    padding: 16px 32px;
    border-radius: 100px;
    border: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: var(--transition-fast);
    width: 100%;
}

.primary-btn {
    background: linear-gradient(135deg, var(--primary-color), #6200ea);
    color: white;
    box-shadow: 0 8px 20px rgba(138, 43, 226, 0.3);
}

.primary-btn:active {
    transform: translateY(2px);
    box-shadow: 0 4px 10px rgba(138, 43, 226, 0.5);
}

.secondary-btn {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.secondary-btn:active {
    background: rgba(255, 255, 255, 0.1);
}

.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: white;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(10px);
}

.danger-text-btn {
    background: transparent;
    color: var(--error-color);
    border: none;
    font-family: inherit;
    font-size: 0.9rem;
    padding: 8px;
    opacity: 0.8;
}

/* --- START SCREEN --- */
#start-screen {
    justify-content: center;
    align-items: center;
    padding: 24px;
    text-align: center;
}

.logo-large {
    margin-bottom: 60px;
    animation: slideDown 0.8s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.logo-icon-large {
    font-size: 4rem;
    display: block;
    margin-bottom: 16px;
    animation: float 4s ease-in-out infinite;
}

.logo-large h1 {
    font-size: 2.5rem;
    font-weight: 800;
    background: linear-gradient(135deg, #fff, var(--primary-color));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 8px;
}

.subtitle {
    color: var(--text-secondary);
    font-weight: 300;
}

.main-actions {
    width: 100%;
    max-width: 300px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.pulse-glow {
    animation: pulseGlow 2s infinite;
}

@keyframes pulseGlow {
    0% {
        box-shadow: 0 0 0 0 rgba(138, 43, 226, 0.4);
    }

    70% {
        box-shadow: 0 0 0 15px rgba(138, 43, 226, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(138, 43, 226, 0);
    }
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

@keyframes slideDown {
    from {
        transform: translateY(-30px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.count-badge {
    background: var(--primary-color);
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 800;
}


/* --- SCANNER SCREEN --- */
#scanner-screen {
    background: #000;
}

/* Scanner controls moved to bottom HUD */

/* Status Bubble (Now clickable) */

.status-indicator {
    background: rgba(22, 27, 34, 0.9);
    backdrop-filter: blur(10px);
    padding: 12px 24px;
    border-radius: 100px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    font-size: 1rem;
    font-weight: 800;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.status-indicator.clickable:active {
    transform: scale(0.95);
    background: rgba(138, 43, 226, 0.3);
}

.status-indicator.pop-animation {
    transform: scale(1.1);
    background: rgba(138, 43, 226, 0.4);
    border-color: var(--primary-color);
}

.scanner-main {
    flex: 1;
    position: relative;
    height: 100%;
}

.scanner-frame {
    width: 100%;
    height: 100%;
}

#camera-feed {
    width: 100%;
    height: 100%;
    object-fit: cover;
    background-color: #000;
    /* Fallback */
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
}

/* Guide Box & Animation */
.guide-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 65%;
    height: 60%;
    /* Vertical/Portrait ratio for full card */
    border-radius: 12px;
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.7);
    z-index: 2;
}

.corner {
    position: absolute;
    width: 30px;
    height: 30px;
    border: 3px solid var(--primary-color);
    border-radius: 4px;
}

.guide-box.scanning .corner {
    border-color: var(--accent-color);
}

.top-left {
    top: -2px;
    left: -2px;
    border-right: none;
    border-bottom: none;
}

.top-right {
    top: -2px;
    right: -2px;
    border-left: none;
    border-bottom: none;
}

.bottom-left {
    bottom: -2px;
    left: -2px;
    border-right: none;
    border-top: none;
}

.bottom-right {
    bottom: -2px;
    right: -2px;
    border-left: none;
    border-top: none;
}

.scan-line {
    position: absolute;
    width: 100%;
    height: 2px;
    background: var(--accent-color);
    box-shadow: 0 0 10px 2px var(--accent-color);
    top: 0;
    left: 0;
    z-index: 3;
    animation: scanLine 2s linear infinite;
}

@keyframes scanLine {
    0% {
        top: 0;
        opacity: 0;
    }

    10% {
        opacity: 1;
    }

    90% {
        opacity: 1;
    }

    100% {
        top: 100%;
        opacity: 0;
    }
}

/* HUD Component underneath guide box */
.scanner-hud-bottom {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    display: flex;
    flex-direction: column;
    padding: 20px;
    gap: 12px;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.9) 0%, rgba(0, 0, 0, 0.5) 60%, transparent 100%);
    z-index: 20;
}

.scanner-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 8px;
}

.danger-btn-circle {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--error-color);
    border: 2px solid rgba(255, 255, 255, 0.2);
    color: white;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 10px rgba(255, 82, 82, 0.4);
}

.danger-btn-circle:active {
    transform: scale(0.9);
}

/* OCR Debug Box */
.ocr-debug-box {
    background: rgba(0, 0, 0, 0.6);
    color: var(--accent-color);
    padding: 12px;
    border-radius: 8px;
    font-size: 0.85rem;
    font-family: monospace;
    backdrop-filter: blur(4px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    min-height: 40px;
    word-break: break-all;
}

/* Toast */
.toast {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: rgba(22, 27, 34, 0.95);
    border: 1px solid var(--primary-color);
    color: white;
    padding: 12px 24px;
    border-radius: 100px;
    font-size: 0.95rem;
    font-weight: 600;
    backdrop-filter: blur(10px);
    z-index: 30;
    opacity: 0;
    pointer-events: none;
    transition: var(--transition-smooth);
    white-space: nowrap;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}


/* --- LIST SCREEN --- */
#list-screen {
    background: var(--bg-color);
}

.list-header {
    padding: 24px 20px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.list-actions {
    padding: 8px 20px;
    text-align: right;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.list-main {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    /* Important for swipe */
    padding: 0 20px 40px;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 60%;
    color: var(--text-secondary);
    text-align: center;
    gap: 16px;
}

.empty-icon {
    font-size: 4rem;
    opacity: 0.5;
}

.cards-grid {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding-top: 16px;
}

.card-item-wrapper {
    position: relative;
    width: 100%;
    overflow: hidden;
    border-radius: var(--border-radius-md);
    margin-bottom: 16px;
    background: var(--surface-color);
}

.card-item-background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    border-radius: var(--border-radius-md);
    z-index: 1;
}

.bg-delete {
    background: var(--error-color);
}

.bg-add {
    background: var(--success-color);
}

.action-text {
    color: white;
    font-weight: 800;
    font-size: 1.2rem;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.confirm-delete-btn {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 120px;
    /* Più largo per effetto semi-copertura */
    background: var(--error-color);
    color: white;
    border: none;
    font-weight: 800;
    font-size: 1rem;
    border-radius: 0 var(--border-radius-md) var(--border-radius-md) 0;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1;
    /* Sotto la parte trascinata, ma visibile */
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, width 0.2s ease;
    box-shadow: -5px 0 15px rgba(0, 0, 0, 0.2);
}

.confirm-delete-btn.show {
    opacity: 1;
    pointer-events: auto;
}

.confirm-delete-btn:active {
    background: #d32f2f;
}

.card-item {
    display: flex;
    background: var(--surface-color);
    border-radius: var(--border-radius-md);
    padding: 12px;
    gap: 16px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    align-items: center;
    position: relative;
    z-index: 2;
    transition: transform 0.2s ease-out;
    /* Smooth snapping back */
    animation: slideInLeft 0.3s ease forwards;
}

@keyframes slideInLeft {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.card-item img {
    width: 60px;
    height: 84px;
    object-fit: cover;
    border-radius: 4px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    position: relative;
}

.card-image-wrap {
    position: relative;
    cursor: pointer;
}

.qty-badge {
    position: absolute;
    top: -8px;
    right: -8px;
    background: var(--primary-color);
    color: white;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.8rem;
    font-weight: 800;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
    border: 2px solid var(--surface-color);
}

.card-item-details {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.card-item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 8px;
}

.card-item-header h4 {
    font-size: 1rem;
    line-height: 1.2;
    margin-right: 8px;
}

.badge {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-secondary);
    border: 1px solid rgba(255, 255, 255, 0.05);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
}

.card-item-price {
    font-size: 1.25rem;
    font-weight: 800;
    color: var(--accent-color);
}

.card-item-calc {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* List Footer / Totals */
.list-footer {
    padding: 20px;
    background: rgba(13, 17, 23, 0.9);
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    backdrop-filter: blur(10px);
}

.grand-total-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(135deg, rgba(138, 43, 226, 0.2), rgba(0, 229, 255, 0.1));
    border: 1px solid rgba(138, 43, 226, 0.3);
    padding: 16px 20px;
    border-radius: var(--border-radius-md);
}

.grand-total-box span {
    font-size: 0.9rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.grand-total-box h3 {
    font-size: 1.8rem;
    color: white;
    font-weight: 800;
    text-shadow: 0 0 15px rgba(0, 229, 255, 0.3);
}

/* Modal / Lightbox */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    backdrop-filter: blur(5px);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal.hidden {
    display: none !important;
}

.modal-content {
    position: relative;
    max-width: 90%;
    max-height: 90%;
}

#zoom-img {
    max-width: 100%;
    max-height: 85vh;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8);
}

.close-zoom {
    position: absolute;
    top: -40px;
    right: 0;
    background: rgba(255, 255, 255, 0.2);
}

/* --- DEBUG TOOLS --- */
#capture-canvas {
    position: absolute;
    top: calc(50% - 30% - 50px);
    /* Sopra la guide box (centro - metà altezza - offset) */
    left: 50%;
    transform: translateX(-50%);
    width: 65%;
    /* Stessa larghezza della guide-box */
    height: auto;
    z-index: 100;
    border: 2px solid #ff0000;
    box-shadow: 0 0 15px rgba(255, 0, 0, 0.4);
    pointer-events: none;
    background: #000;
    /* Filtro CSS per essere sicuri che l'utente lo veda in scala di grigi */
    filter: grayscale(100%) contrast(150%) brightness(120%);
}