/* ============================================================
   Gestion de courses — feuille de style unique (desktop + mobile)
   ------------------------------------------------------------
   Une seule CSS pour les deux interfaces. Les règles spécifiques
   mobile/desktop sont isolées sous `body[data-mode="..."]`.
   ============================================================ */

/* --- Reset minimal & base ---------------------------------- */

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
                 Arial, "Noto Sans", sans-serif;
    font-size: 16px;
    line-height: 1.4;
    color: #1f2937;
    background: #f3f4f6;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

h1, h2 { margin: 0; }
button { font: inherit; cursor: pointer; }
input, select { font: inherit; }
ul { list-style: none; padding: 0; margin: 0; }

.visually-hidden {
    position: absolute !important;
    width: 1px; height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0); clip-path: inset(50%);
    white-space: nowrap; border: 0; padding: 0; margin: -1px;
}

/* L'attribut HTML `hidden` doit toujours masquer, même quand on force
   `display: flex/grid` sur l'élément via une autre règle. */
[hidden] { display: none !important; }

/* --- Top bar ------------------------------------------------- */

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    background: #1e40af;
    color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar h1 { font-size: 1.25rem; font-weight: 600; }
.mode-toggle { display: flex; gap: 4px; }
.mode-toggle a {
    color: rgba(255, 255, 255, 0.75);
    text-decoration: none;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: background 0.15s, color 0.15s;
}
.mode-toggle a:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
.mode-toggle a.active { background: rgba(255, 255, 255, 0.2); color: #fff; font-weight: 600; }

/* --- Boutons ------------------------------------------------- */

.btn {
    border: 1px solid transparent;
    border-radius: 6px;
    padding: 8px 14px;
    font-size: 0.95rem;
    font-weight: 500;
    transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
    background: #fff;
    color: #1f2937;
    border-color: #d1d5db;
    white-space: nowrap;
}
.btn:hover { background: #f9fafb; }
.btn:active { transform: scale(0.98); }
.btn:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: #2563eb; color: #fff; border-color: #2563eb; }
.btn-primary:hover { background: #1d4ed8; border-color: #1d4ed8; }
.btn-ghost { background: transparent; border-color: transparent; color: #4b5563; }
.btn-ghost:hover { background: #e5e7eb; }
.btn-danger { background: #dc2626; color: #fff; border-color: #dc2626; }
.btn-danger:hover { background: #b91c1c; border-color: #b91c1c; }
.btn-small { padding: 4px 10px; font-size: 0.85rem; }
.btn-icon { padding: 8px 10px; font-size: 1.1rem; }

/* --- Form inputs --------------------------------------------- */

input[type="text"], input[type="number"], select {
    border: 1px solid #d1d5db;
    border-radius: 6px;
    padding: 8px 10px;
    background: #fff;
    color: #1f2937;
    transition: border-color 0.15s, box-shadow 0.15s;
}
input[type="text"]:focus, input[type="number"]:focus, select:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}

/* --- Layout desktop ------------------------------------------ */

body[data-mode="desktop"] .layout {
    display: grid;
    grid-template-columns: minmax(260px, 320px) 1fr;
    gap: 16px;
    padding: 16px;
    max-width: 1200px;
    margin: 0 auto;
    align-items: start;
}
.panel {
    background: #fff;
    border-radius: 10px;
    padding: 16px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}
.panel h2 { font-size: 1.1rem; margin-bottom: 12px; color: #1f2937; }

/* --- Lists panel --------------------------------------------- */

.new-list-form {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
}
.new-list-form input { flex: 1; }

.lists { display: flex; flex-direction: column; gap: 4px; }
.list-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.1s;
    outline: none;
}
.list-item:hover, .list-item:focus-visible { background: #f3f4f6; }
.list-item.active { background: #dbeafe; color: #1e40af; font-weight: 600; }
.list-item .list-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-counter {
    font-size: 0.8rem;
    color: #6b7280;
    background: #e5e7eb;
    padding: 2px 8px;
    border-radius: 12px;
    flex-shrink: 0;
    margin-left: 8px;
}
.list-item.active .list-counter { background: #bfdbfe; color: #1e40af; }

/* --- Detail panel -------------------------------------------- */

.detail-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}
.detail-header h2 { font-size: 1.1rem; }
.detail-actions { display: flex; gap: 6px; }

.add-product-form {
    display: grid;
    grid-template-columns: 1fr 80px 120px auto;
    gap: 8px;
    margin-bottom: 16px;
    padding: 12px;
    background: #f9fafb;
    border-radius: 8px;
}

.products-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 0.9rem;
    color: #4b5563;
}
.products-meta .filters { display: flex; gap: 12px; align-items: center; }
.products-meta label { display: flex; align-items: center; gap: 4px; cursor: pointer; }

.products { display: flex; flex-direction: column; gap: 4px; }
.product-item {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    border-radius: 6px;
    background: #fff;
    border: 1px solid #e5e7eb;
    transition: background 0.1s, border-color 0.1s, opacity 0.15s;
}
.product-item:hover { border-color: #d1d5db; }
.product-item.bought { opacity: 0.5; background: #f9fafb; }
.product-item.bought .product-name-input { text-decoration: line-through; }

.product-checkbox { display: flex; align-items: center; }
.product-checkbox input { width: 20px; height: 20px; cursor: pointer; }

.product-info { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.product-name-input {
    border: 1px solid transparent;
    background: transparent;
    padding: 4px 6px;
    border-radius: 4px;
    width: 100%;
    font-weight: 500;
}
.product-name-input:hover { border-color: #e5e7eb; }
.product-name-input:focus { background: #fff; border-color: #2563eb; }
.product-meta { display: flex; gap: 6px; align-items: center; }
.product-qty-input { width: 70px; padding: 2px 6px; font-size: 0.85rem; }
.product-unit-input { width: 80px; padding: 2px 6px; font-size: 0.85rem; }

.delete-product-btn { font-size: 1rem; padding: 4px 8px; }

/* --- Empty / Loading ----------------------------------------- */

.empty, .empty-state {
    color: #9ca3af;
    text-align: center;
    padding: 20px;
    font-style: italic;
}
.empty-state { font-style: normal; font-size: 0.95rem; padding: 40px 20px; }

/* --- Toast notifications ------------------------------------- */

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: #1f2937;
    color: #fff;
    padding: 10px 16px;
    border-radius: 6px;
    font-size: 0.9rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 100;
    animation: toast-in 0.2s ease-out;
}
.toast.error { background: #dc2626; }
.toast.success { background: #16a34a; }
@keyframes toast-in {
    from { transform: translate(-50%, 10px); opacity: 0; }
    to { transform: translate(-50%, 0); opacity: 1; }
}

/* ============================================================
   MOBILE LAYOUT
   ============================================================ */

body[data-mode="mobile"] {
    background: #f9fafb;
}

.mobile-topbar {
    position: sticky;
    top: 0;
    padding: 10px 14px;
}
.mobile-topbar h1 { font-size: 1.1rem; }

.mobile-layout {
    padding: 12px;
    max-width: 600px;
    margin: 0 auto;
}

.mobile-selector {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    background: #fff;
    padding: 8px;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}
.mobile-selector select {
    flex: 1;
    font-size: 1rem;
    padding: 10px 12px;
    border-radius: 6px;
    border: 1px solid #d1d5db;
    background: #fff;
}

.mobile-summary {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 12px 4px 6px;
    font-size: 0.9rem;
    color: #4b5563;
}
progress#mobile-progress-bar {
    width: 100%;
    height: 8px;
    border: none;
    border-radius: 4px;
    overflow: hidden;
    appearance: none;
    background: #e5e7eb;
    margin-bottom: 12px;
}
progress#mobile-progress-bar::-webkit-progress-bar { background: #e5e7eb; }
progress#mobile-progress-bar::-webkit-progress-value { background: #16a34a; transition: width 0.2s; }
progress#mobile-progress-bar::-moz-progress-bar { background: #16a34a; }

.mobile-products { display: flex; flex-direction: column; gap: 8px; }

.mobile-product {
    display: flex;
    align-items: center;
    gap: 12px;
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 12px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
    transition: background 0.15s, opacity 0.15s;
    -webkit-user-select: none; user-select: none;
    cursor: pointer;
    min-height: 56px;
}
.mobile-product:active { background: #f3f4f6; }
.mobile-product.bought { opacity: 0.55; background: #f9fafb; }
.mobile-product.bought .mobile-product-name { text-decoration: line-through; }

.mobile-product-toggle {
    background: transparent;
    border: 2px solid #d1d5db;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.15s, border-color 0.15s;
}
.mobile-product-toggle .check-circle {
    width: 14px; height: 14px;
    border-radius: 50%;
    background: transparent;
    transition: background 0.15s;
}
.mobile-product.bought .mobile-product-toggle {
    background: #16a34a;
    border-color: #16a34a;
}
.mobile-product.bought .mobile-product-toggle .check-circle {
    background: #fff;
}

.mobile-product-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.mobile-product-name { font-weight: 500; font-size: 1rem; word-break: break-word; }
.mobile-product-meta { font-size: 0.8rem; color: #6b7280; }

/* --- Responsive ---------------------------------------------- */

@media (max-width: 720px) {
    /* Sur petit écran, on force le layout mobile (au cas où un user
       bookmarkerait `/` sur son téléphone). Le body data-mode est
       déjà mis par le serveur ; on laisse tomber la 2-col. */
    body[data-mode="desktop"] .layout {
        grid-template-columns: 1fr;
    }
    body[data-mode="desktop"] .add-product-form {
        grid-template-columns: 1fr 1fr;
    }
    body[data-mode="desktop"] .add-product-form button {
        grid-column: 1 / -1;
    }
}
