/* =========================================================
   Finanzas del Hogar — estilos base (sin frameworks, mobile-first)
   Inspirado en el layout de referencia: sidebar + topbar + tarjetas
   blancas redondeadas con sombra suave, acentos azul/morado.
   ========================================================= */

:root {
    --color-bg: #f4f6fb;
    --color-card: #ffffff;
    --color-border: #e7e9f4;
    --color-text: #1f2433;
    --color-muted: #7b8299;

    --color-primary: #4a6cf7;
    --color-primary-dark: #3757d8;
    --color-primary-light: #eaf0ff;

    --color-purple: #7c5cfc;
    --color-purple-light: #f1ecff;

    --color-green: #1fbf75;
    --color-green-light: #e6f9ef;

    --color-red: #f0506e;
    --color-red-light: #fdeaee;

    --color-orange: #f59e0b;
    --color-orange-light: #fef3e2;

    --radius-lg: 16px;
    --radius-md: 10px;
    --radius-sm: 8px;

    --shadow-card: 0 4px 18px rgba(30, 41, 59, 0.06);
    --sidebar-width: 250px;
}

:root[data-theme="dark"] {
    --color-bg: #10141f;
    --color-card: #1a2032;
    --color-border: #2b3348;
    --color-text: #e8eaf4;
    --color-muted: #9aa1b9;

    --color-primary: #6d8bff;
    --color-primary-dark: #a9bbff;
    --color-primary-light: rgba(109, 139, 255, 0.16);

    --color-purple: #b39dfd;
    --color-purple-light: rgba(179, 157, 253, 0.16);

    --color-green: #34d399;
    --color-green-light: rgba(52, 211, 153, 0.14);

    --color-red: #fb7185;
    --color-red-light: rgba(251, 113, 133, 0.16);

    --color-orange: #fbbf24;
    --color-orange-light: rgba(251, 191, 36, 0.14);

    --shadow-card: 0 4px 18px rgba(0, 0, 0, 0.35);
}

/* ---------- Scrollbar (mismo estilo del sistema, claro/oscuro) ---------- */

* {
    scrollbar-width: thin;
    scrollbar-color: var(--color-border) transparent;
}

*::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

*::-webkit-scrollbar-track {
    background: transparent;
}

*::-webkit-scrollbar-thumb {
    background-color: var(--color-border);
    border-radius: var(--radius-sm);
    border: 2px solid transparent;
    background-clip: padding-box;
}

*::-webkit-scrollbar-thumb:hover {
    background-color: var(--color-muted);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    font-size: 15px;
    line-height: 1.45;
    transition: background-color 0.2s ease, color 0.2s ease;
}

/* Cambio de tema suave, sin parpadeo, en los elementos con más superficie de color. */
.sidebar, .topbar, .card, .auth-card, input, select, textarea {
    transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { margin: 0 0 8px; font-weight: 700; }

img { max-width: 100%; }

/* ---------- App shell ---------- */

.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--color-card);
    border-right: 1px solid var(--color-border);
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    overflow-y: auto;
    z-index: 40;
    transition: transform 0.25s ease;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 20px 22px;
    font-weight: 800;
    font-size: 1.05rem;
    color: var(--color-text);
}

.sidebar-brand .brand-badge {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--color-primary), var(--color-purple));
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 800;
}

.sidebar-nav {
    list-style: none;
    margin: 8px 0;
    padding: 0 12px;
}

.sidebar-nav li { margin-bottom: 2px; }

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: var(--color-muted);
    font-weight: 600;
    font-size: 0.92rem;
    text-decoration: none;
}

.sidebar-nav a svg { flex-shrink: 0; width: 18px; height: 18px; }

/* Tamaño por defecto para cualquier ícono svg dentro de un botón (topbar,
   toggle de sidebar, tema, logout, etc.) — sin esto el navegador usa un
   tamaño de reemplazo enorme (~300x150) porque el svg no trae width/height. */
.btn svg, .sidebar-toggle svg { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-nav a:hover {
    background: var(--color-bg);
    color: var(--color-text);
}

.sidebar-nav li.active a {
    background: var(--color-primary-light);
    color: var(--color-primary-dark);
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    z-index: 35;
}

.main {
    flex: 1;
    margin-left: var(--sidebar-width);
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.topbar {
    background: var(--color-card);
    border-bottom: 1px solid var(--color-border);
    padding: 14px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    position: sticky;
    top: 0;
    z-index: 30;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 0;
}

.topbar-title {
    font-size: 1.15rem;
    font-weight: 800;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-toggle {
    display: none;
    border: none;
    background: var(--color-bg);
    border-radius: var(--radius-sm);
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.topbar-user {
    font-weight: 700;
    font-size: 0.9rem;
    color: var(--color-text);
}

#theme-toggle .theme-icon-light { display: none; }
:root[data-theme="dark"] #theme-toggle .theme-icon-dark { display: none; }
:root[data-theme="dark"] #theme-toggle .theme-icon-light { display: inline-flex; }

.content {
    padding: 22px;
    flex: 1;
}

/* ---------- Month switcher ---------- */

.month-switcher {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--color-bg);
    border-radius: var(--radius-sm);
    padding: 4px;
}

.month-switcher a, .month-switcher span {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px 10px;
    border-radius: 6px;
    font-weight: 700;
    font-size: 0.85rem;
    color: var(--color-text);
}

.month-switcher a:hover { background: var(--color-card); text-decoration: none; }
.month-switcher .month-label { min-width: 130px; text-align: center; }

/* ---------- Cards ---------- */

.card {
    background: var(--color-card);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    padding: 20px;
    margin-bottom: 20px;
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.card-header h2, .card-header h3 { margin: 0; }

.card-title-sm { font-size: 0.95rem; color: var(--color-muted); font-weight: 700; margin: 0 0 4px; }

.grid {
    display: grid;
    gap: 18px;
}

.grid-stats { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

/* ---------- Stat tiles ---------- */

.stat-card {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 10px 26px rgba(30, 41, 59, 0.12);
}

:root[data-theme="dark"] .stat-card:hover {
    box-shadow: 0 10px 26px rgba(0, 0, 0, 0.5);
}

.stat-icon {
    width: 46px;
    height: 46px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #fff;
    transition: transform 0.15s ease;
}

.stat-card:hover .stat-icon { transform: scale(1.08); }

.stat-icon svg { width: 22px; height: 22px; }

.bg-blue { background: var(--color-primary); }
.bg-purple { background: var(--color-purple); }
.bg-green { background: var(--color-green); }
.bg-red { background: var(--color-red); }
.bg-orange { background: var(--color-orange); }

.stat-label { color: var(--color-muted); font-size: 0.85rem; font-weight: 600; margin-bottom: 2px; }
.stat-value { font-size: 1.5rem; font-weight: 800; line-height: 1.2; }
.stat-sub { font-size: 0.78rem; color: var(--color-muted); margin-top: 2px; }

.stat-trend {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
    font-weight: 700;
    margin-top: 4px;
}
.stat-trend.trend-up { color: var(--color-green); }
.stat-trend.trend-down { color: var(--color-red); }

/* ---------- Gráficos (Chart.js) ---------- */

.chart-box { position: relative; height: 260px; }
.chart-box-wide { height: 300px; }

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

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: none;
    border-radius: var(--radius-sm);
    padding: 10px 16px;
    font-weight: 700;
    font-size: 0.88rem;
    cursor: pointer;
    text-decoration: none;
    transition: filter 0.15s ease, background 0.15s ease;
}

.btn:hover { text-decoration: none; filter: brightness(0.95); }

.btn-primary { background: var(--color-primary); color: #fff; }
.btn-secondary { background: var(--color-bg); color: var(--color-text); }
.btn-danger { background: var(--color-red-light); color: var(--color-red); }
.btn-success { background: var(--color-green-light); color: var(--color-green); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 10px; font-size: 0.78rem; }
.btn-icon { padding: 8px; width: 34px; height: 34px; }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- Forms ---------- */

form label {
    display: block;
    font-weight: 700;
    font-size: 0.82rem;
    color: var(--color-muted);
    margin-bottom: 14px;
}

.auth-form label,
.install-card form label { margin-bottom: 14px; }

input, select, textarea {
    display: block;
    width: 100%;
    margin-top: 6px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-border);
    background: var(--color-card);
    font-size: 0.92rem;
    color: var(--color-text);
    font-family: inherit;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-light);
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 0 16px;
}

.form-section-title {
    font-size: 0.95rem;
    font-weight: 800;
    margin: 18px 0 10px;
    padding-top: 12px;
    border-top: 1px solid var(--color-border);
}
.form-section-title:first-child { border-top: none; padding-top: 0; margin-top: 0; }

.form-actions { margin-top: 6px; display: flex; gap: 10px; flex-wrap: wrap; }

.field-hint { font-weight: 500; color: var(--color-muted); font-size: 0.78rem; margin-top: 4px; }

.field-label {
    display: block;
    font-weight: 700;
    font-size: 0.82rem;
    color: var(--color-muted);
    margin-bottom: 8px;
}

.checkbox-group {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 20px;
    margin-bottom: 14px;
}

.checkbox-inline {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-weight: 600;
    font-size: 0.88rem;
    color: var(--color-text);
    margin-bottom: 0;
    cursor: pointer;
}

.checkbox-inline input[type="checkbox"] {
    display: inline-block;
    width: 16px;
    height: 16px;
    min-width: 16px;
    margin: 0;
    padding: 0;
    border: none;
    border-radius: 0;
    background: none;
    box-shadow: none;
    accent-color: var(--color-primary);
    cursor: pointer;
    flex-shrink: 0;
}

.checkbox-inline input[type="checkbox"]:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
    box-shadow: none;
}

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; min-width: 480px; }

thead th {
    text-align: left;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-muted);
    padding: 10px 12px;
    border-bottom: 1px solid var(--color-border);
    white-space: nowrap;
}

tbody td {
    padding: 12px;
    border-bottom: 1px solid var(--color-border);
    vertical-align: middle;
    font-size: 0.9rem;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--color-bg); }

.text-right { text-align: right; }
.text-muted { color: var(--color-muted); }
.text-strong { font-weight: 700; }
.nowrap { white-space: nowrap; }

/* ---------- Badges & alerts ---------- */

.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 800;
}

.badge-blue { background: var(--color-primary-light); color: var(--color-primary-dark); }
.badge-purple { background: var(--color-purple-light); color: var(--color-purple); }
.badge-green { background: var(--color-green-light); color: var(--color-green); }
.badge-red { background: var(--color-red-light); color: var(--color-red); }
.badge-orange { background: var(--color-orange-light); color: var(--color-orange); }
.badge-muted { background: var(--color-bg); color: var(--color-muted); }

.alert {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    font-weight: 600;
    margin-bottom: 14px;
    border-left: 4px solid transparent;
}

.alert-success { background: var(--color-green-light); color: #0d7a4c; border-color: var(--color-green); }
.alert-danger { background: var(--color-red-light); color: #a8213a; border-color: var(--color-red); }
.alert-warning { background: var(--color-orange-light); color: #92620a; border-color: var(--color-orange); }
.alert-info { background: var(--color-primary-light); color: var(--color-primary-dark); border-color: var(--color-primary); }

:root[data-theme="dark"] .alert-success { color: #6ee7b7; }
:root[data-theme="dark"] .alert-danger { color: #fda4af; }
:root[data-theme="dark"] .alert-warning { color: #fcd34d; }

.rules-list {
    margin: 0;
    padding-left: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.rules-list li {
    line-height: 1.5;
}

.empty-state {
    text-align: center;
    padding: 36px 16px;
    color: var(--color-muted);
}

/* ---------- Progress / budget bars ---------- */

.budget-bar {
    height: 10px;
    border-radius: 999px;
    background: var(--color-bg);
    overflow: hidden;
    margin: 8px 0 4px;
}

.budget-bar-fill { height: 100%; border-radius: 999px; }
.budget-bar-fill.ok { background: var(--color-green); }
.budget-bar-fill.warn { background: var(--color-orange); }
.budget-bar-fill.over { background: var(--color-red); }

/* ---------- Modales ---------- */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 1000;
}

.modal-overlay.open { display: flex; }

.modal-box {
    background: var(--color-card);
    border-radius: 16px;
    width: 100%;
    max-width: 760px;
    max-height: 85vh;
    overflow-y: auto;
    padding: 22px;
    box-shadow: 0 20px 50px rgba(15, 23, 42, 0.35);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.modal-header h2 { margin: 0; }

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    color: var(--color-muted);
    padding: 2px 8px;
    border-radius: 8px;
}

.modal-close:hover { background: var(--color-bg); color: var(--color-text); }

/* ---------- Controles de compra (lista de compras) ---------- */

.buy-controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.buy-input { width: 70px; padding: 6px 8px; font-size: 0.82rem; }
.buy-input-qty { width: 52px; }
.buy-status { font-size: 0.78rem; white-space: nowrap; }
.btn-orange { background: var(--color-orange-light); color: var(--color-orange); }

/* ---------- Auth / install pages ---------- */

.auth-body {
    background: linear-gradient(160deg, var(--color-primary-light), var(--color-bg) 55%);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.auth-card {
    background: var(--color-card);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    padding: 32px 28px;
    width: 100%;
    max-width: 420px;
}

.install-card { max-width: 520px; }

.auth-title { text-align: center; font-size: 1.4rem; margin-bottom: 20px; }

/* ---------- Recipe builder ---------- */

.ingredient-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    background: var(--color-bg);
    border-radius: var(--radius-sm);
    padding: 8px 10px;
    margin-bottom: 8px;
}

.ingredient-row .row-ingredient {
    flex: 1 1 200px;
    margin: 0;
}

.ingredient-row .row-remove {
    flex-shrink: 0;
}

/* ---------- Comidas de hoy (dashboard) ---------- */

.today-meals {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.today-meal-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    flex-wrap: wrap;
}

.today-meal-row.is-now {
    background: var(--color-primary-light);
}

.today-meal-label {
    font-weight: 800;
    font-size: 0.85rem;
    color: var(--color-muted);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.today-meal-row.is-now .today-meal-label {
    color: var(--color-primary-dark);
}

.today-meal-content {
    font-weight: 700;
    text-align: right;
}

.today-meal-content .today-meal-person {
    font-weight: 500;
    color: var(--color-muted);
    font-size: 0.82rem;
}

/* ---------- Meal plan grid ---------- */

.meal-plan-grid {
    display: grid;
    grid-template-columns: 110px repeat(7, minmax(150px, 1fr));
    gap: 10px;
    overflow-x: auto;
}

.meal-plan-grid .mp-head, .meal-plan-grid .mp-row-label {
    font-weight: 800;
    font-size: 0.8rem;
    color: var(--color-muted);
    display: flex;
    align-items: center;
}

.meal-plan-cell {
    background: var(--color-bg);
    border-radius: var(--radius-sm);
    padding: 10px;
}

.meal-plan-cell + .meal-plan-cell { margin-top: 0; }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
    .sidebar {
        transform: translateX(-100%);
        box-shadow: 0 0 0 rgba(0,0,0,0);
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 0 0 30px rgba(15, 23, 42, 0.25);
    }
    .sidebar-backdrop.open { display: block; }

    .main { margin-left: 0; }

    .sidebar-toggle { display: flex; }

    .topbar { padding: 12px 16px; }
    .content { padding: 14px; }

    .meal-plan-grid {
        grid-template-columns: 90px repeat(7, minmax(140px, 1fr));
    }
}

@media (max-width: 560px) {
    .topbar-title { font-size: 1rem; }
    .stat-value { font-size: 1.25rem; }
    .card { padding: 16px; border-radius: var(--radius-md); }
    .ingredient-row .row-remove { width: 100%; }
}
