/* WarKart 1.8: phone layouts use real dimensions and readable type.
   Load after style.css. Safe-area values also cover landscape camera cutouts. */
:root {
  --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);
}

@media (max-width: 760px), (max-width: 1100px) and (max-height: 540px) {
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  html, body { height: 100%; height: 100dvh; overscroll-behavior: none; }
  .menu-screen, .hud, .overlay-screen, .loading-screen, .fatal-error {
    height: 100%; height: 100dvh;
  }
  .menu-screen {
    display: flex; flex-direction: column; gap: 0; min-width: 0;
    padding: var(--safe-top) calc(12px + var(--safe-right)) var(--safe-bottom) calc(12px + var(--safe-left));
    overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch; scroll-padding-block: 12px;
  }
  .menu-header {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: 'brand actions' 'nav nav'; align-items: center;
    gap: 4px 8px; height: auto; min-height: 0; padding: 8px 0 12px; flex: 0 0 auto;
  }
  .menu-header::before { inset: 0 -14px; }
  .brand { grid-area: brand; width: 100%; max-width: 180px; height: 68px; }
  .header-actions { grid-area: actions; gap: 6px; margin: 0; min-width: 0; }
  .header-actions .icon-button { width: 44px; height: 44px; border-width: 2px; }
  .header-actions .icon { width: 21px; height: 21px; }
  .gold-wallet { min-width: 0; max-width: 82px; gap: 4px; padding: 4px; }
  .gold-wallet > div > span { display: none; }
  .gold-wallet > div { min-width: 0; }
  .gold-wallet strong { display: block; max-width: 44px; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
  .coin-medallion, .gold-wallet .gold-icon { width: 28px; height: 28px; }
  .local-status { display: none; }
  .menu-nav {
    grid-area: nav; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
    position: static; width: 100%; gap: 8px; transform: none;
  }
  .menu-nav button, .menu-nav .nav-current {
    min-height: 44px; padding: 8px 6px; display: flex; align-items: center; justify-content: center;
    font-size: 13px; letter-spacing: .5px;
  }
  .menu-main {
    width: min(100%, 460px); margin: 28px auto 20px; padding: 22px 18px 18px;
    align-self: auto; flex: 0 0 auto;
  }
  .menu-main > .window-title { margin: -38px auto 14px; font-size: 14px; padding: 6px 18px; }
  .menu-main h1 { font-size: 30px; margin: 8px 0 12px; }
  .menu-description { font-size: 14px; line-height: 1.55; margin-bottom: 18px; }
  .section-label, .menu-main .section-label { font-size: 12px; letter-spacing: .6px; }
  .setup-block { padding-top: 14px; }
  .mode-options { gap: 6px; margin-top: 12px; }
  .mode-option { height: 78px; min-height: 44px; padding: 9px 3px; gap: 8px; }
  .mode-option span { font-size: 12px; letter-spacing: 0; }
  .mode-option .icon { width: 26px; height: 26px; }
  .mode-description { min-height: 0; margin: 12px 0 16px; font-size: 13px; line-height: 1.5; }
  .race-options { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; padding-top: 14px; margin-bottom: 18px; }
  .race-options label { gap: 7px; }
  .race-options label > span, .grid-size > span { font-size: 11px; }
  .race-options select { min-height: 46px; padding: 8px; font-size: 16px; }
  .grid-size { grid-column: 1 / -1; flex-direction: row; justify-content: center; align-items: center; gap: 10px; padding: 0; }
  .grid-size strong { font-size: 12px; }
  .button { min-height: 46px; padding: 10px 14px; font-size: 15px; }
  .start-button { min-height: 54px; font-size: 20px; }
  .start-caption { margin-top: 12px; font-size: 11px; line-height: 1.5; }
  .track-badge { display: none; }
  .selected-kart {
    width: min(100%, 460px); margin: 0 auto 20px; min-height: 152px; padding: 18px 14px;
    align-self: auto; flex: 0 0 auto; gap: 10px;
  }
  .kart-portrait { width: 64px; height: 64px; }
  .kart-number { font-size: 12px; }
  .selected-kart .section-label { font-size: 10px; }
  .selected-kart h2 { margin: 8px 0; font-size: 20px; line-height: 1.2; overflow-wrap: anywhere; }
  .selected-kart p { font-size: 12px; line-height: 1.4; }
  .selected-kart .mini-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .selected-kart .mini-stat { font-size: 10px; letter-spacing: 0; }
  .selected-kart .stat-pips i { width: 6px; height: 6px; }
  .kart-change { width: 54px; min-width: 54px; min-height: 64px; padding-left: 8px; font-size: 10px; }
  .kart-change .icon { width: 24px; height: 24px; }
  .menu-footer { min-height: 48px; margin: 0; padding: 12px 0; font-size: 10px; line-height: 1.5; }
  .menu-footer > span:first-child { max-width: none; }

  /* Native dialogs stay inside the visible viewport and scroll at text size. */
  .modal, .garage-modal, .controls-modal, .settings-modal, .records-modal {
    inset: 0; margin: auto; width: calc(100vw - 24px - var(--safe-left) - var(--safe-right));
    max-width: 760px; max-height: calc(100dvh - 24px - var(--safe-top) - var(--safe-bottom));
    padding: 48px 18px 20px; overflow: auto; overscroll-behavior: contain;
    scroll-padding-block: 54px 16px; -webkit-overflow-scrolling: touch;
  }
  .dialog-close {
    position: sticky; top: 8px; right: auto; z-index: 5;
    width: 44px; height: 44px; margin: -38px -6px 0 auto;
  }
  .dialog-close .icon-button { width: 44px; height: 44px; }
  .dialog-close .icon { width: 20px; height: 20px; }
  .modal > .eyebrow { font-size: 13px; letter-spacing: .3px; }
  .modal > h2 { font-size: 27px; padding: 4px 0 16px; }
  .dialog-intro { font-size: 13px; line-height: 1.55; margin: 14px 0 20px; }
  .dialog-footnote, .controller-intro, .spell-guide-intro { font-size: 12px; line-height: 1.6; }
  .kart-options { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .garage-modal .kart-card { min-width: 0; padding: 24px 10px 12px; }
  .garage-modal .kart-illustration { width: 96px; height: 96px; margin: 6px auto 12px; }
  .garage-modal .kart-card-name { min-height: 42px; margin-bottom: 7px; font-size: 16px; line-height: 1.3; overflow-wrap: anywhere; }
  .garage-modal .kart-driver { min-height: 34px; margin-bottom: 10px; font-size: 12px; line-height: 1.4; }
  .kart-card-number { font-size: 12px; }
  .kart-card-selected { font-size: 10px; letter-spacing: 0; }
  .garage-modal .kart-card-stat { display: grid; grid-template-columns: 1fr; gap: 5px; padding: 8px 0; font-size: 11px; }
  .kart-card-stat .stat-pips i { width: 9px; height: 7px; }
  .garage-modal .garage-footer {
    position: sticky; bottom: -20px; z-index: 3; display: flex; flex-direction: column; align-items: stretch;
    gap: 10px; padding: 12px 8px 18px; margin: 18px -8px -1px;
    background: linear-gradient(#262521f7, #171815); box-shadow: 0 -5px 14px #0008;
  }
  .garage-modal .garage-footer > p { font-size: 12px; line-height: 1.45; text-align: left; }
  .garage-modal .garage-footer > .button { width: 100%; min-width: 0; min-height: 48px; font-size: 15px; }
  .controls-columns { grid-template-columns: 1fr; gap: 24px; margin-top: 22px; }
  .controls-columns h3 { font-size: 15px; }
  .control-row { min-height: 46px; font-size: 13px; gap: 10px; padding: 6px 0; flex-wrap: wrap; }
  .control-row > span:first-child { flex: 1 1 135px; }
  .control-row > span:last-child { flex: 0 0 auto; }
  .control-row kbd { min-height: 26px; font-size: 11px; }
  .control-row small { font-size: 11px; }
  .driving-tip { padding: 12px; }
  .driving-tip p { font-size: 12px; }
  .spellbook-heading { font-size: 20px; }
  .item-guide { grid-template-columns: 1fr; gap: 10px; }
  .item-guide > div { padding: 12px; }
  .item-guide strong { font-size: 15px; }
  .item-guide p { font-size: 13px; line-height: 1.55; }
  .item-guide > div > img, .item-guide .spell-icon { width: 48px; height: 48px; }
  .setting-row { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; min-height: 70px; padding: 14px 0; font-size: 15px; }
  .setting-row > span small { max-width: none; margin-top: 5px; font-size: 12px; line-height: 1.45; }
  .setting-row input[type=text], .setting-row select { width: 100%; min-width: 0; min-height: 46px; padding: 10px; font-size: 16px; }
  .setting-row input[type=range] { width: 100%; min-height: 44px; }
  .setting-row:has(input[type=checkbox]) { grid-template-columns: minmax(0, 1fr) 44px; }
  .setting-row input[type=checkbox] { width: 28px; height: 28px; justify-self: center; }
  .records-content { margin-top: 18px; overscroll-behavior-x: contain; }
  .records-table { min-width: 540px; font-size: 13px; }
  .records-table th { font-size: 12px; }
  .records-table small { font-size: 11px; }

  .overlay-screen {
    padding: calc(12px + var(--safe-top)) calc(12px + var(--safe-right)) calc(12px + var(--safe-bottom)) calc(12px + var(--safe-left));
    overscroll-behavior: contain;
  }
  .pause-card, .results-card {
    max-height: calc(100dvh - 24px - var(--safe-top) - var(--safe-bottom));
    padding: 28px 18px 20px; overflow: auto; -webkit-overflow-scrolling: touch;
  }
  .pause-card.classic-options { width: 340px; max-width: 100%; padding: 34px 22px 20px; }
  .classic-options h2.window-title { font-size: 18px; }
  .classic-options .pause-status { font-size: 13px; }
  .pause-actions { gap: 8px; }
  .pause-actions .button { min-height: 46px; padding: 9px 10px; font-size: 15px; }
  .pause-actions #resume-race { margin-top: 8px; }
  .results-heading { gap: 10px; }
  .results-heading h2 { font-size: 25px; }
  .results-heading .eyebrow { font-size: 10px; }
  .result-trophy { display: none; }
  .result-position { font-size: 40px; }
  .result-position small { font-size: 13px; }
  .results-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; margin: 18px 0; padding: 16px 0; }
  .results-stats > div:last-child { grid-column: 1 / -1; flex-direction: row; align-items: center; justify-content: space-between; }
  .results-stats span { font-size: 11px; }
  .results-stats strong, .results-stats > div:last-child strong { font-size: 19px; }
  .gold-reward { grid-template-columns: 42px minmax(0, 1fr) minmax(0, 1fr); gap: 0 9px; padding: 12px; }
  .reward-coin, .reward-coin .gold-icon { width: 40px; height: 40px; }
  .reward-earned > span, .reward-total > span { font-size: 12px; }
  .reward-earned strong { font-size: 25px; }
  .reward-total strong { font-size: 20px; }
  .gold-reward > p { font-size: 12px; }
  .results-body { grid-template-columns: 1fr; gap: 20px; }
  .results-body h3 { font-size: 14px; }
  .result-standing { padding: 10px 8px; font-size: 13px; }
  .result-standing .standing-result { font-size: 12px; }
  .result-standing small { font-size: 10px; }
  .lap-splits li, .lap-splits li strong { font-size: 13px; }
  .lap-splits li.best span::after { font-size: 10px; }
  .results-actions { display: grid; grid-template-columns: 1fr; gap: 10px; }
  .results-actions > .button { min-height: 48px; font-size: 14px; }
  .result-save-note { font-size: 12px; }
  .loading-screen { padding: 20px; text-align: center; }
  .loading-screen > span { font-size: 14px; }
  .loading-logo { height: auto; max-height: 30dvh; }
  .fatal-error { padding: calc(28px + var(--safe-top)) 22px calc(28px + var(--safe-bottom)); }
  .fatal-error h1 { font-size: 28px; }
  .fatal-error p { font-size: 15px; }

  /* Shared race type sizes. Absolute anchors are set separately by orientation. */
  .hud { --hud-edge: 10px; --dial-size: clamp(140px, 39vw, 162px); }
  .hud-top { gap: 6px; align-items: stretch; }
  .race-place { min-width: 0; min-height: 54px; gap: 6px; padding: 4px 7px 4px 4px; }
  .driver-portrait { width: 40px; height: 40px; }
  .position-copy { align-items: flex-start; gap: 4px; }
  .position-copy > span { font-size: 30px; }
  .position-copy small { padding-top: 2px; font-size: 12px; }
  .position-copy b { margin-top: 4px; font-size: 10px; letter-spacing: 0; }
  .hud-lap, .hud-timing, .hud-top-right { margin: 0; min-height: 54px; }
  .hud-lap { padding: 6px 8px; gap: 4px; justify-content: center; }
  .hud-lap > span { font-size: 10px; letter-spacing: .5px; }
  .hud-lap strong { font-size: 23px; }
  .hud-lap small { font-size: 14px; }
  .hud-timing { display: flex; gap: 12px; padding: 6px 9px; justify-content: space-between; align-items: center; }
  .hud-timing > div, .hud-timing > div:last-child { display: flex; gap: 4px; min-width: 0; }
  .hud-timing span { font-size: 10px; letter-spacing: .2px; }
  .hud-timing strong { font-size: 14px; white-space: nowrap; }
  .hud-top-right { padding: 4px; gap: 4px; justify-content: flex-end; min-width: 0; }
  .hud-top-right > span { display: none; }
  .hud-top-right .icon-button { width: 44px; height: 44px; }
  .hud-top-right .icon { width: 22px; height: 22px; }
  .hud-gold { min-width: 0; max-width: 70px; gap: 3px; font-size: 13px; }
  .hud-gold strong { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
  .hud-gold .gold-icon { width: 24px; height: 24px; }
  .standings { display: none; }
  .minimap-panel, .speedometer { transform: none; }
  .minimap-heading { position: relative; inset: auto; height: 24px; padding: 3px 6px; font-size: 11px; }
  #minimap-clock { font-size: 11px; }
  .minimap-disc { width: 100%; height: auto; aspect-ratio: 1; }
  .minimap-viewport { inset: 10%; }
  .compass-n { top: 10%; left: 50%; transform: translateX(-50%); font-size: 10px; }
  .minimap-zoom button { width: 44px; height: 44px; min-width: 44px; min-height: 44px; font-size: 28px; }
  .speedometer { width: var(--dial-size); text-align: center; }
  .speed-dial { width: 100%; height: auto; aspect-ratio: 1; }
  .speedometer .gauge-labels { font-size: 18px; fill: #f3dfb5; stroke-width: 2; }
  .speed-title { top: 32%; font-size: 10px; letter-spacing: .6px; }
  .speed-digits { inset: 56% auto auto 50%; width: 46%; padding: 0; background: transparent; }
  .speed-digits strong { font-size: 30px; line-height: 1; }
  .speed-digits > span { margin-top: 2px; font-size: 10px; letter-spacing: .6px; }
  .speedometer .turbo-meter { display: grid; gap: 2px; margin-top: 2px; }
  .speedometer .nitro-label { margin: 0; padding: 0 6px; font-size: 12px; letter-spacing: .2px; }
  .speedometer .nitro-label .icon { width: 14px; height: 15px; }
  .speedometer .nitro-bar { height: 32px; margin: 0; }
  .speedometer-foot { font-size: 11px; }
  .race-bottom-center { gap: 8px; align-items: stretch; }
  .item-slot { width: 100%; min-width: 0; max-width: none; min-height: 72px; padding: 8px; gap: 8px; }
  .item-icon { width: 44px; height: 44px; }
  .item-icon .icon { width: 26px; height: 26px; }
  .item-slot > div:nth-child(2) { min-width: 0; max-width: none; gap: 4px; }
  .item-slot > div:nth-child(2) > span { font-size: 10px; line-height: 1.2; letter-spacing: 0; }
  .item-slot strong, .item-slot.empty strong { font-size: 14px; line-height: 1.2; overflow-wrap: anywhere; }
  .drift-indicator { position: static; order: -1; width: 100%; padding: 6px 8px; }
  .drift-indicator > span { font-size: 11px; }
  .drift-indicator > div { height: 7px; margin-top: 4px; }
  .drift-indicator small { font-size: 10px; }
  .active-effect, .effect-badge {
    display: grid; grid-template-columns: 1fr; grid-template-rows: 30px 16px;
    width: 52px; min-width: 52px; padding: 4px; gap: 2px; justify-items: center;
  }
  .active-effect img, .effect-badge img { width: 30px; height: 30px; grid-row: 1; }
  .active-effect strong, .effect-badge strong {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip-path: inset(50%); white-space: nowrap;
  }
  .active-effect > span, .effect-time { font-size: 12px; line-height: 16px; }
  .race-notice { padding: 9px 12px; font-size: 14px; line-height: 1.35; }
  .wrong-way { padding: 10px 12px; font-size: 20px; line-height: 1.2; white-space: normal; }
  .wrong-way small { font-size: 10px; letter-spacing: 0; line-height: 1.4; }
  .countdown { top: 43%; font-size: 100px; }
  .countdown.go { font-size: 78px; }
  .fps-counter { top: auto; bottom: calc(140px + var(--safe-bottom)); left: 50%; transform: translateX(-50%); font-size: 11px; }
  .toast { bottom: calc(144px + var(--safe-bottom)); max-width: calc(100vw - 24px - var(--safe-left) - var(--safe-right)); font-size: 13px; line-height: 1.4; pointer-events: none; }
}

/* A phone in portrait: instruments flank the lower view; the map sits above. */
@media (max-width: 760px) and (orientation: portrait) {
  .hud-top {
    display: grid; grid-template-columns: minmax(0, 1fr) 64px auto;
    grid-template-areas: 'position lap actions' 'time time time';
    top: calc(8px + var(--safe-top)); left: calc(10px + var(--safe-left)); right: calc(10px + var(--safe-right));
  }
  .race-place { grid-area: position; }
  .hud-lap { grid-area: lap; }
  .hud-top-right { grid-area: actions; }
  .hud-timing { grid-area: time; min-height: 42px; }
  .hud-timing > div { flex-direction: row; align-items: center; gap: 6px; }
  .minimap-panel {
    top: calc(126px + var(--safe-top)); bottom: auto; left: auto;
    right: calc(10px + var(--safe-right)); width: 132px;
  }
  .minimap-zoom { left: 0; right: 0; top: auto; bottom: -48px; flex-direction: row; justify-content: center; gap: 8px; }
  .active-effects {
    top: calc(126px + var(--safe-top)); left: calc(10px + var(--safe-left)); right: auto;
    display: grid; grid-template-columns: repeat(3, 52px); justify-content: start; gap: 6px;
    max-width: calc(100vw - 164px - var(--safe-left) - var(--safe-right));
  }
  .speedometer { bottom: calc(140px + var(--safe-bottom)); right: calc(10px + var(--safe-right)); }
  .race-bottom-center {
    bottom: calc(140px + var(--safe-bottom)); left: calc(10px + var(--safe-left)); right: auto; transform: none;
    width: calc(100vw - var(--dial-size) - 32px - var(--safe-left) - var(--safe-right));
  }
  .race-center {
    top: max(calc(344px + var(--safe-top)), 38dvh); left: calc(12px + var(--safe-left));
    right: calc(12px + var(--safe-right)); width: auto; max-width: none; transform: none;
  }
}

/* A phone turned sideways gets a separate layout, not a scaled portrait HUD. */
@media (max-width: 1100px) and (max-height: 540px) and (orientation: landscape) {
  .menu-screen { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(220px, .8fr); grid-template-rows: auto auto auto; gap: 18px; align-content: start; }
  .menu-header {
    grid-column: 1 / -1; grid-template-columns: 150px minmax(0, 1fr) auto;
    grid-template-areas: 'brand nav actions'; gap: 12px; padding: 5px 0 8px;
  }
  .brand { width: 150px; height: 58px; }
  .menu-nav { gap: 6px; }
  .menu-nav button, .menu-nav .nav-current { font-size: 12px; padding: 7px 6px; }
  .header-actions { gap: 5px; }
  .menu-main { grid-column: 1; grid-row: 2; width: 100%; margin: 10px 0 0; padding: 21px 18px 18px; }
  .menu-main h1 { font-size: 28px; }
  .menu-description { font-size: 13px; }
  .selected-kart { grid-column: 2; grid-row: 2; align-self: start; width: 100%; margin: 10px 0 0; flex-wrap: wrap; }
  .selected-kart-copy { flex-basis: calc(100% - 78px); }
  .kart-change { width: 100%; min-width: 0; min-height: 44px; flex-direction: row; border-left: 0; border-top: 1px solid #716747; padding: 9px 0 0; margin: 3px 0 0; gap: 8px; font-size: 12px; }
  .menu-footer { grid-column: 1 / -1; grid-row: 3; }
  .modal { max-width: 840px; padding-left: 24px; padding-right: 24px; }
  .kart-options { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .garage-modal .kart-card-stat { grid-template-columns: minmax(0, 1fr) auto; font-size: 10px; }
  .garage-modal .garage-footer { flex-direction: row; align-items: center; gap: 16px; }
  .garage-modal .garage-footer > .button { width: auto; min-width: 170px; }
  .controls-columns { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
  .item-guide { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .setting-row { grid-template-columns: minmax(0, 1fr) minmax(160px, .85fr); }
  .pause-card.classic-options { width: 470px; padding: 32px 22px 18px; }
  .pause-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .pause-actions .button { min-height: 44px; font-size: 14px; }
  .pause-actions #resume-race { grid-column: 1 / -1; margin: 0; }
  .classic-options .pause-status { margin-bottom: 10px; }
  .results-card { max-width: 760px; margin: auto; }
  .results-body { grid-template-columns: 1.4fr 1fr; }
  .results-stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .results-stats > div:last-child { grid-column: auto; flex-direction: column; align-items: flex-start; }
  .results-actions { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .hud { --dial-size: 144px; }
  .hud-top {
    display: grid; grid-template-columns: auto 68px minmax(0, 1fr) auto;
    top: calc(6px + var(--safe-top)); left: calc(10px + var(--safe-left)); right: calc(10px + var(--safe-right));
  }
  .hud-timing { flex-direction: row; justify-content: space-around; gap: 10px; }
  .hud-timing > div { flex-direction: column; align-items: flex-start; gap: 3px; }
  .hud-timing strong { font-size: 15px; }
  .minimap-panel { top: calc(74px + var(--safe-top)); left: calc(12px + var(--safe-left)); right: auto; bottom: auto; width: 108px; }
  .minimap-heading { height: 23px; padding: 3px 4px; gap: 4px; font-size: 10px; }
  #minimap-clock { font-size: 10px; }
  .minimap-zoom { left: auto; right: -50px; top: 4px; bottom: auto; flex-direction: column; gap: 6px; }
  .speedometer { right: calc(12px + var(--safe-right)); bottom: calc(84px + var(--safe-bottom)); }
  .speed-digits strong { font-size: 28px; }
  .speed-title { font-size: 10px; }
  .speedometer .nitro-bar { height: 30px; }
  .race-bottom-center { width: 220px; left: 50%; right: auto; bottom: calc(84px + var(--safe-bottom)); transform: translateX(-50%); gap: 6px; }
  .item-slot { min-height: 66px; padding: 7px 9px; }
  .item-slot strong, .item-slot.empty strong { font-size: 15px; }
  .drift-indicator { padding: 5px 8px; }
  .active-effects {
    top: calc(68px + var(--safe-top)); left: calc(180px + var(--safe-left)); right: calc(180px + var(--safe-right));
    display: flex; flex-wrap: nowrap; justify-content: center; gap: 6px; max-width: none;
  }
  .active-effect, .effect-badge { width: 46px; min-width: 46px; grid-template-rows: 26px 14px; padding: 3px; }
  .active-effect img, .effect-badge img { width: 26px; height: 26px; }
  .active-effect > span, .effect-time { font-size: 11px; line-height: 14px; }
  .race-center { top: calc(124px + var(--safe-top)); left: 50%; right: auto; width: min(340px, 42vw); max-width: calc(100vw - 350px - var(--safe-left) - var(--safe-right)); transform: translateX(-50%); }
  .race-notice { font-size: 13px; padding: 8px 10px; }
  .wrong-way { font-size: 18px; padding: 8px 10px; }
  .countdown { top: 43%; font-size: 80px; }
  .countdown.go { font-size: 64px; }
  .fps-counter { left: 50%; top: auto; bottom: calc(10px + var(--safe-bottom)); }
  .toast { bottom: calc(90px + var(--safe-bottom)); max-width: 340px; }
}

/* Hold controls accept several independent fingers. Only actual buttons capture
   input; gaps between clusters do not create a full-width invisible hit area. */
@media (pointer: coarse) {
  .touch-controls {
    display: flex; align-items: flex-end; justify-content: space-between;
    left: calc(12px + var(--safe-left)); right: calc(12px + var(--safe-right));
    bottom: calc(10px + var(--safe-bottom)); gap: 18px; pointer-events: none;
    user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  }
  .touch-steering {
    display: grid; grid-template-columns: repeat(2, 58px);
    grid-template-areas: 'reset reset' 'left right'; gap: 8px; align-items: stretch;
  }
  .touch-actions {
    display: grid; grid-template-columns: repeat(3, 52px);
    grid-template-areas: 'item drift boost' 'brake gas gas'; gap: 8px; align-items: stretch;
  }
  .touch-controls button {
    --button-art-edge: 8px; pointer-events: auto; touch-action: none;
    min-width: 44px; min-height: 44px; padding: 6px 3px; border-width: 4px;
    font-family: 'Trebuchet MS', Arial, sans-serif; font-weight: 700; letter-spacing: .1px;
  }
  .touch-controls .touch-steering button { width: 58px; height: 60px; font-size: 22px; }
  .touch-controls .touch-actions button { width: 52px; height: 54px; font-size: 11px; }
  .touch-controls [data-control=left] { grid-area: left; }
  .touch-controls [data-control=right] { grid-area: right; }
  .touch-controls .touch-steering [data-control=reset] { grid-area: reset; width: 100%; height: 48px; font-size: 12px; }
  .touch-controls [data-control=item] { grid-area: item; }
  .touch-controls [data-control=drift] { grid-area: drift; }
  .touch-controls [data-control=boost] { grid-area: boost; }
  .touch-controls [data-control=brake] { grid-area: brake; }
  .touch-controls .touch-actions .touch-gas { grid-area: gas; width: 100%; height: 54px; font-size: 15px; }
  .touch-controls button:active, .touch-controls button.pressed { transform: none; filter: brightness(1.3); box-shadow: 0 0 0 2px #ffe1a0b3, 0 2px 9px #000a; }
  .speedometer-foot, .item-slot > kbd, .drift-indicator small, .wrong-way small, .race-key-hints { display: none; }
}

@media (pointer: coarse) and (max-width: 1100px) and (max-height: 540px) and (orientation: landscape) {
  .touch-controls { bottom: calc(8px + var(--safe-bottom)); }
  .touch-steering { grid-template-columns: repeat(2, 60px); gap: 8px; }
  .touch-controls .touch-steering button { width: 60px; height: 58px; }
  .touch-controls .touch-steering [data-control=reset] { height: 44px; }
  .touch-actions { grid-template-columns: repeat(4, 52px) 64px; grid-template-areas: 'item drift boost brake gas'; gap: 8px; }
  .touch-controls .touch-actions button { width: 52px; height: 56px; }
  .touch-controls .touch-actions .touch-gas { width: 64px; height: 60px; }
}

@media (max-width: 370px) and (orientation: portrait) {
  .hud-top { grid-template-columns: minmax(0, 1fr) 60px auto; }
  .hud-gold { max-width: 58px; }
  .hud-gold .gold-icon { width: 22px; height: 22px; }
  .hud-timing { padding-inline: 7px; gap: 8px; }
  .hud-timing span { font-size: 10px; }
  .hud-timing strong { font-size: 13px; }
  .position-copy b { font-size: 10px; }
}

/* Browser toolbars can leave a short portrait viewport. Put the zoom buttons
   beside the map and keep messages in the open column to its left. */
@media (max-width: 760px) and (max-height: 700px) and (orientation: portrait) {
  .minimap-zoom {
    left: auto; right: calc(100% + 8px); top: 64px; bottom: auto;
    width: 44px; flex-direction: column; gap: 6px;
  }
  .active-effects { display: flex; flex-wrap: nowrap; gap: 4px; }
  .active-effect, .effect-badge {
    width: 34px; min-width: 34px; padding: 2px;
    grid-template-rows: 26px 16px;
  }
  .active-effect img, .effect-badge img { width: 26px; height: 26px; }
  .race-center {
    top: calc(192px + var(--safe-top)); left: calc(12px + var(--safe-left));
    right: calc(204px + var(--safe-right)); width: auto;
  }
  .wrong-way { min-width: 0; padding: 8px; font-size: 18px; overflow-wrap: anywhere; }
}

/* Short landscape keeps a full-size touch row below the instruments. Status
   badges, messages, and the spell/drift row each have their own center band. */
@media (max-width: 1100px) and (max-height: 340px) and (orientation: landscape) {
  .hud { --dial-size: 110px; }
  .speedometer { bottom: calc(76px + var(--safe-bottom)); }
  .speed-digits strong { font-size: 24px; }
  .speedometer .nitro-bar { height: 28px; }
  .active-effects { top: calc(68px + var(--safe-top)); gap: 4px; }
  .active-effect, .effect-badge {
    width: 58px; min-width: 58px; padding: 2px;
    grid-template-columns: 22px auto; grid-template-rows: 22px; gap: 4px;
  }
  .active-effect img, .effect-badge img { width: 22px; height: 22px; grid-column: 1; grid-row: 1; }
  .active-effect > span, .effect-time { grid-column: 2; grid-row: 1; font-size: 12px; line-height: 16px; }
  .race-center {
    top: calc(102px + var(--safe-top)); display: flex; align-items: flex-start; gap: 6px;
    width: min(450px, calc(100vw - 358px - var(--safe-left) - var(--safe-right)));
    max-width: none;
  }
  .race-notice { flex: 1; min-width: 0; padding: 6px 8px; font-size: 13px; }
  .wrong-way { flex: 1; min-width: 0; margin: 0; padding: 4px 8px; font-size: 16px; }
  .race-bottom-center {
    bottom: calc(76px + var(--safe-bottom)); flex-direction: row; align-items: center;
    justify-content: center; gap: 8px;
    width: min(360px, calc(100vw - 360px - var(--safe-left) - var(--safe-right)));
  }
  .item-slot { flex: 0 1 212px; width: 212px; }
  .drift-indicator { flex: 0 1 140px; width: 140px; }
}

@media (pointer: coarse) and (max-width: 1100px) and (max-height: 340px) and (orientation: landscape) {
  .touch-steering { grid-template-columns: 52px 52px 64px; grid-template-areas: 'left right reset'; }
  .touch-controls .touch-steering button { width: 52px; height: 58px; }
  .touch-controls .touch-steering [data-control=reset] { width: 64px; height: 58px; }
}

/* WarKart 1.9: a two-axis joystick drives the kart, while the center spell slot
   casts its pickup. The three remaining buttons never cover the instruments. */
@media (pointer: coarse) {
  .touch-controls {
    left: calc(12px + var(--safe-left)); right: calc(12px + var(--safe-right));
    bottom: calc(12px + var(--safe-bottom)); gap: 12px; align-items: flex-end;
  }
  .touch-joystick {
    position: relative; flex: 0 0 112px; width: 112px; height: 112px;
    pointer-events: auto; touch-action: none; user-select: none;
    -webkit-user-select: none; -webkit-touch-callout: none;
  }
  .joystick-base {
    position: absolute; inset: 0; border-radius: 50%; border: 4px ridge #aaa18c;
    background: radial-gradient(circle, #161916dd 0 43%, #3c3c34e8 45% 65%, #171b17f2 68%);
    box-shadow: inset 0 0 0 2px #11140e, inset 0 0 18px #000, 0 4px 9px #0009;
  }
  .joystick-base::before {
    content: ''; position: absolute; inset: 12px; border: 1px solid #bd9a5166; border-radius: 50%;
    background: linear-gradient(90deg, transparent calc(50% - .5px), #d4ba7a3b 50%, transparent calc(50% + .5px)),
                linear-gradient(transparent calc(50% - .5px), #d4ba7a3b 50%, transparent calc(50% + .5px));
  }
  .joystick-knob {
    position: absolute; left: 50%; top: 50%; width: 52px; height: 52px;
    transform: translate(calc(-50% + var(--joystick-x, 0px)), calc(-50% + var(--joystick-y, 0px)));
    border: 4px ridge #b69c6e; border-radius: 50%; pointer-events: none;
    background: radial-gradient(circle at 35% 25%, #a74f35, #5c2118 52%, #24180f 90%);
    box-shadow: inset 0 1px 7px #eac78c55, 0 3px 7px #000b;
  }
  .touch-joystick.active .joystick-knob { filter: brightness(1.25); border-color: #e3c180; }
  .touch-actions {
    display: grid; grid-template-columns: repeat(2, 52px);
    grid-template-areas: 'reset reset' 'drift boost'; gap: 8px;
  }
  .touch-controls .touch-actions button { width: 52px; height: 52px; min-height: 44px; font-size: 11px; }
  .touch-controls .touch-actions [data-control=reset] { grid-area: reset; width: 100%; height: 44px; font-size: 12px; }
  .touch-controls .touch-actions [data-control=drift] { grid-area: drift; }
  .touch-controls .touch-actions [data-control=boost] { grid-area: boost; }
  .race-center { pointer-events: none; }
  .race-notice { font-size: 13px; padding: 8px 10px; }
  .wrong-way { min-width: 0; margin: 0; padding: 8px 10px; font-size: 16px; }
}

@media (pointer: coarse) and (max-width: 760px) and (orientation: portrait) {
  .speedometer { bottom: calc(132px + var(--safe-bottom)); }
  .race-bottom-center {
    left: calc(50% - 44px); right: auto; bottom: calc(12px + var(--safe-bottom));
    width: 88px; transform: none; gap: 0; align-items: stretch;
  }
  .item-slot {
    display: flex; flex-direction: column; justify-content: center; align-items: center;
    width: 88px; min-height: 94px; padding: 6px 3px; gap: 4px; border-width: 3px;
    text-align: center;
  }
  .item-slot > div:nth-child(2) { width: 100%; gap: 0; }
  .item-slot > div:nth-child(2) > span { display: none; }
  .item-slot strong, .item-slot.empty strong { font-size: 11px; line-height: 1.15; }
  .item-icon { width: 44px; height: 44px; }
  .drift-indicator {
    position: fixed; left: calc(12px + var(--safe-left)); bottom: calc(144px + var(--safe-bottom));
    width: calc(100vw - var(--dial-size) - 36px - var(--safe-left) - var(--safe-right));
    margin: 0; padding: 6px 8px;
  }
  .race-center {
    top: calc(266px + var(--safe-top)); left: calc(12px + var(--safe-left));
    right: calc(var(--dial-size) + 26px + var(--safe-right)); width: auto;
    max-width: none; transform: none; display: block;
  }
  .fps-counter { top: auto; bottom: calc(118px + var(--safe-bottom)); left: 50%; }
  .toast { bottom: calc(132px + var(--safe-bottom)); }
}

@media (pointer: coarse) and (max-width: 1100px) and (max-height: 540px) and (orientation: landscape) {
  .touch-joystick { flex-basis: 108px; width: 108px; height: 108px; }
  .joystick-knob { width: 48px; height: 48px; }
  .touch-actions { grid-template-columns: repeat(3, 52px); grid-template-areas: 'reset drift boost'; }
  .touch-controls .touch-actions button, .touch-controls .touch-actions [data-control=reset] { width: 52px; height: 54px; }
  .minimap-panel { top: calc(70px + var(--safe-top)); width: 100px; }
  .minimap-heading { height: 22px; }
  .speedometer { bottom: calc(80px + var(--safe-bottom)); }
  .race-bottom-center {
    bottom: calc(12px + var(--safe-bottom)); left: calc(50% - 106px); right: auto;
    width: 212px; transform: none; flex-direction: column; align-items: stretch; gap: 6px;
  }
  .item-slot { flex: none; width: 212px; min-height: 66px; padding: 7px 9px; }
  .drift-indicator { position: static; flex: none; width: 212px; padding: 5px 8px; }
  .race-center {
    top: calc(128px + var(--safe-top)); left: 50%; right: auto; transform: translateX(-50%);
    width: min(350px, calc(100vw - 350px - var(--safe-left) - var(--safe-right)));
    max-width: none; display: block;
  }
  .fps-counter { bottom: calc(126px + var(--safe-bottom)); }
}

@media (pointer: coarse) and (max-width: 1100px) and (max-height: 340px) and (orientation: landscape) {
  .minimap-panel { top: calc(68px + var(--safe-top)); width: 84px; }
  .minimap-heading { padding-inline: 3px; gap: 3px; font-size: 10px; }
  .speedometer { bottom: calc(76px + var(--safe-bottom)); }
  .race-center { top: calc(102px + var(--safe-top)); }
  .race-notice { padding: 6px 8px; }
  .wrong-way { padding: 4px 8px; }
}
