/*
 * rentalm: estilos sin build. Pensado primero para el celular y adaptado a escritorio desde 760px.
 */
:root {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --text: #1d2330;
    --muted: #657085;
    --border: #dde1e8;
    --primary: #2457c5;
    --primary-hover: #1c469f;
    --primary-soft: #e8eefb;
    --danger: #c62f2f;
    --danger-bg: #fdeeee;
    --success: #1f7a4a;
    --success-bg: #e8f5ee;
    --radius: 10px;
    --tap: 44px;
    --tabbar-height: 60px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    color-scheme: light;
}

* { box-sizing: border-box; }

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

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

a { color: var(--primary); }
h1 { font-size: 1.35rem; line-height: 1.25; margin: 0; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Barra superior y navegación ---------- */

.topbar {
    position: sticky; top: 0; z-index: 20;
    background: var(--surface); border-bottom: 1px solid var(--border);
    padding-top: env(safe-area-inset-top, 0px);
}
.topbar__inner {
    max-width: 1200px; margin: 0 auto; padding: .5rem 1rem;
    display: flex; align-items: center; gap: 1rem; min-height: 52px;
}
.brand { font-weight: 700; font-size: 1.1rem; text-decoration: none; color: var(--text); }
.session { margin-left: auto; display: flex; align-items: center; gap: .75rem; }
.session form { margin: 0; }
.session__name { display: none; }

/* En el celular las secciones van en una barra fija abajo, al alcance del pulgar. */
.nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    display: grid; grid-template-columns: repeat(4, 1fr);
    background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: var(--safe-bottom);
    box-shadow: 0 -2px 12px rgba(29, 35, 48, .06);
}
.nav__link {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    height: var(--tabbar-height); text-decoration: none; color: var(--muted); font-size: .75rem; font-weight: 500;
}
.nav__icon { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav__link.active { color: var(--primary); }
.nav__link.active .nav__icon { stroke-width: 2.2; }

.container { max-width: 1200px; margin: 0 auto; padding: 1rem 1rem 2rem; }
.has-tabbar .container { padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + 1.5rem); }

.page-header { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: .75rem; margin-bottom: 1rem; }

/* ---------- Tarjetas y avisos ---------- */

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }
.card--narrow { max-width: 440px; margin: 1.5rem auto; }
.card--narrow h1 { margin-bottom: .5rem; }

.alert { padding: .75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; }
.alert--success { background: var(--success-bg); color: var(--success); }
.alert--error { background: var(--danger-bg); color: var(--danger); }
.alert--warning { background: #fff6e5; color: #8a5300; }

.empty { padding: 2rem 1rem; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

/* ---------- Botones ---------- */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
    min-height: var(--tap); padding: .5rem 1rem;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface); color: var(--text); font: inherit; font-weight: 500;
    text-decoration: none; cursor: pointer; touch-action: manipulation;
}
.btn:hover { border-color: var(--muted); }
.btn:focus-visible, .nav__link:focus-visible { outline: 3px solid #b9cbf3; outline-offset: 1px; }
.btn--primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn--danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn--danger-outline { color: var(--danger); }
.btn--danger-outline:hover { border-color: var(--danger); }
.btn--small { min-height: 40px; padding: .35rem .8rem; font-size: .9rem; }
.btn--block { width: 100%; }
.btn--link { min-height: var(--tap); border: 0; background: none; padding: 0 .25rem; color: var(--primary); font-weight: 400; text-decoration: underline; }

/* ---------- Listados: tarjetas en el celular, tabla en escritorio ---------- */

.table-wrap { background: transparent; }
/* En el celular la tabla deja de ser tabla: si no, el contenido sin corte la ensancha más que la pantalla. */
.responsive-table { display: block; width: 100%; border-collapse: collapse; }
.responsive-table thead { display: none; }
.responsive-table tbody { display: flex; flex-direction: column; gap: .75rem; }
.responsive-table tr {
    display: block; background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: .75rem 1rem;
}
.responsive-table td {
    display: flex; justify-content: space-between; align-items: baseline; gap: 1rem;
    padding: .3rem 0; text-align: right; border: 0;
}
.responsive-table td.nowrap { white-space: normal; }
.responsive-table td { overflow-wrap: anywhere; min-width: 0; }
.responsive-table td::before {
    content: attr(data-label); color: var(--muted); font-size: .85rem; text-align: left; flex-shrink: 0;
    overflow-wrap: normal;
}
.responsive-table td.cell-title {
    display: block; text-align: left; font-weight: 600; font-size: 1.05rem;
    padding: 0 0 .4rem; margin-bottom: .3rem; border-bottom: 1px solid var(--border);
}
.responsive-table td.cell-title::before,
.responsive-table td.cell-actions::before { content: none; }
.responsive-table td.cell-actions { display: block; padding: .6rem 0 0; }
.responsive-table td .badge { margin-left: .25rem; }
.responsive-table tr.inactive { background: #fafbfc; }
.responsive-table tr.inactive td { color: var(--muted); }

/* Orden: en el celular, selector arriba de las tarjetas; en escritorio, encabezados con link. */
.sort-mobile { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; margin-bottom: .75rem; }
.sort-mobile label { width: 100%; font-size: .9rem; font-weight: 500; }
.sort-mobile select { flex: 1 1 0; width: auto; min-width: 0; }
.sort-mobile--auto .btn { display: none; }
.sort-link { display: inline-flex; align-items: center; gap: .3rem; color: inherit; text-decoration: none; }
.sort-link:hover { color: var(--text); }
.sort-arrow { font-size: .65rem; }

/* Botones de fila: solo ícono. 44px en el celular (área táctil), 36px en escritorio. */
.row-actions { display: flex; justify-content: flex-end; gap: .5rem; }
.row-actions form { margin: 0; display: flex; }
.btn--icon { width: var(--tap); min-height: var(--tap); padding: 0; }
.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.cell-value { display: inline-flex; flex-wrap: wrap; justify-content: flex-end; align-items: baseline; gap: .25rem .5rem; }

.badge { display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.badge--ok { background: var(--success-bg); color: var(--success); }
.badge--off { background: #eef0f3; color: var(--muted); }
.badge--warn { background: #fff6e5; color: #8a5300; }

/* ---------- Formularios ---------- */

.form { display: flex; flex-direction: column; gap: 1rem; }
fieldset { border: 0; border-top: 1px solid var(--border); margin: 0; padding: 1rem 0 0; min-width: 0; }
fieldset:first-of-type { border-top: 0; padding-top: 0; }
legend { font-weight: 600; padding: 0; margin-bottom: .5rem; }
fieldset > p.muted { margin: 0 0 .75rem; font-size: .9rem; }

.grid { display: grid; grid-template-columns: 1fr; gap: .75rem 1rem; margin-bottom: .75rem; }
.field { display: flex; flex-direction: column; gap: .3rem; min-width: 0; }
.field > span { font-weight: 500; font-size: .9rem; }
.field abbr { color: var(--danger); text-decoration: none; }

/* 16px evita que iOS haga zoom al tocar un campo. */
input, select {
    font: inherit; font-size: 16px; min-height: var(--tap); padding: .55rem .7rem;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: #fff; color: var(--text); width: 100%;
}
input[type="date"] { -webkit-appearance: none; appearance: none; min-width: 0; }
input:focus, select:focus { outline: 3px solid #b9cbf3; outline-offset: 0; border-color: var(--primary); }
.field--error input, .field--error select { border-color: var(--danger); }
.error { color: var(--danger); font-size: .875rem; }

.check { display: flex; align-items: center; gap: .6rem; min-height: var(--tap); cursor: pointer; }
.check input { width: 22px; height: 22px; min-height: 0; margin: 0; }

.actions { display: flex; flex-direction: column-reverse; gap: .5rem; }
.actions form { margin: 0; display: flex; }
.actions .btn { width: 100%; }

/* En el celular, Guardar/Cancelar quedan fijos encima de la barra de secciones. */
.has-tabbar .form > .actions {
    position: sticky; bottom: calc(var(--tabbar-height) + var(--safe-bottom)); z-index: 10;
    flex-direction: row-reverse;
    margin: 0 -1rem -1rem; padding: .75rem 1rem;
    background: rgba(255, 255, 255, .96); border-top: 1px solid var(--border);
    border-radius: 0 0 var(--radius) var(--radius);
}
.has-tabbar .form > .actions .btn { flex: 1; }

/* Filas repetibles (ARBA, TSG). */
.repeat-rows { display: flex; flex-direction: column; gap: .75rem; margin-bottom: .75rem; }
.repeat-row { display: flex; flex-direction: column; gap: .5rem; padding: .75rem; border: 1px solid var(--border); border-radius: var(--radius); background: #fafbfc; }
.repeat-row__fields { margin-bottom: 0; }
.repeat-row__remove { align-self: flex-end; }
[data-add-row] { width: 100%; border-style: dashed; color: var(--primary); }

/* ---------- Escritorio ---------- */

@media (min-width: 760px) {
    h1 { font-size: 1.5rem; }
    .container { padding: 1.5rem 1rem 3rem; }
    .has-tabbar .container { padding-bottom: 3rem; }
    .card { padding: 1.5rem; }
    .card--narrow { margin-top: 3rem; }

    .topbar__inner { gap: 2rem; }
    .session__name { display: inline; }
    .nav {
        position: static; display: flex; gap: .25rem;
        background: none; border: 0; padding: 0; box-shadow: none;
    }
    .nav__link {
        flex-direction: row; gap: .4rem; height: auto; padding: .45rem .8rem;
        border-radius: var(--radius); font-size: .95rem;
    }
    .nav__link:hover { background: var(--bg); color: var(--text); }
    .nav__link.active { background: var(--primary-soft); font-weight: 600; }
    .nav__icon { width: 18px; height: 18px; }

    .table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
    .responsive-table { display: table; }
    .responsive-table td.nowrap { white-space: nowrap; }
    .responsive-table td { overflow-wrap: normal; }
    .responsive-table thead { display: table-header-group; }
    .responsive-table tbody { display: table-row-group; }
    .responsive-table tr { display: table-row; background: none; border: 0; border-radius: 0; padding: 0; }
    .responsive-table th, .responsive-table td {
        display: table-cell; padding: .6rem .75rem; text-align: left; vertical-align: middle;
        border-bottom: 1px solid var(--border);
    }
    .responsive-table td::before { content: none; }
    .responsive-table td.cell-title { display: table-cell; font-size: inherit; font-weight: 500; padding: .6rem .75rem; margin: 0; }
    .responsive-table td.cell-actions { display: table-cell; padding: .6rem .75rem; }
    .responsive-table tbody tr:last-child td.cell-title { border-bottom: 0; }
    .responsive-table td .badge { margin-left: 0; }
    .responsive-table tbody tr:last-child td { border-bottom: 0; }
    .responsive-table tr.inactive { background: none; }
    .responsive-table .num { text-align: right; }
    .responsive-table th.num .sort-link { justify-content: flex-end; }
    .sort-mobile { display: none; }
    /* Columnas de texto largo (direcciones, nombres): que no se angosten hasta partir cada palabra. */
    .responsive-table .col-wide { min-width: 18rem; }
    .responsive-table .col-wider { min-width: 23rem; }
    th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #fafbfc; font-weight: 600; }

    .row-actions { flex-wrap: nowrap; gap: .35rem; }
    .btn--icon { width: 36px; min-height: 36px; }
    .icon { width: 18px; height: 18px; }
    .cell-value { justify-content: flex-start; }

    .grid { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }

    .actions { flex-direction: row; flex-wrap: wrap; align-items: center; }
    .actions .btn { width: auto; }
    .has-tabbar .form > .actions {
        position: static; flex-direction: row; margin: 0; padding: 0;
        background: none; border: 0;
    }
    .has-tabbar .form > .actions .btn { flex: 0 0 auto; }

    .repeat-row { flex-direction: row; align-items: flex-end; gap: 1rem; padding: 0; border: 0; background: none; }
    .repeat-row__fields { flex: 1; }
    .repeat-row__remove { align-self: auto; margin-bottom: 2px; }
    [data-add-row] { width: auto; }
}

/* ---------- Periodos de pago ---------- */

.back-link { display: inline-flex; align-items: center; min-height: var(--tap); margin-top: -.5rem; text-decoration: none; font-weight: 500; }
.page-subtitle { margin: .2rem 0 0; }
textarea {
    font: inherit; font-size: 16px; padding: .55rem .7rem; width: 100%; resize: vertical;
    border: 1px solid var(--border); border-radius: var(--radius); background: #fff; color: var(--text);
}
textarea:focus { outline: 3px solid #b9cbf3; outline-offset: 0; border-color: var(--primary); }
.field--error textarea { border-color: var(--danger); }
.responsive-table td.cell-notes { white-space: normal; }
/* Columnas calculadas (Total, Costos, Costo vacío): separadas de los importes cargados y en negrita. */
.responsive-table td.cell-computed { font-weight: 600; }
.responsive-table td.cell-computed--first { border-top: 1px solid var(--border); margin-top: .3rem; padding-top: .55rem; }
.grid--amounts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid--amounts .field > span { min-height: 2.6em; display: flex; align-items: flex-end; line-height: 1.3; }

@media (min-width: 760px) {
    .table--compact th, .table--compact td { padding: .5rem .55rem; font-size: .9rem; }
    .table--compact th { font-size: .72rem; }
    .responsive-table td.cell-notes { min-width: 12rem; max-width: 20rem; }
    .grid--amounts { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
    .table--compact th.col-computed, .table--compact td.cell-computed { background: #f3f6fc; }
    .table--compact th.col-usd, .table--compact td.cell-usd { background: #d0f6c8; }
    .table--compact th.col-computed--first, .table--compact td.cell-computed--first { border-left: 2px solid var(--border); margin: 0; padding-top: .5rem; }
    .table--compact tbody tr:last-child td.cell-computed--first { border-top: 0; }
    .grid--amounts .field > span { min-height: 0; }

    /* Tabla ancha: el periodo y los botones quedan fijos al desplazarse de costado. */
    .table--compact th:first-child, .table--compact td.cell-title,
    .table--compact th:last-child, .table--compact td.cell-actions {
        position: sticky; z-index: 1; background: var(--surface);
    }
    .table--compact th:first-child, .table--compact th:last-child { background: #fafbfc; }
    .table--compact th:first-child, .table--compact td.cell-title { left: 0; box-shadow: 1px 0 0 var(--border); }
    .table--compact th:last-child, .table--compact td.cell-actions { right: 0; box-shadow: -1px 0 0 var(--border); }
}

/* ---------- Rescisión de contratos ---------- */

/* Formulario corto: Guardar/Cancelar no necesitan quedar fijos abajo en el celular. */
.has-tabbar .form > .actions--plain {
    position: static; flex-direction: column-reverse; margin: 0; padding: 0; background: none; border: 0;
}
.cancel-termination { margin-top: 1rem; text-align: center; }
@media (min-width: 760px) {
    .has-tabbar .form > .actions--plain { flex-direction: row; }
}

/* ---------- Dashboard ---------- */

.filter-bar { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem 1rem; align-items: end; margin-bottom: 1rem; }
.filter-bar__actions { grid-column: 1 / -1; display: flex; gap: .5rem; }
.filter-bar__actions .btn { flex: 1; white-space: nowrap; padding-inline: .6rem; }
@media (min-width: 760px) {
    .filter-bar { display: flex; flex-wrap: wrap; padding: 1rem 1.25rem; }
    .filter-bar .field { width: 13rem; }
    .filter-bar__actions .btn { flex: 0 0 auto; }
    .table--dashboard th { white-space: normal; min-width: 4.5rem; }
}

/* Propiedades alquiladas: naranja si no están todas; rojo debajo del 70 %; rojo y negrita debajo del 50 %. */
.responsive-table td.occupancy--partial { color: #c2410c; }
.responsive-table td.occupancy--low { color: var(--danger); }
.responsive-table td.occupancy--critical { color: var(--danger); font-weight: 700; }

/* Tarjetas de resumen del Dashboard: 2 por fila en el celular, las 5 en una fila en escritorio. */
.stat-cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat-card {
    display: flex; flex-direction: column; justify-content: space-between; gap: .35rem;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem;
}
.stat-card__title { font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.stat-card__value { font-size: 1.35rem; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat-card:last-child:nth-child(odd) { grid-column: 1 / -1; }
@media (min-width: 760px) {
    .stat-cards { grid-template-columns: repeat(5, minmax(0, 1fr)); }
    .stat-card:last-child:nth-child(odd) { grid-column: auto; }
    .stat-card__value { font-size: 1.5rem; }
}

/* ---------- Gráfico del Dashboard (SVG) ---------- */

.chart-card { margin-top: 1rem; }
.chart-card__header { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: .5rem 1.5rem; margin-bottom: .5rem; }
.chart-card__title { font-size: 1rem; margin: 0; }
.chart-legend { display: flex; flex-wrap: wrap; gap: .25rem 1rem; list-style: none; margin: 0; padding: 0; font-size: .85rem; color: var(--muted); }
.chart-legend li { display: inline-flex; align-items: center; gap: .4rem; }
.chart-legend__swatch { display: inline-block; width: 20px; height: 0; border-top: 2px solid var(--primary); }
.chart-legend__swatch--trend { border-top: 2px dashed var(--muted); }
/* En el celular el gráfico mantiene un ancho mínimo legible y se desplaza de costado. */
.chart-scroll { overflow-x: auto; margin: 0 -.25rem; }
.line-chart { display: block; width: 100%; min-width: 640px; height: auto; font-family: inherit; }
.line-chart__grid line { stroke: var(--border); stroke-width: 1; }
.line-chart__zero { stroke: #b9c0cc; stroke-width: 1; }
.line-chart__axis { font-size: 12px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.line-chart__line { fill: none; stroke: var(--primary); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.line-chart__trend { stroke: var(--muted); stroke-width: 2; stroke-dasharray: 6 5; }
.line-chart__dot { fill: var(--primary); stroke: var(--surface); stroke-width: 2; }
.line-chart__value { font-size: 11px; font-weight: 600; fill: var(--text); font-variant-numeric: tabular-nums; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; stroke-linejoin: round; }
.line-chart__hit { fill: transparent; cursor: default; }
.line-chart__point:hover .line-chart__dot { r: 6; }
@media (max-width: 759px) {
    /* El SVG se dibuja a ~720px de ancho: se agranda el texto para que siga siendo legible. */
    .line-chart { min-width: 720px; }
    .line-chart__axis { font-size: 15px; }
    .line-chart__value { font-size: 14px; }
}

/* ---------- Campos de fecha (dd/mm/aaaa) ---------- */

.date-input { position: relative; display: flex; gap: .4rem; }
.date-input input[type="text"] { flex: 1; min-width: 0; }
.date-input__picker { flex: 0 0 var(--tap); width: var(--tap); }
/* El input nativo queda invisible pero presente: es el que abre el calendario del navegador. */
.date-input__native {
    position: absolute; right: 0; bottom: 0; width: 1px; height: 1px; min-height: 0;
    padding: 0; border: 0; opacity: 0; pointer-events: none;
}
.field--error .date-input input[type="text"] { border-color: var(--danger); }
