/* ==========================================================================
   FINICHE — ACCOUNT, SIGN-IN, PANEL STATES & RESPONSIVE LAYER
   Loaded after styles.css and built only from its tokens. Signature element: the
   segmented one-time-code field. Everything here works without inline styles,
   so the CSP needs no 'unsafe-inline'.
   ========================================================================== */

:root {
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --otp-cell: 44px;
  --otp-gap: 8px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* Shared keyboard focus ring for the custom interactive surfaces */
.product-card:focus-visible,
.row-clickable:focus-visible,
.tab-btn:focus-visible,
.pill:focus-visible,
.chip:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

.context-pill:focus-within,
.lang-capsule:focus-within { border-color: var(--border-focus); }

/* The safe-ratio bar starts empty; app.js sets the real width once data arrives */
.kpi-progress-bar { width: 0; }

/* Counts with nothing to report disappear instead of showing a made-up number */
.tab-count:empty { display: none; }

/* ---------------------------------------------------------------------------
   Buttons: one small size and a text-link style shared by every panel
   --------------------------------------------------------------------------- */
.btn-sm {
  height: 28px;
  padding-inline: 10px;
  font-size: 12px;
}

.btn-primary:disabled,
.btn-outline:disabled,
.btn-ghost-sm:disabled,
.btn-ghost-xs:disabled,
.btn-link:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-link {
  background: none;
  border: none;
  padding: 2px 0;
  color: var(--accent-text);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: transparent;
}

.btn-link:hover:not(:disabled) { text-decoration-color: currentColor; }

.btn-link:disabled {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------------------
   Account area (top bar)
   --------------------------------------------------------------------------- */
.account-area {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.account-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: 34px;
  padding: 2px 2px 2px 4px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  background: var(--bg-elevated);
  min-width: 0;
}

.account-initial {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  background: var(--accent);
  color: #ffffff;
  font-size: 12px;
  font-weight: 700;
}

.account-email {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-primary);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn-sm {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: transparent;
  border-color: transparent;
}

.icon-btn-sm:hover { background: var(--bg-hover); }

/* ---------------------------------------------------------------------------
   Sign-in dialog
   --------------------------------------------------------------------------- */
.signin-dialog {
  margin: auto;
  width: min(420px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--bg-surface);
  color: var(--text-primary);
  box-shadow: 0 24px 64px -12px rgba(0, 0, 0, 0.6);
}

.signin-dialog[open] { animation: signin-in 180ms var(--ease-out); }

.signin-dialog::backdrop {
  background: rgba(9, 9, 11, 0.7);
  backdrop-filter: blur(4px);
}

@keyframes signin-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
}

.signin-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-5) 0;
}

.signin-title {
  flex: 1;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.signin-lead {
  margin-top: var(--space-1);
  color: var(--text-secondary);
  font-size: 13px;
}

.signin-reason {
  margin: var(--space-4) var(--space-5) 0;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  border: 1px solid var(--amber-border);
  background: var(--amber-bg);
  color: var(--amber-text);
  font-size: 12.5px;
  font-weight: 600;
}

.signin-step {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-5);
}

.signin-step label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}

.signin-step input[type='email'] {
  flex: none; /* .search-input.full sets flex: 1, which collapses height in this column */
  height: 40px;
  padding-inline: var(--space-3);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 14px;
}

.signin-step input[aria-invalid='true'] { border-color: var(--danger-text); }

#signin-code-sent { overflow-wrap: anywhere; }

/* Signature: six mono cells drawn behind one real input, so paste, autofill
   (autocomplete="one-time-code") and screen readers all see a single field. */
.otp-input {
  --otp-width: calc(var(--otp-cell) * 6 + var(--otp-gap) * 5);
  width: var(--otp-width);
  max-width: 100%;
  height: 52px;
  margin-block: var(--space-1);
  padding: 0 0 0 calc(var(--otp-cell) / 2 - 0.3em);
  border: none;
  border-radius: 0;
  background-color: transparent;
  background-image: linear-gradient(
    to right,
    var(--bg-elevated) 0,
    var(--bg-elevated) var(--otp-cell),
    transparent var(--otp-cell),
    transparent calc(var(--otp-cell) + var(--otp-gap))
  );
  background-size: calc(var(--otp-cell) + var(--otp-gap)) 100%;
  background-repeat: repeat-x;
  box-shadow: none;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 600;
  letter-spacing: calc(var(--otp-cell) + var(--otp-gap) - 0.6em);
  caret-color: var(--accent-text);
}

.otp-input:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 4px;
  border-radius: var(--radius-sm);
}

.otp-input[aria-invalid='true'] {
  background-image: linear-gradient(
    to right,
    var(--danger-bg) 0,
    var(--danger-bg) var(--otp-cell),
    transparent var(--otp-cell),
    transparent calc(var(--otp-cell) + var(--otp-gap))
  );
}

.field-error {
  display: flex;
  gap: 6px;
  align-items: flex-start;
  color: var(--danger-text);
  font-size: 12.5px;
  font-weight: 600;
}

.field-error::before {
  content: '!';
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  font-size: 11px;
  line-height: 1;
}

.signin-dev {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  border: 1px dashed var(--sky-border);
  background: var(--sky-bg);
  color: var(--sky-text);
  font-family: var(--font-mono);
  font-size: 12.5px;
}

.signin-submit {
  height: 40px;
  justify-content: center;
  margin-top: var(--space-2);
  font-size: 13.5px;
}

.signin-secondary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-top: var(--space-1);
}

/* ---------------------------------------------------------------------------
   Panel states: loading, empty, error, locked
   --------------------------------------------------------------------------- */
.ui-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--space-2);
  padding: var(--space-6) var(--space-4);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--bg-surface);
  color: var(--text-secondary);
}

.ui-state-icon {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: var(--text-muted);
}

.ui-state--error { border-color: var(--danger-border); }

.ui-state--error .ui-state-icon { color: var(--danger-text); }

.ui-state--locked .ui-state-icon { color: var(--accent-text); }

.ui-state-title {
  color: var(--text-primary);
  font-size: 14px;
  font-weight: 600;
}

.ui-state-body {
  max-width: 46ch;
  font-size: 12.5px;
}

.ui-state .btn-outline { margin-top: var(--space-1); }

.ui-state-bar {
  width: 120px;
  height: 3px;
  border-radius: var(--radius-pill);
  background: linear-gradient(90deg, transparent, var(--accent-text), transparent) var(--bg-elevated);
  background-size: 50% 100%;
  background-repeat: no-repeat;
  animation: ui-sweep 1.1s linear infinite;
}

@keyframes ui-sweep {
  from {
    background-position: -100% 0;
  }
  to {
    background-position: 200% 0;
  }
}

.ui-state-row td {
  padding: var(--space-3);
  border-bottom: none;
}

.ui-state-row:hover { background: transparent !important; }

/* Skeleton product cards while the first page loads */
.skeleton-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 15px;
  min-height: 210px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  background: var(--bg-surface);
}

.skeleton-line {
  display: block;
  height: 12px;
  border-radius: var(--radius-xs);
  background: linear-gradient(90deg, var(--bg-elevated) 0%, var(--bg-hover) 50%, var(--bg-elevated) 100%);
  background-size: 200% 100%;
  animation: skeleton-pulse 1.4s ease-in-out infinite;
}

.skeleton-line.w40 { width: 40%; }
.skeleton-line.w90 { width: 90%; height: 16px; }
.skeleton-line.w70 { width: 70%; }
.skeleton-line.block { height: 64px; margin-top: auto; }

@keyframes skeleton-pulse {
  from { background-position: 100% 0; }
  to { background-position: -100% 0; }
}

/* ---------------------------------------------------------------------------
   Honest data affordances
   --------------------------------------------------------------------------- */
.no-trend {
  display: inline-flex;
  align-items: center;
  height: 28px;
  font-size: 11.5px;
  color: var(--text-muted);
  font-style: italic;
}

.hm-untracked {
  background: transparent;
  color: var(--text-muted);
  border: 1px dashed var(--border-strong);
  font-weight: 500;
}

/* Sidebar "Data coverage" card */
.coverage-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 11px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
}

.coverage-title {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.coverage-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
}

.coverage-list li {
  display: flex;
  justify-content: space-between;
  gap: var(--space-2);
  color: var(--text-secondary);
}

.coverage-count {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  font-weight: 600;
}

.coverage-list .coverage-empty { color: var(--text-muted); }

/* ---------------------------------------------------------------------------
   Classes that replaced inline style attributes
   --------------------------------------------------------------------------- */
.p-title-main {
  flex: 1;
  min-width: 0;
}

.card-title {
  margin-top: var(--space-2);
  min-height: auto;
}

.card-body { margin-top: 6px; }

.badge-wrap {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.row-clickable { cursor: pointer; }

.row-product {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 220px;
}

.row-thumb {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  object-fit: contain;
  border-radius: var(--radius-xs);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
}

.drawer-thumb {
  text-align: center;
  padding: var(--space-3);
  background: var(--bg-elevated);
}

.drawer-thumb img {
  max-height: 200px;
  max-width: 100%;
  object-fit: contain;
  border-radius: var(--radius-sm);
}

.load-more-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--space-3);
  margin: var(--space-4) 0;
}

.tm-result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.tm-result-summary { margin-top: var(--space-2); }

.ext-sha { margin-top: 3px; }

.seed-input {
  min-width: 0;
  width: 220px;
}

/* ---------------------------------------------------------------------------
   Responsive: tablet (<=1100px) and phone (<=780px)
   --------------------------------------------------------------------------- */
@media (max-width: 1100px) {
  /* Title keeps its own row; controls wrap instead of widening the page */
  .topbar {
    flex-wrap: wrap;
  }

  .sidebar-toggle,
  .workspace-title {
    flex-shrink: 0;
  }

  .global-controls {
    flex-wrap: wrap;
    flex-shrink: 1;
    min-width: 0;
    justify-content: flex-end;
  }

  .topbar {
    height: auto;
    min-height: var(--topbar-height);
    padding-block: var(--space-2);
  }

  .account-email {
    max-width: 140px;
  }
}

@media (max-width: 780px) {
  /* Sidebar becomes a sticky, horizontally scrolling tab strip under the brand */
  .sidebar {
    position: sticky;
    top: 0;
    height: auto;
    border-inline-end: none;
    border-bottom: 1px solid var(--border-subtle);
    z-index: 35;
  }

  .sidebar-brand {
    height: 48px;
  }

  .sidebar-scroll {
    flex-direction: row;
    gap: var(--space-1);
    padding: 6px 10px 8px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
  }

  .sidebar-scroll::-webkit-scrollbar {
    display: none;
  }

  .nav-group,
  .main-tabs {
    flex-direction: row;
    flex-shrink: 0;
  }

  .nav-group-label,
  .sidebar-footer,
  .sidebar-toggle {
    display: none;
  }

  .tab-btn {
    white-space: nowrap;
    width: auto;
  }

  .topbar {
    position: static;
    padding: 10px 14px;
    gap: var(--space-2);
  }

  .topbar-left,
  .global-controls {
    width: 100%;
  }

  .global-controls {
    justify-content: flex-start;
    gap: 6px;
  }

  .workspace {
    padding: 14px 12px 40px;
  }

  .kpi-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-2);
  }

  .search-sort-group,
  .search-box-wrap {
    width: 100%;
  }

  .search-input.with-icon {
    min-width: 0;
    flex: 1;
    width: 100%;
  }

  .platform-pills,
  .filter-chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: thin;
    padding-bottom: 2px;
    max-width: 100%;
  }

  .pill,
  .chip {
    flex-shrink: 0;
  }

  .filter-chips-row {
    flex-wrap: wrap;
  }

  .card-header-row > *,
  .section-banner > * {
    min-width: 0;
  }

  .seed-input {
    width: 100%;
  }

  .item-row,
  .prof-actions {
    flex-wrap: wrap;
  }

  .item-row > :first-child {
    min-width: 0;
    flex: 1 1 200px;
  }

  .diag-tabs-header {
    overflow-x: auto;
  }

  .quick-actions {
    grid-template-columns: 1fr;
  }

  .account-email {
    max-width: 120px;
  }

  .toast {
    inset-inline: 12px;
  }

  .otp-input {
    --otp-cell: 40px;
    --otp-gap: 6px;
  }
}

@media (max-width: 420px) {
  .account-email,
  .lang-dropdown {
    display: none;
  }
}

/* Long action labels wrap instead of pushing the page wider than a 320px phone. */
@media (max-width: 360px) {
  .btn-primary {
    max-width: 100%;
    white-space: normal;
  }
}

/* ---------------------------------------------------------------------------
   Reduced motion: no transforms, sweeps or pulses
   --------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  .product-card:hover {
    transform: none;
  }
}
