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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f5f5f5;
  color: #333;
  line-height: 1.5;
}

header {
  background: #2a2a2a;
  color: #fff;
  padding: 1rem 2rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}

header h1 { font-size: 1.2rem; font-weight: 600; }
header span { font-size: 0.85rem; color: #999; }

main {
  /* Three-column viewport-fit: options | source | output.
     Each panel scrolls internally so the page itself never scrolls. */
  margin: 0;
  padding: 1rem 1.25rem;
  display: grid;
  grid-template-columns: 260px minmax(320px, 1fr) minmax(280px, 1fr);
  gap: 1rem;
  height: calc(100vh - 64px - 44px - 2rem); /* 100vh minus header, footer, vertical padding */
  align-items: stretch;
}

/* Each direct grid child becomes a flex column taking its column's full
   height. Internal scroll only kicks in if content genuinely exceeds the
   panel — typical case is no scroll at all. */
main > .panel {
  height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* Below this viewport width, drop the output panel to its own row.
   Below the next breakpoint, collapse to a single column. */
@media (max-width: 1100px) {
  main {
    grid-template-columns: 260px 1fr;
    grid-template-rows: 1fr auto;
    height: auto;
  }
  #output-panel { grid-column: 1 / -1; }
}

@media (max-width: 768px) {
  main { grid-template-columns: 1fr; height: auto; }
}

/* Panels */
.panel {
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 1rem 1.1rem;
}

/* Settings mode toggle — two pill buttons */
.settings-mode {
  display: flex;
  gap: 0;
  margin-bottom: 0.85rem;
}
.mode-btn {
  flex: 1;
  padding: 0.4rem 0.6rem;
  border: 1px solid #ddd;
  background: #f6f6f6;
  color: #555;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 500;
}
.mode-btn:first-child { border-radius: 4px 0 0 4px; }
.mode-btn:last-child  { border-radius: 0 4px 4px 0; border-left: none; }
.mode-btn:hover { background: #eee; }
.mode-btn.active {
  background: #2a2a2a;
  color: #fff;
  border-color: #2a2a2a;
}

/* When Advanced is on, slim down the spacing so the whole panel fits in
   the card without scrolling. */
#advanced-options .option-group { margin-bottom: 0.5rem; }
#advanced-options .option-group label { margin-bottom: 0.15rem; font-size: 0.75rem; }
#advanced-options .option-group select,
#advanced-options .option-group input { padding: 0.28rem 0.35rem; font-size: 0.8rem; }
#advanced-options .checkbox-row { margin-bottom: 0.3rem; font-size: 0.82rem; }
#advanced-options hr { margin: 0.5rem 0 !important; }
#advanced-options p { margin: 0.25rem 0 !important; font-size: 0.75rem !important; }

.panel h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: #555;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Tabs — styled as a pill-button group to match the Settings mode toggle. */
.tabs {
  display: flex;
  gap: 0;
  margin-bottom: 0.85rem;
  border-bottom: none;
}

.tab-btn {
  flex: 1;
  padding: 0.4rem 0.6rem;
  border: 1px solid #ddd;
  background: #f6f6f6;
  color: #555;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 500;
}
.tab-btn:first-child { border-radius: 4px 0 0 4px; }
.tab-btn:last-child  { border-radius: 0 4px 4px 0; border-left: none; }
.tab-btn:hover { background: #eee; }

.tab-btn.active {
  background: #2a2a2a;
  color: #fff;
  border-color: #2a2a2a;
  font-weight: 500;
}

.tab-content { display: none; }
/* Active tab fills the remaining card height so its grid can stretch. */
.tab-content.active {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

/* Drop zone */
.dropzone {
  border: 2px dashed #ccc;
  border-radius: 6px;
  padding: 2rem;
  text-align: center;
  color: #999;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.dropzone.dragover {
  border-color: #666;
  background: #f0f0f0;
}

.dropzone p { margin-bottom: 0.5rem; }
.dropzone small { font-size: 0.8rem; }

/* Channel table */
.channel-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.75rem;
  font-size: 0.85rem;
}

.channel-table th {
  text-align: left;
  padding: 0.4rem;
  border-bottom: 1px solid #ddd;
  color: #888;
  font-weight: 500;
}

.channel-table td {
  padding: 0.4rem;
  border-bottom: 1px solid #f0f0f0;
}

.channel-table select {
  width: 100%;
  padding: 0.25rem;
  border: 1px solid #ddd;
  border-radius: 3px;
}

/* Poly Haven grid */
.ph-search { width: 100%; padding: 0.5rem; border: 1px solid #ddd; border-radius: 4px; margin-bottom: 0.75rem; }
/* Asset grid fills remaining card height; scrolls internally if needed.
   When a Poly Haven asset is selected, the detail block below shrinks the
   grid via flex layout — no fixed max-height cutoff. */
.ph-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 0.4rem;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  align-content: start;
}
.ph-item { cursor: pointer; border: 2px solid transparent; border-radius: 4px; overflow: hidden; }
.ph-item.selected { border-color: #333; }
.ph-item img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.ph-item-name { font-size: 0.7rem; padding: 0.2rem; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Options */
.option-group { margin-bottom: 0.75rem; }
.option-group label { display: block; font-size: 0.8rem; color: #666; margin-bottom: 0.25rem; }
.option-group select, .option-group input { width: 100%; padding: 0.35rem; border: 1px solid #ddd; border-radius: 3px; font-size: 0.85rem; }
.option-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
.checkbox-row { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.5rem; }
.checkbox-row input[type="checkbox"] { width: auto; }

/* Buttons */
.btn {
  display: inline-block;
  padding: 0.6rem 1.25rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  width: 100%;
  margin-top: 0.5rem;
}

.btn-primary { background: #2a2a2a; color: #fff; }
.btn-primary:hover { background: #444; }
.btn-primary:disabled { background: #aaa; cursor: not-allowed; }
.btn-download { background: #2e7d32; color: #fff; }
.btn-download:hover { background: #388e3c; }

/* Output panel */
.status { font-size: 0.85rem; color: #888; margin-bottom: 0.75rem; }
.status.error { color: #c62828; }
.status.success { color: #2e7d32; }

/* Compact two-column summary so it fits in the output card without scrolling. */
.result-summary {
  font-size: 0.78rem;
  margin-bottom: 0.6rem;
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 0.6rem;
  row-gap: 0.2rem;
  align-items: baseline;
}
.result-summary dt {
  font-weight: 600;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.7rem;
}
.result-summary dd { margin: 0; }

.color-swatch {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 1px solid #ccc;
  border-radius: 3px;
  vertical-align: middle;
  margin-right: 0.3rem;
}

/* Preview image grows to fill the remaining card height. The output panel
   is a flex column, so flex:1 here pushes the summary + Download button
   to the bottom of the card — mirroring the position of Fetch & Convert
   in the middle card. */
.preview-img {
  width: 100%;
  flex: 1;
  min-height: 0;
  max-height: none;
  object-fit: contain;
  border-radius: 4px;
  margin-bottom: 0.6rem;
  display: none;
}

.preview-img.visible { display: block; }

/* Resolution picker */
.res-picker { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
.res-btn {
  padding: 0.3rem 0.6rem;
  border: 1px solid #ddd;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
  font-size: 0.8rem;
}
.res-btn.active { background: #2a2a2a; color: #fff; border-color: #2a2a2a; }

/* Per-map thumbnail grid with rotation controls */
.map-thumb-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 0.5rem;
  margin-top: 0.75rem;
  padding: 0.5rem;
  background: #fafafa;
  border-radius: 4px;
}
.map-thumb {
  text-align: center;
  font-size: 0.75em;
  position: relative;
}
.map-thumb-img-wrap {
  width: 100%;
  aspect-ratio: 1 / 1;
  background: #eaeaea;
  border-radius: 3px;
  overflow: hidden;
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.map-thumb-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.2s ease;
  pointer-events: none;
}
.map-thumb-rot-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-size: 0.7em;
  padding: 1px 4px;
  border-radius: 2px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}
.map-thumb[data-rot="90"] .map-thumb-rot-badge,
.map-thumb[data-rot="180"] .map-thumb-rot-badge,
.map-thumb[data-rot="270"] .map-thumb-rot-badge { opacity: 1; }

/* USED / IGNORED indicator (bottom-left of each map thumbnail) */
.map-thumb-usage-badge {
  position: absolute;
  bottom: 2px;
  left: 2px;
  font-size: 0.65em;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 2px;
  letter-spacing: 0.05em;
  pointer-events: none;
}
.map-thumb[data-used="true"] .map-thumb-usage-badge {
  background: rgba(46, 125, 50, 0.85);
  color: #fff;
}
.map-thumb[data-used="false"] .map-thumb-usage-badge {
  background: rgba(0, 0, 0, 0.55);
  color: #ccc;
}
/* Dim the whole tile when the map will not be consumed. */
.map-thumb[data-used="false"] .map-thumb-img { opacity: 0.45; filter: grayscale(0.5); }
.map-thumb[data-used="false"] .map-thumb-label { color: #999; font-style: italic; }

.map-thumb-label {
  margin-top: 0.25rem;
  color: #555;
  text-transform: capitalize;
}

/* Header info button + popover */
.header-info { margin-left: auto; position: relative; }

#info-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid #555;
  background: transparent;
  color: #bbb;
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
}
#info-btn:hover,
#info-btn[aria-expanded="true"] {
  background: #3a3a3a;
  color: #fff;
  border-color: #777;
}

.info-popover {
  position: absolute;
  right: 0;
  top: calc(100% + 0.6rem);
  width: 270px;
  background: #fff;
  color: #333;
  border: 1px solid #ddd;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  padding: 0.75rem 1rem;
  font-size: 0.85rem;
  z-index: 100;
}
.info-popover p { margin: 0.3rem 0; line-height: 1.45; }
