:root {
    --primary-color: #4a90e2;
    /* Nice blue */
    --secondary-color: #f5f6fa;
    /* Light background */
    --text-color: #2f3640;
    /* Dark text */
    --text-muted: #7f8fa6;
    --accent-color: #eebb4d;
    /* Yellow for 'shop' mode maybe? */
    --danger-color: #e74c3c;
    --success-color: #2ecc71;
    --border-radius: 12px;
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    --transition: all 0.3s ease;
    --font-family: 'Inter', sans-serif;

    /* UI Component Backgrounds */
    --card-bg: #ffffff;
    --input-bg: transparent;
    --hover-bg: #f8f9fa;
    --border-color: #eee;
}

@media (prefers-color-scheme: dark) {
    :root {
        --primary-color: #5da0f2;
        /* slightly brighter blue for contrast */
        --secondary-color: #121212;
        /* Very dark bg */
        --text-color: #e0e0e0;
        /* Light text */
        --text-muted: #888;
        --card-bg: #1e1e1e;
        /* Dark card surface */
        --hover-bg: #2a2a2a;
        --border-color: #333;
        --shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    }
}

body {
    font-family: var(--font-family);
    background-color: var(--secondary-color);
    color: var(--text-color);
    margin: 0;
    display: flex;
    justify-content: center;
    min-height: 100vh;
    overflow-x: hidden;
    overscroll-behavior: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.app-container {
    width: 100%;
    max-width: 500px;
    background: var(--card-bg);
    padding: 2rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    /* Full height on mobile */
}

@media (min-width: 600px) {
    .app-container {
        margin: 2rem;
        border-radius: var(--border-radius);
        min-height: auto;
        height: fit-content;
        max-height: 90vh;
        overflow-y: auto;
    }
}

header {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 2rem;
}

h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0;
    display: none;
    /* Just in case */
}

.header-right {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    width: 100%;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-right: auto;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 0.4rem;
    border-radius: 8px;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    background-color: #f0f0f0;
    color: var(--primary-color);
}

.hidden-input {
    display: none;
}

/* Mode Switch */
.mode-switch-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.mode-label {
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-muted);
    transition: var(--transition);
}

.mode-label.active {
    color: var(--primary-color);
    font-weight: 700;
}

.switch {
    position: relative;
    display: inline-block;
    width: 48px;
    height: 24px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
    border-radius: 24px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--primary-color);
}

input:checked+.slider:before {
    transform: translateX(24px);
}

/* Tabs */
.tabs-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
    border-bottom: 1px solid #eee;
    padding-bottom: 0.5rem;
}

.tabs-scroll {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    padding-bottom: 0.2rem;
    flex: 1;
}

.tab-item {
    padding: 0.4rem 0.8rem;
    border-radius: 20px;
    background: var(--hover-bg);
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: var(--transition);
    border: 1px solid transparent;
    touch-action: pan-y;
}

.tab-item:hover {
    background: color-mix(in srgb, var(--hover-bg) 80%, var(--primary-color) 20%);
}

.tab-item.active {
    background: var(--primary-color);
    color: white;
}

.dragging-tab {
    opacity: 0.5;
    background: #e9ecef;
}

.over-tab {
    border-left: 2px solid var(--primary-color);
    margin-left: -2px;
    /* compensate for border to avoid layout shift */
}

.add-tab-btn {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: none;
    background: var(--hover-bg);
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
}

.add-tab-btn:hover {
    background: #e0e0e0;
    color: var(--primary-color);
}

/* Inline Input Group */
.inline-input-group {
    display: flex;
    gap: 0.5rem;
    width: 100%;
    box-sizing: border-box;
    background: var(--hover-bg);
    border-radius: var(--border-radius);
    padding: 0.4rem;
    transition: var(--transition);
    border: 1px solid transparent;
}

.inline-input-group:focus-within {
    background: var(--card-bg);
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-color) 20%, transparent);
}

.inline-item-input {
    flex: 1;
    padding: 0.6rem 0.8rem;
    border: none;
    background: transparent;
    font-family: inherit;
    font-size: 1rem;
    outline: none;
    color: var(--text-color);
}

/* Grocery List */
.grocery-list {
    list-style: none;
    padding: 0;
    margin: 0;
    flex: 1;
}

.grocery-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* Space for delete btn */
    padding: 1rem;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border-color);
    transition: var(--transition);
    cursor: grab;
    flex-wrap: wrap;
    /* Allow wrapping on small screens if needed */
    gap: 1rem;
    touch-action: pan-y;
}

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

.grocery-item.dragging {
    opacity: 0.5;
    background: #f9f9f9;
}

.item-info {
    flex: 1;
    font-weight: 500;
}

/* Shop Mode Content */
.item-content-shop {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0.8rem;
}

.shop-details {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.item-text {
    cursor: pointer;
    user-select: none;
    transition: var(--transition);
    position: relative;
    display: inline-block;
}

.item-text::after {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    width: 0;
    height: 2px;
    background-color: var(--text-muted);
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.item-text:hover {
    color: var(--primary-color);
}

.grocery-item.completed .item-text {
    color: var(--text-muted);
}

.grocery-item.completed .item-text::after {
    width: 100%;
}

.grocery-item.completed .buy-badge {
    opacity: 0.5;
}

.item-checkbox {
    appearance: none;
    -webkit-appearance: none;
    width: 24px;
    height: 24px;
    border: 2px solid #cbd5e1;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background-color: white;
    flex-shrink: 0;
}

.item-checkbox:active {
    transform: scale(0.9);
}

.item-checkbox:checked {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
}

.item-checkbox::after {
    content: '';
    position: absolute;
    left: 7px;
    top: 3px;
    width: 5px;
    height: 10px;
    border: solid white;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg) scale(0);
    opacity: 0;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.item-checkbox:checked::after {
    transform: rotate(45deg) scale(1);
    opacity: 1;
}

/* Quantity Controls (Home Mode - Compact) */
.quantity-controls {
    display: flex;
    align-items: center;
}

.qty-compact-group {
    display: flex;
    align-items: center;
    background: var(--hover-bg);
    padding: 0.3rem 0.5rem;
    border-radius: 8px;
    gap: 0.3rem;
}

.qty-val {
    font-weight: 700;
    min-width: 1.2rem;
    text-align: center;
    font-size: 0.9rem;
}

.qty-separator {
    color: var(--text-muted);
    font-weight: 400;
    margin: 0 0.2rem;
    font-size: 0.9rem;
}

.qty-btn {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--primary-color);
    font-weight: bold;
    font-size: 1rem;
    transition: var(--transition);
    padding: 0;
}

.qty-btn:hover {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

/* Buy Badge (Shop Mode) */
.buy-badge {
    font-size: 0.8rem;
    padding: 0.2rem 0.6rem;
    border-radius: 12px;
    font-weight: 600;
    width: fit-content;
    transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.buy-badge.needed {
    background-color: color-mix(in srgb, var(--primary-color) 15%, transparent);
    color: var(--primary-color);
}

.buy-badge.stocked {
    background-color: #fab1a0;
    color: #c0392b;
    display: none;
    /* Hide if nothing to buy since we filter now */
}

/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(2px);
}

.modal-overlay.visible {
    display: flex;
    animation: fadeIn 0.3s;
}

.modal {
    background: var(--card-bg);
    padding: 2rem;
    border-radius: var(--border-radius);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    width: 90%;
    max-width: 400px;
    animation: slideUp 0.3s;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

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

#modal-title {
    margin-top: 0;
    margin-bottom: 1.5rem;
    text-align: center;
}

.form-group {
    margin-bottom: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-group.hidden {
    display: none;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.modal-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

#modal-input,
#delete-input,
.modal-select {
    width: 100%;
    padding: 0.8rem;
    border: 2px solid var(--border-color);
    background: var(--input-bg);
    color: var(--text-color);
    border-radius: 8px;
    font-size: 1rem;
    box-sizing: border-box;
    font-family: inherit;
    transition: border-color var(--transition);
}

#delete-input {
    margin-bottom: 1.5rem;
}

#modal-input:focus,
#delete-input:focus,
.modal-select:focus {
    border-color: var(--primary-color);
    outline: none;
}

#delete-input:focus {
    border-color: var(--danger-color);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
}

.modal-btn {
    padding: 0.6rem 1.2rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
}

.modal-btn.cancel {
    background: var(--hover-bg);
    color: var(--text-muted);
}

.modal-btn.cancel:hover {
    background: color-mix(in srgb, var(--hover-bg) 80%, black 20%);
}

.modal-btn.save {
    background: var(--primary-color);
    color: white;
}

.modal-btn.save:hover {
    background: #357abd;
}

.modal-btn.delete {
    background: var(--danger-color);
    color: white;
}

.modal-btn.delete:hover {
    background: #c0392b;
}

.modal-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: #ccc !important;
    color: #666 !important;
}

.text-danger {
    color: var(--danger-color);
    margin-top: 0;
}

/* --- Sections --- */

/* Prevent CSS animation lag on mobile drag-and-drop polyfill images */
.dnd-poly-drag-image,
.dnd-poly-drag-image *,
.dragging,
.dragging-section,
.dragging-tab {
    transition: none !important;
}

.section-container {
    background: var(--card-bg);
    border-radius: var(--border-radius);
    padding: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    border: 1px solid var(--border-color);
    transition: var(--transition);
    touch-action: pan-y;
}

.section-container.dragging-section {
    opacity: 0.6;
    background: var(--hover-bg);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.section-container.over-section {
    border: 2px dashed var(--primary-color);
    background: var(--card-bg);
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
    cursor: grab;
    user-select: none;
}

.section-header:active {
    cursor: grabbing;
}

.section-header.over {
    background-color: var(--card-bg);
    border: 2px dashed var(--primary-color);
    border-radius: var(--border-radius);
    padding: 0.5rem;
}

.section-title {
    font-size: 1.2rem;
    font-weight: 700;
    margin: 0;
    color: var(--text-color);
    cursor: pointer;
    transition: var(--transition);
}

.section-title:hover {
    color: var(--primary-color);
}

.section-items-list {
    list-style: none;
    padding: 0;
    margin: 0;
    min-height: 20px;
    /* target for empty lists */
}

/* Make inner grocery items flush inside the section box */
.section-container .grocery-item {
    padding: 0.8rem 0;
    border-radius: 0;
}

.section-container .grocery-item.dragging {
    background: var(--hover-bg);
    opacity: 0.8;
    border-radius: 8px;
    /* Give it shape when floating */
    padding: 0.8rem;
}

.section-container .add-item-row {
    padding-top: 1rem;
    padding-bottom: 0;
    border-bottom: none;
    cursor: default;
    /* not draggable */
}

/* Add a Section Row */
.add-section-row {
    list-style: none;
    padding: 1rem;
    background: transparent;
    border: none;
    border-radius: var(--border-radius);
    margin-top: 1rem;
    transition: var(--transition);
}

.add-section-row:hover,
.add-section-row:focus-within {
    border-color: var(--primary-color);
    background: var(--card-bg);
}

.add-section-input {
    flex: 1;
    padding: 0.8rem 1rem;
    border: none;
    background: transparent;
    font-family: inherit;
    font-size: 1.1rem;
    font-weight: 500;
    outline: none;
}