:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #1a2230;
  --muted: #637083;
  --border: #dde3ea;
  --brand: #1e3a5f;
  --brand-text: #ffffff;
  --primary: #2563eb;
  --free: #15803d;
  --free-bg: #e7f6ec;
  --busy: #c2410c;
  --busy-bg: #fdeee4;
  --booked: #6d28d9;
  --booked-bg: #efe9fd;
  --danger: #b91c1c;
  --tabbar-h: 60px;
  --cal-room-w: 76px;
  --cal-day-w: 42px;
  --cal-head-h: 46px;
  --cal-row-h: 42px;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 5%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1520;
    --surface: #182130;
    --text: #e6ebf2;
    --muted: #9aa6b8;
    --border: #2a3546;
    --brand: #142a46;
    --primary: #3b82f6;
    --free: #4ade80;
    --free-bg: #133222;
    --busy: #fb923c;
    --busy-bg: #3a2414;
    --booked: #c4b5fd;
    --booked-bg: #2a2142;
    --danger: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }

/* ---------- botones ---------- */

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 10px;
  min-height: 44px;
  cursor: pointer;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn.big { min-height: 52px; font-size: 18px; }
.btn.ghost { background: transparent; border-color: rgb(255 255 255 / 35%); color: var(--brand-text); }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
}

/* ---------- formularios ---------- */

label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }

input, textarea, select {
  font: inherit;
  font-size: 16px; /* evita el zoom automático en iPhone */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  width: 100%;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }

fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 12px; }
legend { font-weight: 700; font-size: 15px; color: var(--text); margin-bottom: 4px; padding: 0; }

.form-error { color: var(--danger); margin: 0; font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; margin: 0; font-weight: 400; }

/* ---------- login ---------- */

.login {
  min-height: 100dvh;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 16px;
  padding: 24px 16px;
}
.login-logo { border-radius: 18px; }
.login-card { width: min(360px, calc(100vw - 32px)); display: grid; gap: 14px; }
.card { background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); border: 1px solid var(--border); }

/* ---------- barra superior ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--brand);
  color: var(--brand-text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
}
.topbar-actions { display: flex; gap: 8px; flex-shrink: 0; margin-left: auto; }

.offline-banner { display: none; background: var(--danger); color: #fff; text-align: center; padding: 8px 16px; font-weight: 600; font-size: 14px; }
body.offline .offline-banner { display: block; }

/* ---------- vista día ---------- */

.main { max-width: 600px; margin: 0 auto; padding: 12px 16px calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom)); }

.daynav { display: grid; grid-template-columns: 48px 1fr 48px; gap: 8px; align-items: center; margin-bottom: 10px; }
.btn.icon { padding: 0; min-width: 44px; font-size: 16px; }
.daypick {
  position: relative;
  display: block;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 8px;
  color: var(--text);
  font-size: 17px;
  font-weight: 700;
  text-transform: capitalize;
}
/* El input de fecha va invisible encima: al tocar abre el calendario nativo del celular. */
.daypick input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; padding: 0; border: 0; }
.daypick input::-webkit-calendar-picker-indicator { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; }
.back-today { display: block; margin: -2px auto 10px; }

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 8px; }
.stat { border-radius: var(--radius); padding: 10px 8px; font-weight: 600; font-size: 13px; text-align: center; }
.stat b { display: block; font-size: 26px; line-height: 1.1; }
.stat.free { background: var(--free-bg); color: var(--free); }
.stat.busy { background: var(--busy-bg); color: var(--busy); }
.stat.booked { background: var(--booked-bg); color: var(--booked); }
.moves { margin: 0 0 8px; text-align: center; color: var(--muted); font-size: 14px; }
.moves b { color: var(--text); }

.filters { display: flex; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 4px; gap: 4px; margin-bottom: 4px; }
.filters button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 9px 4px;
  border-radius: 9px;
  cursor: pointer;
}
.filters button.active { background: var(--brand); color: var(--brand-text); }

section h2 { font-size: 14px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 18px 0 8px; }

.grid { display: grid; gap: 10px; }

.room {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 6px solid var(--free);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 12px 12px 14px;
  display: grid;
  gap: 6px;
  cursor: pointer;
}
.room.is-ocupada { border-left-color: var(--busy); }
.room.is-reservada { border-left-color: var(--booked); }
.room:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.room-head { display: flex; align-items: center; gap: 8px; }
.room-head h3 { font-size: 19px; flex: 1; }
.icon-btn.small { width: 36px; height: 36px; font-size: 17px; margin-right: -6px; }

.pill { font-size: 13px; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--free-bg); color: var(--free); white-space: nowrap; }
.is-ocupada .pill { background: var(--busy-bg); color: var(--busy); }
.is-reservada .pill { background: var(--booked-bg); color: var(--booked); }

.room-info, .room-facts { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--muted); font-size: 14px; }
.room-price { font-weight: 700; font-size: 17px; }
.room-price small { font-weight: 500; color: var(--muted); font-size: 13px; }
.room-guest { display: flex; flex-wrap: wrap; gap: 4px 14px; font-weight: 600; background: var(--busy-bg); padding: 8px 10px; border-radius: 10px; overflow-wrap: anywhere; }
.is-reservada .room-guest { background: var(--booked-bg); }
.room-guest .dates { flex-basis: 100%; font-weight: 500; font-size: 14px; }
.room-note, .room-fixed-note { font-size: 14px; color: var(--muted); white-space: pre-line; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.room-leaving { font-size: 14px; font-weight: 600; color: var(--busy); }
.room-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.room .toggle { min-width: 112px; }

.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------- barra de pestañas ---------- */

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  height: var(--tabbar-h);
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  display: grid;
  place-content: center;
  gap: 2px;
  cursor: pointer;
}
.tabbar button span { font-size: 22px; line-height: 1; filter: grayscale(1); opacity: 0.6; }
.tabbar button.active { color: var(--primary); }
.tabbar button.active span { filter: none; opacity: 1; }

/* ---------- calendario ---------- */

.main.view-cal { max-width: none; padding: 10px 0 0; }
.calnav { display: grid; grid-template-columns: 44px 1fr auto 44px; gap: 8px; align-items: center; padding: 0 16px 8px; }
.cal-title { text-align: center; font-weight: 700; line-height: 1.2; }
.cal-title small { display: block; font-weight: 500; color: var(--muted); font-size: 13px; text-transform: none; }
.cal-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: center; padding: 0 16px 8px; font-size: 13px; font-weight: 600; }
.cal-legend span::before { content: ""; display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.cal-legend .ocupada::before { background: var(--busy); }
.cal-legend .reservada::before { background: var(--booked); }
.cal-legend .hint::before, .cal-legend .special::before { display: none; }
.cal-legend .special { color: var(--booked); }

.cal-scroll {
  overflow: auto;
  height: calc(100dvh - 210px - var(--tabbar-h) - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  min-height: 300px;
  border-top: 1px solid var(--border);
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.cal-grid { display: grid; width: max-content; position: relative; }

.cal-corner, .cal-day, .cal-room { background: var(--surface); }
.cal-corner { grid-row: 1; grid-column: 1; position: sticky; top: 0; left: 0; z-index: 4; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.cal-day {
  grid-row: 1;
  position: sticky;
  top: 0;
  z-index: 3;
  display: grid;
  place-content: center;
  text-align: center;
  line-height: 1.1;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}
.cal-day b { font-size: 16px; color: var(--text); }
.cal-day.weekend { background: color-mix(in srgb, var(--surface) 88%, var(--muted)); }
.cal-day.today { background: var(--primary); color: #fff; }
.cal-day.today b { color: #fff; }

.cal-room {
  grid-column: 1;
  position: sticky;
  left: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  padding: 0 6px 0 10px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.15;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}

.cal-cell {
  border: 0;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: transparent;
  padding: 0;
  cursor: pointer;
}
.cal-cell.weekend { background: color-mix(in srgb, var(--bg) 70%, var(--border)); }
.cal-cell.today { background: color-mix(in srgb, var(--primary) 10%, transparent); }
.cal-cell:active { background: color-mix(in srgb, var(--primary) 25%, transparent); }
.cal-room.first-depto, .cal-cell.first-depto { border-top: 2px solid var(--muted); }

.cal-stay {
  z-index: 1;
  margin: 5px 2px;
  border: 0;
  border-radius: 8px;
  padding: 0 7px;
  font: inherit;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.cal-stay.ocupada { background: var(--busy); }
.cal-stay.reservada { background: var(--booked); }
@media (prefers-color-scheme: dark) { .cal-stay { color: #111; } }
.cal-stay.cut-left { border-top-left-radius: 0; border-bottom-left-radius: 0; margin-left: 0; }
.cal-stay.cut-right { border-top-right-radius: 0; border-bottom-right-radius: 0; margin-right: 0; }

/* ---------- diálogos (hoja inferior en celular) ---------- */

dialog {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  width: 100%;
  max-width: 560px;
  max-height: 100dvh;
  margin: auto auto 0;
}
dialog::backdrop { background: rgb(10 15 25 / 55%); }

.sheet {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  display: grid;
  gap: 14px;
  max-height: 92dvh;
  overflow-y: auto;
}

.sheet-head { display: flex; align-items: center; justify-content: space-between; position: sticky; top: -8px; background: var(--surface); padding-top: 8px; z-index: 1; }
.sheet-head h2 { font-size: 21px; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1.5fr; gap: 10px; }

.status-switch { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.status-switch label { display: block; }
.status-switch input { position: absolute; opacity: 0; pointer-events: none; }
.status-switch span {
  display: block;
  text-align: center;
  padding: 12px;
  border-radius: 12px;
  border: 2px solid var(--border);
  font-size: 17px;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
}
.status-switch input:focus-visible + span { outline: 2px solid var(--primary); }
.status-switch input[value="reservada"]:checked + span { border-color: var(--booked); background: var(--booked-bg); color: var(--booked); }
.status-switch input[value="ocupada"]:checked + span { border-color: var(--busy); background: var(--busy-bg); color: var(--busy); }

.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.nights-row { display: grid; grid-template-columns: 48px 1fr 48px; gap: 8px; align-items: center; }
.nights { text-align: center; line-height: 1.25; }
.nights b { display: block; font-size: 18px; }
.nights small { color: var(--muted); font-weight: 600; }

.admin-box { border-top: 1px dashed var(--border); padding-top: 14px; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

/* ---------- usuarios ---------- */

.user-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.user-list li { border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; display: grid; gap: 8px; }
.user-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--bg); color: var(--muted); margin-left: 4px; }
.tag.admin { background: var(--brand); color: var(--brand-text); }
.new-user { display: grid; gap: 10px; border-top: 1px dashed var(--border); padding-top: 14px; }
.new-user h3 { font-size: 16px; }

/* ---------- aviso flotante ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: #1a2230;
  color: #fff;
  padding: 12px 18px;
  border-radius: 12px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
  max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); }

/* ---------- precios especiales ---------- */

.tag.special { background: var(--booked-bg); color: var(--booked); margin-left: 6px; vertical-align: 2px; }
.cal-price { display: block; font-size: 10px; font-weight: 700; color: var(--booked); line-height: 1; }

.price-form { display: grid; gap: 12px; }
.pick-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.pick-head b { margin-right: auto; font-size: 15px; }
.room-picks { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: block; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
  display: block;
  padding: 7px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.chip input:checked + span { background: var(--brand); border-color: var(--brand); color: var(--brand-text); }
.chip input:focus-visible + span { outline: 2px solid var(--primary); }

.list-title { font-size: 16px; border-top: 1px dashed var(--border); padding-top: 14px; }
.price-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.price-list li:not(.hint) { border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; display: grid; gap: 6px; }
.price-row { display: flex; justify-content: space-between; gap: 8px; text-transform: capitalize; }
.price-amount { font-weight: 700; color: var(--booked); text-transform: none; }

/* ---------- pago ---------- */

:root { --paid: #15803d; --paid-bg: #e7f6ec; --unpaid: #b45309; --unpaid-bg: #fef3c7; }
@media (prefers-color-scheme: dark) { :root { --paid: #4ade80; --paid-bg: #133222; --unpaid: #fbbf24; --unpaid-bg: #3a2c0c; } }

.pay-chip {
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 4px 11px;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  min-height: 32px;
}
.pay-chip.paid { color: var(--paid); background: var(--paid-bg); }
.pay-chip.unpaid { color: var(--unpaid); background: var(--unpaid-bg); }
.unpaid-count { color: var(--unpaid); }

.pay-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 2px solid var(--unpaid);
  background: var(--unpaid-bg);
  border-radius: 12px;
  color: var(--text);
  cursor: pointer;
}
.pay-toggle:has(input:checked) { border-color: var(--paid); background: var(--paid-bg); }
.pay-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.pay-track { position: relative; flex: none; width: 46px; height: 28px; border-radius: 999px; background: var(--muted); transition: background 0.15s; }
.pay-track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.pay-toggle input:checked + .pay-track { background: var(--paid); }
.pay-toggle input:checked + .pay-track::after { transform: translateX(18px); }
.pay-toggle input:focus-visible + .pay-track { outline: 2px solid var(--primary); outline-offset: 2px; }
.pay-text { display: grid; line-height: 1.25; }
.pay-text b { font-size: 16px; color: var(--unpaid); }
.pay-text b::before { content: attr(data-off); }
.pay-toggle input:checked ~ .pay-text b { color: var(--paid); }
.pay-toggle input:checked ~ .pay-text b::before { content: attr(data-on); }
.pay-text small { color: var(--muted); font-weight: 500; }
.pay-toggle input:not(:checked) ~ .pay-text small { display: none; }

/* ---------- logs ---------- */

.log-filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.log-filters label:last-child { grid-column: 1 / -1; }
.log-filters select { padding: 9px 10px; }
.log-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.log-day { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin-top: 8px; }
.log-item { border: 1px solid var(--border); border-left: 4px solid var(--muted); border-radius: 10px; padding: 8px 10px; display: grid; gap: 2px; }
.log-item.log-estadia { border-left-color: var(--busy); }
.log-item.log-habitacion { border-left-color: var(--primary); }
.log-item.log-precio { border-left-color: var(--booked); }
.log-item.log-usuario { border-left-color: var(--brand); }
.log-top { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.log-time { color: var(--muted); font-variant-numeric: tabular-nums; }
.log-icon { margin-left: auto; }
.log-action { font-weight: 700; }
.log-detail { font-size: 14px; color: var(--muted); overflow-wrap: anywhere; }
.log-changes { list-style: none; margin: 4px 0 0; padding: 6px 8px; background: var(--bg); border-radius: 8px; font-size: 13px; display: grid; gap: 2px; }
.log-changes li { overflow-wrap: anywhere; }
.log-changes span { color: var(--muted); }
.log-changes s { color: var(--muted); }
