/* ── Course21 — style.css ──
   Vanilla CSS, mobile-first, palette emerald (#10B981). */

* { box-sizing: border-box; }

:root {
    /* ── Thème clair (par défaut) ── */
    --bg:        #f7f7f5;
    --bg-card:   #ffffff;
    --bg-soft:   #f0efed;
    --border:    #e5e5e3;
    --text:      #111827;
    --text-soft: #6b7280;
    --text-mute: #9ca3af;
    --accent:    #10B981;
    --accent-d:  #059669;
    --danger:    #EF4444;
    --danger-d:  #DC2626;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.04);
    --shadow:    0 4px 16px rgba(0,0,0,.06);
    --radius:    10px;
    --radius-lg: 14px;
    --topbar-h:  56px;
    --content-max: 640px;

    /* iOS safe areas */
    --safe-top:    env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ── Thème sombre auto (suit le système si l'user n'a pas choisi) ── */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg:        #0f1115;
        --bg-card:   #181b21;
        --bg-soft:   #232732;
        --border:    #2a2f3a;
        --text:      #e7e9ee;
        --text-soft: #9ca3af;
        --text-mute: #6b7280;
        --accent:    #34D399;
        --accent-d:  #10B981;
        --danger:    #F87171;
        --danger-d:  #EF4444;
        --shadow-sm: 0 1px 2px rgba(0,0,0,.5);
        --shadow:    0 4px 18px rgba(0,0,0,.5);
    }
}

/* ── Override forcé par toggle utilisateur ── */
:root[data-theme="dark"] {
    --bg:        #0f1115;
    --bg-card:   #181b21;
    --bg-soft:   #232732;
    --border:    #2a2f3a;
    --text:      #e7e9ee;
    --text-soft: #9ca3af;
    --text-mute: #6b7280;
    --accent:    #34D399;
    --accent-d:  #10B981;
    --danger:    #F87171;
    --danger-d:  #EF4444;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.5);
    --shadow:    0 4px 18px rgba(0,0,0,.5);
}

html, body {
    margin: 0;
    padding: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
}

body { min-height: 100vh; }

button { font-family: inherit; cursor: pointer; }
input, button, textarea { font-family: inherit; font-size: inherit; }
input:focus, button:focus, textarea:focus { outline: none; }

.hidden { display: none !important; }

/* ── Loader ─────────────────────────────────────────────── */
.init-loader {
    position: fixed; inset: 0;
    display: flex; align-items: center; justify-content: center;
    background: var(--bg);
    z-index: 9999;
}
.init-spinner {
    width: 32px; height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toasts ─────────────────────────────────────────────── */
.toasts {
    position: fixed;
    top: calc(var(--safe-top) + 12px);
    left: 50%; transform: translateX(-50%);
    z-index: 10000;
    display: flex; flex-direction: column; gap: 8px;
    pointer-events: none;
}
.toast {
    background: var(--text);
    color: #fff;
    padding: 10px 16px;
    border-radius: 8px;
    font-size: 14px;
    box-shadow: var(--shadow);
    animation: toast-in .22s ease;
}
.toast.error { background: var(--danger); }
@keyframes toast-in {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ── Auth screen ────────────────────────────────────────── */
.auth-screen {
    min-height: 100vh;
    display: flex; align-items: center; justify-content: center;
    padding: 24px;
    padding-top:    calc(var(--safe-top) + 24px);
    padding-bottom: calc(var(--safe-bottom) + 24px);
    background:
        radial-gradient(circle at 30% 10%, rgba(16,185,129,.10), transparent 60%),
        var(--bg);
}
.auth-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    padding: 32px 28px;
    width: 100%; max-width: 380px;
}
.auth-logo {
    font-size: 26px; font-weight: 700; color: var(--text);
    margin-bottom: 4px;
}
.auth-logo span { color: var(--accent); }
.auth-subtitle {
    font-size: 15px; font-weight: 500; color: var(--text-soft);
    margin: 0 0 22px;
}
.auth-switch {
    text-align: center; font-size: 13px; color: var(--text-soft);
    margin: 14px 0 0;
}
.auth-switch a { color: var(--accent); text-decoration: none; font-weight: 600; }

/* ── Forms ──────────────────────────────────────────────── */
.form-row { margin-bottom: 14px; }
.form-row label {
    display: block; font-size: 12px; font-weight: 600;
    color: var(--text-soft); margin-bottom: 5px;
    text-transform: uppercase; letter-spacing: .05em;
}
.form-row input, .form-row textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-card);
    color: var(--text);
    transition: border-color .12s;
}
.form-row input:focus, .form-row textarea:focus {
    border-color: var(--accent);
}
.form-error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 6px 0 0; }

/* ── Buttons ────────────────────────────────────────────── */
.btn-primary {
    background: var(--accent); color: #fff;
    border: none; border-radius: 8px;
    padding: 10px 16px; font-weight: 600;
    transition: background .12s;
}
.btn-primary:hover { background: var(--accent-d); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-block { width: 100%; padding: 12px; margin-top: 4px; }

.btn-secondary {
    background: var(--bg-soft); color: var(--text);
    border: 1px solid var(--border); border-radius: 8px;
    padding: 10px 16px; font-weight: 500;
}
.btn-secondary:hover { background: var(--border); }

.btn-danger {
    background: var(--danger); color: #fff;
    border: none; border-radius: 8px;
    padding: 10px 16px; font-weight: 600;
}
.btn-danger:hover { background: var(--danger-d); }

.btn-icon {
    background: transparent; border: none;
    width: 40px; height: 40px;
    border-radius: 8px;
    color: var(--text-soft);
    font-size: 20px;
    display: flex; align-items: center; justify-content: center;
}
.btn-icon:hover { background: var(--bg-soft); color: var(--text); }

.btn-icon-sidebar {
    background: transparent; border: none;
    width: 24px; height: 24px;
    border-radius: 6px;
    color: var(--text-mute);
    font-size: 18px; line-height: 1;
}
.btn-icon-sidebar:hover { background: var(--bg-soft); color: var(--text); }

.btn-link {
    background: transparent; border: none;
    color: var(--accent); font-weight: 600; font-size: 13px;
    padding: 0;
}

.btn-logout {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 12px; font-weight: 500;
    color: var(--text-soft);
}
.btn-logout:hover { background: var(--bg-soft); color: var(--text); }

/* Bouton de bascule thème — affiche soleil ou lune selon le thème actif */
.btn-theme {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 6px;
    width: 28px; height: 28px;
    padding: 0;
    color: var(--text-soft);
    font-size: 14px;
    line-height: 1;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.btn-theme:hover { background: var(--bg-soft); color: var(--text); }
.btn-theme .theme-icon-sun  { display: none; }
.btn-theme .theme-icon-moon { display: inline; }
:root[data-theme="dark"] .btn-theme .theme-icon-sun  { display: inline; }
:root[data-theme="dark"] .btn-theme .theme-icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .btn-theme .theme-icon-sun  { display: inline; }
    :root:not([data-theme="light"]) .btn-theme .theme-icon-moon { display: none; }
}

/* ── App layout ─────────────────────────────────────────── */
.app {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 240px;
    background: var(--bg-card);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    padding: calc(var(--safe-top) + 16px) 12px calc(var(--safe-bottom) + 12px);
    flex-shrink: 0;
}
.sidebar-header {
    padding: 4px 8px 14px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 12px;
}
.sidebar-logo {
    font-size: 19px; font-weight: 700; color: var(--text);
}
.sidebar-logo span { color: var(--accent); }

.sidebar-section {
    display: flex; align-items: center; justify-content: space-between;
    padding: 6px 8px;
    margin-top: 4px;
}
.sidebar-section-title {
    font-size: 11px; font-weight: 600;
    color: var(--text-mute);
    text-transform: uppercase; letter-spacing: .08em;
}

.sidebar-nav {
    display: flex; flex-direction: column; gap: 2px;
    padding: 0 4px;
    flex: 1;
    overflow-y: auto;
}
.nav-item {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--text);
    font-size: 14px;
    transition: background .12s;
}
.nav-item:hover { background: var(--bg-soft); }
.nav-item.active { background: var(--bg-soft); font-weight: 600; }
.nav-item .dot {
    width: 8px; height: 8px; border-radius: 50%;
    flex-shrink: 0;
}
.nav-item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .count {
    font-size: 12px; color: var(--text-mute); font-variant-numeric: tabular-nums;
}

.sidebar-footer {
    margin-top: auto;
    border-top: 1px solid var(--border);
    padding-top: 12px;
    display: flex; align-items: center; gap: 8px;
}
.sidebar-user { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.avatar {
    width: 28px; height: 28px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff; font-weight: 600; font-size: 13px;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.user-name {
    font-size: 13px; color: var(--text); font-weight: 500;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.sidebar-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.3);
    z-index: 50;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
}
.sidebar-overlay.show {
    opacity: 1;
    pointer-events: auto;
}

/* ── Main ───────────────────────────────────────────────── */
.main {
    flex: 1;
    display: flex; flex-direction: column;
    min-width: 0;
}
.topbar {
    display: flex; align-items: center; gap: 8px;
    height: var(--topbar-h);
    padding: calc(var(--safe-top) + 8px) 16px 8px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0; z-index: 10;
}
.topbar #btn-menu { display: none; }
.list-title {
    flex: 1;
    margin: 0;
    font-size: 17px; font-weight: 600;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.add-row {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    padding: 10px 16px;
}
.add-row form {
    display: flex; gap: 8px; flex-wrap: wrap;
    max-width: var(--content-max);
    margin: 0 auto;
}
.add-row input {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
}
.add-row input:focus { border-color: var(--accent); }
#add-name { flex: 1 1 220px; min-width: 0; }
#add-qty  { flex: 0 0 80px;  }
#add-cat  { flex: 0 0 110px; }
.add-row .btn-primary { padding: 10px 16px; min-width: 44px; }

/* Quand une suggestion auto-rayon est dispo, on rend le placeholder du champ
   Rayon visiblement différent pour signaler l'utilisateur. */
#add-cat.has-suggestion::placeholder {
    color: var(--accent);
    opacity: 0.85;
    font-weight: 500;
}

/* ── Items ──────────────────────────────────────────────── */
.items-wrap {
    flex: 1;
    padding: 12px 16px calc(var(--safe-bottom) + 24px);
    overflow-y: auto;
}
.items-container {
    max-width: var(--content-max);
    margin: 0 auto;
}
.cat-group { margin-bottom: 14px; }
.cat-group:last-child { margin-bottom: 0; }
.cat-header {
    margin: 14px 4px 6px;
    font-size: 11px; font-weight: 600;
    color: var(--text-mute);
    text-transform: uppercase;
    letter-spacing: .08em;
}
.cat-group:first-child .cat-header { margin-top: 4px; }

.items {
    list-style: none; margin: 0; padding: 0;
    display: flex; flex-direction: column; gap: 4px;
}
.item {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 12px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    transition: background .12s, opacity .12s;
}
.item:hover { background: var(--bg-soft); }
.item .check {
    width: 22px; height: 22px;
    border: 2px solid var(--border);
    border-radius: 50%;
    flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    transition: all .12s;
    background: var(--bg-card);
}
.item .check:hover { border-color: var(--accent); }
.item .check.done {
    background: var(--accent);
    border-color: var(--accent);
}
.item .check.done::after {
    content: '✓';
    color: #fff; font-size: 13px; font-weight: 700;
}
.item .body {
    flex: 1; min-width: 0;
    display: flex; align-items: baseline; gap: 8px;
    cursor: text;
}
.item .name {
    font-size: 15px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    min-width: 0;
}
.item .qty {
    font-size: 12px;
    font-weight: 500;
    color: var(--text-soft);
    background: var(--bg-soft);
    padding: 2px 7px;
    border-radius: 6px;
    flex-shrink: 0;
    font-variant-numeric: tabular-nums;
}
.item .del {
    background: transparent; border: none;
    color: var(--text-mute);
    width: 28px; height: 28px;
    border-radius: 6px;
    font-size: 16px; line-height: 1;
    opacity: 0;
    transition: opacity .12s, background .12s, color .12s;
    flex-shrink: 0;
}
.item:hover .del,
.item:focus-within .del { opacity: 1; }
.item .del:hover { background: var(--bg-soft); color: var(--danger); }

/* sur tactile, le del reste visible (pas de hover fiable) */
@media (hover: none) {
    .item .del { opacity: .6; }
}

.items-done .item {
    opacity: .55;
}
.items-done .item .name {
    text-decoration: line-through;
    color: var(--text-soft);
}

.checked-block {
    margin-top: 16px;
}
.checked-block summary {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 4px;
    font-size: 13px; color: var(--text-soft);
    cursor: pointer;
    list-style: none;
}
.checked-block summary::-webkit-details-marker { display: none; }
.checked-block summary::before {
    content: '▸';
    font-size: 10px;
    transition: transform .15s;
}
.checked-block[open] summary::before { transform: rotate(90deg); }
.checked-block summary .btn-link { margin-left: auto; }
.checked-block ul { margin-top: 8px; }

.empty-state {
    color: var(--text-mute);
    text-align: center;
    margin: 32px 0;
    font-size: 14px;
}

/* ── Modale ─────────────────────────────────────────────── */
.modal {
    position: fixed; inset: 0;
    z-index: 100;
    display: flex; align-items: center; justify-content: center;
    padding: 16px;
}
.modal-backdrop {
    position: absolute; inset: 0;
    background: rgba(0,0,0,.4);
}
.modal-card {
    position: relative;
    background: var(--bg-card);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 100%; max-width: 400px;
    box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 14px; font-size: 18px; }
.modal-actions {
    display: flex; gap: 8px; justify-content: flex-end;
    margin-top: 16px;
}
.modal-actions .btn-danger { margin-right: auto; }

.color-picker {
    display: flex; gap: 8px; flex-wrap: wrap;
}
.color-swatch {
    width: 30px; height: 30px;
    border-radius: 50%;
    border: 2px solid transparent;
    transition: transform .12s, border-color .12s;
}
.color-swatch:hover { transform: scale(1.1); }
.color-swatch.selected {
    border-color: var(--text);
    transform: scale(1.1);
}

/* ── Mobile ─────────────────────────────────────────────── */
@media (max-width: 720px) {
    .topbar #btn-menu { display: flex; }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 100;
        width: 84vw; max-width: 280px;
        transform: translateX(-100%);
        transition: transform .22s ease;
        box-shadow: 4px 0 18px rgba(0,0,0,.08);
    }
    .sidebar.open { transform: translateX(0); }
}
