/* ==========================================================================
   Dashboard GA4 - estilos v3 (responsive)
   ========================================================================== */

:root {
  --bg:            #fbf9f6;
  --bg-card:       #ffffff;
  --border:        #ece8e1;
  --border-strong: #e0dcd3;
  --text:          #1c1c1c;
  --text-dim:      #6f6b65;
  --text-mute:     #a8a39b;

  --pink:          #e57588;
  --pink-soft:     #f6d4dc;
  --pink-bg:       #fce9ed;
  --mint:          #7fc4ad;
  --mint-soft:     #cfe6dd;
  --violet:        #a097c9;
  --violet-soft:   #d8d4ea;
  --amber:         #e3b56a;
  --amber-soft:    #f1ddb6;
  --peach:         #e9a48c;
  --sand:          #d9cdb3;

  --green-up:      #4a9778;
  --red-down:      #c4596a;

  --radius:        14px;
  --radius-sm:     10px;
  --shadow:        0 1px 2px rgba(60,40,20,.04), 0 1px 1px rgba(60,40,20,.03);

  --font-display:  'Fraunces', Georgia, serif;
  --font-body:     'Inter Tight', -apple-system, BlinkMacSystemFont, sans-serif;

  --sidebar-w:     56px;
  --topbar-mobile-h: 56px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea, select { font-family: inherit; }
.hidden { display: none !important; }
.ta-r { text-align: right; }
.mt-32 { margin-top: 32px; }

/* ----- Sidebar ------------------------------------------------------------ */
.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; align-items: center;
  padding: 16px 0;
  z-index: 50;
  transition: transform .25s ease;
}
.sidebar-logo {
  width: 32px; height: 32px;
  border-radius: 8px;
  background: var(--pink);
  color: #fff;
  display: grid; place-items: center;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 18px;
  margin-bottom: 24px;
}
.sidebar-nav { display: flex; flex-direction: column; gap: 4px; }
.sidebar-bottom { margin-top: auto; display: flex; flex-direction: column; align-items: center; gap: 12px; }

.nav-btn {
  width: 36px; height: 36px;
  border-radius: 8px;
  display: grid; place-items: center;
  color: var(--text-mute);
  transition: background .15s, color .15s;
}
.nav-btn svg { width: 18px; height: 18px; }
.nav-btn:hover { background: rgba(0,0,0,.04); color: var(--text); }
.nav-btn.active { background: rgba(229,117,136,.12); color: var(--pink); }

.user-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--text);
  color: #fff;
  display: grid; place-items: center;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .03em;
}

.sidebar-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 49;
  opacity: 0; pointer-events: none;
  transition: opacity .25s;
}
.sidebar-backdrop.open { opacity: 1; pointer-events: auto; }

/* ----- Mobile top bar (oculta en desktop) ----- */
.mobile-topbar { display: none; }
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 8px;
  display: grid; place-items: center;
  color: var(--text);
}
.icon-btn:hover { background: rgba(0,0,0,.05); }
.icon-btn svg { width: 20px; height: 20px; }

/* ----- Main / topbar ----------------------------------------------------- */
.main { margin-left: var(--sidebar-w); padding: 20px 28px 60px; }
.view { display: none; }
.view.active { display: block; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px;
  gap: 16px;
  flex-wrap: wrap;
}
.topbar-left { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.brand {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 19px;
  letter-spacing: -.01em;
}
.slash { color: var(--text-mute); }
.page-sub { color: var(--text-dim); }

.property-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  max-width: 280px;
}
.property-pill > span:nth-child(2) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.property-pill svg { width: 12px; height: 12px; color: var(--text-mute); flex-shrink: 0; }
.property-pill .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--mint);
  flex-shrink: 0;
}
.property-pill:hover { border-color: var(--border-strong); }

.topbar-right { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  color: var(--text);
  transition: background .15s, border-color .15s;
  white-space: nowrap;
}
.btn-pill svg { width: 14px; height: 14px; color: var(--text-dim); }
.btn-pill:hover { background: #fff; border-color: var(--border-strong); }
.btn-pill.btn-dark {
  background: var(--text);
  border-color: var(--text);
  color: #fff;
}
.btn-pill.btn-dark svg { color: #fff; }
.btn-pill.btn-dark:hover { background: #000; }
.btn-pill.btn-danger {
  background: #fff;
  border-color: var(--pink-soft);
  color: var(--red-down);
}
.btn-pill--static { cursor: default; opacity: .85; }
.btn-pill--static:hover { background: var(--bg-card); }
.btn-pill.is-loading { opacity: .7; cursor: progress; }
.btn-pill.is-loading svg:first-of-type { animation: spin 1s linear infinite; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* ----- Dropdown ---------------------------------------------------------- */
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 180px;
  max-height: 400px;
  overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 24px rgba(60,40,20,.08);
  list-style: none;
  padding: 4px;
  display: none;
  z-index: 30;
}
.dropdown-menu.open { display: block; }
.dropdown-menu--wide { min-width: 260px; left: 0; right: auto; }
.dropdown-menu li {
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: background .12s;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.dropdown-menu li:hover { background: var(--bg); }
.dropdown-menu li.active { background: rgba(229,117,136,.08); }
.dropdown-menu li .li-sub { font-size: 11px; color: var(--text-mute); }
.dropdown-menu li.empty { color: var(--text-mute); cursor: default; font-style: italic; }
.dropdown-menu li.empty:hover { background: transparent; }

/* ----- KPI grid ---------------------------------------------------------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 20px;
}
.kpi {
  background: var(--bg-card);
  padding: 14px 16px;
  position: relative;
  min-height: 110px;
}
.kpi-label { color: var(--text-dim); font-size: 12px; margin-bottom: 8px; }
.kpi-value {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 26px;
  letter-spacing: -.02em;
  line-height: 1.1;
}
.kpi-delta {
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 12px;
  margin-top: 6px;
}
.kpi-delta.up   { color: var(--green-up); }
.kpi-delta.down { color: var(--red-down); }
.kpi-spark {
  position: absolute;
  right: 12px; bottom: 12px;
  width: 70px; height: 24px;
}

/* ----- Cards ------------------------------------------------------------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.card-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  margin-bottom: 12px;
  gap: 8px;
  flex-wrap: wrap;
}
.card-title {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 15px;
  letter-spacing: -.005em;
}
.card-sub { color: var(--text-dim); font-size: 12px; }
.card-sub-inline {
  color: var(--text-dim);
  font-family: var(--font-body);
  font-weight: 400;
  font-size: 12px;
  margin-left: 6px;
}

.legend-dot {
  display: inline-block; width: 14px; height: 2px;
  vertical-align: middle; margin-right: 6px;
}
.legend-pink { background: var(--pink); }
.legend-label { font-size: 12px; color: var(--text-dim); margin-right: 12px; }
.legend-value { font-family: var(--font-display); font-size: 15px; font-weight: 500; }

/* ----- Charts row -------------------------------------------------------- */
.charts-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}
.chart-wrap { height: 260px; }
.line-chart { width: 100%; height: 100%; display: block; }

/* ----- Three-col row ----------------------------------------------------- */
.three-col {
  display: grid;
  grid-template-columns: 1fr 1.2fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}

/* ----- Devices ---------------------------------------------------------- */
.device-block { display: grid; grid-template-columns: 130px 1fr; gap: 18px; align-items: center; }
.donut-wrap svg { width: 100%; height: 130px; }
.device-list { list-style: none; display: flex; flex-direction: column; gap: 12px; }
.device-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.device-name { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.device-name .sw { width: 8px; height: 8px; border-radius: 50%; }
.device-meta { font-size: 11px; color: var(--text-mute); margin-top: 2px; }
.device-pct { font-family: var(--font-display); font-size: 14px; font-weight: 500; }

/* ----- Bar lists --------------------------------------------------------- */
.bar-list { list-style: none; display: flex; flex-direction: column; gap: 14px; padding-top: 4px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 130px) 1fr 60px; gap: 12px; align-items: center; }
.bar-label { font-size: 13px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track {
  height: 6px;
  background: #f4f1ec;
  border-radius: 3px;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  border-radius: 3px;
  background: var(--pink);
  width: 0%;
  transition: width .6s ease;
}
.bar-list--violet .bar-fill { background: var(--violet); }
.bar-value { text-align: right; font-family: var(--font-display); font-size: 13px; }

/* ----- Tables ------------------------------------------------------------ */
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table thead th {
  text-align: left;
  font-weight: 500;
  font-size: 11px;
  letter-spacing: .04em;
  color: var(--text-mute);
  border-bottom: 1px solid var(--border);
  padding: 8px 4px;
  background: #faf6f0;
  white-space: nowrap;
}
.data-table tbody td {
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
}
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table td.num { font-family: var(--font-display); font-weight: 500; }
.data-table td.ellipsis { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.delta-up   { color: var(--green-up); }
.delta-down { color: var(--red-down); }

/* ----- Detail view ------------------------------------------------------- */
.detail-content { display: flex; flex-direction: column; gap: 16px; }
.detail-content .card { padding: 18px 20px; }

/* ----- Settings ---------------------------------------------------------- */
.settings-wrap { max-width: 760px; display: flex; flex-direction: column; gap: 16px; }
.settings-card { padding: 24px 28px; }
.settings-h2 {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 18px;
  margin-bottom: 8px;
}
.settings-help {
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 16px;
  line-height: 1.55;
}
.field-label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin: 12px 0 6px;
}
.field-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: #fff;
  font-size: 13px;
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.field-input:focus {
  outline: none;
  border-color: var(--pink);
  box-shadow: 0 0 0 3px var(--pink-bg);
}
.field-textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; resize: vertical; }

.settings-actions {
  display: flex; gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
}
.settings-status {
  margin-top: 16px;
  font-size: 13px;
  min-height: 20px;
}
.settings-status.ok    { color: var(--green-up); }
.settings-status.err   { color: var(--red-down); }
.settings-status.info  { color: var(--text-dim); }

/* ----- Properties list (settings) -------------------------------------- */
.properties-list { display: flex; flex-direction: column; gap: 10px; }
.property-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}
.property-item-info { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.property-item-id { font-size: 11px; color: var(--text-mute); font-family: ui-monospace, monospace; }
.property-item input.field-input { padding: 6px 10px; font-size: 13px; }
.property-item-actions { display: flex; gap: 6px; }
.btn-mini {
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  font-size: 11px;
  cursor: pointer;
  color: var(--text-dim);
}
.btn-mini:hover { border-color: var(--border-strong); color: var(--text); }
.btn-mini.btn-mini--danger { color: var(--red-down); border-color: var(--pink-soft); }

.manual-add { margin-top: 16px; }
.manual-add summary { cursor: pointer; color: var(--text-dim); font-size: 13px; padding: 8px 0; }
.manual-add summary:hover { color: var(--text); }
.manual-add-form { display: flex; flex-direction: column; gap: 8px; padding: 12px 0; }
.manual-add-form .field-input { padding: 8px 10px; }

.muted-block {
  padding: 20px;
  text-align: center;
  color: var(--text-mute);
  font-style: italic;
  background: var(--bg);
  border-radius: var(--radius-sm);
  font-size: 13px;
}

/* ----- Status box -------------------------------------------------------- */
.status-box {
  margin: 24px 0;
  padding: 16px 18px;
  border-radius: var(--radius);
  background: var(--bg-card);
  border: 1px dashed var(--border-strong);
  color: var(--text-dim);
  font-size: 13px;
}
.status-box.err  { border-style: solid; border-color: var(--pink-soft); color: var(--red-down); background: #fff7f8; }
.status-box.warn { border-style: solid; border-color: var(--amber-soft); color: #8a5a1e; background: #fdf6e7; }

/* ----- Tooltip de gráficos ---------------------------------------------- */
.chart-tooltip {
  position: absolute;
  display: none;
  background: var(--text);
  color: #fff;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.4;
  pointer-events: none;
  z-index: 60;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
}
.chart-tooltip strong {
  font-family: var(--font-display);
  font-weight: 500;
}
.line-chart .hot { cursor: crosshair; }

/* ----- Sin datos -------------------------------------------------------- */
.muted { color: var(--text-mute); font-style: italic; text-align: center; padding: 16px !important; }


/* ==========================================================================
   RESPONSIVE
   ========================================================================== */

/* Tablet (≤1200px): grid de KPI a 4 columnas, charts a 1 columna */
@media (max-width: 1200px) {
  .kpi-grid    { grid-template-columns: repeat(4, 1fr); }
  .charts-row  { grid-template-columns: 1fr; }
  .three-col   { grid-template-columns: 1fr 1fr; }
  .three-col > .card:last-child { grid-column: span 2; }
}

/* Móvil grande / tablet pequeño (≤900px): todo en una columna */
@media (max-width: 900px) {
  .main { padding: 16px 16px 60px; }
  .three-col   { grid-template-columns: 1fr; }
  .three-col > .card:last-child { grid-column: auto; }
  .kpi-grid    { grid-template-columns: repeat(2, 1fr); }
  .kpi         { min-height: 96px; padding: 12px 14px; }
  .kpi-value   { font-size: 22px; }
  .chart-wrap  { height: 220px; }
}

/* Móvil (≤640px): sidebar colapsa a overlay, top bar mobile aparece */
@media (max-width: 640px) {
  /* Sidebar oculta por defecto, se muestra como overlay */
  .sidebar {
    transform: translateX(-100%);
    box-shadow: 4px 0 16px rgba(0,0,0,.15);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop { display: block; }

  /* Main ocupa todo el ancho, con espacio para top bar */
  .main {
    margin-left: 0;
    padding: 0 12px 60px;
  }

  /* Top bar móvil visible */
  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--topbar-mobile-h);
    margin: 0 -12px 12px;
    padding: 0 12px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 40;
  }
  .mobile-topbar .brand { font-size: 16px; }

  /* Topbar desktop: oculta el título grande y deja solo el selector */
  .topbar { margin-bottom: 16px; gap: 8px; }
  .topbar-left .brand { display: none; }
  .topbar-left .slash { display: none; }
  .topbar-right .btn-pill span:not(#rangeLabel) { display: inline; }
  /* PDF y Actualizar pasan a icon-only */
  #btnPdf span,
  #btnRefresh span { display: none; }
  .btn-pill--static { display: none; }

  /* Property pill puede crecer */
  .property-pill { max-width: 100%; flex: 1; }

  /* Tablas: scroll horizontal */
  .data-table { font-size: 12px; min-width: 480px; }

  /* KPI grid en móvil: 2 columnas */
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .kpi-spark { width: 50px; }

  /* Card padding más compacto */
  .card { padding: 14px 16px; }

  /* Settings */
  .settings-card { padding: 18px 20px; }
  .property-item { grid-template-columns: 1fr; }
  .property-item-actions { justify-content: flex-end; }
}

/* Móvil pequeño (≤400px) */
@media (max-width: 400px) {
  .kpi-grid    { grid-template-columns: 1fr; }
  .kpi         { min-height: 80px; }
  .topbar-right { width: 100%; justify-content: space-between; }
}

/* ----- Print ------------------------------------------------------------ */
@media print {
  .sidebar, .sidebar-backdrop, .mobile-topbar,
  .topbar-right, .status-box { display: none !important; }
  .main { margin-left: 0; padding: 12px; }
  body { background: white; }
  .card { break-inside: avoid; box-shadow: none; border-color: #ddd; }
  .kpi-grid, .charts-row, .three-col { break-inside: avoid; }
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
  @page { size: A4 landscape; margin: 10mm; }
}
