/* ============================================================
   Archives PWA — Mobile-first Design
   ============================================================ */

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary:       #6b4a93;
    --primary-dark:  #4a3268;
    --primary-light: #e8dff5;
    --accent:        #FF6F00;
    --success:       #2E7D32;
    --error:         #C62828;
    --surface:       #FFFFFF;
    --background:    #F5F7FA;
    --on-primary:    #FFFFFF;
    --text:          #212121;
    --text-secondary:#757575;
    --border:        #E0E0E0;
    --radius:        10px;
    --shadow:        0 2px 8px rgba(0,0,0,0.1);
    --tab-height:    56px;
    --header-height: 56px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
    font-size: 16px;
    color: var(--text);
    background: var(--background);
    -webkit-font-smoothing: antialiased;
}

/* ---- LOGIN ---- */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
}

.login-container {
    background: var(--surface);
    border-radius: 16px;
    padding: 40px 32px;
    width: 100%;
    max-width: 380px;
    margin: 16px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.25);
    text-align: center;
}

.login-logo {
    margin-bottom: 16px;
}

.login-container h1 {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 4px;
}

.login-subtitle {
    color: var(--text-secondary);
    margin-bottom: 32px;
    font-size: 0.9rem;
}

.login-form {
    text-align: left;
}

/* ---- APP LAYOUT ---- */
.app-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    background: var(--primary);
    color: var(--on-primary);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    z-index: 100;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.app-title {
    font-size: 1.1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
}

.btn-icon {
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.btn-icon:hover {
    background: rgba(255,255,255,0.15);
}

.tab-nav {
    position: fixed;
    top: var(--header-height);
    left: 0;
    right: 0;
    height: var(--tab-height);
    background: var(--surface);
    display: flex;
    border-bottom: 2px solid var(--border);
    z-index: 99;
    box-shadow: var(--shadow);
}

.tab-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    margin-bottom: -2px;
    cursor: pointer;
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--text-secondary);
    transition: all 0.2s;
    padding: 4px 8px;
}

.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-btn:hover:not(.active) {
    background: var(--background);
    color: var(--text);
}

.app-main {
    margin-top: calc(var(--header-height) + var(--tab-height));
    padding: 16px;
    min-height: calc(100vh - var(--header-height) - var(--tab-height));
}

.tab-content {
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ---- CARD ---- */
.card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: var(--shadow);
}

.section-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--primary-light);
}

/* ---- FORMS ---- */
.form-group {
    margin-bottom: 14px;
}

label {
    display: block;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 5px;
    text-transform: uppercase;
    letter-spacing: 0.4px;
}

select,
input[type="text"],
input[type="number"],
input[type="password"],
input[type="search"],
input[type="email"] {
    width: 100%;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
    color: var(--text);
    background: var(--surface);
    transition: border-color 0.2s, box-shadow 0.2s;
    -webkit-appearance: none;
    appearance: none;
}

select:focus,
input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(25,118,210,0.12);
}

input::placeholder {
    color: #BDBDBD;
}

.carton-input-row {
    display: flex;
    gap: 8px;
    align-items: center;
}

.carton-input-row input {
    flex: 1;
}

/* ---- BUTTONS ---- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 20px;
    border-radius: 8px;
    border: none;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

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

.btn-primary:hover:not(:disabled) {
    background: var(--primary-dark);
    box-shadow: 0 4px 12px rgba(25,118,210,0.35);
}

.btn-primary:active:not(:disabled) {
    transform: scale(0.97);
}

.btn-secondary {
    background: var(--background);
    color: var(--text);
    border: 1.5px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
    background: #EEEEEE;
}

.btn-sm {
    padding: 8px 14px;
    font-size: 0.85rem;
}

.btn-block {
    width: 100%;
    margin-top: 8px;
}

.btn-scan {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: white;
    width: 100%;
    padding: 16px;
    font-size: 1rem;
    border-radius: 10px;
    border: none;
    margin-bottom: 16px;
    box-shadow: 0 4px 12px rgba(25,118,210,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.btn-scan:hover {
    box-shadow: 0 6px 16px rgba(25,118,210,0.4);
    transform: translateY(-1px);
}

.btn-scan:active {
    transform: translateY(0);
}

/* ---- SCANNER ---- */
.scan-area {
    margin-bottom: 12px;
}

#scanner-container {
    position: relative;
    border-radius: 10px;
    overflow: hidden;
    background: #000;
    margin-bottom: 12px;
}

#scanner-video {
    width: 100%;
    height: 240px;
    object-fit: cover;
    display: block;
}

.scan-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.scan-frame {
    width: 200px;
    height: 120px;
    border: 3px solid rgba(255,255,255,0.8);
    border-radius: 8px;
    box-shadow: 0 0 0 4000px rgba(0,0,0,0.4);
    animation: scanPulse 2s infinite;
}

@keyframes scanPulse {
    0%, 100% { border-color: rgba(255,255,255,0.8); }
    50%       { border-color: rgba(25,200,100,0.9); }
}

#stop-scan-btn {
    position: absolute;
    bottom: 12px;
    right: 12px;
    background: rgba(0,0,0,0.6);
    color: white;
    border-color: rgba(255,255,255,0.4);
}

/* ---- FEEDBACK ---- */
.feedback {
    padding: 12px 14px;
    border-radius: 8px;
    margin-bottom: 12px;
    font-weight: 500;
}

.feedback.success {
    background: #E8F5E9;
    color: var(--success);
    border-left: 4px solid var(--success);
}

.feedback.error {
    background: #FFEBEE;
    color: var(--error);
    border-left: 4px solid var(--error);
}

.error-msg {
    background: #FFEBEE;
    color: var(--error);
    padding: 10px 12px;
    border-radius: 6px;
    font-size: 0.9rem;
    margin-bottom: 12px;
}

/* ---- SEARCH RESULTS ---- */
.result-item {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 14px 16px;
    margin-bottom: 8px;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: all 0.15s;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    border-left: 4px solid transparent;
}

.result-item:hover, .result-item:active {
    border-left-color: var(--primary);
    transform: translateX(2px);
}

.result-main {
    flex: 1;
    min-width: 0;
}

.result-numero {
    font-weight: 700;
    font-size: 0.95rem;
    color: var(--text);
    font-family: monospace;
}

.result-client {
    font-size: 0.88rem;
    color: var(--text-secondary);
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.result-libelle {
    font-size: 0.82rem;
    color: #9E9E9E;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.result-carton {
    background: var(--primary);
    color: white;
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 0.78rem;
    font-weight: 700;
    white-space: nowrap;
    margin-left: 12px;
    flex-shrink: 0;
}

.result-certification {
    display: inline-block;
    background: #ede7f6;
    color: var(--primary);
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    margin-top: 4px;
}

.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}

.empty-state svg {
    opacity: 0.3;
    margin-bottom: 12px;
}

.empty-state p {
    font-size: 0.95rem;
}

/* ---- CARTONS LIST ---- */
.carton-item {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 14px 16px;
    margin-bottom: 8px;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: all 0.15s;
    display: flex;
    align-items: center;
    gap: 14px;
}

.carton-item:hover {
    transform: translateX(2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.12);
}

.carton-number {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: white;
    width: 52px;
    height: 52px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    font-weight: 700;
    flex-shrink: 0;
}

.carton-info {
    flex: 1;
}

.carton-count {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text);
}

.carton-date {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 3px;
}

.carton-arrow {
    color: var(--text-secondary);
    flex-shrink: 0;
}

/* ---- MODAL ---- */
.modal {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: flex-end;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.5);
}

.modal-content {
    position: relative;
    background: var(--surface);
    border-radius: 20px 20px 0 0;
    padding: 24px 20px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
    animation: slideUp 0.25s ease;
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to   { transform: translateY(0); }
}

.modal-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: var(--background);
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}

.modal-title {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 20px;
    padding-right: 40px;
    color: var(--primary);
}

.detail-row {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
    gap: 12px;
}

.detail-row:last-child {
    border-bottom: none;
}

.detail-label {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.4px;
    flex-shrink: 0;
}

.detail-value {
    font-size: 0.95rem;
    color: var(--text);
    text-align: right;
    word-break: break-word;
}

.detail-carton-badge {
    background: var(--primary);
    color: white;
    padding: 6px 16px;
    border-radius: 20px;
    font-size: 1rem;
    font-weight: 700;
}

.carton-dossier-item {
    padding: 10px 0;
    border-bottom: 1px solid var(--border);
}

.carton-dossier-item:last-child {
    border-bottom: none;
}

.carton-dossier-num {
    font-weight: 700;
    font-family: monospace;
    font-size: 0.95rem;
}

.carton-dossier-client {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

/* ---- TOAST ---- */
.toast {
    position: fixed;
    bottom: calc(env(safe-area-inset-bottom) + 20px);
    left: 50%;
    transform: translateX(-50%);
    background: #323232;
    color: white;
    padding: 12px 24px;
    border-radius: 24px;
    font-size: 0.9rem;
    font-weight: 500;
    z-index: 300;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    animation: toastIn 0.3s ease;
    white-space: nowrap;
    max-width: 90vw;
    text-align: center;
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(-50%) translateY(20px); }
    to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ---- LOADING ---- */
.loading-spinner {
    display: flex;
    justify-content: center;
    padding: 24px;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ---- CHAMPS AUTO (Gamsys) ---- */
.field-auto {
    background: #f3f0f7 !important;
    color: var(--text);
    cursor: default;
}

.field-certif {
    width: 100%;
    padding: 12px 14px;
    border: 1.5px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
    min-height: 46px;
    color: var(--text-secondary);
}

.field-certif:not(:empty) {
    color: var(--text);
    font-weight: 500;
}

.gamsys-badge {
    font-size: 0.68rem;
    font-weight: 700;
    background: var(--primary-light);
    color: var(--primary);
    padding: 1px 6px;
    border-radius: 10px;
    vertical-align: middle;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

/* ---- GAMSYS STATUS ---- */
.gamsys-status {
    font-size: 0.82rem;
    margin-top: 5px;
    padding: 4px 8px;
    border-radius: 4px;
    font-weight: 500;
}

.gamsys-status.loading {
    color: var(--text-secondary);
}

.gamsys-status.found {
    color: var(--success);
    background: #E8F5E9;
}

.gamsys-status.notfound {
    color: var(--text-secondary);
}

/* ---- UTILITIES ---- */
[hidden] { display: none !important; }

.text-muted { color: var(--text-secondary); font-size: 0.85rem; }

/* ---- SAFE AREA ---- */
.app-main {
    padding-bottom: calc(env(safe-area-inset-bottom) + 16px);
}

/* ---- DESKTOP ---- */
@media (min-width: 480px) {
    .app-main {
        max-width: 480px;
        margin-left: auto;
        margin-right: auto;
    }
    .tab-nav {
        max-width: 480px;
        left: 50%;
        transform: translateX(-50%);
    }
    .modal-content {
        max-width: 480px;
        margin: 0 auto;
    }
}
