:root {
  --bg: #f7f6f3;
  --surface: #ffffff;
  --border: #e8e5df;
  --border-strong: #d0ccc4;
  --text: #1a1814;
  --text-muted: #7a7570;
  --amz: #ff9900;
  --amz-dark: #c45500;
  --best: #b91c1c;
  --best-bg: #fef3f2;
  --best-border: #fca5a5;
  --sidebar-w: 188px;
  --header-h: 48px;
}

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

body {
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* ── Header ──────────────────────────────────────────────────────── */
#header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 18px;
  position: sticky;
  top: 0;
  z-index: 10;
}

#logo {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.4px;
  color: var(--text);
  text-decoration: none;
}

.amz-pill {
  font-size: 9px;
  font-weight: 600;
  background: var(--amz);
  color: #111;
  padding: 2px 6px;
  border-radius: 3px;
  letter-spacing: 0.02em;
}

.header-sep {
  width: 1px;
  height: 18px;
  background: var(--border);
}

.header-sub {
  font-size: 11px;
  color: var(--text-muted);
}

.header-right {
  margin-left: auto;
  display: flex;
  align-items: center;
}

.last-update {
  font-size: 10px;
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
}

/* ── Layout ──────────────────────────────────────────────────────── */
#app {
  display: flex;
  height: calc(100vh - var(--header-h));
}

/* ── Sidebar ─────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px 0;
}

.sb-sec {
  padding: 0 12px 12px;
  border-bottom: 1px solid var(--border);
}
.sb-sec:last-child {
  border-bottom: none;
}

.sb-title {
  font-size: 9.5px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 6px;
  padding-top: 10px;
}

.sb-lbl {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 3px 0;
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
}
.sb-lbl:hover {
  color: var(--amz-dark);
}
.sb-lbl input[type='checkbox'] {
  width: 13px;
  height: 13px;
  accent-color: var(--amz);
  cursor: pointer;
}

.source-row {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11px;
  color: var(--text-muted);
  padding: 3px 0;
}

.range-row {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 5px;
}
.range-row input {
  width: 54px;
  font-size: 11px;
  font-family: 'DM Mono', monospace;
  padding: 3px 6px;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.range-row input:focus {
  border-color: var(--amz);
}
.range-row span {
  color: var(--text-muted);
  font-size: 10px;
}

/* ── Main ────────────────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

/* ── Toolbar ─────────────────────────────────────────────────────── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tl-lbl {
  font-size: 11px;
  color: var(--text-muted);
}

#ppwrap {
  display: flex;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  overflow: hidden;
}
.ppb {
  padding: 4px 10px;
  font-size: 11px;
  font-family: 'DM Sans', sans-serif;
  cursor: pointer;
  border: none;
  background: var(--surface);
  color: var(--text-muted);
  transition: background 0.12s;
}
.ppb:not(:last-child) {
  border-right: 1px solid var(--border);
}
.ppb.on {
  background: var(--text);
  color: #fff;
  font-weight: 500;
}
.ppb:hover:not(.on) {
  background: var(--bg);
}

#search {
  padding: 5px 10px;
  font-size: 12px;
  font-family: 'DM Sans', sans-serif;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  width: 220px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
#search:focus {
  border-color: var(--amz);
  background: var(--surface);
}

#rc {
  margin-left: auto;
  font-size: 11px;
  font-family: 'DM Mono', monospace;
  color: var(--text-muted);
}

/* ── Tabela ──────────────────────────────────────────────────────── */
#twrap {
  overflow: auto;
  flex: 1;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

thead {
  position: sticky;
  top: 0;
  z-index: 3;
}

th {
  text-align: left;
  padding: 6px 10px;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface);
  border-bottom: 1px solid var(--border-strong);
  white-space: nowrap;
  letter-spacing: 0.02em;
}

td {
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  white-space: nowrap;
  background: var(--surface);
}

tr:hover td {
  background: #fdfcf9;
}
tr.best-row td {
  background: var(--best-bg);
}

.empty {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
}

/* Cabeçalho de grupo */
.group-hdr td {
  background: var(--bg);
  border-top: 2px solid var(--border-strong);
  border-bottom: 1px solid var(--border);
  padding: 6px 10px;
  white-space: normal;
}

.g-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
}
.g-brand {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
}
.g-cat {
  display: inline-block;
  font-size: 9px;
  font-weight: 600;
  background: var(--border);
  color: var(--text-muted);
  padding: 1px 7px;
  border-radius: 20px;
  letter-spacing: 0.03em;
}

/* PPU */
.ppu {
  font-weight: 600;
  font-size: 13px;
  font-family: 'DM Mono', monospace;
}
.ppu.best {
  color: var(--best);
}

/* Badges */
.bdg {
  display: inline-flex;
  align-items: center;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.bdg-best {
  background: var(--best-bg);
  color: var(--best);
  border: 1px solid var(--best-border);
}

/* Produto */
.prod-link {
  color: #0f6b7c;
  text-decoration: none;
  font-size: 11.5px;
}
.prod-link:hover {
  color: var(--amz-dark);
  text-decoration: underline;
}

.asin {
  font-family: 'DM Mono', monospace;
  font-size: 9.5px;
  color: var(--text-muted);
  margin-left: 5px;
  opacity: 0.7;
}

.dim {
  font-size: 10px;
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
  margin-left: 4px;
}

/* ── Footer ──────────────────────────────────────────────────────── */
#footer {
  background: var(--surface);
  border-top: 1px solid var(--border-strong);
  padding: 40px 20px; /* Mais espaço para respirar */
  margin-top: 0;
}

.footer-content {
  max-width: 900px; /* Não deixa o texto esticar demais */
  margin: 0;
}

.disclaimer {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 12px;
  line-height: 1.6;
}

.disclaimer strong {
  color: var(--text); /* Dá destaque ao aviso da Amazon */
  font-weight: 600;
}

.disclaimer-small {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  opacity: 0.7;
  margin-top: 20px;
  padding-top: 15px;
  border-top: 1px dashed var(--border);
  line-height: 1.4;
}

.copyright {
  margin-top: 25px;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: -0.2px;
}

@media (max-width: 768px) {
  /* 1. Ocupar a largura toda e empilhar verticalmente */
  #app {
    flex-direction: column;
    height: auto; /* Remove o bloqueio de altura do desktop */
    overflow-y: visible;
  }

  /* 2. Ajustar a Sidebar para o topo */
  #sidebar {
    width: 100%;
    min-width: 100%;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 10px 0;
  }

  /* 3. Colocar as categorias em linha (scroll horizontal) para poupar espaço */
  .sb-sec {
    display: flex;
    flex-direction: column;
  }

  /* Se tiveres muitas categorias, podes fazê-las scrollar horizontalmente: */
  /* .sb-sec { overflow-x: auto; white-space: nowrap; flex-direction: row; gap: 15px; } */

  /* 4. Ajustar o Main */
  #main {
    height: auto;
    overflow: visible;
  }

  /* 5. Tabela: Garantir que o utilizador pode fazer scroll lateral se a tabela for larga */
  #twrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  table {
    min-width: 500px; /* Garante que a tabela não "encolhe" as colunas até ficarem ilegíveis */
  }

  /* 6. Reduzir tamanhos de fonte para mobile */
  .ppu {
    font-size: 11px;
  }
  .g-name {
    font-size: 11px;
  }
}

@media (max-width: 480px) {
  #header {
    padding: 0 10px;
  }

  #logo {
    font-size: 14px;
  }

  .last-update {
    font-size: 9px;
  }

  /* Toolbar: Esconder ou simplificar informações */
  #rc {
    font-size: 9px;
  }

  /* Ajustar as células da tabela para não ocuparem tanto espaço */
  td,
  th {
    padding: 5px 6px;
    font-size: 11px;
  }
}

/* --- Lógica para o Details em Desktop --- */
.mobile-filters summary {
  list-style: none; /* Remove a seta no Firefox/Safari */
  outline: none;
}

.mobile-filters summary::-webkit-details-marker {
  display: none; /* Remove a seta no Chrome/Edge */
}

/* No Desktop (Ecrãs > 768px) */
@media (min-width: 769px) {
  .mobile-filters {
    /* Faz o <details> "desaparecer" como contentor, mantendo os filhos visíveis */
    display: contents !important;
  }

  .mobile-filters .sb-content {
    /* Garante que o conteúdo aparece sempre */
    display: block !important;
    visibility: visible !important;
  }

  .mobile-filters summary {
    /* Transforma o título num bloco normal, sem cliques e sem setas */
    display: block !important;
    pointer-events: none !important;
    cursor: default !important;
    list-style: none !important;
  }

  /* Remove as setas nativas e a que adicionámos */
  .mobile-filters summary::-webkit-details-marker,
  .mobile-filters summary::after {
    display: none !important;
  }
}

/* No Mobile (Ecrãs <= 768px) */
@media (max-width: 768px) {
  .mobile-filters {
    display: block; /* Volta a ser um bloco normal para funcionar o toggle */
    background: var(--surface);
    border-radius: 6px;
  }

  .mobile-filters summary {
    cursor: pointer;
    padding: 10px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--amz-dark);
  }

  .mobile-filters summary::after {
    content: '↓';
    font-size: 12px;
    font-weight: bold;
  }

  .mobile-filters[open] summary::after {
    content: '↑';
  }

  /* Só mostra o conteúdo se o atributo [open] estiver lá */
  .mobile-filters[open] .sb-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px;
    padding-bottom: 10px;
  }

  .mobile-filters:not([open]) .sb-content {
    display: none;
  }
}
