/* Behavior Analytics dashboard (analytics-dash.js).
   Uses the portal's tokens from style.css; nothing here applies outside the
   analytics tab except the popover and the report overlay, which are appended
   to <body> and are namespaced .adash-*. */

/* ------------------------------------------------------------------ toolbar */
.adash-top { margin-bottom: 20px; }
.adash-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 12px;
}
.adash-bar-right { display: flex; flex-wrap: wrap; gap: 8px; margin-left: auto; }

.adash-seg {
  display: inline-flex;
  padding: 3px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-width: 100%;
  overflow-x: auto;
  scrollbar-width: none;
}
.adash-seg::-webkit-scrollbar { display: none; }
.adash-seg-btn {
  padding: 7px 12px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease;
}
.adash-seg-btn:hover:not(:disabled) { color: var(--text); }
.adash-seg-btn.is-on { background: var(--surface-hover); color: var(--accent); font-weight: 600; }
.adash-seg-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.adash-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 7px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.adash-btn:hover:not(:disabled) { background: var(--surface-hover); border-color: var(--border-strong); }
.adash-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.adash-btn-primary { background: var(--accent); border-color: var(--accent); color: var(--bg); font-weight: 600; }
.adash-btn-primary:hover:not(:disabled) { background: #F2D693; border-color: #F2D693; }
.adash-btn-danger { background: transparent; border-color: var(--error); color: var(--error); }
.adash-view-k { color: var(--text-muted); font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; }
.adash-view-name { max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.adash-caret {
  width: 0; height: 0;
  border-left: 4px solid transparent; border-right: 4px solid transparent;
  border-top: 5px solid var(--text-secondary);
}

.adash-custom { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; }
.adash-date { display: flex; flex-direction: column; gap: 2px; font-size: 11px; color: var(--text-muted); }
.adash-date input {
  min-height: 36px;
  padding: 6px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  color-scheme: dark;
}

.adash-rangeline {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 16px;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-secondary);
}
.adash-sync { color: var(--text-muted); }
.adash-loading .adash-kpis { opacity: 0.45; }

.adash-kpis {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 12px;
  margin-top: 14px;
  transition: opacity 0.2s ease;
}
.adash-kpi {
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  min-width: 0;
}
.adash-kpi-v {
  font-family: var(--font-display);
  font-size: 24px;
  font-weight: 600;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}
.adash-kpi-k {
  margin-top: 2px;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

/* --------------------------------------------------------------------- grid */
.adash-grid {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 16px;
  align-items: start;
}
.adash-grid > .card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  grid-column: span 6;
  padding: 0;
  overflow: hidden;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
/* The grid sets display, which would otherwise beat the hidden attribute. */
.adash-grid > .card[hidden] { display: none; }
.adash-grid > .card.adash-w-3  { grid-column: span 3; }
.adash-grid > .card.adash-w-4  { grid-column: span 4; }
.adash-grid > .card.adash-w-6  { grid-column: span 6; }
.adash-grid > .card.adash-w-8  { grid-column: span 8; }
.adash-grid > .card.adash-w-12 { grid-column: span 12; }

.adash-head {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 8px 8px 6px;
  border-bottom: 1px solid var(--border);
}
.adash-head .card-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.adash-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  color: var(--text-muted);
  transition: background 0.15s ease, color 0.15s ease;
}
.adash-icon:hover { background: var(--surface-hover); color: var(--text); }
.adash-grip { cursor: grab; touch-action: none; }
.adash-grip:active { cursor: grabbing; }

.adash-grid > .card > .card-body {
  padding: 14px 16px 16px;
  min-height: 0;
  overflow: hidden;
}
.adash-grid > .card.adash-min > .card-body,
.adash-grid > .card.adash-min > .adash-resize { display: none; }
.adash-grid > .card.adash-min > .adash-head { border-bottom-color: transparent; }

/* A section given an explicit height scrolls inside it, and a chart fills it. */
.adash-grid > .card.adash-fixed-h > .card-body { overflow: auto; }
.adash-grid > .card.adash-fixed-h .data-table { max-height: none; }
.adash-grid > .card.adash-fixed-h .chart-wrap { flex: 1 1 auto; height: auto; min-height: 120px; }

.adash-resize {
  position: absolute;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  width: 22px;
  height: 22px;
  padding: 3px;
  color: var(--text-muted);
  cursor: nwse-resize;
  touch-action: none;
  opacity: 0;
  transition: opacity 0.15s ease, color 0.15s ease;
}
.adash-grid > .card:hover > .adash-resize,
.adash-grid > .card.adash-resizing > .adash-resize { opacity: 1; }
.adash-resize:hover { color: var(--accent); }

.adash-grid > .card.adash-dragging { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 12px 32px rgba(0, 0, 0, 0.45); opacity: 0.92; }
.adash-grid > .card.adash-resizing { border-color: var(--accent); }
.adash-grid > .card.adash-flash { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
body.adash-busy { user-select: none; -webkit-user-select: none; }

.adash-empty {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 48px 16px;
  border: 1px dashed var(--border-strong);
  border-radius: 8px;
  color: var(--text-secondary);
}

/* ------------------------------------------------------------------ popover */
.adash-pop {
  position: fixed;
  z-index: 300;
  width: 280px;
  max-width: calc(100vw - 16px);
  max-height: min(560px, calc(100vh - 16px));
  overflow-y: auto;
  padding: 10px;
  background: var(--surface-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
}
.adash-pop.adash-pop-wide { width: 360px; }
.adash-pop-title {
  padding: 2px 6px 8px;
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
}
.adash-pop-group {
  padding: 10px 6px 4px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.adash-pop-list { display: flex; flex-direction: column; gap: 2px; }
.adash-pop-list + .adash-pop-form, .adash-chips + .adash-pop-list { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.adash-pop-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 38px;
  padding: 8px;
  border-radius: 6px;
  font-size: 13px;
  text-align: left;
  color: var(--text);
}
.adash-pop-item:hover:not(:disabled) { background: var(--surface-hover); }
.adash-pop-item:disabled { opacity: 0.4; cursor: not-allowed; }
.adash-pop-item.adash-danger, .adash-link.adash-danger { color: var(--error); }
.adash-pop-meta { margin-left: auto; font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.adash-radio {
  flex-shrink: 0;
  width: 14px; height: 14px;
  border: 2px solid var(--border-strong);
  border-radius: 50%;
}
.adash-pop-item.is-on .adash-radio { border-color: var(--accent); background: radial-gradient(var(--accent) 45%, transparent 50%); }

.adash-switch {
  position: relative;
  flex-shrink: 0;
  width: 32px; height: 18px;
  background: var(--border-strong);
  border-radius: 9px;
  transition: background 0.15s ease;
}
.adash-switch::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 14px; height: 14px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: transform 0.15s ease, background 0.15s ease;
}
.adash-toggle.is-on .adash-switch { background: var(--accent-dim); }
.adash-toggle.is-on .adash-switch::after { transform: translateX(14px); background: #fff; }
.adash-toggle-text { display: flex; flex-direction: column; min-width: 0; }
.adash-toggle-text small { font-size: 11px; line-height: 1.35; color: var(--text-secondary); }

.adash-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 2px 6px; }
.adash-chip {
  min-height: 32px;
  padding: 5px 11px;
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  font-size: 12px;
  color: var(--text-secondary);
}
.adash-chip:hover { color: var(--text); }
.adash-chip.is-on { border-color: var(--accent); color: var(--accent); background: rgba(232, 199, 122, 0.08); }

.adash-pop-form {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 6px;
}
.adash-pop-form label { flex: 1; display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--text-muted); min-width: 0; }
.adash-pop-form input, .adash-report-form input {
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  font-size: 14px;
}
.adash-pop-form input:focus, .adash-report-form input:focus, .adash-date input:focus { outline: none; border-color: var(--accent); }
.adash-pop-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
  padding: 8px 6px 2px;
  border-top: 1px solid var(--border);
}
.adash-pop-ask { flex: 1 1 100%; font-size: 13px; }
.adash-link { font-size: 12px; color: var(--text-secondary); text-decoration: underline; text-underline-offset: 2px; }
.adash-link:hover:not(:disabled) { color: var(--text); }
.adash-link:disabled { opacity: 0.4; cursor: not-allowed; }

.adash-top button:focus-visible, .adash-grid .adash-icon:focus-visible, .adash-pop button:focus-visible,
.adash-report-side button:focus-visible, .adash-check input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ------------------------------------------------------------------- report */
.adash-report-overlay {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  background: #0B0D10;
}
body.adash-report-open { overflow: hidden; }
.adash-report-side {
  flex: 0 0 320px;
  padding: 20px;
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.adash-report-h { font-family: var(--font-display); font-size: 20px; font-weight: 600; margin-bottom: 12px; }
.adash-report-form { display: flex; flex-direction: column; gap: 10px; }
.adash-report-form label { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--text-muted); }
.adash-report-hint { margin: 8px 6px 0; font-size: 12px; line-height: 1.45; color: var(--text-secondary); }
.adash-report-list { display: flex; flex-direction: column; }
.adash-check { display: flex; align-items: center; gap: 10px; min-height: 34px; padding: 4px 6px; font-size: 13px; border-radius: 6px; cursor: pointer; }
.adash-check:hover { background: var(--surface-hover); }
.adash-check input { width: 16px; height: 16px; accent-color: var(--accent); }
.adash-check small { margin-left: auto; font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-muted); }
.adash-report-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }

.adash-report-stage { flex: 1; min-width: 0; overflow: auto; padding: 24px; }
.adash-report-page {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 44px 48px;
  background: #fff;
  color: #1a1c20;
  font-family: var(--font-ui);
  font-size: 12.5px;
  line-height: 1.5;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
}
.rep-empty { color: #555; }
.rep-brand { font-family: var(--font-display); font-size: 13px; font-weight: 600; letter-spacing: 0.32em; color: #1a1c20; }
.rep-brand span { display: block; margin-top: 2px; font-family: var(--font-ui); font-size: 9.5px; font-weight: 400; letter-spacing: 0.14em; text-transform: uppercase; color: #6b7280; }
.rep-head { padding-bottom: 14px; border-bottom: 2px solid #b8923a; }
.rep-head h1 { margin-top: 18px; font-family: var(--font-display); font-size: 26px; font-weight: 600; line-height: 1.2; text-wrap: balance; }
.rep-meta { margin-top: 6px; font-size: 12px; color: #4b5563; }
.rep-meta span { margin: 0 4px; color: #b8923a; }
.rep-kpis { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; margin: 18px 0 6px; }
.rep-kpis div { padding: 10px 10px 9px; border: 1px solid #e5e7eb; border-radius: 6px; }
.rep-kpis b { display: block; font-family: var(--font-display); font-size: 20px; font-weight: 600; line-height: 1.15; font-variant-numeric: tabular-nums; }
.rep-kpis span { font-size: 9px; letter-spacing: 0.06em; text-transform: uppercase; color: #6b7280; }
.rep-sec { margin-top: 22px; break-inside: avoid; page-break-inside: avoid; }
.rep-sec h2 { font-family: var(--font-display); font-size: 15px; font-weight: 600; margin-bottom: 4px; }
.rep-note, .rep-more { font-size: 11.5px; color: #4b5563; }
.rep-more { margin-top: 4px; }
.rep-table { width: 100%; margin-top: 6px; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.rep-table th { padding: 4px 8px 4px 0; border-bottom: 1px solid #d1d5db; font-size: 9.5px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; text-align: left; color: #6b7280; }
.rep-table td { padding: 4px 8px 4px 0; border-bottom: 1px solid #f0f1f3; vertical-align: middle; overflow-wrap: anywhere; }
.rep-table .n { text-align: right; white-space: nowrap; width: 1%; padding-left: 12px; }
.rep-table .b { width: 34%; padding-right: 0; }
.rep-table .b i { display: block; height: 9px; background: #b8923a; border-radius: 2px; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.rep-foot { margin-top: 26px; padding-top: 10px; border-top: 1px solid #d1d5db; font-size: 10px; line-height: 1.5; color: #6b7280; }

/* --------------------------------------------------------------- responsive */
@media (max-width: 1100px) {
  .adash-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .adash-grid > .card.adash-w-3, .adash-grid > .card.adash-w-4 { grid-column: span 3; }
  .adash-grid > .card.adash-w-6, .adash-grid > .card.adash-w-8, .adash-grid > .card.adash-w-12 { grid-column: span 6; }
  .adash-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 700px) {
  .adash-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .adash-grid > .card[class] { grid-column: auto; }
  .adash-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .adash-kpi-v { font-size: 20px; }
  .adash-bar-right { margin-left: 0; width: 100%; }
  .adash-bar-right .adash-btn { flex: 1; justify-content: center; }
  .adash-seg { width: 100%; }
  .adash-seg-btn { flex: 1 1 auto; min-height: 40px; padding: 7px 6px; font-size: 12.5px; }
  .adash-icon { width: 40px; height: 40px; }
  /* Touch has no hover, and width is fixed at one column: height is set from
     the section menu instead of a corner drag. */
  .adash-resize { display: none; }
  .adash-pop.is-sheet {
    left: 0; right: 0; bottom: 0; top: auto;
    width: auto; max-width: none;
    max-height: 80vh;
    padding: 14px 12px calc(14px + env(safe-area-inset-bottom));
    border-radius: 14px 14px 0 0;
    border-bottom: none;
  }
  .adash-pop-item { min-height: 44px; }
  .adash-chip { min-height: 40px; padding: 8px 14px; }
  .adash-report-overlay { flex-direction: column; overflow-y: auto; }
  .adash-report-side { flex: none; border-right: none; border-bottom: 1px solid var(--border); }
  .adash-report-stage { overflow: visible; padding: 12px; }
  .adash-report-page { padding: 24px 18px; }
  .rep-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (prefers-reduced-motion: reduce) {
  .adash-top *, .adash-grid *, .adash-pop * { transition: none !important; }
}

/* -------------------------------------------------------------------- print */
/* Only the report page prints. @page margin 0 is what stops the browser adding
   its own header and footer (the date and the admin URL); the page supplies its
   own margins as padding instead. */
@media print {
  @page { margin: 0; }
  body.adash-report-open { overflow: visible !important; background: #fff !important; }
  body.adash-report-open > *:not(.adash-report-overlay) { display: none !important; }
  .adash-report-overlay { position: static; display: block; background: #fff; }
  .adash-report-side { display: none; }
  .adash-report-stage { overflow: visible; padding: 0; }
  .adash-report-page { max-width: none; box-shadow: none; padding: 14mm 15mm; }
  .rep-kpis { grid-template-columns: repeat(6, minmax(0, 1fr)); }
}
