/* Shared viewport boundaries. Keep the original artwork and screen-specific grids. */
:root {
  --screen-height: 100vh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --dialog-height: calc(var(--screen-height) - max(16px, var(--safe-top)) - max(16px, var(--safe-bottom)));
}
@supports (height: 100dvh) { :root { --screen-height: 100dvh; } }

/* The shared commentary heading moves into the battle's right panel. Its
   regular negative margins are clipped there, so match the army title inset. */
body.hasha-battle .hasha-report-panel > h2[data-i18n="side.liveCommentary"] {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 2.35rem;
  margin: 0 0 0.6rem;
  padding: 0.35rem 0.75rem;
  border-radius: 6px 6px 0 0;
  text-align: center;
}
@media (max-width: 999px) {
  body.hasha-battle .hasha-report-panel:not([hidden]) { margin-top: 0; }
}

/* A long description must never leave its last action outside a scrollable area. */
.modal-overlay > .rule-modal,
#matchOverOverlay > .match-over-modal {
  min-height: 0;
  max-height: var(--dialog-height);
  overflow-y: auto;
}
.modal-overlay > .rule-book-modal { height: min(52rem, var(--dialog-height)); }
#storyOverlay:not(.is-opening-narration):not(.is-chapter-view):not(.is-ending) > .story-menu-modal {
  max-height: var(--dialog-height);
}
:is(.hp-rice-alert, .hp-role-dialog, .hp-roles-dialog, #hashaDifficulty, #hashaPrisoners, #hashaTitleReturnConfirm, .hasha-tutorial-dialog) {
  box-sizing: border-box;
  max-height: var(--dialog-height);
  overflow-y: auto;
}
/* Native top-layer dialogs otherwise center against the unsafe viewport edges. */
dialog:is(.hp-rice-alert, .hp-role-dialog, .hp-roles-dialog, #hashaDifficulty, #hashaPrisoners, #hashaTitleReturnConfirm, .hasha-tutorial-dialog) {
  inset: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  margin: auto;
  max-width: min(var(--native-dialog-max, 100vw), calc(100vw - max(12px, var(--safe-left)) - max(12px, var(--safe-right))));
}

/* Keep the preparation dialogs' original content-sized desktop cap. The
   viewport guard below must not replace their existing 420px maximum. */
dialog.hp-role-dialog { --native-dialog-max: 420px; }

@media (max-width: 999px), (pointer: coarse) {
  /* svh keeps document flow steady while Safari's toolbar opens and closes.
     Fullscreen dialogs use dvh above so their scroll area follows the visible space. */
  body { min-height: 100svh; padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left); }
  .app-shell { min-height: 100svh; }
  body.hasha-battle .app-shell { min-height: 0; }
  .modal-overlay:not(#hashaTitleOverlay):not(#hashaDomestic):not(#hashaPreparation):not(.is-opening-narration):not(.is-chapter-view):not(.is-ending),
  #hashaCommandOverlay:not(.hg-inline) {
    padding: max(16px, var(--safe-top)) max(12px, var(--safe-right)) max(16px, var(--safe-bottom)) max(12px, var(--safe-left));
  }
  #hashaPreparation {
    padding: max(8px, var(--safe-top)) max(8px, var(--safe-right)) max(8px, var(--safe-bottom)) max(8px, var(--safe-left));
  }
  #hashaDomestic { padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left); }
  #hashaTitle {
    padding-left: max(16px, var(--safe-left)); padding-right: max(16px, var(--safe-right));
    padding-top: max(12px, var(--safe-top)); padding-bottom: max(28px, var(--safe-bottom));
  }
  .hasha-war-report {
    padding: max(12px, var(--safe-top)) max(12px, var(--safe-right)) max(12px, var(--safe-bottom)) max(12px, var(--safe-left));
  }
  .hasha-war-report > section { min-height: 0; max-height: var(--dialog-height); }
  .hasha-ending-scene {
    padding: max(12px, var(--safe-top)) max(12px, var(--safe-right)) max(12px, var(--safe-bottom)) max(12px, var(--safe-left));
  }
  /* Avoid Safari's input-focus zoom without disabling the user's pinch zoom. */
  input:is([type=text], [type=number], [type=url], :not([type])), textarea, select { font-size: max(16px, 1em); }
}
@media (max-width: 600px) {
  /* Give the longer English completion label room without shrinking its type. */
  html:lang(en) body.hasha-deployment #gameplayControls { grid-template-columns: .9fr .95fr 1.45fr .65fr; }
  /* English army names need their own row, rather than being squeezed between badges. */
  html:lang(en) body.hasha-battle #score.gunshi-matchup {
    grid-template-columns: minmax(0,1fr) auto minmax(0,1fr);
    grid-template-rows: auto auto; row-gap:4px;
  }
  html:lang(en) body.hasha-battle #score .sente-side .gunshi-side-label { grid-column:1; grid-row:1; }
  html:lang(en) body.hasha-battle #score .sente-side .gunshi-custom-name { grid-column:1; grid-row:2; }
  html:lang(en) body.hasha-battle #score .gunshi-versus { grid-column:2; grid-row:1 / 3; }
  html:lang(en) body.hasha-battle #score .gote-side .gunshi-side-label { grid-column:3; grid-row:1; }
  html:lang(en) body.hasha-battle #score .gote-side .gunshi-custom-name { grid-column:3; grid-row:2; }
  html:lang(en) body.hasha-battle #score.gunshi-matchup .gunshi-custom-name {
    white-space:normal; overflow:visible; line-height:1.25; letter-spacing:.02em; overflow-wrap:anywhere;
  }
  html:lang(en) body.hasha-battle .piece-stand-slot:has(> .hasha-officer-name) { width:86px; flex-basis:86px; }
  html:lang(en) body.hasha-battle .piece-stand-slot > .hasha-officer-name { font-size:18px; }
  html:lang(en) body.hasha-battle .piece-stand-slot > .gunshi-selected-warrior-name.hasha-officer-name { font-size:18px; }
}
/* Reuse the board notice fade without the assault rays. */
#gunshiTakebackNotice.hasha-reinforcement-notice::before,
#gunshiTakebackNotice.hasha-reinforcement-notice::after { display:none; }
html body.hasha-battle #gunshiTakebackNotice.hasha-reinforcement-notice span {
 font-size:clamp(1.69rem,7.8vw,4.16rem);
 color:#175536;
 -webkit-text-stroke:clamp(1px,.2vw,2px) #fff0cd;
 text-shadow:0 0 .12em #fff0cd,0 .08em .12em #17332155;
}
/* Enemy reserves have at most three units: keep the piece row independent of names. */
body.hasha-battle .hasha-enemy-reserves {
 display:flex;justify-content:center;align-items:start;gap:8px;padding:6px 4px;
}
body.hasha-battle .hasha-enemy-reserves .piece-stand-slot {
 display:grid;grid-template-columns:minmax(0,1fr);grid-template-rows:auto auto;
 align-items:center;justify-items:center;row-gap:2px;
 width:calc((100% - 16px)/3);flex:0 1 100px;height:auto;min-height:0;padding:0;
}
body.hasha-battle .hasha-enemy-reserves .hasha-reserve-cell {
 position:relative;display:flex;align-items:center;justify-content:center;
 width:var(--gunshi-board-cell-width,3rem);height:var(--gunshi-board-cell-height,3rem);
 grid-row:1;grid-column:1;
}
html body.hasha-battle .hasha-enemy-reserves .piece-stand-slot > .hasha-officer-name {
 grid-row:2;grid-column:1;position:static;transform:none;
 width:max-content;max-width:100%;min-width:0;padding:0.08rem 0.22rem;
 font-size:clamp(10px,2.8vw,13px);line-height:1.15;
 white-space:normal;overflow-wrap:normal;text-overflow:clip;
}
html:lang(en) body.hasha-battle .hasha-enemy-reserves .piece-stand-slot > .hasha-officer-name {
 font-size:clamp(7px,calc(var(--gunshi-board-cell-width,3rem) * .18),11px);
}
body.hasha-battle .hasha-enemy-reserves .piece-stand-slot:disabled { opacity:1; }
