/* Shared reading and alignment rules. These do not change the playing field. */
body#top[data-page='play'] {
  --ui-detail-ink: #c9bed5;
  --ui-focus: #c7ff4a;
}
html[data-theme='light'] body#top[data-page='play'] {
  --ui-detail-ink: #62516f;
  --ui-focus: #496d00;
}
body#top[data-page='play'] :is(#action-bar, .bar-center-container, #combat-result-container) { min-width: 0; }
body#top[data-page='play'] #cb-outcome {
  white-space: normal !important;
  overflow: visible !important;
  text-overflow: clip;
  overflow-wrap: anywhere;
}
body#top[data-page='play'] :is(#btn-new-game, #btn-go-exit, #btn-core-ok, #btn-intro-start-game) {
  justify-content: center !important;
  align-items: center;
  text-align: center;
}
html[data-theme] body#top[data-page='play'] #go-title {
  color: var(--ui-detail-ink) !important;
  opacity: 1;
  font: 700 11px/1.4 Inter, sans-serif;
  letter-spacing: 1.4px;
}
body#top[data-page='play'] #go-tokens {
  font: 500 12px/1.5 Inter, sans-serif;
  letter-spacing: 0;
}
body#top[data-page='play'] :is(.act-btn, .btn, .menu-toggle, #phone-reference-toggle):focus-visible {
  outline: 2px solid var(--ui-focus);
  outline-offset: 3px;
}

/* Desktop and tablet: long results wrap inside the bridge, never push a plaque. */
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar {
  padding:4px 22px!important; gap:12px; align-items:center;
}
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar .bar-center-container {
  min-width:0; flex:1 1 0!important;
}
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar #combat-result-container {
  box-sizing: border-box;
  padding: 0!important;
  gap: 2px;
}
body#top[data-page='play']:not(.phone-match-active).combat-active #combat-accept-status { margin: 0; }
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar #cb-outcome {
  max-width: 100%;
  font: 800 14px/1.2 Inter, sans-serif !important;
  letter-spacing: .3px !important;
}
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar:has(#evade-decision-container[style*='flex']) #cb-outcome {
  font-size:12px!important;
}
body#top[data-page='play']:not(.phone-match-active).combat-active .bg3-effect-desc {
  display: flex !important;
  min-height: 56px;
  box-sizing: border-box;
}
/* Keep the roll and result badges in the same slot, so a changed label does
   not resize the frame or trigger a different viewport-fit scale. */
body#top[data-page='play']:not(.phone-match-active).combat-active .bg3-prompt-badge {
  box-sizing: border-box;
  min-height: 44px;
}
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar #btn-combat-done {
  position:absolute!important; top:calc(100% + 4px)!important; right:auto!important;
  bottom:auto!important; left:50%!important; transform:translateX(-50%)!important;
  margin:0!important;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  width:112px; min-width:112px; max-width:112px;
  height: 44px;
  min-height: 44px;
  padding: 8px 12px !important;
  font: 700 12px/1.2 Inter, sans-serif !important;
  letter-spacing: .4px;
}
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar #evade-decision-container {
  position:static!important; inset:auto!important; transform:none!important;
  width:240px!important; flex:0 0 240px;
  justify-content: center;
  align-items: stretch;
  gap: 8px !important;
}
html[data-theme] body#top[data-page='play']:not(.phone-match-active).combat-active #action-bar #evade-decision-container .btn {
  box-sizing: border-box;
  flex: 1 1 0;
  width:auto!important;
  min-width: 0 !important;
  min-height: 44px;
  padding: 8px 10px !important;
  justify-content: center;
  text-align: center;
  white-space: normal !important;
  font: 700 11px/1.3 Inter, sans-serif !important;
  letter-spacing: .2px;
}

@media (orientation: landscape) and (max-width: 1100px) and (max-height: 599px) {
  body#top[data-page='play'].phone-match-active {
    --phone-result-max-width: min(660px, calc(100vw - 2 * (max(var(--phone-dock-left), var(--phone-dock-right)) + var(--phone-menu-size) + var(--phone-footer-control-gap))));
    --ui-result-height: 64px;
    --phone-combat-height: min(270px, calc(100dvh - var(--phone-safe-top) - var(--phone-action-bottom) - 100px));
    --phone-combat-top: max(calc(8px + var(--phone-safe-top)), min(calc((100dvh + var(--phone-safe-top) - var(--phone-safe-bottom) - var(--phone-combat-height)) / 2 - 30px), calc(100dvh - var(--phone-action-bottom) - 84px - var(--phone-combat-height) - 8px)));
  }
  body#top[data-page='play'].phone-match-active.combat-active:not(:has(#evade-decision-container[style*='flex'])) #action-bar {
    left: 50%;
    right: auto;
    bottom: calc(var(--phone-action-bottom) + (var(--phone-menu-size) - var(--ui-result-height)) / 2);
    width: min(660px, var(--phone-result-max-width));
    max-width: var(--phone-result-max-width);
    transform: translateX(-50%) !important;
    height: var(--ui-result-height);
    min-height: var(--ui-result-height);
    padding: 6px 12px;
    gap: 12px;
    align-items: center;
    border: 1px solid color-mix(in srgb, var(--phone-result-accent) 50%, transparent) !important;
    background: linear-gradient(115deg, color-mix(in srgb, var(--phone-result-accent) 10%, #120d22b8), #140d2199) !important;
    box-shadow: 0 5px 20px #0002 !important;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
  }
  body#top[data-page='play'].phone-match-active.combat-active:not(:has(#evade-decision-container[style*='flex'])) #action-bar[data-online-outcome-flow='true'] {
    --ui-result-height: 68px;
  }
  body#top[data-page='play'].phone-match-active.combat-active #action-bar::before,
  body#top[data-page='play'].phone-match-active.combat-active #action-bar::after { display: none; }
  body#top[data-page='play'].phone-match-active.combat-active:not(:has(#evade-decision-container[style*='flex'])) #action-bar .bar-center-container {
    align-self: stretch;
    height: auto;
    padding: 0;
    box-sizing: border-box;
    border: 0;
    background: transparent;
    box-shadow: none;
  }
  html[data-theme='light'] body#top[data-page='play'].phone-match-active.combat-active:not(:has(#evade-decision-container[style*='flex'])) #action-bar {
    background: linear-gradient(115deg, color-mix(in srgb, var(--phone-result-accent) 8%, #fffafcde), #fffafcc9) !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #action-bar #combat-result-container {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    align-content: center;
    gap: 2px 10px;
    width: 100%;
    height: 100%;
  }
  body#top[data-page='play'].phone-match-active.combat-active #action-bar #cb-outcome {
    flex: 0 0 100%;
    font: 800 13px/1.4 Orbitron, sans-serif !important;
    letter-spacing: .3px !important;
  }
  html[data-theme] body#top[data-page='play'].phone-match-active.combat-active #action-bar #combat-accept-status {
    font: 600 12px/1.4 Inter, sans-serif !important;
    min-height: 0;
    margin: 0;
    text-wrap: wrap;
  }
  html[data-theme] body#top[data-page='play'].phone-match-active.combat-active #combat-accept-countdown {
    font: 500 11px/1.4 Inter, sans-serif !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #action-bar #btn-combat-done {
    flex: 0 0 100px;
    align-self: center;
    width: 100px;
    min-width: 100px !important;
    max-width: 100px;
    height: var(--phone-action-height);
    min-height: var(--phone-action-height);
    padding: 8px !important;
    border-radius: 9px;
    font: 700 12px/1.3 Inter, sans-serif !important;
    letter-spacing: 0 !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #evade-decision-container .btn {
    height: var(--phone-action-height);
    min-height: var(--phone-action-height);
    font: 700 12px/1.3 Inter, sans-serif !important;
    letter-spacing: 0;
  }
  /* A label, artwork and explanation each own a row. Fixed-size special
     symbols must not escape their artwork slot and collide with captions. */
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .combat-matrix-cell {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 22px minmax(0, 1fr) 32px;
    align-items: stretch;
    justify-items: center;
    gap: 6px;
    padding: 6px !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .cb-battle-die-pill {
    grid-row: 1;
    position: static !important;
    inset: auto !important;
    transform: none !important;
    align-self: center;
    margin: 0;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-battle-die-widget {
    grid-row: 2;
    display: grid !important;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) minmax(28px, auto);
    gap: 4px;
    width: 100%;
    height: 100% !important;
    min-height: 0 !important;
    margin: 0 !important;
    border-radius: 8px;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-die-container {
    grid-row: 1;
    align-self: center;
    justify-self: center;
    width: auto !important;
    height: min(var(--phone-battle-size), 100%) !important;
    max-width: 100%;
    aspect-ratio: 1;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen :is(.bg3-3d-canvas, .bg3-die-body) {
    width: 100% !important;
    height: 100% !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen :is(.bg3-final-symbol, .bg3-final-symbol svg) {
    width: 100% !important;
    height: 100% !important;
    max-width: 100%;
    max-height: 100%;
    min-width: 0;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-prompt-badge {
    grid-row: 2;
    align-self: center;
    justify-self: center;
    width: max-content;
    margin: 0 !important;
    height: 40px;
    min-height: 40px;
    max-width: 100%;
    box-sizing: border-box;
    padding: 3px 6px !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-effect-desc {
    grid-row: 3;
    position: static;
    inset: auto;
    width: 100%;
    max-height: none;
    align-self: center;
    margin: 0 !important;
    padding: 0 !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-effect-text {
    font: 600 12px/1.3 Inter, sans-serif !important;
  }
  /* Reserve the explanation row before rolling as well as after it. The
     widget itself remains the touch target; the badge is only a label. */
  body#top[data-page='play'].phone-match-active #go-stats .stat-lbl {
    font: 700 10px/1.35 Inter, sans-serif;
    letter-spacing: .2px;
    color: var(--ui-detail-ink);
  }
  body#top[data-page='play'].phone-match-active #gameover-modal #go-reason {
    font: 700 11px/1.4 Inter, sans-serif !important;
    letter-spacing: .3px !important;
  }
  body#top[data-page='play'].phone-match-active #gameover-modal :is(#btn-new-game, #btn-go-exit) {
    display: flex;
    font: 700 12px/1.3 Inter, sans-serif !important;
    letter-spacing: .2px;
  }
  body#top[data-page='play'].phone-match-active #phone-reference-toggle { font: 700 12px/1.3 Inter, sans-serif; }
  body#top[data-page='play'].phone-match-active #game-screen > .row-popup:not(.popup-observer) {
    --prompt-height: calc(96px + var(--phone-action-height) - 44px);
    grid-template-rows: minmax(0, 1fr) var(--phone-action-height) !important;
  }
  body#top[data-page='play'].phone-match-active #game-screen > .row-popup:not(.popup-observer) .popup-actions .btn {
    height: var(--phone-action-height) !important;
    min-height: var(--phone-action-height) !important;
  }
}
@media (orientation: landscape) and (max-width: 700px) and (max-height: 599px),
       (orientation: landscape) and (max-width: 1100px) and (max-height: 350px) {
  body#top[data-page='play'].phone-match-active {
    --phone-combat-height: min(clamp(192px, 60dvh, 270px), calc(100dvh - var(--phone-safe-top) - var(--phone-action-bottom) - 112px));
    --phone-combat-top: max(calc(8px + var(--phone-safe-top)), min(calc((100dvh + var(--phone-safe-top) - var(--phone-safe-bottom) - var(--phone-combat-height)) / 2 - 46px), calc(100dvh - var(--phone-action-bottom) - 96px - var(--phone-combat-height) - 8px)));
  }
  body#top[data-page='play'].phone-match-active.combat-active:not(:has(#evade-decision-container[style*='flex'])) #action-bar[data-online-outcome-flow='true'] {
    --ui-result-height: 76px;
  }
  body#top[data-page='play'].phone-match-active.combat-active:not(:has(#evade-decision-container[style*='flex'])) #action-bar { padding: 8px; gap: 8px; }
  body#top[data-page='play'].phone-match-active.combat-active #action-bar #btn-combat-done {
    flex-basis: 88px;
    width: 88px;
    min-width: 88px !important;
    max-width: 88px;
    padding: 6px !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .combat-matrix-cell {
    grid-template-rows: minmax(0, 1fr) 32px;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .combat-matrix-cell .cb-battle-die-pill { display: none !important; }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-battle-die-widget { grid-row: 1; }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .combat-matrix-cell .bg3-effect-desc { grid-row: 2; }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-effect-text { font-size: 11px !important; }
}
/* Narrow phones need extra room for the longest explanations in every state. */
@media (orientation: landscape) and (max-width: 900px) and (max-height: 599px) {
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .combat-matrix-cell {
    grid-template-rows: 22px minmax(0, 1fr) 48px;
  }
}
@media (orientation: landscape) and (max-width: 700px) and (max-height: 599px),
       (orientation: landscape) and (max-width: 900px) and (max-height: 350px) {
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .combat-matrix-cell {
    grid-template-rows: minmax(0, 1fr) 48px;
  }
}
@media (orientation: landscape) and (max-width: 649px) and (max-height: 599px) {
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .combat-matrix-cell {
    grid-template-rows: minmax(0, 1fr) 52px;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-effect-desc {
    width: calc(100% + 12px); max-width: none;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-effect-text {
    font: 600 10px/1.25 Inter, sans-serif !important;
  }
  body#top[data-page='play'].phone-match-active.combat-active #game-screen .bg3-prompt-badge {
    font-size: 9px !important; letter-spacing: 0 !important;
  }
}
