/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    /* Dark theme (default) */
    --bg: #1a1a2e;
    --bg-card: #16213e;
    --bg-card-hover: #1a2744;
    --bg-section: #0f3460;
    --text: #e8e8e8;
    --text-muted: #a0a0b0;
    --text-heading: #ffffff;
    --accent: #e94560;
    --accent-soft: rgba(233, 69, 96, 0.15);
    --green: #4ecca3;
    --green-soft: rgba(78, 204, 163, 0.15);
    --yellow: #f5c518;
    --border: rgba(255, 255, 255, 0.08);
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    --nav-bg: #0f0f23;
    --check-bg: #2a2a4a;
    --checked-text: #666680;
    --radius: 12px;
    --radius-sm: 8px;
}

/* Explicit light theme */
html[data-theme="light"] {
    --bg: #f5f5f7;
    --bg-card: #ffffff;
    --bg-card-hover: #f0f0f4;
    --bg-section: #e8e8f0;
    --text: #333344;
    --text-muted: #666680;
    --text-heading: #1a1a2e;
    --accent: #d63050;
    --accent-soft: rgba(214, 48, 80, 0.1);
    --green: #2d9e7a;
    --green-soft: rgba(45, 158, 122, 0.1);
    --border: rgba(0, 0, 0, 0.08);
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    --nav-bg: #ffffff;
    --check-bg: #f0f0f4;
    --checked-text: #aaaabc;
}

/* Auto light — OS preference, no explicit choice */
@media (prefers-color-scheme: light) {
    html:not([data-theme="dark"]):not([data-theme="light"]) {
        --bg: #f5f5f7;
        --bg-card: #ffffff;
        --bg-card-hover: #f0f0f4;
        --bg-section: #e8e8f0;
        --text: #333344;
        --text-muted: #666680;
        --text-heading: #1a1a2e;
        --accent: #d63050;
        --accent-soft: rgba(214, 48, 80, 0.1);
        --green: #2d9e7a;
        --green-soft: rgba(45, 158, 122, 0.1);
        --border: rgba(0, 0, 0, 0.08);
        --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        --nav-bg: #ffffff;
        --check-bg: #f0f0f4;
        --checked-text: #aaaabc;
    }
}

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

#app {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 600px;
    margin: 0 auto;
    padding-bottom: calc(60px + env(safe-area-inset-bottom, 0px));
    background: var(--bg);
}

/* ── Header ── */
#header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    padding-top: max(12px, env(safe-area-inset-top));
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

#header-title {
    text-align: center;
    flex: 1;
}

#header h1 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-heading);
    letter-spacing: -0.02em;
}

#week-label {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.header-btn {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 1.8rem;
    padding: 4px 12px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    line-height: 1;
}

.header-btn:active { opacity: 0.6; }
.header-btn:disabled { opacity: 0.2; cursor: default; }

/* ── Main ── */
#main {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
}

.view { display: none; padding: 16px; padding-bottom: 24px; }
.view.active { display: block; }

/* ── Cards ── */
.card-list { display: flex; flex-direction: column; gap: 12px; }

.meal-card, .recipe-card {
    background: var(--bg-card);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
}

.meal-card:active, .recipe-card:active {
    transform: scale(0.98);
    background: var(--bg-card-hover);
}

.card-day {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--accent);
    margin-bottom: 4px;
}

.card-meal-name {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-heading);
    margin-bottom: 4px;
}

.card-desc {
    font-size: 0.85rem;
    color: var(--text-muted);
    line-height: 1.4;
    margin-bottom: 8px;
}

.card-meta {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.badge {
    font-size: 0.72rem;
    font-weight: 600;
    padding: 3px 8px;
    border-radius: 20px;
    white-space: nowrap;
}

.badge-ip {
    background: var(--accent-soft);
    color: var(--accent);
}

.badge-sale {
    background: var(--green-soft);
    color: var(--green);
}

.badge-time {
    background: var(--bg-section);
    color: var(--text-muted);
}

/* ── Shopping List ── */
#shopping-progress {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    padding: 12px 16px;
    background: var(--bg-card);
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

#progress-bar {
    flex: 1;
    height: 6px;
    background: var(--check-bg);
    border-radius: 3px;
    overflow: hidden;
}

#progress-fill {
    height: 100%;
    background: var(--green);
    border-radius: 3px;
    transition: width 0.3s ease;
    width: 0%;
}

#progress-text {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.btn-small {
    background: var(--accent-soft);
    color: var(--accent);
    border: none;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}

.btn-small:active { opacity: 0.7; }

.shopping-section {
    margin-bottom: 16px;
}

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    cursor: pointer;
    user-select: none;
}

.section-header h3 {
    font-size: 0.85rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--accent);
}

.section-count {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.section-items {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.shop-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: opacity 0.2s;
    border: 1px solid var(--border);
}

.shop-item.checked {
    background: var(--bg-card);
}

.shop-item.checked .item-name {
    text-decoration: line-through;
    color: var(--checked-text);
}

.shop-checkbox {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
}

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

.shop-item.checked .shop-checkbox::after {
    content: '✓';
    color: white;
    font-size: 0.8rem;
    font-weight: 700;
}

.item-info { flex: 1; min-width: 0; }

.item-name {
    font-size: 0.95rem;
    color: var(--text);
    transition: color 0.2s;
}

.item-qty {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.item-sale {
    font-size: 0.7rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 20px;
    background: var(--green-soft);
    color: var(--green);
    white-space: nowrap;
}

/* ── Recipe Detail ── */
.btn-back {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    padding: 8px 0;
    margin-bottom: 8px;
}

.btn-back:active { opacity: 0.6; }

#recipe-content h2 {
    font-size: 1.3rem;
    font-weight: 800;
    color: var(--text-heading);
    margin-bottom: 4px;
}

.recipe-meta {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
}

.recipe-meta .badge { font-size: 0.8rem; }

.recipe-section-title {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--accent);
    margin: 20px 0 10px;
}

.ingredient-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.ingredient-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    cursor: pointer;
}

.ingredient-item.checked-ing .ing-name {
    text-decoration: line-through;
    color: var(--checked-text);
}

.ing-check {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    border: 2px solid var(--text-muted);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.ingredient-item.checked-ing .ing-check {
    background: var(--green);
    border-color: var(--green);
}

.ingredient-item.checked-ing .ing-check::after {
    content: '✓';
    color: white;
    font-size: 0.65rem;
    font-weight: 700;
}

.ing-name { font-size: 0.9rem; }
.ing-amount { font-size: 0.8rem; color: var(--text-muted); margin-left: auto; white-space: nowrap; }

.step-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 10px;
    counter-reset: steps;
}

.step-item {
    counter-increment: steps;
    display: flex;
    gap: 12px;
    padding: 12px;
    background: var(--bg-card);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

.step-item::before {
    content: counter(steps);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 700;
    font-size: 0.85rem;
    flex-shrink: 0;
}

.step-text {
    font-size: 0.9rem;
    line-height: 1.5;
    padding-top: 3px;
}

/* ── Cook Mode ── */
.btn-cook-mode {
    display: block;
    width: 100%;
    margin-top: 20px;
    padding: 16px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 1.1rem;
    font-weight: 700;
    cursor: pointer;
}

.btn-cook-mode:active { opacity: 0.8; }

.cook-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 100;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px;
    padding-top: max(20px, env(safe-area-inset-top));
    padding-bottom: 40px;
}

.cook-header {
    margin-bottom: 20px;
}

.cook-header h2 {
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--text-heading);
    margin-top: 8px;
}

.cook-section { margin-bottom: 24px; }

.cook-overlay .ingredient-list { gap: 8px; }
.cook-overlay .ingredient-item { padding: 12px 16px; }
.cook-overlay .ing-name { font-size: 1.05rem; }
.cook-overlay .ing-amount { font-size: 0.95rem; }

.cook-overlay .step-list { gap: 14px; }
.cook-overlay .step-item { padding: 16px; }
.cook-overlay .step-text { font-size: 1.1rem; line-height: 1.6; }
.cook-overlay .step-item::before { width: 32px; height: 32px; font-size: 1rem; }

/* ── Bottom Nav ── */
#bottom-nav {
    position: fixed;
    bottom: env(safe-area-inset-bottom, 0px);
    left: 0;
    right: 0;
    display: flex;
    background: var(--nav-bg);
    border-top: 1px solid var(--border);
    padding: 6px 0;
    z-index: 10;
}

.nav-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.15s;
}

.nav-btn.active { color: var(--accent); }
.nav-icon { font-size: 1.3rem; }
.nav-label { font-size: 0.7rem; font-weight: 600; }

/* ── Empty State ── */
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}

.empty-icon { font-size: 3rem; margin-bottom: 16px; }
.empty-state p { font-size: 1rem; margin-bottom: 4px; }
.empty-state .sub { font-size: 0.85rem; }

/* ── Utilities ── */
.hidden { display: none !important; }

/* ── Add Item Bar ── */
.add-item-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.add-item-bar input {
    flex: 1;
    padding: 10px 14px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.9rem;
    font-family: inherit;
    outline: none;
}

.add-item-bar input::placeholder { color: var(--text-muted); }
.add-item-bar input:focus { border-color: var(--accent); }

.add-item-bar button {
    padding: 10px 18px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
}

.add-item-bar button:active { opacity: 0.8; }

/* ── Swipe / Delete ── */
.shop-item-wrapper {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius-sm);
    margin-bottom: 2px;
}

.swipe-action {
    position: absolute;
    top: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    padding: 0 20px;
    font-weight: 700;
    font-size: 0.85rem;
    color: white;
}

.swipe-action-left {
    right: 0;
    background: var(--accent);
}

.swipe-action-right {
    left: 0;
    background: var(--green);
}

.shop-item {
    position: relative;
    z-index: 1;
    transition: transform 0.15s ease;
}

.btn-delete-item {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.1rem;
    padding: 4px 8px;
    cursor: pointer;
    flex-shrink: 0;
    line-height: 1;
    opacity: 0.6;
}

.btn-delete-item:hover { opacity: 1; color: var(--accent); }

/* ── Toast ── */
.toast {
    position: fixed;
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-card);
    color: var(--text);
    padding: 10px 20px;
    border-radius: 24px;
    font-size: 0.85rem;
    font-weight: 600;
    box-shadow: 0 4px 16px rgba(0,0,0,0.3);
    border: 1px solid var(--border);
    z-index: 200;
    display: flex;
    align-items: center;
    gap: 12px;
    animation: toastIn 0.25s ease;
}

.toast-undo {
    color: var(--accent);
    background: none;
    border: none;
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
    padding: 0;
}

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

/* Smooth scrollbar */
#main::-webkit-scrollbar { width: 4px; }
#main::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Settings Bottom Sheet ── */
.settings-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 50;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.settings-backdrop.visible {
    opacity: 1;
    pointer-events: auto;
}

.settings-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-width: 600px;
    margin: 0 auto;
    background: var(--bg-card);
    border-radius: 16px 16px 0 0;
    padding: 12px 24px;
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    z-index: 51;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.2);
}

.settings-sheet.visible {
    transform: translateY(0);
}

.settings-handle {
    width: 36px;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: 0 auto 16px;
}

.settings-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-heading);
    margin-bottom: 20px;
}

.settings-section {
    margin-bottom: 20px;
}

.settings-label {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    display: block;
    margin-bottom: 8px;
}

.theme-toggle {
    display: flex;
    background: var(--bg-section);
    border-radius: var(--radius-sm);
    padding: 3px;
    gap: 3px;
}

.theme-opt {
    flex: 1;
    padding: 8px 12px;
    border: none;
    background: transparent;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.15s;
}

.theme-opt.active {
    background: var(--accent);
    color: white;
}

.settings-user-info {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.settings-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.settings-name {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-heading);
}

.settings-email {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.btn-sign-out {
    width: 100%;
    padding: 10px;
    background: var(--accent-soft);
    color: var(--accent);
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
}

.btn-sign-out:active { opacity: 0.7; }

.settings-version {
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-muted);
    opacity: 0.5;
    padding-top: 8px;
}

/* ── Auth Gate ── */
.auth-gate {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 200;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 24px;
}

.auth-gate-icon {
    font-size: 4rem;
}

.auth-gate h1 {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--text-heading);
}

.auth-gate p {
    font-size: 0.9rem;
    color: var(--text-muted);
    text-align: center;
    padding: 0 40px;
}

.btn-google-sign-in {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 24px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text);
    cursor: pointer;
    box-shadow: var(--shadow);
}

.btn-google-sign-in:active { opacity: 0.8; }

.btn-google-sign-in svg {
    width: 20px;
    height: 20px;
}

.auth-error {
    color: var(--accent);
    font-size: 0.85rem;
    font-weight: 600;
    min-height: 1.2em;
}
