/* Zakładka Handel — warsztaty, dostawy z wiosek i towary. Układ jak army.css. */

.handel-shell {
  position: relative;
  z-index: 1;
  flex: 0 0 auto;
  min-height: 0;
  width: 100%;
  max-width: 1560px;
  margin: 0 auto;
  padding: 0.65rem 1rem 1rem;
  display: grid;
  grid-template-rows: auto 1fr;
  align-content: start;
  gap: 0.45rem;
}

.handel-content {
  display: grid;
  grid-template-columns: var(--side-w) minmax(0, 1fr) var(--side-w);
  grid-template-areas: "setup main detail";
  gap: 0.5rem 1rem;
  min-height: 0;
  align-items: start;
}

/* .handel-setup, .handel-main, .handel-detail: patrz components.css (card) */

.handel-setup {
  grid-area: setup;
}

.handel-main {
  grid-area: main;
  min-height: 0;
}

.handel-detail {
  /* Bez position:relative — nadpisywało .panel-sticky (jak w polityka.css):
     top ze sticky przy relative spycha cały prawy panel w dół o wysokość headera. */
  grid-area: detail;
}

.handel-detail.is-inactive {
  visibility: hidden;
}

.handel-view {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

/* Klasy z display:flex/grid nadpisują atrybut hidden — bez tego wszystkie
   widoki i pusta lista wyników byłyby widoczne naraz (ten sam haczyk co w army.css). */
.handel-shell [hidden] {
  display: none !important;
}

.handel-note {
  margin: 0;
  font-size: var(--fs-2xs);
  color: var(--muted);
}

/* --- Przełącznik widoku --- */

.handel-view-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

/* --- Wyszukiwarka miasta --- */

.handel-search-results {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 0.3rem;
}

.handel-search-result {
  appearance: none;
  display: flex;
  flex-direction: column;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: var(--radius-md);
  padding: 0.35rem 0.5rem;
  color: var(--ink);
  cursor: pointer;
  font: 600 var(--fs-sm) var(--font-body);
}

.handel-search-result:hover,
.handel-search-result.is-active {
  background: rgba(232, 194, 122, 0.12);
}

.handel-search-result span {
  font-weight: 400;
  font-size: var(--fs-2xs);
  color: var(--muted);
}

/* --- Nagłówki sekcji w panelu środkowym --- */

.handel-section-title {
  margin: 0;
  color: var(--accent-2);
  font: 600 1rem var(--font-display);
}

.handel-table th.handel-center,
.handel-table td.handel-center {
  text-align: center;
}

.handel-title-center {
  text-align: center;
}

.handel-section-sub {
  margin: 0;
  font-size: var(--fs-xs);
  color: var(--muted);
}

.handel-town-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.3rem 0.8rem;
}

.handel-meta {
  font-size: var(--fs-xs);
  color: var(--muted);
}

.handel-meta strong {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

/* --- Lista warsztatów w widoku miasta --- */

.handel-fit-list {
  --handel-fit-cols: minmax(0, 1fr) 5.5rem 5.5rem 4.5rem 5rem 6rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.handel-fit-row {
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: rgba(36, 28, 20, 0.6);
}

.handel-fit-row.is-none {
  opacity: 0.6;
}

.handel-fit-row.is-best {
  border-color: var(--accent);
  background: rgba(70, 50, 30, 0.75);
}

.handel-fit-row[open] {
  opacity: 1;
}

/* Wiersz to <details>: podsumowanie w trzech kolumnach, receptury po rozwinięciu. */
.handel-fit-summary {
  display: grid;
  grid-template-columns: var(--handel-fit-cols);
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.6rem;
  cursor: pointer;
  list-style: none;
}

.handel-fit-summary::-webkit-details-marker {
  display: none;
}

.handel-fit-summary:focus-visible {
  outline: 2px solid var(--accent-2);
  outline-offset: -2px;
  border-radius: var(--radius-lg);
}

.handel-fit-name {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.handel-fit-title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 0.4rem;
}

.handel-fit-title strong {
  font: 600 var(--fs-md) var(--font-body);
}

.handel-fit-title strong::before {
  content: "▸";
  display: inline-block;
  width: 0.9rem;
  color: var(--muted);
  transition: transform 0.15s ease;
}

.handel-fit-row[open] .handel-fit-title strong::before {
  transform: rotate(90deg);
}

/* Nagłówki kolumn: ta sama siatka i odstępy co wiersz (ramka 1px + padding). */
.handel-fit-cols {
  display: grid;
  grid-template-columns: var(--handel-fit-cols);
  gap: 0.6rem;
  padding: 0 calc(0.6rem + 1px);
  font-size: var(--fs-2xs);
  color: var(--muted);
  text-align: right;
}

.handel-fit-cols span:first-child {
  text-align: left;
}

.handel-fit-cell {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.1rem;
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-sm);
  text-align: right;
}

.handel-fit-cell small {
  font-size: var(--fs-2xs);
  color: var(--muted);
}

.handel-profit {
  font-weight: 700;
  cursor: help;
}

.handel-fit-row.is-best .handel-profit {
  color: var(--accent-2);
}

/* Widełki pod zyskiem i źródło dobrobytu pod ceną zakupu. */
/* --- Receptury po rozwinięciu --- */

.handel-recipes {
  border-top: 1px solid var(--line);
  padding: 0.4rem 0.6rem 0.55rem 1.5rem;
}

.handel-recipe-list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.handel-recipe {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: baseline;
  gap: 0.2rem 0.7rem;
  font-size: var(--fs-xs);
}

.handel-recipe small {
  color: var(--muted);
  font-size: var(--fs-2xs);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.handel-recipe.is-no-gold .handel-recipe-io {
  color: var(--muted);
}

.handel-recipe-status {
  justify-self: end;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.handel-recipe-status.is-ok {
  color: var(--accent-2);
  font-weight: 600;
}

.handel-recipe-status.is-no_gold,
.handel-recipe-status.is-gated,
.handel-recipe-status.is-no_input,
.handel-recipe-status.is-no_price {
  color: var(--muted);
  font-size: var(--fs-2xs);
  cursor: help;
}

/* --- Plakietki oceny dopasowania --- */

.handel-badge {
  display: inline-flex;
  align-items: center;
  padding: 0 0.4rem;
  border-radius: var(--radius-pill);
  background: rgba(232, 194, 122, 0.12);
  font-size: var(--fs-2xs);
  line-height: 1.15rem;
  white-space: nowrap;
}

.handel-badge.is-best {
  background: rgba(232, 194, 122, 0.28);
  color: var(--accent-2);
  font-weight: 700;
}

.handel-badge.is-good {
  background: rgba(232, 194, 122, 0.18);
  color: var(--accent-2);
}

.handel-badge.is-none {
  background: rgba(120, 90, 70, 0.18);
  color: var(--muted);
}

.handel-badge.is-approx {
  background: rgba(120, 90, 70, 0.22);
  color: var(--muted);
  cursor: help;
}


/* --- Tabela rankingu / gdzie kupić --- */

.handel-table-wrap {
  overflow-x: auto;
}

/* .handel-table: patrz components.css (table-base) */

/* .handel-table th, td, thead th: patrz components.css (list-table) — bez break-word w nagłówkach:
   na wąskim ekranie „VILLAGES” pękało na VILL / AGE / S. */

.handel-table thead th.handel-hearth-group {
  text-align: center;
  vertical-align: bottom;
  border-bottom: none;
  padding-bottom: 0.1rem;
}

.handel-table thead tr + tr th {
  padding-top: 0.1rem;
  border-bottom-color: var(--accent-2);
}

/* .handel-table tbody tr:hover: patrz components.css (list-table) */

.handel-table .handel-num {
  text-align: right;
}

.handel-best {
  color: var(--accent-2);
  font-weight: 700;
  background: rgba(232, 194, 122, 0.08);
}

.handel-row-link {
  appearance: none;
  background: transparent;
  border: 0;
  padding: 0 0.08em;
  margin: 0 -0.08em;
  border-radius: var(--radius-xs);
  color: var(--ent-link);
  font: 600 var(--fs-md) var(--font-body);
  text-align: left;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

/* Kolor i hover jak .ent-link (encyclopedia.css) — te same linki co w pozostałych zakładkach. */
.handel-row-link:hover,
.handel-row-link:focus-visible {
  color: var(--ent-link-hover);
  background: var(--ent-link-bg);
}

.handel-row-link:focus-visible {
  outline: 2px solid var(--ent-link);
  outline-offset: 1px;
}

/* --- Panel dostawców (prawa kolumna) --- */

.handel-supplier-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.handel-supplier {
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 0.5rem 0.6rem;
  background: rgba(36, 28, 20, 0.6);
}

.handel-supplier-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.3rem 0.45rem;
}

.handel-supplier-head strong {
  font: 600 var(--fs-md) var(--font-body);
}

.handel-supplier-type {
  font-size: var(--fs-xs);
  color: var(--accent-2);
}

.handel-supplier-meta {
  margin: 0.2rem 0 0;
  font-size: var(--fs-2xs);
  color: var(--muted);
}

.handel-prod-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 0.4rem;
  margin: 0.35rem 0 0;
  padding: 0;
  list-style: none;
}

.handel-prod {
  display: inline-flex;
  gap: 0.25rem;
  padding: 0 0.35rem;
  border-radius: var(--radius-pill);
  background: rgba(232, 194, 122, 0.1);
  font-size: var(--fs-2xs);
  line-height: 1.15rem;
  white-space: nowrap;
}

.handel-prod b {
  font-variant-numeric: tabular-nums;
}

.handel-prod.is-input {
  background: rgba(232, 194, 122, 0.26);
  color: var(--accent-2);
  font-weight: 600;
}

/* --- Responsywność --- */

@media (max-width: 1100px) {
  .handel-content {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    grid-template-areas:
      "tabs"
      "setup"
      "main"
      "detail";
    row-gap: 0.75rem;
  }

  .handel-detail.is-inactive {
    display: none;
  }

}

@media (max-width: 900px) {
  .handel-fit-cols {
    display: none;
  }

  .handel-fit-list {
    --handel-fit-cols: minmax(0, 1fr);
  }

  .handel-fit-cell {
    flex-direction: row;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.6rem;
  }

  .handel-fit-cell::before {
    content: attr(data-label);
    font-size: var(--fs-2xs);
    color: var(--muted);
    text-align: left;
  }
}

@media (max-width: 560px) {
  .handel-shell {
    padding-left: 0.6rem;
    padding-right: 0.6rem;
  }

  .handel-recipes {
    padding-left: 0.6rem;
  }

  .handel-recipe {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .handel-recipe small {
    grid-column: 1;
    white-space: normal;
  }

  .handel-col-hide-sm {
    display: none;
  }

  .handel-table th,
  .handel-table td {
    padding-left: 0.25rem;
    padding-right: 0.25rem;
  }

  .handel-table thead th {
    letter-spacing: 0;
  }
}

/* Widok Miasto: tabela warsztatów; nazwa otwiera kartę warsztatu. */
.handel-table tr.is-none {
  opacity: 0.7;
}

.handel-table tr.is-best .handel-row-link {
  color: var(--accent-2);
}

/* Pierwsza kolumna tabeli warsztatów (nazwa, plakietki, receptura) może się łamać. */
.handel-table td:first-child .handel-fit-name {
  white-space: normal;
  min-width: 10rem;
}

/* Na telefonie tabele przewijają się w bok, a pierwsza kolumna (nazwa) zostaje przy przewijaniu. */
@media (max-width: 640px) {
  .handel-shell {
    --handel-solid: #241c14;
  }

  .handel-table th:first-child,
  .handel-table td:first-child {
    position: sticky;
    left: 0;
    z-index: 1;
    max-width: 9rem;
    background: var(--handel-solid);
  }

  .handel-table thead th:first-child {
    z-index: 3;
  }

  /* 8,2 rem to treść, a z paddingiem komórki 144 px (jak max-width komórki wyżej). */
  .handel-table td:first-child .handel-fit-name {
    min-width: 8.2rem;
  }

  .handel-table td:first-child .handel-row-link {
    white-space: normal;
  }

  /* Plakietka łamie się w wąskiej komórce zamiast wychodzić na następną kolumnę. */
  .handel-table td:first-child .handel-badge {
    max-width: 100%;
    white-space: normal;
  }

  /* Cała komórka z nazwą jest celem dotyku, nie tylko tekst (ok. 20 px wysokości). */
  .handel-table td:first-child .handel-row-link::after {
    content: "";
    position: absolute;
    inset: 0;
  }
}

/* Tabela o stałych kolumnach (szerokości z buildTable) — nie skacze między towarami. */
.handel-table.handel-table-fixed {
  table-layout: fixed;
}

.handel-table.handel-table-fixed th,
.handel-table.handel-table-fixed td {
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-word;
}

.handel-table.handel-table-fixed thead th {
  overflow: visible;
  text-overflow: unset;
  word-break: normal;
}

/* Nazwa w nagłówku, która otwiera kartę encyklopedii. */
.handel-title-link {
  padding: 0 0.08em;
  margin: 0 -0.08em;
  border: 0;
  border-radius: var(--radius-xs);
  background: none;
  color: var(--ent-link);
  font: inherit;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.handel-title-link:hover,
.handel-title-link:focus-visible {
  color: var(--ent-link-hover);
  background: var(--ent-link-bg);
}

.handel-title-link:focus-visible {
  outline: 2px solid var(--ent-link);
  outline-offset: 1px;
}

/* Zamknięcie karty encyklopedii w prawym panelu (jak na mapie — bez osobnego nagłówka). */
.handel-detail-close {
  position: sticky;
  top: 0;
  float: right;
  z-index: 2;
  margin: -0.3rem -0.3rem 0 0.5rem;
}

/* Długa lista w komórce (np. typy wiosek) łamie się zamiast poszerzać tabelę. */
.handel-wrap {
  display: block;
  white-space: normal;
}

/* Tekst z ikoną gry w komórce tabeli — ikona i liczba w jednej linii. */
.handel-with-icon {
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}

/* Ikona po liczbie (złoto, dobrobyt): odstęp z lewej zamiast z prawej. */
.handel-icon.handel-icon-after {
  margin-right: 0;
  margin-left: 0.3em;
}

/* Ikony z gry przy warsztatach i towarach (catalog.icons). */
.handel-icon {
  width: 1.25em;
  height: 1.25em;
  margin-right: 0.35em;
  flex-shrink: 0;
  vertical-align: -0.25em;
  object-fit: contain;
}

/* Legenda zakresów pod tabelą: tylko tam, gdzie nie ma podpowiedzi po najechaniu. */
.handel-legend {
  display: none;
  margin-top: 0.6rem;
  font-size: var(--fs-sm);
  color: var(--muted);
}

.handel-legend summary {
  min-height: 2.25rem;
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--accent-2);
}

.handel-legend p {
  margin: 0.4rem 0 0;
}

@media (hover: none) {
  .handel-legend {
    display: block;
  }
}
