/* ============================================================
   Vila Anadia Backoffice — design system + mobile-first layout.
   ============================================================ */

:root {
  /* Warm light palette, matching the property's branding. */
  --bg:           #faf7f0;
  --bg-2:         #f3eee5;
  --surface:      #ffffff;
  --surface-2:    #fdfaf3;
  --text:         #14233a;
  --text-muted:   #6b7382;
  --text-faint:   #9aa1b0;
  --primary:      #14233a;
  --primary-ink:  #ffffff;
  --accent:       #b8895a;
  --accent-ink:   #ffffff;
  --ok:           #15803d;
  --warn:         #b25e09;
  --err:          #b91c1c;
  --warn-bg:      #fdf2dd;
  --err-bg:       #fdecec;
  --ok-bg:        #e7f5ec;
  --border:       #e6dfd2;
  --border-soft:  #efe9dd;
  --ring:         rgba(184,137,90,.35);

  --r-1: 6px;
  --r-2: 10px;
  --r-3: 14px;
  --r-pill: 999px;

  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 20px;
  --s-6: 24px;
  --s-8: 32px;
  --s-10: 40px;

  --sh-1: 0 1px 2px rgba(20,35,58,.04);
  --sh-2: 0 4px 14px rgba(20,35,58,.06);
  --sh-3: 0 12px 32px rgba(20,35,58,.10);

  --appbar-h: 56px;
  --bottomnav-h: 60px;
  --content-max: 1100px;

  --t-fast: 120ms cubic-bezier(.4,0,.2,1);
  --t-base: 200ms cubic-bezier(.4,0,.2,1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:          #0a0e1a;
    --bg-2:        #0e1422;
    --surface:     #131a2b;
    --surface-2:   #1a2235;
    --text:        #e8ecf5;
    --text-muted:  #9aa3b8;
    --text-faint:  #6b7588;
    --primary:     #e8ecf5;
    --primary-ink: #0a0e1a;
    --accent:      #d4a574;
    --accent-ink:  #1a1410;
    --border:      #283149;
    --border-soft: #1f2741;
    --ring:        rgba(212,165,116,.35);
    --warn-bg:     #2a210d;
    --err-bg:      #2b1414;
    --ok-bg:       #0f1f17;
    --sh-1: 0 1px 2px rgba(0,0,0,.40);
    --sh-2: 0 4px 14px rgba(0,0,0,.40);
    --sh-3: 0 12px 32px rgba(0,0,0,.55);
  }
}

* { box-sizing: border-box; }
*::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, 'Inter', 'SF Pro Text', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
svg { display: block; flex-shrink: 0; }
[hidden] { display: none !important; }

::selection { background: var(--ring); }

/* ===== Login ===== */
.login {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--s-4);
  background:
    radial-gradient(80% 60% at 50% 0%, rgba(184,137,90,.10), transparent 70%),
    var(--bg);
}
.login-card {
  width: min(380px, 100%);
  background: var(--surface);
  border-radius: var(--r-3);
  padding: var(--s-8) var(--s-6);
  box-shadow: var(--sh-3);
  border: 1px solid var(--border-soft);
}
.login-brand { text-align: center; margin-bottom: var(--s-6); }
.login-brand .mark {
  width: 56px; height: 56px; margin: 0 auto var(--s-3);
  background: var(--primary); color: var(--primary-ink);
  border-radius: var(--r-3);
  display: grid; place-items: center;
}
.login-brand h1 { margin: 0; font-size: 1.25rem; letter-spacing: -.01em; }
.login-brand p { margin: 2px 0 0; color: var(--text-muted); font-size: .85rem; }
.login form { display: grid; gap: var(--s-3); }
.field { display: grid; gap: var(--s-2); font-size: .85rem; color: var(--text-muted); }
.field input {
  width: 100%; padding: 12px 14px; font-size: 16px;
  border-radius: var(--r-2); border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text);
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--ring); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 18px; min-height: 44px; font-size: .92rem; font-weight: 500;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: var(--r-2); transition: background var(--t-fast), transform var(--t-fast), border-color var(--t-fast);
}
.btn:hover { background: var(--bg-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-primary:hover { background: color-mix(in srgb, var(--primary) 88%, white); }
.btn-accent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-block { width: 100%; }
.err { color: var(--err); font-size: .85rem; margin: 0; text-align: center; }

/* ===== App shell ===== */
.appbar {
  position: sticky; top: 0; z-index: 30;
  height: var(--appbar-h);
  display: flex; align-items: center; gap: var(--s-3);
  padding: 0 var(--s-4);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border-soft);
}
.appbar .brand {
  margin: 0; font-size: .95rem; font-weight: 600; letter-spacing: -.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.appbar .brand small { color: var(--text-muted); font-weight: 400; margin-left: 6px; }
.appbar-spacer { flex: 1; }
.appbar-actions { display: flex; gap: 4px; align-items: center; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px;
  background: transparent; color: var(--text);
  border: none; border-radius: var(--r-2);
  transition: background var(--t-fast), transform var(--t-fast);
}
.icon-btn:hover { background: var(--bg-2); }
.icon-btn:active { transform: scale(.95); }
.icon-btn.is-busy svg { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Content ===== */
.view { padding: var(--s-4); max-width: var(--content-max); margin: 0 auto; padding-bottom: calc(var(--bottomnav-h) + 24px); }
.view h2 { margin: 0 0 4px; font-size: 1.05rem; letter-spacing: -.01em; }
.view h3 { margin: var(--s-6) 0 var(--s-3); font-size: .85rem; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: .08em; }
.muted { color: var(--text-muted); }
.lede { color: var(--text-muted); font-size: .9rem; margin: 0 0 var(--s-5); max-width: 60ch; }

/* Filter bar */
.filters {
  display: flex; flex-wrap: wrap; gap: var(--s-2);
  align-items: center;
  margin-bottom: var(--s-4);
}
.filters input[type="month"] {
  padding: 8px 12px; font-size: 16px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); border-radius: var(--r-pill);
  min-height: 40px;
}
.filters input[type="month"]:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--ring); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  padding: 8px 14px; min-height: 40px; font-size: .85rem; font-weight: 500;
  background: var(--surface); color: var(--text-muted);
  border: 1px solid var(--border); border-radius: var(--r-pill);
  transition: background var(--t-fast), color var(--t-fast), border-color var(--t-fast);
}
.chip:hover { background: var(--bg-2); color: var(--text); }
.chip.is-active { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.counter { margin-left: auto; color: var(--text-muted); font-size: .85rem; font-variant-numeric: tabular-nums; }

/* Booking cards (mobile-first) */
.booking-list { display: grid; gap: var(--s-3); }
.booking {
  background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r-3);
  padding: var(--s-4); box-shadow: var(--sh-1);
  transition: box-shadow var(--t-fast), border-color var(--t-fast);
}
.booking:focus-within { border-color: var(--accent); box-shadow: var(--sh-2); }
.booking.is-missing { border-left: 3px solid var(--warn); }
.booking-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s-3); }
.booking-dates { font-weight: 600; font-size: .92rem; font-variant-numeric: tabular-nums; }
.booking-dates .arrow { color: var(--text-faint); margin: 0 4px; }
.booking-nights { color: var(--text-muted); font-size: .78rem; margin-top: 2px; }
.unit-pill {
  display: inline-flex; align-items: center;
  padding: 4px 10px; border-radius: var(--r-pill);
  background: var(--bg-2); color: var(--text);
  font-size: .78rem; font-weight: 600; letter-spacing: .02em;
  border: 1px solid var(--border);
}
.unit-pill.unit-t3 { background: color-mix(in srgb, var(--accent) 18%, transparent); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }

.booking-fields {
  display: grid; gap: var(--s-3);
  grid-template-columns: 1fr 1fr; margin-top: var(--s-3);
}
.booking-fields .full { grid-column: 1 / -1; }
.booking-fields label { display: grid; gap: 4px; font-size: .72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; }
.booking-fields input, .booking-fields select {
  width: 100%; padding: 8px 12px; min-height: 40px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-2);
  font-size: 15px;
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
.booking-fields input:focus, .booking-fields select:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring);
}
.booking-foot { display: flex; align-items: center; gap: var(--s-2); margin-top: var(--s-3); font-size: .75rem; color: var(--text-faint); }
.source-tag {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: var(--r-pill);
  background: var(--bg-2); color: var(--text-muted);
  font-size: .72rem; text-transform: uppercase; letter-spacing: .06em;
}
.save-state { margin-left: auto; font-size: .75rem; color: var(--text-muted); transition: opacity var(--t-base); }
.save-state.is-saving { color: var(--warn); }
.save-state.is-saved { color: var(--ok); }
.save-state.is-error { color: var(--err); }
.save-state.fade { opacity: 0; }

/* Empty / loading */
.empty {
  padding: var(--s-10) var(--s-4); text-align: center; color: var(--text-muted);
  background: var(--surface); border: 1px dashed var(--border); border-radius: var(--r-3);
}
.empty .glyph { font-size: 2rem; margin-bottom: var(--s-2); }
.empty h3 { margin: 0 0 4px; font-size: 1rem; color: var(--text); text-transform: none; letter-spacing: 0; }
.empty p { margin: 0; }

.skeleton {
  background: linear-gradient(90deg, var(--surface) 0%, var(--bg-2) 50%, var(--surface) 100%);
  background-size: 200% 100%; animation: shimmer 1.4s infinite;
  border-radius: var(--r-2);
}
.skeleton-card { height: 168px; }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ===== INE view ===== */
.ine-summary {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--s-3);
  margin-bottom: var(--s-4);
}
.ine-stat {
  background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r-3);
  padding: var(--s-3) var(--s-4); box-shadow: var(--sh-1);
}
.ine-stat .label { font-size: .72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; }
.ine-stat .value { font-size: 1.35rem; font-weight: 600; letter-spacing: -.01em; margin-top: 2px; font-variant-numeric: tabular-nums; }

.month-list { display: grid; gap: var(--s-2); }
.month-row {
  display: grid; grid-template-columns: 1fr auto; gap: var(--s-3);
  align-items: center;
  background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r-3);
  padding: var(--s-3) var(--s-4); box-shadow: var(--sh-1);
}
.month-row .month-meta strong { font-size: 1rem; letter-spacing: -.01em; }
.month-row .month-meta .sub { color: var(--text-muted); font-size: .82rem; margin-top: 2px; }
.month-row .month-actions { display: flex; gap: 6px; }
.month-row .month-actions .btn { padding: 8px 12px; min-height: 38px; font-size: .85rem; }

.preview {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--r-2);
  padding: var(--s-4); overflow-x: auto; max-height: 50vh; overflow-y: auto;
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* ===== Bottom nav (mobile) ===== */
.bottomnav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; align-items: stretch;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-top: 1px solid var(--border-soft);
}
.bottomnav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--text-muted); font-size: .72rem; font-weight: 500;
  text-decoration: none;
  transition: color var(--t-fast);
}
.bottomnav a svg { opacity: .8; }
.bottomnav a.is-active { color: var(--accent); }
.bottomnav a.is-active svg { opacity: 1; }
.bottomnav a:hover { color: var(--text); text-decoration: none; }
.bottomnav a:hover svg { opacity: 1; }

/* ===== Toasts ===== */
.toasts {
  position: fixed; left: 0; right: 0; bottom: calc(var(--bottomnav-h) + 12px + env(safe-area-inset-bottom));
  display: grid; gap: 8px; justify-items: center;
  z-index: 50; pointer-events: none;
}
.toast {
  background: var(--text); color: var(--bg); border-radius: var(--r-pill);
  padding: 10px 18px; font-size: .85rem; font-weight: 500;
  box-shadow: var(--sh-3); max-width: calc(100% - 32px);
  animation: toast-in .25s ease-out;
  pointer-events: auto;
}
.toast.is-err { background: var(--err); color: white; }
.toast.is-ok  { background: var(--ok);  color: white; }
.toast.is-warn{ background: var(--warn);color: white; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== Responsive ===== */
@media (min-width: 760px) {
  .view { padding: var(--s-6); padding-bottom: var(--s-8); }
  .ine-summary { grid-template-columns: repeat(4, 1fr); }

  /* Bookings: switch to table layout on desktop */
  .booking-list.as-table { display: block; background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r-3); overflow: hidden; box-shadow: var(--sh-1); }
  .booking-list.as-table .booking { border-radius: 0; border: 0; border-bottom: 1px solid var(--border-soft); padding: 12px var(--s-4); box-shadow: none; display: grid; grid-template-columns: 130px 70px 90px 60px 1.2fr 100px 90px auto; align-items: center; gap: var(--s-3); }
  .booking-list.as-table .booking:last-child { border-bottom: 0; }
  .booking-list.as-table .booking.is-missing { border-left: 3px solid var(--warn); }
  .booking-list.as-table .booking-head { display: contents; }
  .booking-list.as-table .booking-nights { display: none; }
  .booking-list.as-table .booking-fields { display: contents; }
  .booking-list.as-table .booking-fields label { display: block; }
  .booking-list.as-table .booking-fields label > span { display: none; }
  .booking-list.as-table .booking-foot { display: contents; }
  .booking-list.as-table .booking-foot .source-tag { justify-self: end; }
  .booking-list.as-table .booking-foot .save-state { margin-left: 0; }
  .booking-list.as-table .booking-list-head {
    display: grid; grid-template-columns: 130px 70px 90px 60px 1.2fr 100px 90px auto;
    align-items: center; gap: var(--s-3);
    padding: 10px var(--s-4); background: var(--surface-2);
    border-bottom: 1px solid var(--border); font-size: .72rem;
    color: var(--text-muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 600;
  }

  /* Hide bottom nav on desktop, show top nav inside appbar */
  .bottomnav { display: none; }
  .appbar-nav { display: flex; gap: 4px; }
  .appbar-nav a {
    padding: 8px 14px; border-radius: var(--r-pill);
    color: var(--text-muted); text-decoration: none; font-weight: 500; font-size: .9rem;
    transition: background var(--t-fast), color var(--t-fast);
  }
  .appbar-nav a:hover { background: var(--bg-2); color: var(--text); text-decoration: none; }
  .appbar-nav a.is-active { background: var(--primary); color: var(--primary-ink); }
  .toasts { bottom: 16px; }
}
@media (max-width: 759px) {
  .appbar-nav { display: none; }
}

.booking-list-head { display: none; }
