/*
 * Meals PWA styles.
 * Mobile first. Light and dark palettes are tokens on :root; components only
 * ever reference tokens so both themes stay in step.
 */

:root {
    color-scheme: light dark;

    --color-bg: #f4f1ea;
    --color-surface: #ffffff;
    --color-surface-raised: #fbfaf6;
    --color-border: #dcd6c8;
    --color-text: #1d1f1c;
    --color-text-muted: #6b6f68;
    --color-accent: #1f5f4a;
    --color-accent-text: #ffffff;
    --color-accent-soft: #e2efe9;
    --color-price: #1f5f4a;
    --color-price-high: #a3462b;
    --color-chip: #ece8dd;
    --color-highlight: #fff3c4;
    --color-backdrop: rgba(29, 31, 28, 0.45);

    --radius-card: 14px;
    --radius-pill: 999px;
    --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 14px rgba(0, 0, 0, 0.05);
    --shadow-sheet: 0 -8px 30px rgba(0, 0, 0, 0.18);

    --font-stack: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;

    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---- Reset and base ---------------------------------------------------- */

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    max-width: 100vw;
    overflow-x: hidden;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font-stack);
    font-size: 16px;
    line-height: 1.4;
    -webkit-font-smoothing: antialiased;
}

button,
input,
select {
    font: inherit;
    color: inherit;
}

/* iOS Safari auto-zooms the page when a focused control's text is smaller
   than 16px. Keep every text control at 16px so the layout never jumps. */
input,
select {
    font-size: 16px;
}

button {
    cursor: pointer;
}

svg[hidden] {
    display: none;
}

.icon {
    width: 1.25em;
    height: 1.25em;
    flex: 0 0 auto;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

[hidden] {
    display: none !important;
}

/* ---- Header ------------------------------------------------------------ */

.app-header {
    position: sticky;
    top: 0;
    z-index: 5;
    padding: calc(var(--space-3) + var(--safe-top)) var(--space-4) var(--space-2);
    background: var(--color-accent);
    color: var(--color-accent-text);
}

.app-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
}

.app-title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin: 0;
    font-size: 1.35rem;
    font-weight: 700;
    letter-spacing: 0.01em;
}

.button-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.14);
    color: inherit;
}

.button-icon .icon {
    width: 22px;
    height: 22px;
}

.button-icon:active {
    background: rgba(255, 255, 255, 0.28);
}

.search-field {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-top: var(--space-3);
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    background: rgba(255, 255, 255, 0.16);
    color: inherit;
}

.search-field input {
    flex: 1 1 auto;
    min-width: 0;
    padding: var(--space-2) 0;
    border: 0;
    background: transparent;
    color: inherit;
    outline: none;
}

.search-field input::placeholder {
    color: rgba(255, 255, 255, 0.75);
}

.search-field input::-webkit-search-cancel-button {
    filter: invert(1);
}

.meal-tabs {
    display: flex;
    gap: var(--space-1);
    margin: var(--space-3) calc(var(--space-4) * -1) 0;
    padding: 0 var(--space-4);
    overflow-x: auto;
    scrollbar-width: none;
}

.meal-tabs::-webkit-scrollbar {
    display: none;
}

.meal-tab {
    flex: 0 0 auto;
    padding: var(--space-1) var(--space-3);
    border: 0;
    border-radius: var(--radius-pill);
    background: transparent;
    color: rgba(255, 255, 255, 0.85);
    font-size: 0.95rem;
    font-weight: 500;
}

.meal-tab.is-active {
    background: var(--color-surface);
    color: var(--color-accent);
    font-weight: 600;
}

/* ---- List -------------------------------------------------------------- */

.app-main {
    max-width: 44rem;
    margin: 0 auto;
    padding: var(--space-3) var(--space-4) calc(var(--space-6) + 64px + var(--safe-bottom));
}

.filter-row {
    display: flex;
    gap: var(--space-2);
    margin: 0 calc(var(--space-4) * -1) var(--space-3);
    padding: 0 var(--space-4);
    overflow-x: auto;
    scrollbar-width: none;
}

.filter-row::-webkit-scrollbar {
    display: none;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    flex: 0 0 auto;
    height: 34px;
    padding: 0 var(--space-3);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-pill);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: 0.85rem;
    font-weight: 500;
    white-space: nowrap;
}

.chip-toggle.is-active {
    border-color: var(--color-accent);
    background: var(--color-accent-soft);
    color: var(--color-accent);
}

.chip-toggle.is-active .icon {
    fill: currentColor;
}

.chip-select {
    padding-right: var(--space-2);
}

.chip-select select {
    max-width: 11rem;
    padding: 0;
    border: 0;
    background: transparent;
    color: inherit;
    font-size: 16px;
    font-weight: inherit;
    outline: none;
}

.stats-strip {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--space-2);
    margin-bottom: var(--space-3);
}

.stat {
    min-width: 0;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    background: var(--color-surface);
}

.stat-value {
    display: block;
    overflow: hidden;
    color: var(--color-text);
    font-size: 1rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stat-label {
    display: block;
    color: var(--color-text-muted);
    font-size: 0.7rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.meal-list {
    display: grid;
    gap: var(--space-2);
    margin: 0;
    padding: 0;
    list-style: none;
}

.meal-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    width: 100%;
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
    background: var(--color-surface);
    box-shadow: var(--shadow-card);
    text-align: left;
    transition: background 0.2s ease, transform 0.1s ease;
}

.meal-card:active {
    transform: scale(0.99);
}

.meal-card.is-highlighted {
    background: var(--color-highlight);
    border-color: var(--color-accent);
}

.meal-card-body {
    min-width: 0;
    flex: 1 1 auto;
}

.meal-card-name {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 600;
    line-height: 1.25;
}

.meal-card-tags {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
    margin-top: var(--space-1);
}

.tag {
    display: inline-block;
    padding: 1px var(--space-2);
    border-radius: var(--radius-pill);
    background: var(--color-chip);
    color: var(--color-text-muted);
    font-size: 0.75rem;
    font-weight: 500;
    letter-spacing: 0.02em;
}

.tag-planned {
    background: var(--color-accent-soft);
    color: var(--color-accent);
}

.meal-card-heart {
    width: 0.9em;
    height: 0.9em;
    margin-left: var(--space-1);
    vertical-align: -0.05em;
    fill: var(--color-price-high);
    stroke: var(--color-price-high);
}

.meal-card-unpriced {
    display: inline-block;
    padding: 2px var(--space-2);
    border-radius: var(--radius-pill);
    background: var(--color-chip);
    color: var(--color-text-muted);
    font-size: 0.75rem;
}

.meal-card-price {
    flex: 0 0 auto;
    text-align: right;
}

.meal-card-each {
    display: block;
    color: var(--color-price);
    font-size: 1.3rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
}

.meal-card-each.is-high {
    color: var(--color-price-high);
}

.meal-card-family {
    display: block;
    color: var(--color-text-muted);
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
}

.empty-state {
    margin: var(--space-6) 0;
    color: var(--color-text-muted);
    text-align: center;
}

/* ---- Footer ------------------------------------------------------------ */

.app-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-3);
    padding: 0 var(--space-4) calc(72px + var(--safe-bottom));
    color: var(--color-text-muted);
    font-size: 0.8rem;
    text-align: center;
}

.button-link .icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
}

.button-link {
    padding: 0;
    border: 0;
    background: none;
    color: var(--color-accent);
    font-weight: 600;
    text-decoration: underline;
}

/* ---- Detail sheet ------------------------------------------------------ */

.sheet-backdrop {
    position: fixed;
    inset: 0;
    z-index: 10;
    background: var(--color-backdrop);
}

.detail-sheet {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 11;
    max-height: 88vh;
    margin: 0 auto;
    max-width: 44rem;
    padding: var(--space-2) var(--space-4) calc(var(--space-5) + var(--safe-bottom));
    overflow-y: auto;
    border-radius: 20px 20px 0 0;
    background: var(--color-surface);
    box-shadow: var(--shadow-sheet);
    animation: sheet-rise 0.22s ease-out;
}

@keyframes sheet-rise {
    from {
        transform: translateY(40px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.detail-handle {
    width: 40px;
    height: 4px;
    margin: 0 auto var(--space-3);
    border-radius: 2px;
    background: var(--color-border);
}

/* Give the handle a generous invisible touch target for the drag. */
.detail-handle::before {
    content: "";
    display: block;
    height: var(--space-4);
    margin-top: calc(var(--space-4) * -1);
}

.detail-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
}

.detail-header .button-icon {
    background: var(--color-chip);
    color: var(--color-text);
}

.detail-title {
    margin: 0;
    font-size: 1.4rem;
    font-weight: 700;
    line-height: 1.2;
}

.detail-meta {
    margin: var(--space-1) 0 0;
    color: var(--color-text-muted);
    font-size: 0.9rem;
}

.detail-costs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-2);
    margin: var(--space-4) 0;
}

.detail-cost {
    padding: var(--space-3);
    border-radius: 12px;
    background: var(--color-accent-soft);
    text-align: center;
}

.detail-cost-value {
    display: block;
    color: var(--color-price);
    font-size: 1.6rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
}

.detail-cost-label {
    display: block;
    margin-top: 2px;
    color: var(--color-text-muted);
    font-size: 0.8rem;
}

.ingredient-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

.ingredient-table th,
.ingredient-table td {
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--color-border);
    text-align: left;
    vertical-align: top;
}

.ingredient-table th {
    color: var(--color-text-muted);
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.ingredient-table .numeric {
    text-align: right;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.ingredient-table tfoot td {
    border-bottom: 0;
    font-weight: 600;
}

.detail-note {
    margin: var(--space-4) 0 0;
    color: var(--color-text-muted);
    font-size: 0.8rem;
}

/* ---- Buttons ----------------------------------------------------------- */

.button-primary,
.button-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    min-height: 40px;
    padding: 0 var(--space-3);
    border-radius: 10px;
    font-size: 0.9rem;
    font-weight: 600;
    text-decoration: none;
}

.button-primary {
    border: 1px solid var(--color-accent);
    background: var(--color-accent);
    color: var(--color-accent-text);
}

.button-primary.is-active {
    background: var(--color-accent-soft);
    color: var(--color-accent);
}

.button-secondary {
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text);
}

.button-secondary.is-active {
    border-color: var(--color-price-high);
    color: var(--color-price-high);
}

.button-secondary.is-active .icon {
    fill: currentColor;
}

.detail-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-bottom: var(--space-4);
}

.detail-actions .button-primary {
    flex: 1 1 100%;
}

.detail-actions .button-secondary {
    flex: 1 1 auto;
}

.detail-unpriced {
    margin: 0 0 var(--space-3);
    padding: var(--space-2) var(--space-3);
    border-radius: 10px;
    background: var(--color-highlight);
    font-size: 0.85rem;
}

.ingredient-total td {
    border-bottom: 0;
    font-weight: 600;
}

/* ---- Plan view --------------------------------------------------------- */

.section-title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin: 0;
    font-size: 1.2rem;
    font-weight: 700;
}

.section-hint {
    margin: var(--space-1) 0 var(--space-3);
    color: var(--color-text-muted);
    font-size: 0.85rem;
}

.plan-summary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-2);
    margin-bottom: var(--space-3);
}

.plan-total {
    padding: var(--space-3);
    border-radius: 12px;
    background: var(--color-accent-soft);
    text-align: center;
}

.plan-total-value {
    display: block;
    color: var(--color-price);
    font-size: 1.5rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.plan-total-label {
    display: block;
    color: var(--color-text-muted);
    font-size: 0.75rem;
}

.plan-list {
    display: grid;
    gap: var(--space-2);
    margin: 0 0 var(--space-5);
    padding: 0;
    list-style: none;
}

.plan-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
    background: var(--color-surface);
}

.plan-item-body {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    align-items: flex-start;
    min-width: 0;
    padding: 0;
    border: 0;
    background: none;
    text-align: left;
}

.plan-item-name {
    font-weight: 600;
}

.plan-item-cost {
    color: var(--color-text-muted);
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
}

.stepper {
    display: inline-flex;
    align-items: center;
    flex: 0 0 auto;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-pill);
    background: var(--color-surface-raised);
}

.stepper-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    padding: 0;
    border: 0;
    background: none;
    color: var(--color-text);
}

.stepper-count {
    min-width: 1.5em;
    font-weight: 700;
    text-align: center;
}

.shopping-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
}

.shopping-actions {
    display: flex;
    gap: var(--space-1);
}

.shopping-actions .button-secondary {
    min-height: 34px;
    padding: 0 var(--space-2);
    font-size: 0.8rem;
}

.shopping-list {
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-card);
    background: var(--color-surface);
    list-style: none;
}

.shopping-item + .shopping-item {
    border-top: 1px solid var(--color-border);
}

.shopping-label {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    cursor: pointer;
}

.shopping-label input {
    width: 20px;
    height: 20px;
    margin: 0;
    accent-color: var(--color-accent);
}

.shopping-text {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.shopping-qty,
.shopping-cost {
    color: var(--color-text-muted);
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.shopping-item.is-checked .shopping-text {
    color: var(--color-text-muted);
    text-decoration: line-through;
}

/* ---- Bottom nav -------------------------------------------------------- */

.bottom-nav {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 6;
    display: flex;
    justify-content: center;
    gap: var(--space-6);
    padding: var(--space-1) var(--space-4) calc(var(--space-1) + var(--safe-bottom));
    border-top: 1px solid var(--color-border);
    background: var(--color-surface);
}

.nav-button {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    min-width: 72px;
    padding: var(--space-1) var(--space-2);
    border: 0;
    border-radius: 10px;
    background: none;
    color: var(--color-text-muted);
    font-size: 0.7rem;
    font-weight: 600;
}

.nav-button .icon {
    width: 22px;
    height: 22px;
}

.nav-button.is-active {
    color: var(--color-accent);
}

.nav-badge {
    position: absolute;
    top: 0;
    right: 8px;
    min-width: 18px;
    padding: 1px 5px;
    border-radius: var(--radius-pill);
    background: var(--color-accent);
    color: var(--color-accent-text);
    font-size: 0.65rem;
    text-align: center;
}

/* The meal-type tabs and search belong to the list view only. */
body.is-plan-view .meal-tabs,
body.is-plan-view .search-field {
    display: none;
}

/* ---- Account / sign-in sheet ------------------------------------------- */

.app-header-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

/* The account sheet can open on top of a meal sheet, so it stacks above it. */
#account-backdrop {
    z-index: 13;
}

.account-sheet {
    z-index: 14;
}

.button-icon.is-signed-in {
    background: var(--color-accent-text);
    color: var(--color-accent);
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin-top: var(--space-3);
}

.form-field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.form-label {
    color: var(--color-text-muted);
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.form-field input {
    min-height: 44px;
    padding: 0 var(--space-3);
    border: 1px solid var(--color-border);
    border-radius: 10px;
    background: var(--color-surface-raised);
    color: var(--color-text);
    font-size: 16px;
}

.form-field input:focus {
    outline: 2px solid var(--color-price);
    outline-offset: 1px;
}

.form-error {
    margin: 0;
    color: var(--color-price-high);
    font-size: 0.85rem;
}

.login-form .button-primary {
    min-height: 44px;
}

.login-form .button-primary[disabled] {
    opacity: 0.6;
}

.account-signed-in {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin-top: var(--space-3);
}

.account-details {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-2);
    margin: 0;
}

.account-detail {
    padding: var(--space-3);
    border-radius: 12px;
    background: var(--color-surface-raised);
}

.account-detail dt {
    color: var(--color-text-muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.account-detail dd {
    margin: var(--space-1) 0 0;
    font-weight: 600;
    overflow-wrap: anywhere;
}

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

.toast {
    position: fixed;
    bottom: calc(72px + var(--safe-bottom));
    left: 50%;
    z-index: 15;
    max-width: calc(100vw - 2rem);
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-pill);
    background: var(--color-text);
    color: var(--color-bg);
    font-size: 0.9rem;
    box-shadow: var(--shadow-card);
    transform: translateX(-50%);
}

/* ---- Dark theme -------------------------------------------------------- */

@media (prefers-color-scheme: dark) {
    :root {
        --color-bg: #14211c;
        --color-surface: #1d2b25;
        --color-surface-raised: #24352e;
        --color-border: #2f4239;
        --color-text: #ecefe9;
        --color-text-muted: #9aa69f;
        --color-accent: #0f3d2e;
        --color-accent-text: #ecefe9;
        --color-accent-soft: #24352e;
        --color-price: #8fd3b5;
        --color-price-high: #f0a07f;
        --color-chip: #2a3a33;
        --color-highlight: #3a3a1c;
        --color-backdrop: rgba(0, 0, 0, 0.6);
        --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.3);
    }

    .meal-tab.is-active {
        background: var(--color-surface-raised);
        color: var(--color-text);
    }
}

/* ---- Wider screens ----------------------------------------------------- */

@media (min-width: 40rem) {
    .app-header {
        padding-left: max(var(--space-4), calc((100vw - 44rem) / 2));
        padding-right: max(var(--space-4), calc((100vw - 44rem) / 2));
    }

    .meal-list {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .detail-sheet {
        bottom: auto;
        top: 50%;
        transform: translateY(-50%);
        max-height: 80vh;
        border-radius: 20px;
        animation: none;
    }
}
