/* English Defense display sizing. Scale whole interface panels together so
   text, sprites, canvas controls and their hit targets keep the same geometry.
   The page backgrounds stay at viewport size. */
:root { --defense-ui-scale: 1; }

@supports (zoom: 1) {
  @media (min-width: 1600px) { :root { --defense-ui-scale: 1.125; } }
  @media (min-width: 1800px) { :root { --defense-ui-scale: 1.25; } }
  @media (min-width: 2100px) { :root { --defense-ui-scale: 1.5; } }
  @media (min-width: 2500px) { :root { --defense-ui-scale: 1.75; } }
  @media (min-width: 2900px) { :root { --defense-ui-scale: 2; } }
  @media (min-width: 3300px) { :root { --defense-ui-scale: 2.25; } }
  @media (min-width: 3700px) { :root { --defense-ui-scale: 2.5; } }
  @media (min-width: 4400px) { :root { --defense-ui-scale: 3; } }
  @media (min-width: 5100px) { :root { --defense-ui-scale: 3.5; } }
  @media (min-width: 5800px) { :root { --defense-ui-scale: 4; } }

  body > .topbar,
  #app > .wrap:not(.home-wrap),
  #app > .battle-wrap,
  #app > .loading,
  #guide-main,
  body > .guide-page-header,
  body > dialog,
  body > footer,
  .home-wrap > .home-status,
  .home-wrap > .max-guide,
  .home-wrap > .home-bottom,
  .home-wrap > .info-banner {
    zoom: var(--defense-ui-scale);
  }

  @media (min-width: 1600px) {
    /* vw lengths are measured before element zoom. Account for that when
       centering the navigation, instead of magnifying its outer padding. */
    body > .topbar {
      padding-inline: max(24px, calc((100vw / var(--defense-ui-scale) - 1320px) / 2));
    }
    body > .guide-page-header {
      padding-inline: max(20px, calc((100vw / var(--defense-ui-scale) - 1120px) / 2));
    }
    #guide-main { min-height: calc(80vh / var(--defense-ui-scale)); }
  }
}

#app > .home-wrap { padding-bottom: 0; }

@media (min-width: 801px) {
  #app {
    min-height: calc(100svh - 154px * var(--defense-ui-scale));
  }

  /* Give the title the opening viewport. Keep the artwork's exact aspect
     ratio: its percentage-positioned menu buttons stay on the artwork. */
  #app > .home-wrap {
    max-width: none;
    width: 100%;
    padding: 0;
    box-shadow: none;
  }
  .home-wrap > .title-scene {
    width: min(100%, calc((100svh - 92px * var(--defense-ui-scale)) * 1672 / 941));
    margin-inline: auto;
    aspect-ratio: 1672 / 941;
    border-radius: 0;
    border: 0;
    box-shadow: none;
  }
  .home-wrap > .home-bottom,
  .home-wrap > .info-banner {
    max-width: 1320px;
    margin-inline: auto;
  }
  .home-wrap > .home-bottom { padding: 0 28px 35px; }
  .home-wrap > .info-banner { width: calc(100% - 56px); }
}

/* A short landscape window may need to scroll; keep a usable title rather
   than squeezing its menu into a tiny strip. Phones retain the mobile menu. */
@media (min-width: 801px) and (max-height: 500px) {
  .home-wrap > .title-scene { width: min(100%, 900px); }
}

/* Battles reserve real rows for ink and all six defenders. Only the field
   shrinks to the remaining height; the full 1280×580 world stays contained.
   This is scoped to an active battle, independent of result-page sizing. */
@media screen {
  body:has(> #app > .battle-wrap) {
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    overflow: hidden;
  }
  body:has(> #app > .battle-wrap) > #app {
    min-width: 0;
    min-height: 0;
    overflow: hidden;
  }
  body:has(> #app > .battle-wrap) > .topbar,
  body:has(> #app > .battle-wrap) > footer { zoom: 1; }
  body:has(> #app > .battle-wrap) > .topbar {
    padding-inline: max(16px, calc((100vw - 1720px) / 2));
  }
  #app > .battle-wrap {
    zoom: 1;
    width: min(100%, 1920px);
    height: 100%;
    max-width: none;
    min-width: 0;
    min-height: 0;
    padding: clamp(10px, 1.5dvh, 18px) max(14px, env(safe-area-inset-right)) clamp(10px, 1.5dvh, 18px) max(14px, env(safe-area-inset-left));
    display: flex;
    flex-direction: column;
  }
  #app > .battle-wrap .battle-head {
    flex: 0 0 auto;
    gap: 12px;
    margin-bottom: 10px;
  }
  #app > .battle-wrap .battle-head > div { min-width: 0; }
  #app > .battle-wrap .battle-head h2 { font-size: clamp(22px, 2.1vw, 32px); line-height: 1.1; }
  #app > .battle-wrap .battle-head .button { min-height: 40px; }
  #app > .battle-wrap .sandbox-battle-note { flex: 0 0 auto; margin: 0 0 8px; line-height: 1.4; }
  #app > .battle-wrap .battle-layout {
    flex: 1 1 0;
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) clamp(280px, 27vw, 420px);
    grid-template-rows: minmax(0, 1fr);
    align-items: stretch;
    gap: 14px;
  }
  #app > .battle-wrap .battle-field {
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto auto auto auto;
  }
  #app > .battle-wrap .field-frame {
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
  }
  #app > .battle-wrap .field-frame canvas {
    display: block;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    aspect-ratio: auto;
    object-fit: contain;
    object-position: center;
  }
  #app > .battle-wrap .field-hud { padding: 9px 14px; gap: 18px; }
  #app > .battle-wrap .base-health { min-width: 0; width: 45%; }
  #app > .battle-wrap .base-health span { gap: 6px; }
  #app > .battle-wrap .energy-bar { min-width: 0; gap: 12px; padding: 10px 0; }
  #app > .battle-wrap .energy-bar > span { white-space: nowrap; }
  #app > .battle-wrap .energy-bar progress { min-width: 0; }
  #app > .battle-wrap .deploy-grid { grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; padding-bottom: 5px; }
  #app > .battle-wrap .deploy {
    min-width: 0;
    min-height: 0;
    padding: 4px 4px 7px;
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto auto;
    justify-items: center;
    gap: 3px;
  }
  #app > .battle-wrap .deploy img { width: 100%; height: clamp(40px, 9dvh, 88px); object-fit: contain; }
  #app > .battle-wrap .deploy strong { min-width: 0; max-width: 100%; margin: 0; font-size: clamp(10px, .8vw, 12px); line-height: 1.2; }
  #app > .battle-wrap .deploy span { white-space: nowrap; line-height: 1.2; }
  #app > .battle-wrap .lock-alert { min-height: 0; padding-top: 3px; font-size: 11px; line-height: 1.4; overflow-wrap: anywhere; }
  #app > .battle-wrap .lock-alert:empty { padding: 0; }
  #app > .battle-wrap .battle-stats { margin: 5px 0 0; gap: 8px; font-size: 11px; line-height: 1.3; }
  #app > .battle-wrap .battle-stats > span { min-width: 0; }
  #app > .battle-wrap .question-panel {
    min-width: 0;
    min-height: 0;
    height: 100%;
    padding: clamp(14px, 1.3vw, 24px);
    overflow: auto;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
    scrollbar-color: #bba674 #fff9eb;
  }
  #app > .battle-wrap .question-panel:focus-visible { outline: 3px solid #368271; outline-offset: -5px; }
  #app > .battle-wrap .question-panel .answers { display: flex; flex-direction: column; }
  #app > .battle-wrap .question-panel .answer { min-height: 44px; font-size: clamp(12px, 1vw, 15px); }
  #app > .battle-wrap .battle-instructions { margin: 18px 0; }
  #app > .battle-wrap .battle-instructions h3 { font-size: clamp(23px, 2.1vw, 31px); line-height: 1.15; }
  #app > .battle-wrap .battle-instructions p { margin-bottom: 0; font-size: 14px; }
  #app > .battle-wrap .battle-feedback-next { margin: 12px 0 0; font-size: 12px; line-height: 1.5; }
  #app > .battle-wrap .pause-overlay > div { max-height: 100%; overflow: auto; padding: 10px; }
  #app > .battle-wrap .pause-overlay h2 { font-size: clamp(22px, 3vw, 36px); margin-bottom: 8px; }
  #app > .battle-wrap .pause-overlay .button { min-height: 44px; padding: 8px 16px; }
}

/* Portrait phones keep deployment above a separate question reading area.
   Later sandbox mobile rules must not restore tall headers or card portraits. */
@media screen and (max-width: 800px) {
  body:has(> #app > .battle-wrap) > footer { display: none; }
  #app > .battle-wrap { padding: 8px max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left)); }
  #app > .battle-wrap .battle-head { gap: 8px; flex-wrap: nowrap; margin-bottom: 8px; }
  #app > .battle-wrap .battle-head h2 { font-size: 22px; }
  #app > .battle-wrap .battle-head small { font-size: 9px; letter-spacing: 1px; line-height: 1.3; display: block; }
  #app > .battle-wrap .sandbox-battle-actions { gap: 5px; justify-content: flex-end; }
  #app > .battle-wrap .battle-head .button { min-height: 38px; padding: 7px 9px; font-size: 11px; }
  #app > .battle-wrap .sandbox-battle-note { margin: 0 0 6px; font-size: 10px; }
  #app > .battle-wrap .battle-layout { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) clamp(150px, 28dvh, 240px); gap: 8px; }
  #app > .battle-wrap .field-hud { padding: 6px 8px; gap: 10px; font-size: 9px; }
  #app > .battle-wrap .base-health span { margin-bottom: 4px; }
  #app > .battle-wrap .energy-bar { padding: 7px 0; gap: 7px; font-size: 10px; }
  #app > .battle-wrap .deploy-grid { gap: 4px; }
  #app > .battle-wrap .deploy { padding: 3px 2px 5px; gap: 2px; }
  #app > .battle-wrap .deploy img { height: clamp(32px, 7dvh, 54px); }
  #app > .battle-wrap .deploy strong { font-size: 9px; }
  #app > .battle-wrap .deploy span { font-size: 9px; padding: 3px 4px; }
  #app > .battle-wrap .lock-alert, #app > .battle-wrap .battle-stats { font-size: 9px; }
  #app > .battle-wrap .battle-stats { margin-top: 3px; gap: 5px; }
  #app > .battle-wrap .question-panel { padding: 12px; }
  #app > .battle-wrap .battle-instructions { margin: 10px 0; }
  #app > .battle-wrap .battle-instructions h3 { font-size: 22px; }
}

/* The home logo, wallet, sound and in-battle exit remain reachable when the
   full navigation/footer would use most of a short window. */
@media screen and (max-height: 760px) {
  body:has(> #app > .battle-wrap) > footer { display: none; }
}
@media screen and (max-height: 600px) {
  body:has(> #app > .battle-wrap) > .topbar {
    zoom: 1;
    height: 58px;
    min-height: 58px;
    padding: 5px max(12px, env(safe-area-inset-right)) 5px max(12px, env(safe-area-inset-left));
    flex-wrap: nowrap;
    gap: 10px;
  }
  body:has(> #app > .battle-wrap) > .topbar .brand { width: 138px; height: 46px; flex-shrink: 0; }
  body:has(> #app > .battle-wrap) > .topbar nav { display: none; }
  body:has(> #app > .battle-wrap) > .topbar .wallet { margin-left: auto; }
  #app > .battle-wrap { padding-block: 6px; }
  #app > .battle-wrap .battle-head { margin-bottom: 6px; gap: 6px; }
  #app > .battle-wrap .battle-head h2 { font-size: 20px; }
  #app > .battle-wrap .battle-head small { font-size: 9px; letter-spacing: 1px; }
  #app > .battle-wrap .battle-head .button { min-height: 36px; padding: 7px 9px; font-size: 11px; }
  #app > .battle-wrap .sandbox-battle-note { margin: 0 0 5px; font-size: 10px; line-height: 1.3; }
  #app > .battle-wrap .field-hud { padding: 5px 8px; font-size: 9px; }
  #app > .battle-wrap .base-health span { margin-bottom: 3px; }
  #app > .battle-wrap .energy-bar { padding: 6px 0; font-size: 10px; gap: 7px; }
  #app > .battle-wrap .deploy { padding-block: 3px 5px; gap: 2px; }
  #app > .battle-wrap .deploy img { height: 32px; }
  #app > .battle-wrap .deploy strong { font-size: 9px; }
  #app > .battle-wrap .deploy span { font-size: 9px; padding: 2px 4px; }
  #app > .battle-wrap .lock-alert, #app > .battle-wrap .battle-stats { font-size: 9px; }
  #app > .battle-wrap .battle-stats { margin-top: 3px; }
  #app > .battle-wrap .pause-overlay p { display: none; }
}
@media screen and (max-width: 800px) and (max-height: 600px) {
  #app > .battle-wrap .battle-layout { grid-template-rows: minmax(0, 1fr) clamp(110px, 23dvh, 138px); }
}
/* Short landscape windows use columns even below the normal mobile breakpoint. */
@media screen and (min-width: 560px) and (max-height: 500px) {
  #app > .battle-wrap .battle-layout { grid-template-columns: minmax(0, 1fr) clamp(210px, 34vw, 300px); grid-template-rows: minmax(0, 1fr); gap: 10px; }
  #app > .battle-wrap .question-panel { padding: 12px; }
}
/* Extreme zoom or an open keyboard can leave less height than the battle
   heading alone. Scroll the battle panel then, with positive space for both
   panes, instead of letting the question row collapse the field to zero. */
@media screen and (max-height: 340px), screen and (max-width: 559px) and (max-height: 440px) {
  #app > .battle-wrap { overflow-y: auto; overscroll-behavior: contain; }
  #app > .battle-wrap .battle-layout { flex: 0 0 240px; min-height: 240px; grid-template-rows: minmax(0, 1fr); }
  #app > .battle-wrap .battle-field { grid-template-rows: minmax(80px, 1fr) auto auto auto auto; }
}
@media screen and (max-width: 559px) and (max-height: 440px) {
  #app > .battle-wrap .battle-layout { flex-basis: 408px; min-height: 408px; grid-template-rows: 240px 160px; }
}

@media print {
  :root { --defense-ui-scale: 1; }
  .home-wrap > .title-scene { width: 100%; }
}
