/* Súper — estilos mobile-first. Tema claro/oscuro según el sistema. */

:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-2: #eef1f4;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --primary: #0f766e;
    --primary-contrast: #ffffff;
    --primary-soft: #ccfbf1;
    --pos: #15803d;
    --neg: #dc2626;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 2px 8px rgba(15, 23, 42, .05);
    --tabbar-h: 64px;
    --safe-b: env(safe-area-inset-bottom, 0px);
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1120;
        --surface: #131c2e;
        --surface-2: #1c2740;
        --text: #e2e8f0;
        --muted: #94a3b8;
        --border: #24324d;
        --primary: #14b8a6;
        --primary-contrast: #042f2e;
        --primary-soft: #134e4a;
        --pos: #4ade80;
        --neg: #f87171;
        --shadow: none;
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--neg); font-size: .9rem; margin: 0; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.spacer { flex: 1; }

/* ---------- Controles ---------- */

input, select, textarea {
    font: inherit;
    color: inherit;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: .7rem .8rem;
    width: 100%;
    min-height: 46px;
    outline: none;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 25%, transparent);
}
input[type="file"] { padding: .5rem; }
input[type="color"] { padding: 2px; width: 46px; flex: none; }
textarea { resize: vertical; }

.btn {
    font: inherit;
    font-weight: 600;
    border: 0;
    border-radius: 10px;
    padding: .7rem 1rem;
    min-height: 44px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .4rem;
    text-decoration: none;
    color: var(--text);
    background: var(--surface-2);
    transition: transform .08s, opacity .15s;
    white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--primary); color: var(--primary-contrast); }
.btn-ghost { background: transparent; border: 1px solid var(--border); }
.btn-chip { min-height: 36px; padding: .4rem .8rem; border-radius: 999px; font-size: .9rem; background: var(--surface); border: 1px solid var(--border); }
.btn-chip.on { background: var(--primary); color: var(--primary-contrast); border-color: var(--primary); }
.btn-danger-ghost { color: var(--neg); background: transparent; border: 1px solid color-mix(in srgb, var(--neg) 40%, transparent); }
.btn-block { width: 100%; }

.icon-btn {
    border: 0;
    background: transparent;
    color: var(--muted);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: inline-grid;
    place-items: center;
    cursor: pointer;
    font-size: 1rem;
    flex: none;
}
.icon-btn:active { background: var(--surface-2); }

.field { display: grid; gap: .3rem; margin: 0 0 .9rem; border: 0; padding: 0; min-width: 0; }
.field > span, .field > legend { font-size: .85rem; font-weight: 600; color: var(--muted); padding: 0; margin-bottom: .3rem; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: .7rem; }
.input-money { font-size: 1.4rem; font-weight: 700; }

/* ---------- Avatares ---------- */

.avatar {
    --c: #888;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--c);
    color: #fff;
    display: inline-grid;
    place-items: center;
    font-weight: 700;
    font-size: .85rem;
    flex: none;
}
.avatar.sm { width: 28px; height: 28px; font-size: .72rem; }
.avatar.xs { width: 18px; height: 18px; font-size: .55rem; vertical-align: -4px; }

/* ---------- Login / carga ---------- */

.login, .loading {
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: 1.5rem;
}
.loading { align-content: center; gap: 1rem; }
.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    padding: 2rem 1.5rem;
    border-radius: 20px;
    box-shadow: var(--shadow);
    display: grid;
    gap: .2rem;
    text-align: center;
}
.login-card .field { text-align: left; }
.login-card h1 { font-size: 1.8rem; }
.login-card > p { margin: 0 0 1rem; }
.login-card .error { margin-bottom: .7rem; }
.login-card .small { margin: 1rem 0 0; }
.login-logo { font-size: 3rem; }

.spinner {
    width: 36px;
    height: 36px;
    border: 4px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Barra superior ---------- */

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: calc(env(safe-area-inset-top, 0px) + .5rem) .5rem .5rem 1rem;
    border-bottom: 1px solid var(--border);
}
.topbar-row { display: flex; align-items: center; gap: .3rem; }
.brand { font-size: 1.25rem; display: flex; align-items: center; gap: .5rem; }

.sync { width: 9px; height: 9px; border-radius: 50%; background: var(--pos); display: inline-block; }
.sync-syncing { background: #f59e0b; animation: pulse 1s infinite; }
.sync-error { background: var(--neg); }
@keyframes pulse { 50% { opacity: .3; } }

.current-member {
    --c: #888;
    display: flex;
    align-items: center;
    gap: .4rem;
    padding: .25rem .7rem .25rem .25rem;
    border-radius: 999px;
    font-size: .9rem;
    font-weight: 600;
    border: 2px solid var(--c);
    background: color-mix(in srgb, var(--c) 12%, var(--surface));
    max-width: 50vw;
}
.current-member span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Contenido ---------- */

main {
    max-width: 720px;
    margin: 0 auto;
    padding: 1rem 1rem calc(var(--tabbar-h) + var(--safe-b) + 1.5rem);
}
body.has-shop-bar main { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 6rem); }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: 1rem; }
.section-head h2 { font-size: 1.15rem; }
.block-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 1.4rem 0 .5rem; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }

.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }
.empty p { margin: .3rem 0; }
.empty-icon { font-size: 2.6rem; margin-bottom: .5rem; }
.small-empty { padding: 1.2rem; }

.hint {
    background: var(--primary-soft);
    color: var(--text);
    border-radius: 10px;
    padding: .6rem .8rem;
    font-size: .9rem;
    margin-bottom: .8rem;
}

/* ---------- Lista ---------- */

.add-form {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: .75rem;
    display: grid;
    gap: .5rem;
}
.add-row { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.add-row .btn { grid-column: 1 / -1; }

.list-toolbar { display: flex; align-items: center; justify-content: space-between; margin: 1rem 0 .6rem; min-height: 36px; }

.cat-group { margin-bottom: 1rem; }
.cat-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--primary); margin: 0 0 .4rem .2rem; }
.items { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.item {
    display: flex;
    align-items: center;
    gap: .7rem;
    padding: .55rem .4rem .55rem .9rem;
    min-height: 58px;
    border-bottom: 1px solid var(--border);
    transition: background .15s;
}
.item:last-child { border-bottom: 0; }
.item-main { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: baseline; column-gap: .5rem; }
.item-name { font-weight: 600; overflow-wrap: anywhere; }
.item-qty { font-size: .85rem; color: var(--primary); font-weight: 600; }
.item-meta { flex-basis: 100%; font-size: .75rem; color: var(--muted); margin-top: .15rem; }

.item[data-action="edit-item"] { cursor: pointer; }
.item[data-action="edit-item"]:active { background: var(--surface-2); }
.shopping .item { cursor: pointer; user-select: none; padding-right: .9rem; }
.check {
    width: 26px;
    height: 26px;
    border-radius: 8px;
    border: 2px solid var(--border);
    display: grid;
    place-items: center;
    font-weight: 800;
    color: var(--primary-contrast);
    flex: none;
    transition: background .15s, border-color .15s;
}
.item.selected { background: color-mix(in srgb, var(--primary) 10%, var(--surface)); }
.item.selected .check { background: var(--primary); border-color: var(--primary); }
.item.selected .item-name { text-decoration: line-through; text-decoration-color: var(--muted); }

/* Barra del modo compra, sobre la de pestañas */
.shop-bar {
    position: fixed;
    left: .75rem;
    right: .75rem;
    bottom: calc(var(--tabbar-h) + var(--safe-b) + .6rem);
    z-index: 25;
    max-width: 696px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .5rem .5rem .5rem 1rem;
    background: var(--text);
    color: var(--bg);
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
}
.shop-bar span { flex: 1; min-width: 0; font-weight: 600; font-size: .9rem; line-height: 1.15; }
.shop-bar .btn { padding: .6rem .8rem; }
.shop-bar .btn-ghost { color: var(--bg); border-color: color-mix(in srgb, var(--bg) 40%, transparent); }

/* ---------- Historial ---------- */

.month { margin-bottom: 1.2rem; }
.month-title { display: flex; justify-content: space-between; font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0 .2rem .5rem; }
.purchase { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: .5rem; overflow: hidden; }
.purchase summary {
    list-style: none;
    display: flex;
    align-items: center;
    gap: .7rem;
    padding: .75rem .9rem;
    cursor: pointer;
}
.purchase summary::-webkit-details-marker { display: none; }
.purchase-main { flex: 1; min-width: 0; display: grid; }
.purchase-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.purchase-total { font-weight: 700; white-space: nowrap; }
.purchase-body { padding: 0 .9rem .9rem; border-top: 1px solid var(--border); }
.purchase-body p { margin: .6rem 0 0; }
.purchase-items { list-style: none; padding: 0; margin: .5rem 0 0; }
.purchase-items li { display: flex; justify-content: space-between; gap: .5rem; padding: .3rem 0; border-bottom: 1px dashed var(--border); }
.purchase-actions { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .8rem; }

/* ---------- Pestañas ---------- */

.tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    height: calc(var(--tabbar-h) + var(--safe-b));
    padding-bottom: var(--safe-b);
    background: var(--surface);
    border-top: 1px solid var(--border);
}
.tab {
    font: inherit;
    font-size: .72rem;
    font-weight: 600;
    background: none;
    border: 0;
    color: var(--muted);
    display: grid;
    place-items: center;
    align-content: center;
    gap: .15rem;
    cursor: pointer;
    position: relative;
}
.tab.active { color: var(--primary); }
.badge {
    position: absolute;
    top: 8px;
    left: calc(50% + 8px);
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--neg);
    color: #fff;
    font-size: .68rem;
    display: grid;
    place-items: center;
}

/* ---------- Diálogos (hoja inferior en el teléfono) ---------- */

dialog.sheet {
    border: 0;
    padding: 0;
    color: var(--text);
    background: var(--surface);
    width: 100%;
    max-width: 520px;
    max-height: 92dvh;
    margin: auto auto 0;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, .2);
}
dialog.sheet[open] { animation: sheet-up .2s ease-out; }
dialog.sheet::backdrop { background: rgba(2, 6, 23, .5); }
dialog.sheet > form { padding: 1.2rem 1.1rem calc(1rem + var(--safe-b)); }
dialog.sheet h2 { font-size: 1.2rem; margin-bottom: 1rem; }
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } }

.dialog-actions { display: flex; gap: .5rem; justify-content: flex-end; align-items: center; margin-top: .4rem; }
.checkout-items {
    list-style: none;
    margin: -.3rem 0 1rem;
    padding: .6rem .8rem;
    background: var(--surface-2);
    border-radius: 10px;
    max-height: 9rem;
    overflow: auto;
    font-size: .9rem;
}
.checkout-items li { padding: .15rem 0; }
.checkout-items li::before { content: "✓ "; color: var(--primary); font-weight: 700; }

.member-edit { display: flex; align-items: center; gap: .5rem; margin-bottom: .5rem; }

.sheet-wide { max-width: 720px; }
.receipt-body { padding: 1rem 1rem 0; display: grid; place-items: center; }
.receipt-body img { max-width: 100%; max-height: 70dvh; border-radius: 10px; }
.receipt-body iframe { width: 100%; height: 70dvh; border: 0; border-radius: 10px; background: #fff; }
#receipt-dialog .dialog-actions { padding: .8rem 1rem calc(1rem + var(--safe-b)); }

/* ---------- Aviso ---------- */

.toast {
    position: fixed;
    left: 50%;
    top: calc(env(safe-area-inset-top, 0px) + .8rem);
    transform: translate(-50%, -150%);
    z-index: 100;
    background: var(--text);
    color: var(--bg);
    padding: .65rem 1rem;
    border-radius: 12px;
    font-size: .9rem;
    font-weight: 600;
    max-width: calc(100% - 2rem);
    box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
    transition: transform .25s, opacity .25s;
    pointer-events: none;
    opacity: 0;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }
.toast-ok { background: var(--pos); color: #fff; }
.toast-error { background: var(--neg); color: #fff; }

/* ---------- Pantallas más grandes ---------- */

@media (min-width: 640px) {
    .add-row { grid-template-columns: 1fr 1fr auto; }
    .add-row .btn { grid-column: auto; }
    dialog.sheet { margin: auto; border-radius: 20px; }
    .topbar { padding-left: max(1rem, calc((100% - 720px) / 2 + 1rem)); padding-right: max(1rem, calc((100% - 720px) / 2 + 1rem)); }
}
