/* ============================================================================
   WOS Calculator — v2 design system  ("Frost")
   ----------------------------------------------------------------------------
   Mobile-first. Every rule below targets phones by default; tablet and desktop
   are layered on with min-width queries at the bottom of each section.

   Compatibility: this file is a drop-in replacement for css/styles.css. It
   re-declares every legacy custom property name (--card-bg, --border, --text,
   --primary, --radius, --transition …) so page-specific sheets that consume
   them (territory-planner.css, the Exalted Troops block in waracademy.html)
   keep working untouched.
   ========================================================================== */

/* ---------------------------------------------------------------- A. Tokens */

:root {
  /* Dark frost is the default surface — the theme the site is designed in. */
  --bg:            #070b16;
  --background:    #070b16;
  --surface:       rgba(148, 183, 236, 0.07);
  --surface-2:     rgba(148, 183, 236, 0.12);
  --surface-solid: #111a2e;
  --card-bg:       rgba(17, 26, 46, 0.72);
  --text:          #eaf2ff;
  --text-light:    #94a9c9;
  --text-dim:      #6b7f9e;
  --border:        rgba(148, 197, 255, 0.16);
  --border-strong: rgba(148, 197, 255, 0.3);

  --primary:       #38bdf8;
  --primary-dark:  #0ea5e9;
  --primary-light: #7dd3fc;
  --accent:        #a78bfa;
  --accent-soft:   rgba(167, 139, 250, 0.16);
  --ok:            #34d399;
  --warn:          #fbbf24;
  --danger:        #fb7185;

  --input-bg:      rgba(9, 15, 28, 0.85);
  --slot-bg:       rgba(148, 183, 236, 0.08);
  --select-bg:     rgba(9, 15, 28, 0.85);
  --ring:          0 0 0 3px rgba(56, 189, 248, 0.35);

  --shadow-sm: 0 1px 2px rgba(2, 6, 18, 0.5);
  --shadow:    0 2px 10px rgba(2, 6, 18, 0.55);
  --shadow-md: 0 10px 28px rgba(2, 6, 18, 0.6);
  --shadow-lg: 0 24px 60px rgba(2, 6, 18, 0.7);
  --glow:      0 0 24px rgba(56, 189, 248, 0.22);

  --radius-sm: 10px;
  --radius:    16px;
  --radius-lg: 22px;
  --radius-pill: 999px;

  --nav-h: 56px;
  --pad: 0.85rem;
  --gap: 0.6rem;

  --transition: all 0.22s cubic-bezier(0.4, 0, 0.2, 1);
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

  color-scheme: dark;
}

body[data-theme="light"] {
  --bg:            #dde7f4;
  --background:    #dde7f4;
  --surface:       rgba(236, 243, 252, 0.9);
  --surface-2:     #e3ecf8;
  --surface-solid: #ffffff;
  --card-bg:       #ffffff;
  --text:          #0d1d33;
  --text-light:    #47617f;
  --text-dim:      #6b829e;
  --border:        rgba(13, 45, 85, 0.16);
  --border-strong: rgba(13, 45, 85, 0.3);

  --primary:       #0284c7;
  --primary-dark:  #0369a1;
  --primary-light: #38bdf8;
  --accent:        #7c3aed;
  --accent-soft:   rgba(124, 58, 237, 0.12);
  --ok:            #059669;
  --warn:          #b45309;
  --danger:        #e11d48;

  --input-bg:      #ffffff;
  --slot-bg:       rgba(14, 116, 190, 0.06);
  --select-bg:     #ffffff;

  --shadow-sm: 0 1px 3px rgba(15, 45, 85, 0.12);
  --shadow:    0 4px 14px rgba(15, 45, 85, 0.14);
  --shadow-md: 0 12px 30px rgba(15, 45, 85, 0.18);
  --shadow-lg: 0 24px 60px rgba(15, 45, 85, 0.24);
  --glow:      0 0 24px rgba(2, 132, 199, 0.16);

  color-scheme: light;
}

/* ------------------------------------------------------------ B. Reset/base */

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  padding: calc(var(--nav-h) + 0.75rem) var(--pad) 2rem;
  min-height: 100vh;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.3s ease, color 0.3s ease;
}

/* Aurora wash behind everything — cheap, fixed, never repaints on scroll. */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(60% 45% at 12% 0%, rgba(56, 189, 248, 0.16), transparent 70%),
    radial-gradient(55% 40% at 92% 8%, rgba(167, 139, 250, 0.14), transparent 70%),
    radial-gradient(70% 50% at 50% 100%, rgba(45, 212, 191, 0.08), transparent 75%);
}

body[data-theme="light"]::before {
  background:
    radial-gradient(60% 45% at 12% 0%, rgba(2, 132, 199, 0.12), transparent 70%),
    radial-gradient(55% 40% at 92% 8%, rgba(124, 58, 237, 0.1), transparent 70%);
}

img { max-width: 100%; height: auto; }

::selection { background: rgba(56, 189, 248, 0.3); }

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 6px;
}

/* --------------------------------------------------------- C. Typography */

h1, h2, h3, h4 {
  margin: 0 0 0.6rem;
  line-height: 1.25;
  font-weight: 700;
  letter-spacing: -0.01em;
}

h1 { font-size: clamp(1.5rem, 5vw, 2.25rem); }
h2 { font-size: clamp(1.15rem, 3.6vw, 1.5rem); }
h3 { font-size: clamp(1rem, 3vw, 1.2rem); }

p { margin: 0 0 0.75rem; }

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-light); }

/* Section headings get an icy tick so long calculator pages stay scannable. */
main section > h2:first-child,
main section > h3:first-child {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text);
}

main section > h2:first-child::before,
main section > h3:first-child::before {
  content: "";
  flex: 0 0 auto;
  width: 4px;
  height: 1.1em;
  border-radius: var(--radius-pill);
  background: linear-gradient(180deg, var(--primary), var(--accent));
  box-shadow: var(--glow);
}

/* --------------------------------------------------- D. App bar and drawer */

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 0.35rem;
  height: var(--nav-h);
  padding: 0 0.6rem;
  background: color-mix(in srgb, var(--surface-solid) 82%, transparent);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  -webkit-backdrop-filter: blur(14px) saturate(160%);
  backdrop-filter: blur(14px) saturate(160%);
}

body[data-theme="light"] .navbar {
  background: rgba(255, 255, 255, 0.85);
}

/* Brand injected by ui-v2.js so no page markup has to change. */
.v2-brand {
  grid-column: 1;
  display: flex;
  align-items: center;
  justify-self: start;
  padding: 0;
}

/* Right-hand cluster, grouped by ui-v2.js */
.v2-controls {
  grid-column: 3;
  display: flex;
  align-items: center;
  justify-self: end;
  gap: 0.3rem;
}

/* Wordmark banner: roughly 4.4:1, so height drives the width. Capped so it
   never crowds the nav links or the controls. */
.v2-brand img {
  width: auto;
  height: calc(var(--nav-h) - 26px);
  max-width: 150px;
  object-fit: contain;
  transition: var(--transition);
}

.v2-brand:hover img { transform: scale(1.06); }

/* Hamburger (navbar.js creates it; we only style it). */
.nav-hamburger {
  order: 9;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 44px;
  height: 44px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
}

.nav-hamburger .bar {
  display: block;
  height: 2px;
  width: 100%;
  border-radius: 2px;
  background: var(--text);
  transition: var(--transition);
}

.navbar.nav-open .nav-hamburger .bar:nth-child(1) { transform: translateY(6px) rotate(45deg); }
.navbar.nav-open .nav-hamburger .bar:nth-child(2) { opacity: 0; }
.navbar.nav-open .nav-hamburger .bar:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }

/* The drawer itself is built by ui-v2.js and placed right after .navbar. */
.v2-drawer {
  grid-column: 2;
  position: fixed;
  top: var(--nav-h);
  bottom: 0;
  right: 0;
  z-index: 999;
  width: min(84vw, 340px);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.75rem 0.75rem calc(1.5rem + env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface-solid);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  transform: translateX(105%);
  visibility: hidden;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1), visibility 0.28s;
}

.navbar.nav-open ~ .v2-drawer {
  transform: translateX(0);
  visibility: visible;
}

.v2-backdrop {
  position: fixed;
  inset: var(--nav-h) 0 0;
  z-index: 998;
  background: rgba(3, 7, 18, 0.55);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s;
}

.v2-backdrop.show { opacity: 1; visibility: visible; }

body.v2-locked { overflow: hidden; }

.v2-drawer-title {
  padding: 0.5rem 0.65rem 0.25rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 48px;
  padding: 0 0.85rem;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-light);
  transition: var(--transition);
}

.nav-item:hover { background: var(--surface); color: var(--text); }

.nav-item.active {
  color: var(--text);
  background: linear-gradient(90deg, rgba(56, 189, 248, 0.2), transparent);
  box-shadow: inset 2px 0 0 var(--primary);
}

.language-selector,
.dark-mode-toggle,
.nav-hamburger,
#globalSettingsBtn { flex: 0 0 auto; }

.dark-mode-toggle { display: flex; align-items: center; }
#globalSettingsBtn { min-width: 40px; min-height: 40px; color: var(--text-light) !important; }

/* Very narrow phones: drop the wordmark so the controls never wrap. */
@media (max-width: 430px) {
  :root { --pad: 0.7rem; }
  .navbar { gap: 0.15rem; padding: 0 0.35rem; }
  .v2-brand img { height: calc(var(--nav-h) - 28px); max-width: 118px; }
  .dropdown-toggle { padding: 0.3rem 0.35rem; }
  .switch { width: 44px; height: 26px; }
  .slider::before { width: 18px; height: 18px; }
  .switch input:checked + .slider::before { transform: translateX(18px); }
  #globalSettingsBtn { padding: 0 0.15rem !important; }
}

/* ------------------------------------------------------- E. Form controls */

input, select, button, textarea { font-family: inherit; }

/* 16px keeps iOS Safari from zooming the viewport on focus. */
input[type="number"], input[type="text"], select, textarea {
  width: 100%;
  min-height: 48px;
  padding: 0.6rem 0.75rem;
  font-size: 16px;
  color: var(--text);
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: var(--transition);
  appearance: none;
  -webkit-appearance: none;
}

select {
  background-image: linear-gradient(45deg, transparent 50%, var(--text-light) 50%),
                    linear-gradient(135deg, var(--text-light) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 2rem;
  cursor: pointer;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: var(--ring);
}

/* Spinners eat width on phones and are useless with a numeric keypad. */
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

button {
  min-height: 48px;
  padding: 0.7rem 1.1rem;
  font-size: 1rem;
  font-weight: 700;
  color: #04121f;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  border: none;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: var(--transition);
  touch-action: manipulation;
}

button:hover { filter: brightness(1.08); box-shadow: var(--shadow), var(--glow); }
button:active { transform: translateY(1px); }

/* Page-level primary action. */
#calculate-btn, main > button {
  display: block;
  width: 100%;
  margin: 1rem 0;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
}

/* Theme switch (markup: label.switch > input + span.slider). */
.switch { position: relative; display: inline-block; width: 52px; height: 30px; cursor: pointer; }
.switch input { opacity: 0; width: 0; height: 0; }

.slider {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  border: 1px solid var(--border);
  transition: var(--transition);
}

.slider::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary-light), var(--primary));
  transition: var(--transition);
}

.switch input:checked + .slider::before { transform: translateX(22px); }

.slider .sun-icon, .slider .moon-icon {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.68rem;
  color: var(--text-light);
  pointer-events: none;
}

.slider .sun-icon  { left: 8px; }
.slider .moon-icon { right: 8px; }

/* Language dropdown. */
.dropdown { position: relative; }

.dropdown-toggle {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  min-height: 40px;
  min-width: 44px;
  padding: 0.3rem 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: none;
}

.flag-icon { width: 22px; height: 16px; border-radius: 3px; object-fit: cover; display: block; }

.caret {
  width: 0; height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid var(--text-light);
}

.dropdown-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 1100;
  display: none;
  min-width: 190px;
  padding: 0.35rem;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
}

.dropdown-menu.show { display: block; }

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  width: 100%;
  min-height: 44px;
  padding: 0 0.6rem;
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
  background: none;
  border: none;
  border-radius: 8px;
  box-shadow: none;
  text-align: left;
}

.dropdown-item:hover { background: var(--surface); filter: none; }

/* ----------------------------------------------------------- F. Page shell */

main {
  display: block;
  max-width: 1200px;
  margin: 0 auto;
}

main > section,
.info-banner {
  margin: 0 0 var(--gap);
  padding: var(--pad);
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

main > section { scroll-margin-top: calc(var(--nav-h) + 8px); }

/* Intro strip. Deliberately compact - on a phone this used to fill the whole
   first screen before any calculator was visible. */
main > div.info-banner {
  position: relative;
  overflow: hidden;
  padding: 0.75rem 0.9rem 0.75rem 1.1rem;
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.16), rgba(167, 139, 250, 0.12));
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--primary);
  border-radius: var(--radius);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

main > div.info-banner h2 {
  margin-bottom: 0.2rem;
  font-size: 1rem;
  color: var(--text);
  border: none;
}

main > div.info-banner h2::before { display: none; }
main > div.info-banner h2 i { color: var(--primary); }

main > div.info-banner p {
  margin-bottom: 0;
  font-size: 0.85rem;
  line-height: 1.45;
  color: var(--text-light);
}

body[data-theme="light"] main > div.info-banner {
  background: linear-gradient(135deg, rgba(2, 132, 199, 0.1), rgba(124, 58, 237, 0.08));
}

/* ------------------------------------------------------------ G. Inventory */

#inventory {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem 0.6rem;
}

#inventory h2 { grid-column: 1 / -1; margin-bottom: 0.25rem; }

#inventory label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-light);
}

#inventory label input { min-height: 44px; }

/* A long field name (Research Speed Bonus) gets the full row. */
#inventory label[data-translate="research-speed"] { grid-column: 1 / -1; }

/* --------------------------------------------------------- H. "Set all" */

.global-set-all,
.piece-set-all {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.global-set-all-inputs {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.5rem 0.75rem;
}

.global-set-all-inputs > label { flex: 1 1 130px; }
.global-set-all-inputs > button { flex: 1 1 100%; }

/* Markup here is <label><span>From</span>: <select></label> - the bare colon
   is a text node, so a flex column would drop it onto a line of its own. */
.global-set-all-inputs label {
  display: block;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-light);
}

.global-set-all-inputs label > span { display: inline; }
.global-set-all-inputs label select { margin-top: 0.3rem; }



.piece-set-all {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem;
  margin-bottom: 0.6rem;
}

.piece-set-all > span { text-align: center; color: var(--text-dim); }

.piece-set-all-btn {
  grid-column: 1 / -1;
  min-height: 42px;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

/* --------------------------------------------------------- I. Charm pieces */

#charms { display: grid; gap: var(--gap); }

.charm-piece {
  padding: var(--pad);
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.charm-piece h2 {
  font-size: 1.05rem;
  padding-bottom: 0.5rem;
  margin-bottom: 0.6rem;
  border-bottom: 1px solid var(--border);
}

.form-labels {
  display: grid;
  grid-template-columns: 1fr 1fr 1.1fr;
  gap: 0.4rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
  padding: 0 0.15rem 0.3rem;
}

.form-labels .from-label,
.form-labels .to-label { text-align: left; }
.form-labels .result-label { text-align: right; }

.charm-slot {
  display: grid;
  grid-template-columns: 1fr 1.1fr;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem;
  margin-bottom: 0.45rem;
  background: var(--slot-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.slot-inputs { display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem; }

.charm-slot .result {
  font-size: 0.82rem;
  font-weight: 700;
  text-align: right;
  color: var(--primary-light);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

/* Some pages (hero gear, experts, troops) wrap their fields in an inner grid.
   #inventory is itself a grid here, so that wrapper would otherwise be stuffed
   into a single narrow column - it has to span the whole section. */
#inventory > div[class*="grid"],
#totals > div[class*="grid"] { grid-column: 1 / -1; }

/* Hero gear drops to one column under 480px; two-up matches the rest of the
   site (the extra id/element specificity beats the page's own sheet). */
@media (max-width: 480px) {
  #inventory > div.gear-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* Experts and Troops wrap fields in .inventory-grid / .totals-grid, whose own
   sheets force a single column on phones. Short numeric fields fit two-up. */
#inventory .inventory-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem 0.6rem;
}

#totals .totals-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
}

@media (min-width: 640px) {
  #inventory .inventory-grid { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
  #totals .totals-grid { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
}

/* ------------------------------------------------------------- I2. Pets */

/* Pets inventory wraps each field in .input-group rather than a bare label. */
#inventory .input-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

#inventory .input-group label {
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-light);
}

#inventory .input-group input { min-height: 44px; }

.pet-container,
.building-container {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
}

.pet-piece,
.building-piece {
  --rarity: var(--primary);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.7rem 0.55rem 0.75rem;
  text-align: center;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-top: 3px solid var(--rarity);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition: var(--transition);
}

.pet-piece:hover,
.building-piece:hover {
  border-color: var(--rarity);
  box-shadow: var(--shadow), 0 0 18px -6px var(--rarity);
}

/* Rarity accents, keyed off the tier the script already stamps on the card. */
.pet-piece[data-tier="Common"]   { --rarity: #94a3b8; }
.pet-piece[data-tier="Uncommon"] { --rarity: #34d399; }
.pet-piece[data-tier="Rare"]     { --rarity: #38bdf8; }
.pet-piece[data-tier="Epic"]     { --rarity: #a78bfa; }
.pet-piece[data-tier="Mythic"]   { --rarity: #fbbf24; }

/* Some portraits are cut out, others are square art with their own backdrop.
   A rounded tile with `cover` renders both consistently. */
.pet-image,
.building-image {
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: 128px;
  margin-bottom: 0.5rem;
  overflow: hidden;
  border-radius: 14px;
  background: radial-gradient(circle at 50% 30%, color-mix(in srgb, var(--rarity) 26%, transparent), transparent 72%),
              var(--slot-bg);
  border: 1px solid color-mix(in srgb, var(--rarity) 45%, transparent);
  box-shadow: inset 0 0 18px -6px color-mix(in srgb, var(--rarity) 60%, transparent);
}

.pet-img,
.building-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.pet-piece:hover .pet-img,
.building-piece:hover .building-img { transform: scale(1.05); }

.pet-piece h3,
.building-piece h2 {
  margin: 0 0 0.1rem;
  font-size: 0.92rem;
  line-height: 1.25;
  color: var(--text);
}

.pet-tier {
  margin-bottom: 0.5rem;
  padding: 0.1rem 0.5rem;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--rarity);
  background: color-mix(in srgb, var(--rarity) 14%, transparent);
  border-radius: var(--radius-pill);
}

.pet-piece .gear-header,
.building-piece .building-header {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  width: 100%;
  padding-bottom: 0.25rem;
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.pet-piece .gear-slot,
.building-piece .building-slot {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  width: 100%;
  margin-top: auto;
}

/* Two selects side by side in a half-width card need a tighter caret. */
.pet-piece .gear-slot select,
.building-piece .building-slot select {
  padding-left: 0.5rem;
  padding-right: 1.4rem;
  background-position: calc(100% - 12px) 50%, calc(100% - 8px) 50%;
  text-align: center;
}

@media (min-width: 560px) {
  .pet-container,
  .building-container { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (min-width: 900px) {
  .pet-container,
  .building-container { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
  .pet-image,
  .building-image { max-width: 150px; }
  .pet-piece h3,
  .building-piece h2 { font-size: 1rem; }
}

#fire-crystals { padding-top: 0.9rem; }

/* Opaque artwork (JPEG) sits on its own light tile so the white background
   reads as deliberate rather than as a broken cut-out. Supply a transparent
   PNG/WebP for these and the rule stops applying by itself. */
.building-image.is-photo {
  background: linear-gradient(180deg, #f7fafc, #dde6f2);
  border-color: rgba(148, 197, 255, 0.4);
}

.building-piece .building-header { line-height: 1.15; }

.building-piece h2 {
  font-size: 0.92rem;
  line-height: 1.25;
  padding: 0;
  border: none;
}

.building-piece h2::before { display: none; }

/* ------------------------------------------------------- I3. Chief Gear */

#chief-gear {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
}

.gear-piece {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem 0.6rem 0.8rem;
  text-align: center;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-top: 3px solid var(--primary);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  transition: var(--transition);
}

.gear-piece:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow), 0 0 20px -8px var(--primary);
}

.gear-icon {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  margin-bottom: 0.4rem;
  font-size: 1.4rem;
  line-height: 1;
  background: color-mix(in srgb, var(--primary) 15%, transparent);
  border: 1px solid color-mix(in srgb, var(--primary) 38%, transparent);
  border-radius: 13px;
}

.gear-piece h2 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
  line-height: 1.25;
  padding: 0;
  border: none;
}

.gear-piece h2::before { display: none; }

.gear-piece .gear-header {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  width: 100%;
  padding-bottom: 0.25rem;
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.gear-piece .gear-slot {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  width: 100%;
  margin-top: auto;
}

.gear-piece .gear-slot select {
  padding-left: 0.45rem;
  padding-right: 1.4rem !important;
  background-position: calc(100% - 12px) 50%, calc(100% - 8px) 50% !important;
  font-size: 0.9rem;
}

/* ---- Set All panel ---- */

.select-all-container {
  margin: 0 0 var(--gap);
  padding: var(--pad);
  background: var(--card-bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.set-all-hint {
  margin: -0.2rem 0 0.7rem;
  font-size: 0.82rem;
  color: var(--text-light);
}

.select-all-inputs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem 0.6rem;
  align-items: end;
}

.select-all-inputs label {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-light);
}

.select-all-inputs label select { margin-top: 0.3rem; }
.select-all-inputs > button { grid-column: 1 / -1; }

.set-all-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
}

.preset-btn {
  flex: 1 1 auto;
  min-height: 42px;
  padding: 0 0.7rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  box-shadow: none;
}

.preset-btn:hover {
  color: var(--primary);
  border-color: var(--primary);
  filter: none;
  box-shadow: none;
}

@media (min-width: 640px) {
  .select-all-inputs { grid-template-columns: 1fr 1fr auto; }
  .select-all-inputs > button { grid-column: auto; min-width: 150px; }
  .select-all-inputs label select { max-width: 240px; }
}

@media (min-width: 900px) {
  #chief-gear { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .gear-icon { width: 54px; height: 54px; font-size: 1.6rem; }
  .gear-piece h2 { font-size: 1.05rem; }
}

/* ------------------------------------------------- J. War Academy / units */

.unit-container { display: grid; gap: var(--gap); }

.unit-piece {
  padding: var(--pad);
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.unit-piece h2 {
  font-size: 1.05rem;
  padding-bottom: 0.5rem;
  margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.upgrade-header {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  padding: 0 0.15rem 0.35rem;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.upgrade-piece {
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  background: var(--slot-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.upgrade-piece label {
  display: block;
  margin-bottom: 0.4rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.upgrade-slot { display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem; }

/* Exalted Troops block (its CSS lives inline in waracademy.html) ----------- */

#exalted-troops .exalted-unit {
  min-width: 0;          /* was 300px - overflowed narrow phones inside a grid */
  flex: 1 1 auto;
}

#exalted-troops .exalted-node {
  flex-wrap: wrap;
  gap: 0.35rem 0.5rem;
}

#exalted-troops .exalted-node label { flex: 1 1 100%; font-size: 0.85rem; }
#exalted-troops .exalted-node .upgrade-slot { flex: 1 1 auto; width: 100%; }
/* The inline block sets `background:` as a shorthand, which wipes out the
   dropdown arrow, so it has to be reinstated here. */
#exalted-troops .exalted-node select {
  max-width: none;
  min-height: 44px;
  padding-right: 2rem;
  background-color: var(--select-bg);
  background-image: linear-gradient(45deg, transparent 50%, var(--text-light) 50%),
                    linear-gradient(135deg, var(--text-light) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
#exalted-troops .exalted-intro { font-size: 0.9rem; }

@media (min-width: 640px) {
  #exalted-troops .exalted-node label { flex: 1 1 auto; }
  #exalted-troops .exalted-node .upgrade-slot { width: auto; max-width: 9.5rem; }
}

/* --------------------------------------------------------------- K. Totals */

#totals { border-color: var(--border-strong); }

.totals-container {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
}

.total-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  padding: 0.7rem 0.5rem;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.total-icon { width: 34px; height: 34px; object-fit: contain; }

.total-item > span { font-size: 0.75rem; font-weight: 600; color: var(--text-light); }

.total-item .total-value {
  font-size: 0.78rem;
  color: var(--text-dim);
}

.total-item .total-value span {
  display: block;
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.gap-message {
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.35;
}

.gap-message.negative { color: var(--danger); }
.gap-message.positive { color: var(--ok); }
.gap-message .number { font-weight: 800; }

.yellow-warning, .warning { color: var(--warn); }

#research-time-needed {
  margin-top: 0.75rem;
  padding: 0.6rem 0.75rem;
  font-size: 0.88rem;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

#research-time-needed .hours { font-weight: 800; color: var(--text); }
#research-time-needed .hint { display: block; font-size: 0.75rem; color: var(--text-dim); }

/* ------------------------------------------------- L. Floating results bar */

.v2-results {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 900;
  transform: translateY(105%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface-solid) 88%, transparent);
  border-top: 1px solid var(--border-strong);
  box-shadow: 0 -8px 28px rgba(2, 6, 18, 0.45);
  -webkit-backdrop-filter: blur(14px) saturate(160%);
  backdrop-filter: blur(14px) saturate(160%);
}

.v2-results.show { transform: translateY(0); }

.v2-results-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  min-height: 56px;
  padding: 0.5rem 0.75rem;
  color: var(--text);
  background: none;
  border: none;
  box-shadow: none;
  text-align: left;
}

.v2-results-bar:hover { filter: none; box-shadow: none; }

.v2-results-chips {
  display: flex;
  flex: 1 1 auto;
  gap: 0.4rem;
  overflow-x: auto;
  scrollbar-width: none;
}

.v2-results-chips::-webkit-scrollbar { display: none; }

.v2-chip {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex: 0 0 auto;
  padding: 0.25rem 0.55rem;
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  font-variant-numeric: tabular-nums;
}

.v2-chip img { width: 16px; height: 16px; object-fit: contain; }
.v2-chip .v2-chip-label { color: var(--text-light); font-weight: 600; }

.v2-results-caret {
  flex: 0 0 auto;
  width: 30px;
  text-align: center;
  color: var(--text-light);
  transition: transform 0.25s ease;
}

.v2-results.open .v2-results-caret { transform: rotate(180deg); }

.v2-results-sheet {
  display: none;
  gap: 0.5rem;
  padding: 0 0.75rem 0.85rem;
  max-height: 55vh;
  overflow-y: auto;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.v2-results.open .v2-results-sheet { display: grid; }

.v2-sheet-row {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.55rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.v2-sheet-row .v2-sheet-label { font-size: 0.72rem; font-weight: 600; color: var(--text-light); }
.v2-sheet-row .v2-sheet-value { font-size: 1.05rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.v2-sheet-row .v2-sheet-gap { font-size: 0.7rem; }

/* Keep the page's last section clear of the bar, and lift the Discord bubble. */
body.v2-has-results { padding-bottom: 5.5rem; }
body.v2-has-results a[aria-label="Join our Discord server"] { bottom: 88px !important; }

/* --------------------------------------------------- M. Support / misc */

#support-me { text-align: center; }
#support-me p { color: var(--text-light); font-size: 0.92rem; margin-bottom: 0; }

.social-icons { display: flex; justify-content: center; gap: 0.75rem; flex-wrap: wrap; }
.social-icons a { display: inline-flex; width: 44px; height: 44px; align-items: center; justify-content: center;
                  border-radius: 50%; background: var(--surface); border: 1px solid var(--border); }

#update-log-popup, .popup {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(3, 7, 18, 0.7);
}

.popup-content {
  width: min(560px, 100%);
  max-height: 80vh;
  overflow-y: auto;
  padding: 1.25rem;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}

/* Controls should not stretch across a wide screen. */
@media (min-width: 640px) {
  #inventory label input { max-width: 280px; }
  .global-set-all-inputs select { max-width: 240px; }
  .piece-set-all select { max-width: 130px; }
  #calculate-btn, main > button { width: auto; min-width: 260px; margin-inline: auto; }
}

/* ================================================== N. Tablet — ≥ 640px  */

@media (min-width: 640px) {
  :root { --pad: 1.1rem; --gap: 1rem; }

  body { padding-left: 1.25rem; padding-right: 1.25rem; }

  #inventory { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
  #inventory h2 { grid-column: 1 / -1; }

  .global-set-all-inputs > label { flex: 0 1 200px; }
  .global-set-all-inputs > button { flex: 0 0 auto; min-width: 150px; }

  .piece-set-all { grid-template-columns: 1fr auto 1fr auto; }
  .piece-set-all-btn { grid-column: auto; }

  .totals-container { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

  #charms, .unit-container { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .v2-results-sheet { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* ================================================= O. Desktop — ≥ 1024px  */

@media (min-width: 1024px) {
  :root { --nav-h: 64px; --pad: 1.25rem; }

  body { padding-left: 2rem; padding-right: 2rem; }

  /* ui-v2.js moves the drawer into the bar and flags it .as-row. */
  .navbar { gap: 0.15rem; padding: 0 1rem; }
  .nav-hamburger { display: none; }
  /* Fill the middle grid track, then centre the links inside it. */
  .v2-backdrop { display: none; }
  .navbar { padding-inline: 1.1rem; }
  .v2-brand img { height: 38px; max-width: 176px; }
  .v2-brand { margin-right: 0.75rem; }

  /* The links row. One rule, so nothing further down can quietly undo it:
     it fills the middle grid track and centres the links inside it. */
  .v2-drawer.as-row {
    position: static;
    grid-column: 2;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
    justify-content: safe center;   /* keeps the start reachable when it scrolls */
    width: auto;                    /* drop the mobile drawer's fixed width */
    max-width: 100%;
    min-width: 0;
    margin: 0;
    gap: 0.1rem;
    padding: 0;
    overflow-x: auto;
    overflow-y: visible;
    scrollbar-width: none;
    background: none;
    border: none;
    box-shadow: none;
    transform: none;
    visibility: visible;
    -webkit-mask-image: linear-gradient(to right, transparent 0, #000 20px, #000 calc(100% - 20px), transparent 100%);
    mask-image: linear-gradient(to right, transparent 0, #000 20px, #000 calc(100% - 20px), transparent 100%);
  }

  .v2-drawer-title { display: none; }

  .v2-drawer.as-row::-webkit-scrollbar { display: none; }

  .nav-item {
    min-height: 34px;
    padding: 0 0.38rem;
    font-size: 0.78rem;
    font-weight: 600;
    white-space: nowrap;
    border-radius: var(--radius-pill);
  }

  .nav-item.active { box-shadow: none; background: var(--accent-soft); }

  #charms, .unit-container { grid-template-columns: repeat(3, minmax(0, 1fr)); }

  .charm-slot { grid-template-columns: 1fr 0.9fr; }

  /* Desktop shows the real totals section; the floating bar is mobile-only. */
  .v2-results { display: none; }
  body.v2-has-results { padding-bottom: 2rem; }
  body.v2-has-results a[aria-label="Join our Discord server"] { bottom: 20px !important; }
}

@media (min-width: 1560px) {
  #charms { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  /* Only widen the links once there is genuinely room for them. */
  .nav-item { padding: 0 0.5rem; }
}

/* ------------------------------------------- O2. Select caret, everywhere */

/* experts.css, troops.css and several inline page blocks set `background:` as
   a shorthand on selects, which silently removes the dropdown arrow. These
   load after this file, so the caret has to be re-asserted. */
select:not([multiple]):not([size]) {
  background-image:
    linear-gradient(45deg, transparent 50%, var(--text-light) 50%),
    linear-gradient(135deg, var(--text-light) 50%, transparent 50%) !important;
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50% !important;
  background-size: 5px 5px, 5px 5px !important;
  background-repeat: no-repeat !important;
  padding-right: 2rem !important;
}

/* Pet cards put two selects side by side, so they keep a tighter caret. */
.pet-piece .gear-slot select,
.building-piece .building-slot select {
  padding-right: 1.4rem !important;
  background-position: calc(100% - 12px) 50%, calc(100% - 8px) 50% !important;
}

/* ----------------------------------------------------- P. Motion / print */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

@media print {
  .navbar, .v2-drawer, .v2-backdrop, .v2-results, #support-me { display: none !important; }
  body { padding: 0; background: #fff; color: #000; }
}

/* App mode (Android TWA) hides the premium upsell — carried over from v1. */
html.app-mode .nav-item[href="premium.html"],
html.app-mode #google-ads { display: none !important; }
