:root {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #e4e7ec;
    --text: #1a1d23;
    --text-soft: #667085;
    --text-faint: #98a2b3;
    --accent: #2b6cb0;
    --accent-soft: #ebf2fa;
    --green: #18794e;
    --green-soft: #e6f4ec;
    --amber: #b7791f;
    --amber-soft: #fbf3e3;
    --red: #c0362c;
    --red-soft: #fcebea;
    --gray-soft: #eceef1;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.05);
    --shadow-lg: 0 12px 32px rgba(16,24,40,.16);
}

* { box-sizing: border-box; }

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

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--text);
    display: grid;
    grid-template-columns: 248px 1fr;
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
}

/* ---------- Sidebar ---------- */
.sidebar {
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: sticky;
    top: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 22px 22px 18px;
    font-weight: 700;
    font-size: 17px;
}

.brand-mark {
    color: var(--accent);
    font-size: 20px;
}

.nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 8px 12px;
    flex: 1;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-soft);
    cursor: pointer;
    font-weight: 500;
    user-select: none;
    transition: background .12s, color .12s;
}

.nav-item:hover { background: var(--surface-2); color: var(--text); }

.nav-item.active {
    background: var(--accent-soft);
    color: var(--accent);
}

.nav-icon { width: 18px; text-align: center; font-size: 15px; }

.nav-count {
    margin-left: auto;
    font-size: 12px;
    color: var(--text-faint);
    background: var(--surface-2);
    border-radius: 20px;
    padding: 1px 8px;
}

.nav-item.active .nav-count { background: #fff; color: var(--accent); }

.sidebar-footer { padding: 14px; border-top: 1px solid var(--border); }

.btn-ghost {
    width: 100%;
    background: none;
    border: 1px solid var(--border);
    color: var(--text-soft);
    padding: 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font: inherit;
    font-size: 13px;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Main ---------- */
.main { min-width: 0; height: 100vh; overflow-y: auto; }

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 22px 32px;
    position: sticky;
    top: 0;
    background: rgba(246,247,249,.85);
    backdrop-filter: blur(8px);
    z-index: 5;
    gap: 16px;
}

.topbar h1 { margin: 0; font-size: 22px; font-weight: 700; }
.subtitle { margin: 3px 0 0; color: var(--text-soft); font-size: 13px; }

.topbar-actions { display: flex; align-items: center; gap: 10px; }

.search {
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    font: inherit;
    font-size: 13px;
    width: 220px;
    outline: none;
}
.search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 9px 16px;
    border-radius: var(--radius-sm);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}
.btn-primary:hover { background: #245a94; }

.content { padding: 4px 32px 60px; }

/* ---------- Dashboard ---------- */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
    margin-bottom: 28px;
}

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px;
    box-shadow: var(--shadow);
    min-width: 0;
    overflow: hidden;
}
.stat-card .stat-label { color: var(--text-soft); font-size: 13px; font-weight: 500; overflow-wrap: break-word; }
.stat-card .stat-value { font-size: 28px; font-weight: 700; margin-top: 6px; overflow-wrap: break-word; }
.stat-card .stat-sub { color: var(--text-faint); font-size: 12px; margin-top: 4px; overflow-wrap: break-word; }
.stat-card.danger { border-color: #f3c0bc; background: var(--red-soft); }
.stat-card.danger .stat-value { color: var(--red); }
.stat-card.danger .stat-sub { color: var(--red); }
.stat-card.accent { background: var(--accent); border-color: var(--accent); }
.stat-card.accent .stat-label, .stat-card.accent .stat-sub { color: rgba(255,255,255,.78); }
.stat-card.accent .stat-value { color: #fff; }

.amount-pos { color: var(--green); font-weight: 600; }
.amount-neg { color: var(--red); font-weight: 600; }

/* ---------- Charts ---------- */
.chart-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}
.chart-total { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--border); color: var(--text-soft); font-size: 13px; }
.chart-total strong { color: var(--text); }

/* Horizontal bars */
.hbars { display: flex; flex-direction: column; gap: 14px; }
.hbar-row { display: grid; grid-template-columns: 76px 1fr auto; align-items: center; gap: 10px; }
.hbar-label { font-size: 13px; font-weight: 500; }
.hbar-track { background: var(--surface-2); border-radius: 20px; height: 12px; overflow: hidden; }
.hbar-fill { height: 100%; border-radius: 20px; transition: width .5s cubic-bezier(.2,.8,.2,1); min-width: 2px; }
.hbar-val { font-size: 13px; font-weight: 600; white-space: nowrap; }

/* Donut */
.donut-wrap { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.donut { width: 140px; height: 140px; flex-shrink: 0; }
.donut circle { transition: stroke-dasharray .5s ease; }
.donut-top { font-size: 15px; font-weight: 700; fill: var(--text); }
.donut-bottom { font-size: 9px; fill: var(--text-faint); text-transform: uppercase; letter-spacing: .05em; }
.legend { display: flex; flex-direction: column; gap: 8px; flex: 1; min-width: 130px; }
.legend-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; display: inline-block; }
.legend-label { flex: 1; }
.legend-val { font-weight: 600; }

/* Monthly bars */
.mbars { display: flex; align-items: flex-end; justify-content: space-around; gap: 8px; height: 150px; padding-top: 8px; }
.mbar-col { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: 1; height: 100%; justify-content: flex-end; }
.mbar-pair { display: flex; align-items: flex-end; gap: 3px; height: 100%; width: 100%; justify-content: center; }
.mbar { width: 14px; border-radius: 4px 4px 0 0; min-height: 2px; transition: height .5s cubic-bezier(.2,.8,.2,1); }
.mbar.ing { background: var(--green); }
.mbar.gas { background: var(--red); }
.mbar-label { font-size: 11px; color: var(--text-soft); text-transform: capitalize; }
.mbar-legend { display: flex; gap: 16px; justify-content: center; margin-top: 10px; font-size: 12px; color: var(--text-soft); }
.mbar-legend span { display: inline-flex; align-items: center; gap: 6px; }

/* Movements card */
.card-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; gap: 12px; flex-wrap: wrap; }
.saldo-pill { background: var(--green-soft); color: var(--green); border-radius: 20px; padding: 4px 12px; font-size: 13px; }
.saldo-pill strong { font-weight: 700; }
.table-scroll { overflow-x: auto; }
.mov-table { min-width: 480px; }
.card-link { display: inline-block; margin-top: 12px; color: var(--accent); font-weight: 500; font-size: 13px; cursor: pointer; }
.card-link:hover { text-decoration: underline; }

/* "+ Añadir" relation button */
.add-rel-btn {
    background: var(--accent-soft); border: none; color: var(--accent);
    padding: 5px 12px; border-radius: var(--radius-sm); cursor: pointer;
    font: inherit; font-size: 13px; font-weight: 600; white-space: nowrap;
}
.add-rel-btn:hover { background: #dceaf6; }

/* Unlink (×) button on related cards / rows */
.unlink-btn {
    flex-shrink: 0;
    background: none; border: none; color: var(--text-faint);
    cursor: pointer; font-size: 18px; line-height: 1;
    width: 26px; height: 26px; border-radius: 6px; margin-left: 4px;
}
.unlink-btn:hover { background: var(--red-soft); color: var(--red); }
.related-card .unlink-btn { margin-left: 8px; }

/* Relate picker */
.btn-primary.full, .btn-secondary.full { width: 100%; }
.relate-section { }
.relate-h { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-faint); margin: 0 0 10px; }
.relate-divider { display: flex; align-items: center; text-align: center; color: var(--text-faint); margin: 20px 0; font-size: 13px; }
.relate-divider::before, .relate-divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.relate-divider span { padding: 0 12px; }

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

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.panel-head {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.panel-head a { color: var(--accent); font-size: 13px; font-weight: 500; cursor: pointer; }
.panel-list { padding: 6px 0; }
.panel-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 18px;
    cursor: pointer;
}
.panel-row:hover { background: var(--surface-2); }
.panel-row .pr-main { flex: 1; min-width: 0; }
.panel-row .pr-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.panel-row .pr-sub { color: var(--text-soft); font-size: 12px; }
.panel-row.overdue { box-shadow: inset 3px 0 0 var(--red); }
.panel-row.overdue:hover { background: var(--red-soft); }

/* ---------- Table ---------- */
.table-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}
table { width: 100%; border-collapse: collapse; }
thead th {
    text-align: left;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-faint);
    font-weight: 600;
    padding: 12px 18px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}
tbody td { padding: 13px 18px; border-bottom: 1px solid var(--border); }
tbody tr { cursor: pointer; transition: background .1s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
tbody tr.row-overdue { background: var(--red-soft); box-shadow: inset 3px 0 0 var(--red); }
tbody tr.row-overdue:hover { background: #fbe1de; }
td.cell-strong { font-weight: 600; }
td.cell-muted { color: var(--text-soft); }

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

/* ---------- Badges ---------- */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 20px;
    background: var(--gray-soft);
    color: var(--text-soft);
    white-space: nowrap;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.amber { background: var(--amber-soft); color: var(--amber); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.blue { background: var(--accent-soft); color: var(--accent); }
.badge.plain::before { display: none; }

.link { color: var(--accent); cursor: pointer; font-weight: 500; }
.link:hover { text-decoration: underline; }

/* ---------- Modal / Drawer ---------- */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(16,24,40,.45);
    display: flex;
    justify-content: flex-end;
    z-index: 50;
    animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }

.drawer {
    background: var(--surface);
    width: min(520px, 100%);
    height: 100%;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    animation: slide .2s cubic-bezier(.2,.8,.2,1);
}
@keyframes slide { from { transform: translateX(30px); opacity: .4; } }

.drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
}
.drawer-head h2 { margin: 0; font-size: 18px; }
.drawer-head .sub { color: var(--text-soft); font-size: 13px; margin-top: 2px; }
.icon-btn {
    border: none; background: none; cursor: pointer;
    font-size: 20px; color: var(--text-soft); line-height: 1;
    width: 32px; height: 32px; border-radius: 8px;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.drawer-body { padding: 24px; overflow-y: auto; flex: 1; }
.drawer-foot {
    padding: 16px 24px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
    justify-content: space-between;
}

/* ---------- Form ---------- */
.field { margin-bottom: 16px; }
.field label { display: block; font-weight: 500; margin-bottom: 6px; font-size: 13px; }
.field .req { color: var(--red); }
.field input, .field select, .field textarea {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 9px 11px;
    font: inherit;
    font-size: 14px;
    background: var(--surface);
    outline: none;
    color: var(--text);
}
.field textarea { resize: vertical; min-height: 72px; }
.field input:focus, .field select:focus, .field textarea:focus {
    border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

.btn-danger {
    background: none; border: 1px solid var(--border); color: var(--red);
    padding: 9px 14px; border-radius: var(--radius-sm); cursor: pointer; font: inherit; font-weight: 500;
}
.btn-danger:hover { background: var(--red-soft); border-color: var(--red-soft); }
.btn-secondary {
    background: var(--surface); border: 1px solid var(--border); color: var(--text);
    padding: 9px 16px; border-radius: var(--radius-sm); cursor: pointer; font: inherit; font-weight: 500;
}
.btn-secondary:hover { background: var(--surface-2); }

/* ---------- Detail view ---------- */
.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 18px 24px;
    margin-bottom: 8px;
}
.detail-item .di-label { color: var(--text-faint); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.detail-item .di-value { margin-top: 4px; font-size: 15px; }

.detail-section { margin-top: 26px; }
.detail-section h3 {
    font-size: 13px; text-transform: uppercase; letter-spacing: .04em;
    color: var(--text-faint); margin: 0 0 12px;
}
.related-card {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    margin-bottom: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
}
.related-card:hover { background: var(--surface-2); }
.related-card .rc-main { flex: 1; min-width: 0; }
.related-card .rc-title { font-weight: 500; }
.related-card .rc-sub { color: var(--text-soft); font-size: 12px; }

/* ---------- Detail page ---------- */
.back-link {
    display: inline-block;
    color: var(--text-soft);
    cursor: pointer;
    font-weight: 500;
    margin-bottom: 16px;
}
.back-link:hover { color: var(--accent); }

.detail-layout {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 20px;
    align-items: start;
}
.detail-layout.single { grid-template-columns: 1fr; }
.detail-main { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.detail-side { display: flex; flex-direction: column; gap: 20px; position: sticky; top: 90px; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
}
.card-danger { border-color: #f3c0bc; }
.card-title {
    font-size: 13px; text-transform: uppercase; letter-spacing: .04em;
    color: var(--text-faint); margin: 0 0 14px; font-weight: 600;
    display: flex; align-items: center; gap: 8px;
}
.count-pill {
    font-size: 12px; background: var(--surface-2); color: var(--text-soft);
    border-radius: 20px; padding: 1px 8px; letter-spacing: 0;
}
.notes { margin: 0; color: var(--text-soft); line-height: 1.5; }
.muted-note { color: var(--text-faint); font-size: 13px; margin: 4px 0 0; }

.detail-footer { margin-top: 24px; }

/* Contact / next-payment cards */
.contact-name {
    font-size: 16px; font-weight: 600; cursor: pointer; color: var(--accent);
    margin-bottom: 8px;
}
.contact-name:hover { text-decoration: underline; }
.contact-lines { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.contact-lines a { color: var(--text); text-decoration: none; }
.contact-lines a:hover { color: var(--accent); }
.contract-mini { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); color: var(--text-soft); font-size: 13px; }

.big-amount { font-size: 30px; font-weight: 700; margin-bottom: 8px; }
.next-pay-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.next-pay-date { color: var(--text-soft); font-size: 13px; margin-bottom: 14px; }

.btn-secondary.full { width: 100%; }

/* Document box */
.doc-box, .doc-link { color: var(--accent); cursor: pointer; font-weight: 500; }
.doc-link:hover { text-decoration: underline; }
.doc-box {
    border: 1px dashed var(--border); border-radius: var(--radius-sm);
    padding: 12px; text-align: center;
}
.doc-box:hover { background: var(--accent-soft); }

/* Mini table inside cards */
.mini-table { width: 100%; border-collapse: collapse; }
.mini-table th {
    text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
    color: var(--text-faint); font-weight: 600; padding: 6px 10px; border-bottom: 1px solid var(--border);
}
.mini-table td { padding: 10px; border-bottom: 1px solid var(--border); font-size: 13px; }
.mini-table tbody tr { cursor: pointer; }
.mini-table tbody tr:hover { background: var(--surface-2); }
.mini-table tbody tr:last-child td { border-bottom: none; }
.mini-table tr.row-overdue { background: var(--red-soft); box-shadow: inset 3px 0 0 var(--red); }

.mini-btn {
    background: var(--surface); border: 1px solid var(--border); color: var(--text-soft);
    padding: 4px 10px; border-radius: 6px; cursor: pointer; font: inherit; font-size: 12px; font-weight: 500;
    white-space: nowrap;
}
.mini-btn:hover { background: var(--green-soft); color: var(--green); border-color: var(--green-soft); }

/* related-card spacing inside cards */
.card .related-card:last-child { margin-bottom: 0; }

@media (max-width: 860px) {
    .detail-layout { grid-template-columns: 1fr; }
    .detail-side { position: static; }
}
@media (max-width: 760px) {
    body { grid-template-columns: 1fr; }
    .sidebar { position: fixed; z-index: 40; width: 248px; transform: translateX(-100%); }
    .search { width: 140px; }
}
