/* One viewport contract. Values affect DOM UI only; canvas/world transforms are untouched. */
:root {
  font-size: calc(16px * var(--md-ui-scale, 1));
  --md-ui-scale: 1; --md-menu-scale: 1; --md-hud-scale: 1;
  --md-vw: 100vw; --md-vh: 100dvh; --md-vx: 0px; --md-vy: 0px;
  --md-gutter: clamp(6px, 1.2vmin, 14px);
  --md-left: max(var(--md-gutter), var(--safe-left, 0px));
  --md-right: max(var(--md-gutter), var(--safe-right, 0px));
  --md-top: max(var(--md-gutter), var(--safe-top, 0px));
  --md-bottom: max(var(--md-gutter), var(--safe-bottom, 0px));
  --md-control-height: max(44px, calc(44px * var(--md-ui-scale)));
  --md-font: clamp(12px, calc(14px * var(--md-ui-scale)), 17px);
  --md-title: clamp(20px, calc(26px * var(--md-ui-scale)), 32px);
  --md-gap: clamp(5px, calc(8px * var(--md-ui-scale)), 12px);
}
.settings-shell { font-size: calc(1rem * var(--text-scale, 1)); }
.settings-shell :is(select,button) { font-size: max(11px,calc(14px * var(--md-ui-scale))); }
#md-landscape-required { display: none; }
html.md-android-portrait #brightness { visibility: hidden; pointer-events: none; }
html.md-android-portrait #md-landscape-required {
  display: grid; place-items: center; position: fixed; inset: 0;
  z-index: 2147483647; box-sizing: border-box;
  padding: max(24px,var(--safe-top,0px)) max(24px,var(--safe-right,0px)) max(24px,var(--safe-bottom,0px)) max(24px,var(--safe-left,0px));
  background: radial-gradient(ellipse at center,#173a4b,#06121c 70%);
  color: #f5e1a4; text-align: center; font: 800 clamp(18px,4vw,28px)/1.5 system-ui,sans-serif;
  letter-spacing: .06em;
}
html[data-md-mobile=true] {
  --screen-padding: var(--md-gutter); --card-gap: var(--md-gap);
  --screen-insets: var(--md-top) var(--md-right) var(--md-bottom) var(--md-left);
  --compact-rank-height: clamp(30px, calc(34px * var(--md-ui-scale)), 42px);
}
html[data-md-mobile=true] #brightness { height: var(--md-vh); max-height: var(--md-vh); }
html[data-md-mobile=true] #app { min-width: 0; min-height: 0; }
html[data-md-mobile=true] :is(#app.game,#app.online-screen) { --battle-left: var(--md-left); --battle-right: var(--md-right); --battle-top: var(--md-top); --battle-bottom: var(--md-bottom); }
html[data-md-mobile=true] #app.settings-page { padding: var(--screen-insets); }
html[data-md-mobile=true] #app .settings-shell { height: 100%; max-height: 100%; width: min(1080px, 100%); grid-template-rows: auto minmax(0,1fr) auto; }
html[data-md-mobile=true] #app .settings-shell header,
html[data-md-mobile=true] #app .settings-shell footer { padding: 4px 8px; }
html[data-md-mobile=true] #app .settings-shell header h1 { font-size: var(--md-title); }
html[data-md-mobile=true] #app .settings-layout { grid-template-columns: minmax(max-content,clamp(90px,18vw,155px)) minmax(0,1fr); min-height: 0; }
html[data-md-mobile=true] #app .settings-content { padding: 6px 10px; min-width: 0; overflow-y: auto; }
html[data-md-mobile=true] #app .setting-control { grid-template-columns: minmax(0,1fr) clamp(104px,24vw,180px) !important; padding-block: calc(6px * var(--md-ui-scale)); gap: 3px 8px; }
html[data-md-mobile=true] #app .setting-control > span,
html[data-md-mobile=true] #app .settings-content :is(label,p) { font-size: var(--md-font); }
html[data-md-mobile=true] #app .setting-control > small { font-size: max(11px,calc(12px * var(--md-ui-scale))); }
html[data-md-mobile=true] #app .settings-content :is(h2,h3) { font-size: calc(17px * var(--md-ui-scale)); }
html[data-md-mobile=true] #app .settings-shell :is(button,select,input:not([type=checkbox]):not([type=radio])) { min-height: var(--md-control-height); }
html[data-md-mobile=true] #app .settings-shell footer { display: flex; flex-wrap: wrap; gap: 4px; }
html[data-md-mobile=true] #app .settings-shell footer button { font-size: max(11px,calc(12px * var(--md-ui-scale))); min-width: 44px; }
html[data-md-mobile=true] #app .settings-shell footer .save-status:empty { display: none; }
/* Form family: one scroll owner, no autofocus and no sticky button obscuring fields. */
#app.menu-bg > .card:has(>form), main.menu-bg > .card:has(>form) {
  box-sizing: border-box; min-width: 0; max-width: 100%;
  max-height: 100%; overflow-y: auto; overscroll-behavior: contain; touch-action: pan-y;
}
html[data-md-mobile=true] #app.menu-bg:has(> .card > form) {
  display: grid; place-items: center; padding: var(--screen-insets);
}
html[data-md-mobile=true] #app.menu-bg > .card:has(>form) {
  width: min(760px,100%); padding: calc(10px * var(--md-ui-scale)); margin: 0;
}
html[data-md-mobile=true] #app.menu-bg > .card:has(>form) > h1 { font-size: var(--md-title); margin: 4px 0 8px; }
html[data-md-mobile=true] #app.menu-bg > .card:has(>form) .eyebrow { font-size: 9px; margin: 0; }
html[data-md-mobile=true] #app.menu-bg > .card > form { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: var(--md-gap); }
html[data-md-mobile=true] #app.menu-bg > .card > form > label { min-width: 0; margin: 0; font-size: var(--md-font); }
html[data-md-mobile=true] #app.menu-bg > .card :is(input:not([type=checkbox]):not([type=radio]),button) { min-height: var(--md-control-height); font-size: max(16px,calc(16px * var(--md-ui-scale))); }
html[data-md-mobile=true] #app.menu-bg > .card > form > :is(p,.error) { grid-column: 1/-1; margin: 0; overflow-wrap: anywhere; }
html[data-md-mobile=true] #app.menu-bg > .card > form .remember-account { display: flex; align-items: center; gap: 7px; min-height: 30px; grid-column: 1/-1; }
html[data-md-mobile=true] #app.menu-bg > .card .back { position: static; margin: 0; }
html.md-keyboard-active #brightness { position: fixed; top: var(--md-vy); left: var(--md-vx); width: var(--md-vw); height: var(--md-vh); max-height: var(--md-vh); }
html.md-keyboard-active #account-rank-header { display: none; }
html.md-keyboard-active #brightness.account-rank-menu { grid-template-rows: minmax(0,1fr); }
html.md-keyboard-active #app.menu-bg:has(> .card > form) { padding: 4px var(--md-right) 4px var(--md-left); align-items: stretch; }
html.md-keyboard-active #app.menu-bg > .card:has(>form) { width: min(900px,100%); max-height: 100%; padding: 6px 10px; }
html.md-keyboard-active #app.menu-bg > .card > .eyebrow { display: none; }
html.md-keyboard-active #app.menu-bg > .card > h1 { font-size: 18px; margin: 0 0 5px; }
html.md-keyboard-active #app .account-shell { max-height: 100%; }
/* Confirmations share the actual visible viewport, including keyboard offsets. */
.md-keyboard-active :is(.game-confirm,.settings-unsaved) { position: fixed; inset: var(--md-vy) auto auto var(--md-vx); width: var(--md-vw); height: var(--md-vh); padding: 4px var(--md-right) 4px var(--md-left); box-sizing: border-box; }
html[data-md-mobile=true] :is(.game-confirm,.settings-unsaved) > section { box-sizing: border-box; max-width: min(740px,100%); max-height: calc(var(--md-vh) - var(--md-top) - var(--md-bottom)); overflow-y: auto; overscroll-behavior: contain; font-size: var(--md-font); padding: calc(12px * var(--md-ui-scale)); }
html[data-md-mobile=true] :is(.game-confirm,.settings-unsaved) button { min-height: var(--md-control-height); min-width: 70px; }
html[data-md-mobile=true] :is(.game-confirm,.settings-unsaved) input { min-height: var(--md-control-height); font-size: 16px; }
/* Global preference is a bounded size bias, never a transform of the world. */
html[data-md-mobile=true] :is(.account-content,.information-content,.terms-body,.account-ranks-list,.daily-season-state) { font-size: var(--md-font); }
html[data-md-mobile=true] #app :is(.information-back,.account-shell footer button,.terms-footer button,.ranks-back,.daily-back) { min-height: var(--md-control-height); font-size: var(--md-font); }
html[data-md-mobile=true] #app.online-screen .online-tactical-a .online-loadout button { flex-basis: clamp(56px,calc(66px * var(--md-ui-scale)),80px); height: clamp(56px,calc(66px * var(--md-ui-scale)),80px); min-height: 44px; }
html[data-md-mobile=true] #app.online-screen .online-tactical-a .online-loadout button :is(strong,span,b) { font-size: max(10px,calc(11px * var(--md-ui-scale))); }
#app.online-screen [data-online-back] { min-height: var(--md-control-height); min-width: 76px; }
#app.online-screen .online-tactical-b { overflow: visible; }
#app.online-screen .online-tactical-b .online-rail-actions:has([data-online-back]) { position: absolute; right: 6px; bottom: 6px; width: max-content; }
html[data-md-mobile=true] #app.game .hud,
html[data-md-mobile=true] #app.game .endless-hud { font-size: clamp(11px,calc(12px * var(--md-hud-scale)),18px); }
html[data-md-mobile=true] #app.game :is(.hud button,.endless-hud button) { min-width: 44px; min-height: max(44px,calc(44px * var(--md-hud-scale))); }
#app.game .endless-hud button { min-width: 44px; min-height: 44px; }
html[data-md-mobile=true] #app.game :is(.tower-bar .tower-card,.endless-tower-bar button) { min-width: max(72px,calc(82px * var(--md-hud-scale))); font-size: max(10px,calc(11px * var(--md-hud-scale))); }
/* Battle chrome has an intrinsic footer row. Larger preference/eligibility text
   grows the strip instead of escaping a fixed62px card. Canvas remains the full
   original battle box; only DOM controls participate in this grid. */
html[data-md-mobile=true] #app.game > .endless-battle { display: grid; grid-template-rows: auto minmax(0,1fr) auto; min-height: 0; }
html[data-md-mobile=true] #app.game > .endless-battle > canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
html[data-md-mobile=true] #app.game .endless-hud { grid-area: 1/1; position: relative; inset: auto; margin: var(--battle-top,6px) var(--battle-right,6px) 0 var(--battle-left,6px); }
html[data-md-mobile=true] #app.game .endless-tower-bar { grid-area: 3/1; position: relative; inset: auto; height: auto; min-height: 72px; align-items: stretch; margin: 0 var(--battle-right,6px) var(--battle-bottom,6px) var(--battle-left,6px); }
html[data-md-mobile=true] #app.game .endless-tower-bar button { height: auto; min-height: 62px; grid-template-rows: minmax(30px,max-content) auto auto; grid-auto-rows: auto; align-content: start; gap: 2px; line-height: 1.15; }
html[data-md-mobile=true] #app.game .endless-tower-panel { grid-area: 2/1; position: relative; inset: auto; justify-self: end; align-self: start; box-sizing: border-box; width: min(235px,45%); min-height: 0; max-height: calc(100% - 12px); overflow-y: auto; margin: 6px var(--battle-right,6px) 6px 0; }
html[data-md-mobile=true] #app.game .endless-tower-panel button { min-height: 44px; }
html[data-md-mobile=true] #app.game .endless-feedback { grid-area: 2/1; position: relative; inset: auto; align-self: end; justify-self: start; max-width: 65%; margin: 4px 10px; }
@media (max-width: 450px) and (orientation: portrait) {
  html[data-md-mobile=true] #app.menu-bg > .card > form { grid-template-columns: minmax(0,1fr); }
  html[data-md-mobile=true] #app .settings-layout { grid-template-columns: minmax(0,1fr); grid-template-rows: auto minmax(0,1fr); }
  html[data-md-mobile=true] #app .settings-nav { flex-direction: row; overflow-x: auto; overflow-y: hidden; }
}

/* Classic campaign chrome shares intrinsic rows while the canvas stays in its
   original full battle box. Speed follows the real first-row height; it no
   longer collides with scaled Quit/Settings at a fixed54px offset. */
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud {
  display: grid;
  grid-template-columns: max-content minmax(0,1fr) max-content max-content;
  grid-template-rows: auto auto minmax(0,1fr) auto;
  gap: 6px;
  padding: var(--battle-top,6px) var(--battle-right,6px) var(--battle-bottom,6px) var(--battle-left,6px);
  min-width: 0; min-height: 0; box-sizing: border-box;
}
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > :is(.top-left,.top-center,.currency-hud,.speed,.wave-chip,.countdown,.tower-storage,.tower-bar,.tower-panel,.mission-premium-actions,.ruby-bomb) {
  position: relative; inset: auto; transform: none; min-width: 0; margin: 0;
}
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .top-left { grid-area: 1/1; align-self: start; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .top-center { grid-area: 1/2; justify-self: center; align-self: start; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .currency-hud { grid-area: 1/3/2/5; justify-self: end; align-self: start; flex-wrap: wrap; justify-content: flex-end; max-width: 100%; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .currency-hud > :is(span,.top-right) { min-width: 0; max-width: 100%; overflow-wrap: anywhere; white-space: normal; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .speed { grid-area: 2/1/3/3; justify-self: start; align-self: start; flex-wrap: wrap; max-width: 100%; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .wave-chip { grid-area: 2/3; justify-self: end; align-self: start; max-width: 100%; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .countdown { grid-area: 3/2; justify-self: center; align-self: start; max-width: 100%; text-align: center; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .countdown:empty { display: none; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .tower-storage { grid-area: 3/1; align-self: end; justify-self: start; max-height: 100%; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .tower-bar { grid-area: 4/1/5/-1; align-self: end; width: 100%; max-width: 100%; height: auto; min-height: 70px; pointer-events: auto; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .tower-panel { grid-area: 3/3/4/5; align-self: start; justify-self: end; width: min(235px,100%); min-height: 0; max-height: 100%; overflow-y: auto; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .mission-premium-actions { grid-area: 3/3/4/5; align-self: end; justify-self: end; width: min(190px,100%); min-height: 0; max-height: 100%; overflow-y: auto; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud:has(>.tower-panel:not([hidden])) > .mission-premium-actions { grid-area: 3/2; }
html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud:has(>.tower-panel:not([hidden])) > .countdown { grid-area: 3/1; }

html[data-md-mobile=true] #app.game:not([data-mode="deltaBase"]) > .hud > .ruby-bomb { grid-area: 2/4; justify-self: end; align-self: start; }
