/* ==========================================================================
   Base: colours, fonts, page background
   ========================================================================== */

:root {
  --bg: #1a1a2e;
  --panel: #22223a;
  --panel-2: #2a2a4a;
  --border: #3a3a5c;
  --slot-border: #5c5c85;
  --control-border: #8a8ab4; /* about 4.7:1 on --panel, for button and flag outlines */
  --text: #e0e0e0;
  --muted: #999;
  --accent: #6cacff;
  --danger: #ef5350;
  --gold: #ffd700;
  font-family: 'Rajdhani', system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* The hidden attribute always wins, even over display rules set on the same element */
[hidden] { display: none !important; }

/* Background image is set per page load by js/app/main.js. The dark overlay keeps the page readable. */
body {
  margin: 0;
  background:
    linear-gradient(rgba(26, 26, 46, 0.8), rgba(26, 26, 46, 0.92)),
    var(--page-bg, none) center top / cover fixed no-repeat,
    var(--bg);
  color: var(--text);
  font-size: 16px;
  min-height: 100vh;
}

button, input, select { font: inherit; color: inherit; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ==========================================================================
   Layout and header
   ========================================================================== */

header.site-header {
  position: relative;
  padding: 16px 16px 8px;
  text-align: center;
}

/* Language flags, in the squad totals row. Each flag is a button so it can be reached by keyboard. */
.lang-switch {
  display: flex;
  gap: 6px;
}

.lang-btn {
  width: 44px;
  height: 30px;
  padding: 0;
  border: 2px solid var(--control-border);
  border-radius: 4px;
  background: none;
  cursor: pointer;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
}

/* Only the flag is dimmed, so the border keeps its contrast */
.lang-btn img {
  display: block;
  width: 100%;
  height: 100%;
  opacity: 0.55;
}

.lang-btn:hover img,
.lang-btn:focus-visible img,
.lang-btn.is-active img { opacity: 1; }

.lang-btn.is-active { border-color: var(--accent); }

header .header-logo {
  display: block;
  width: 100%;
  max-width: 520px;
  height: auto;
  margin: 0 auto;
  filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.5));
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 16px 32px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
}

/* The squad stays visible while the list scrolls beneath it */
.squad-panel {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

.app-error {
  margin: 24px;
  padding: 16px;
  border: 1px solid var(--danger);
  border-radius: 8px;
  color: var(--danger);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 10px 16px;
  z-index: 10;
}

/* ==========================================================================
   Shared controls: inputs, buttons, pills
   ========================================================================== */

.search-row input[type="search"],
.filters-panel select,
.squad-controls input,
.squad-controls select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}

.add-btn, .ghost-btn, .slot-remove, .squad-controls button {
  cursor: pointer;
}

.add-btn {
  background: var(--panel);
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 6px;
  padding: 6px 10px;
}

.add-btn:hover { background: var(--accent); color: var(--bg); }

.ghost-btn {
  background: none;
  border: 1px solid var(--control-border);
  border-radius: 6px;
  padding: 6px 10px;
}

.squad-controls button:disabled { opacity: 0.4; cursor: default; }

/* Filter pills and form toggles share one look and differ in size */
.filter-btn,
.toggle-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
}

.filter-btn { font-size: 0.85rem; padding: 3px 10px; display: inline-flex; align-items: center; gap: 5px; }
.toggle-btn { font-size: 0.75rem; padding: 2px 8px; }

.filter-icon { height: 1.2em; width: auto; }

.filter-btn:hover,
.toggle-btn:hover { color: var(--text); }

.filter-btn.is-active,
.toggle-btn.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
}

.toggle-btn.shiny-toggle.is-active,
.toggle-btn.ss-toggle.is-active {
  background: var(--gold);
  border-color: var(--gold);
}

.badge {
  display: inline-block;
  font-size: 0.75rem;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--gold);
  color: var(--gold);
  vertical-align: middle;
}

/* ==========================================================================
   Squad panel (sticky, top of the page)
   ========================================================================== */

.squad-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 8px;
}

.squad-controls select,
.squad-controls input { width: 100%; }

/* Save / Delete / Clear on the left, the flags centred on the same row, the controls hint on the right. Grid stretch gives both the same height. */
.squad-actions { grid-column: 1; grid-row: 2; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }

.squad-controls .lang-switch {
  grid-column: 1 / -1;
  grid-row: 2;
  justify-self: center;
  align-self: center;
}

.squad-hint {
  grid-column: 2;
  grid-row: 2;
  justify-self: end;
  width: fit-content;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-end;
  gap: 0;
  margin: 0;
  padding: 2px 6px;
  border: 1px solid var(--control-border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.2;
}

.squad-hint .hint-label { color: var(--text); font-weight: 600; }

.squad-totals {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
  font-size: 0.85rem;
}

.squad-totals strong { font-size: 1rem; }

/* Monsters and Cost get the same outline as the controls hint and the buttons */
.squad-totals > span {
  border: 1px solid var(--control-border);
  border-radius: 6px;
  padding: 2px 8px;
}

.over-limit { color: var(--danger); }

/* Fixed 64px square tiles, as many per row as fit (16 fit on one row on desktop). */
.squad-slots {
  display: grid;
  grid-template-columns: repeat(auto-fill, 64px);
  justify-content: center;
  gap: 4px;
}

.slot {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  /* Taller than wide: a band at the top holds the star and the ×, then icon, name and cost. */
  aspect-ratio: 64 / 100;
  overflow: hidden;
  min-height: 0;
  background: var(--panel-2);
  border: 1px solid var(--slot-border);
  border-radius: 6px;
  padding: 20px 2px 4px;
}

.slot-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  flex: 1;
  width: 100%;
  min-width: 0;
  min-height: 0;
  color: inherit;
  text-decoration: none;
}

.slot-link:hover .slot-name { text-decoration: underline; }

.slot-empty {
  justify-content: center;
  color: var(--muted);
  background: transparent;
  border-style: dashed;
  cursor: default;
}

.slot .monster-icon { width: 80%; height: auto; aspect-ratio: 1 / 1; flex: none; }

/* Secret Skill sits in the top-left corner as a small star, clear of the cost line and the × at top right */
.slot .slot-secret {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  padding: 0;
  border-radius: 50%;
  font-size: 0.7rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Tight line box, so the × ends above the icon */
.slot .slot-remove { position: absolute; top: 1px; right: 1px; font-size: 1rem; line-height: 1; padding: 0 3px; }

.slot-info { display: flex; flex-direction: column; align-items: center; min-width: 0; width: 100%; text-align: center; }

.slot-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.8rem; }

.slot-cost { color: var(--muted); font-size: 0.6rem; white-space: nowrap; overflow: hidden; max-width: 100%; }

.slot .toggle-btn { flex: 0 0 auto; }

.slot-remove {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.2rem;
  padding: 0 4px;
}

.slot-remove:hover { color: var(--danger); }

/* ==========================================================================
   Browser: search, filters, result line, grid
   ========================================================================== */

.search-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.search-row input[type="search"] { flex: 1 1 180px; min-width: 0; }

.filter-toggle-btn {
  background: var(--panel-2);
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: var(--accent);
  cursor: pointer;
  padding: 8px 12px;
  white-space: nowrap;
}

.filter-toggle-btn:hover,
.filter-toggle-btn.active {
  background: var(--accent);
  color: var(--bg);
}

.filters-panel {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 8px;
  padding: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.filter-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.filter-label {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

.filter-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.result-line {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  margin-bottom: 8px;
}

.no-results { color: var(--muted); text-align: center; padding: 32px 0; }

/* Icon-only tiles: as many monsters per screen as possible */
.monster-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 8px;
}

/* Banner above each tier letter in the PvP and PvE sorts */
.tier-banner {
  grid-column: 1 / -1;
  margin: 10px 0 0;
  padding: 4px 10px;
  border-left: 4px solid var(--tier-color, var(--border));
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--tier-color, inherit);
}
.tier-banner[data-tier="S+"] { --tier-color: #ff6b6b; }
.tier-banner[data-tier="S"] { --tier-color: #ff8e53; }
.tier-banner[data-tier="S-"] { --tier-color: #ffb142; }
.tier-banner[data-tier="A"] { --tier-color: #f7d94c; }
.tier-banner[data-tier="B"] { --tier-color: #7bed9f; }
.tier-banner[data-tier="C"] { --tier-color: #55e6c1; }
.tier-banner[data-tier="D"] { --tier-color: #70a1ff; }
.tier-banner[data-tier="E"] { --tier-color: #a29bfe; }
.tier-banner[data-tier="F"] { --tier-color: #8e8ea0; }

.monster-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 4px;
}

.monster-card:hover { border-color: var(--accent); }

.card-icon {
  background: none;
  border: 0;
  border-radius: 8px;
  padding: 0;
  cursor: pointer;
  -webkit-touch-callout: none;
  user-select: none;
}

.card-icon:hover .monster-icon,
.card-icon:focus-visible .monster-icon {
  box-shadow: 0 0 0 2px var(--accent);
}

.card-name {
  width: 100%;
  overflow: hidden;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.8rem;
}

.card-toggles {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 3px;
}

/* Forces the item after it onto a new row, so the Skin button sits under the form buttons */
.toggle-break { flex-basis: 100%; height: 0; }

/* ==========================================================================
   Monster icon and element frame
   ========================================================================== */

.monster-icon {
  position: relative;
  width: 48px;
  height: 48px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  overflow: hidden;
  display: inline-block;
}

/* Element frame, drawn over the portrait. One PNG per element in assets/icons/frames/. */
.monster-icon[data-element] {
  border: 0;
  border-radius: 6px;
}

.monster-icon[data-element]::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: var(--frame) center / 100% 100% no-repeat;
}

/* Paths are relative to this stylesheet */
.monster-icon[data-element="Fire"] { --frame: url('../assets/icons/frames/Fire.png'); }
.monster-icon[data-element="Water"] { --frame: url('../assets/icons/frames/Water.png'); }
.monster-icon[data-element="Earth"] { --frame: url('../assets/icons/frames/Earth.png'); }
.monster-icon[data-element="Storm"] { --frame: url('../assets/icons/frames/Storm.png'); }
.monster-icon[data-element="Holy"] { --frame: url('../assets/icons/frames/Holy.png'); }
.monster-icon[data-element="Shadow"] { --frame: url('../assets/icons/frames/Shadow.png'); }

.monster-icon img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.monster-icon-fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: 'Exo 2', sans-serif;
  font-weight: 700;
  color: var(--muted);
}

/* ==========================================================================
   Stats card (hover, or press-and-hold on touch).
   Mouse mode ignores the pointer so it never blocks the icon.
   ========================================================================== */

.stat-tip {
  position: fixed;
  z-index: 20;
  max-width: 240px;
  background: var(--panel-2);
  border: 1px solid var(--accent);
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.5);
  padding: 8px 10px;
  pointer-events: none;
}

.stat-tip.is-touch { pointer-events: auto; }

.stat-tip p { margin: 0; }

.stat-tip-name {
  font-family: 'Exo 2', sans-serif;
  font-weight: 700;
}

.stat-tip-dex {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.85rem;
}

.stat-tip-meta {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 2px;
}

.stat-tip-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 12px;
  margin: 6px 0;
}

.stat-tip-stats div {
  display: flex;
  justify-content: space-between;
}

.stat-tip-stats dt { color: var(--muted); }
.stat-tip-stats dd { margin: 0; font-weight: 600; }

.stat-tip a { color: var(--accent); }

/* ==========================================================================
   Responsive
   ========================================================================== */

/* Under 600px: smaller logo, padding and text. Squad slots show icon and cost only, 40px tiles so 8 fit per row. */
@media (max-width: 600px) {
  header.site-header { padding: 8px 12px 4px; }
  .lang-btn { width: 36px; height: 24px; }
  .squad-slots { grid-template-columns: repeat(auto-fill, 40px); gap: 3px; }
  .slot { aspect-ratio: 40 / 60; padding: 16px 1px 2px; }
  .slot .monster-icon { width: 74%; }
  .slot .slot-secret { width: 12px; height: 12px; font-size: 0.5rem; top: 1px; left: 1px; }
  .slot .slot-remove { top: 0; right: 0; font-size: 0.9rem; }
  header .header-logo { max-width: 280px; }

  .layout { padding: 8px 8px 24px; gap: 8px; }
  .panel { padding: 8px; }
  .squad-controls { gap: 6px; margin-bottom: 8px; }
  .squad-controls select, .squad-controls input, .search-row input[type="search"] { padding: 6px 8px; }

  .squad-totals { margin-bottom: 6px; }
  .squad-totals strong { font-size: 1rem; }

  /* Too narrow for the flags beside Save and the hint: they take their own row under the controls */
  .squad-controls .lang-switch { grid-row: 3; }

  .slot-name { display: none; }

  .monster-grid { grid-template-columns: repeat(auto-fill, minmax(68px, 1fr)); gap: 6px; }
  .monster-card { padding: 4px 2px; }
  .monster-icon { width: 42px; height: 42px; }
  .toggle-btn { font-size: 0.6rem; padding: 1px 5px; }
  /* Slot buttons were 12px. 28px is the most that fits the slot without a layout change; not device-tested. */
  .slot .slot-secret { width: 28px; height: 28px; font-size: 0.8rem; top: 2px; left: 2px; }
  .slot .slot-remove { width: 28px; height: 28px; top: 0; right: 0; font-size: 1rem; }
  .filters-panel { grid-template-columns: 1fr; }
}
