/* ── Design Tokens ─────────────────────────────────────────────────── */
:root {
    --orange: #FF6B35;
    --orange-light: #FF9E7A;
    --bg: #FFF8F0;
    --card: #FFFFFF;
    --text: #2D2D2D;
    --text-dim: #888;
    --border: #EDE0D4;
    --shadow: 0 2px 12px rgba(0,0,0,.06);
    --radius: 16px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --nav-h: 64px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
         --bg: #1A1A2E;
         --card: #16213E;
         --text: #F0F0F5;
         --text-dim: #B8B8D0;
         --border: #3A3A5C;
         --shadow: 0 2px 12px rgba(0,0,0,.3);
    }
}

:root[data-theme="dark"] {
    --bg: #1A1A2E;
    --card: #16213E;
    --text: #F0F0F5;
    --text-dim: #B8B8D0;
    --border: #3A3A5C;
    --shadow: 0 2px 12px rgba(0,0,0,.3);
}

:root[data-theme="light"] {
    --bg: #FFF8F0;
    --card: #FFFFFF;
    --text: #2D2D2D;
    --text-dim: #888;
    --border: #EDE0D4;
    --shadow: 0 2px 12px rgba(0,0,0,.06);
}

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

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

/* IMPORTANT: a CSS `display` rule overrides the `hidden` attribute,
   so pin it globally to keep modals/overlays hidden until shown. */
[hidden] { display: none !important; }


/* ── Top Bar ─────────────────────────────────────────────────────────── */
.topbar {
     position: sticky;
     top: 0;
     z-index: 100;
     display: flex;
     align-items: center;
     gap: 8px;
     padding: calc(var(--safe-top) + 10px) 16px 10px;
     background: var(--bg);
     backdrop-filter: blur(12px);
     -webkit-backdrop-filter: blur(12px);
}

.topbar__search {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0 12px;
    color: var(--text-dim);
}
.topbar__icon { flex-shrink: 0; }
.topbar__search input {
    flex: 1;
    border: none;
    background: transparent;
    padding: 12px 0;
    font-size: 16px;
    color: var(--text);
    outline: none;
}
.topbar__search input::placeholder { color: var(--text-dim); }

.topbar__btn,
.card__fav {
    display: grid;
    place-items: center;
    flex-shrink: 0;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    font-family: inherit;
    line-height: 1;
    -webkit-appearance: none;
    appearance: none;
    transition: background .15s, border-color .15s, transform .15s;
}
.topbar__btn:active,
.card__fav:active { transform: scale(.97); }

.topbar__btn {
    width: 42px;
    height: 42px;
    color: var(--orange);
}

#btn-theme .theme-icon[hidden] { display: none !important; }

.theme-menu { position: relative; flex-shrink: 0; }

.theme-menu__panel {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    min-width: 168px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 6px;
    z-index: 200;
}

.theme-menu__option {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 10px 12px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    font-size: 14px;
    text-align: left;
    cursor: pointer;
}

.theme-menu__option:hover,
.theme-menu__option.active {
    background: var(--bg);
    color: var(--orange);
}

.theme-menu__option.active { font-weight: 600; }

.language-select {
    max-width: 42px;
    padding: 10px 4px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--card);
    color: var(--text);
    font-size: 12px;
    cursor: pointer;
}

@media (min-width: 480px) {
    .language-select { max-width: 170px; padding: 10px 8px; }
}

/* ── Category Chips ──────────────────────────────────────────────────── */
.chips {
    display: flex;
    gap: 8px;
    padding: 4px 16px 10px;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }

.chip {
    flex-shrink: 0;
    padding: 7px 14px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--card);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s, border-color .15s, color .15s;
    -webkit-appearance: none;
    appearance: none;
}
.chip:not(.active):hover {
    border-color: var(--orange);
    color: var(--text);
}
.chip__count {
    color: var(--text-dim);
    font-weight: 500;
}
.chip.active {
    background: var(--orange);
    color: #fff;
    border-color: var(--orange);
}
.chip.active .chip__count {
    color: rgba(255, 255, 255, 0.85);
}

/* ── Recipe Grid ─────────────────────────────────────────────────────── */
.content {
    padding: 0 16px calc(var(--nav-h) + var(--safe-bottom) + 24px);
    min-height: calc(100dvh - 120px);
}

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 14px;
    align-items: stretch;
}

.card {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    background: var(--card);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: transform .15s;
    border: 1px solid var(--border);
    isolation: isolate;
}
.card:active { transform: scale(.97); }

.card__media {
    position: relative;
    width: 100%;
    aspect-ratio: 4/3;
    overflow: hidden;
    background: var(--orange-light);
}
.card__img-bg {
    position: absolute;
    inset: -24px;
    width: calc(100% + 48px);
    height: calc(100% + 48px);
    object-fit: cover;
    filter: blur(16px) saturate(1.1) brightness(0.75);
    transform: scale(1.08);
    pointer-events: none;
}
.card__media .card__img {
    position: relative;
    z-index: 1;
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: transparent;
}
.card__media--portrait .card__img {
    object-fit: contain;
}
.card__img {
    width: 100%;
    aspect-ratio: 4/3;
    object-fit: cover;
    background: var(--orange-light);
}
.detail__media {
    width: 100%;
    height: 220px;
    aspect-ratio: auto;
    border-radius: 16px;
    margin: 14px 0;
}
.card__placeholder {
    width: 100%;
    aspect-ratio: 4/3;
    display: grid;
    place-items: center;
    background: var(--orange);
    font-size: 48px;
}
.card__media > .card__placeholder {
    aspect-ratio: unset;
    height: 100%;
}
.card__body {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 10px 12px 12px;
    min-height: 96px;
}
.card__title {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    min-height: calc(1.3em * 2);
}
.card__meta {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    font-size: 11px;
    color: var(--text-dim);
}
.card__foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-top: auto;
    padding-top: 8px;
}
.card__foot .card__meta {
    margin-top: 0;
    flex: 1;
    min-width: 0;
}
.card__badge {
    font-size: 10px;
    padding: 2px 7px;
    border-radius: 6px;
    background: var(--orange);
    color: #fff;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .3px;
}
.card__badge.easy { background: #4ADE80; color: #052e16; }
.card__badge.medium { background: #FBBF24; color: #451a03; }
.card__badge.hard { background: #F87171; color: #450a0a; }

.card__fav {
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    padding: 0;
    font-size: 17px;
    color: var(--text);
}
.card__fav.is-favorite {
    background: var(--orange);
    border-color: var(--orange);
}
.card__fav:hover {
    border-color: var(--orange);
}

.empty {
     text-align: center;
    padding: 60px 20px;
    color: var(--text-dim);
    font-size: 15px;
}

/* ── Bottom Nav ──────────────────────────────────────────────────────── */
.bottombar {
     position: fixed;
     left: 0; right: 0; bottom: 0;
     display: flex;
    justify-content: space-around;
    background: var(--card);
    border-top: 1px solid var(--border);
     padding-bottom: var(--safe-bottom);
     z-index: 100;
}
.nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px 0;
    font-size: 10px;
    font-weight: 500;
    color: var(--text-dim);
    text-decoration: none;
    transition: color .15s;
}
.nav-item.active { color: var(--orange); }

/* ── Overlay / Detail ────────────────────────────────────────────────── */
.overlay {
     position: fixed;
     inset: 0;
     z-index: 200;
     background: var(--bg);
     overflow-y: auto;
    overscroll-behavior-y: contain;
}
.detail {
     max-width: 600px;
     margin: 0 auto;
     padding: calc(var(--safe-top) + 16px) 16px calc(var(--safe-bottom) + 32px);
}
.detail__back {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    color: var(--orange);
    background: none;
    border: none;
    padding: 8px 0;
    cursor: pointer;
}
.detail__title { font-size: 24px; font-weight: 700; margin: 8px 0 4px; line-height: 1.2; }
.detail__desc { font-size: 14px; color: var(--text-dim); line-height: 1.5; margin-bottom: 16px; }
.detail__stats {
     display: flex;
     gap: 12px;
    flex-wrap: wrap;
     padding: 12px 0;
     border-bottom: 1px solid var(--border);
    margin-bottom: 16px;
}
.stat {
     text-align: center;
     min-width: 70px;
}
.stat__val { font-size: 18px; font-weight: 700; color: var(--orange); }
.stat__label { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

.detail__section { margin-bottom: 20px; }
.detail__section h3 {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-dim);
    margin-bottom: 10px;
}
.ingredient {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 0;
    font-size: 14px;
    border-bottom: 1px solid var(--border);
}
.ingredient input { accent-color: var(--orange); }

.step {
    display: flex;
    gap: 12px;
    padding: 10px 0;
}
.step__num {
    flex-shrink: 0;
    width: 28px; height: 28px;
    border-radius: 50%;
    background: var(--orange);
    color: #fff;
    display: grid;
    place-items: center;
    font-size: 13px;
    font-weight: 700;
}
.step__text { font-size: 14px; line-height: 1.5; padding-top: 3px; }

.detail__tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 12px;
}
.tag {
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--orange);
    color: #fff;
    opacity: .6;
}

.detail__actions {
     display: flex;
     gap: 10px;
     margin-top: 24px;
}

/* ── Modal / Form ────────────────────────────────────────────────────── */
.modal {
     position: fixed;
     inset: 0;
    z-index: 300;
     background: rgba(0,0,0,.45);
     display: grid;
    place-items: end center;
}
.modal__card {
     background: var(--card);
    border-radius: 20px 20px 0 0;
     max-width: 600px;
    width: 100%;
     max-height: 90dvh;
     overflow-y: auto;
     padding: 20px 16px calc(var(--safe-bottom) + 16px);
     animation: slide-up .25s ease;
}
@keyframes slide-up {
     from { transform: translateY(40px); opacity: .8; }
     to   { transform: translateY(0);    opacity: 1;   }
}
.modal__header {
     display: flex;
     justify-content: space-between;
     align-items: center;
     margin-bottom: 16px;
}
.modal__close {
     background: none;
     border: none;
     font-size: 20px;
     color: var(--text-dim);
    cursor: pointer;
     padding: 4px;
}
.form { display: flex; flex-direction: column; gap: 14px; }
.form label { font-size: 13px; font-weight: 600; display: flex; flex-direction: column; gap: 6px; }
.form select[multiple] { min-height: 96px; }
.form input, .form textarea, .form select {
     padding: 11px 12px;
    border-radius: 10px;
     border: 1px solid var(--border);
     background: var(--bg);
     font-size: 14px;
     color: var(--text);
     outline: none;
}
.form input:focus, .form textarea:focus, .form select:focus {
    border-color: var(--orange);
}
.form-row {
     display: grid;
     grid-template-columns: 1fr 1fr;
     gap: 10px;
}
.form-row label:nth-child(3) { grid-column: 1 / -1; }
fieldset {
     border: 1px solid var(--border);
     border-radius: 12px;
     padding: 12px;
     margin: 0;
}
legend { font-size: 12px; font-weight: 600; padding: 0 6px; color: var(--text-dim); }

.btn-primary {
     padding: 12px;
     border: none;
     border-radius: 12px;
     background: var(--orange);
     color: #fff;
     font-size: 15px;
     font-weight: 600;
     cursor: pointer;
     flex: 1;
}
.btn-primary:active { transform: scale(.97); }
.btn-ghost {
     padding: 12px 20px;
     border: 1px solid var(--border);
     border-radius: 12px;
     background: none;
     color: var(--text-dim);
     font-size: 15px;
     cursor: pointer;
}

/* ── Toast ───────────────────────────────────────────────────────────── */
.toast {
     position: fixed;
     bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px);
     left: 50%; transform: translateX(-50%);
     background: var(--text);
     color: var(--bg);
     padding: 10px 20px;
     border-radius: 10px;
     font-size: 14px;
     font-weight: 500;
     z-index: 400;
     animation: fade .3s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ── Offline banner ──────────────────────────────────────────────────── */
.offline-banner {
     display: none;
     text-align: center;
     padding: 6px;
     background: #4A2800;
     color: #FBBF24;
     font-size: 12px;
    font-weight: 600;
}
body.offline .offline-banner { display: block; }
body.offline .bottombar .nav-item[data-page="add"] { opacity: .3; pointer-events: none; }

/* ── Pull-from-YouTube strip ───────────────────────────────────────────── */
.yt {
     display: flex;
     gap: 8px;
     padding: 10px;
     background: var(--bg);
     border: 1px dashed var(--orange);
     border-radius: 12px;
}
.yt input {
     flex: 1;
     min-width: 0;
     padding: 10px;
     border-radius: 10px;
     border: 1px solid var(--border);
     background: var(--card);
     color: var(--text);
     font-size: 13px;
     outline: none;
}
.yt button {
     flex-shrink: 0;
     padding: 0 14px;
     border: none;
     border-radius: 10px;
     background: var(--orange);
     color: #fff;
     font-weight: 600;
     font-size: 13px;
     cursor: pointer;
}
.yt button:disabled { opacity: .6; cursor: default; }
.yt-note {
     font-size: 12px;
     color: var(--text-dim);
     margin: -4px 0 8px;
}
.yt-note a { color: var(--orange); word-break: break-all; }
