/* Spacious refinement of the existing Sunny Toy Circuit UI.
   Geometry only: colors, artwork, materials, typefaces, states and motion
   continue to come from the established theme. Never scale the whole UI. */
html {
  --ui-space-group: 12px;
  --ui-space-section: 24px;
  --ui-safe-l: max(24px, env(safe-area-inset-left));
  --ui-safe-r: max(24px, env(safe-area-inset-right));
  --ui-safe-t: max(20px, env(safe-area-inset-top));
  --ui-safe-b: max(20px, env(safe-area-inset-bottom));
}

/* Home: the kart leads; smaller controls leave room between task groups. */
body:has(#home:not([hidden])) { --home-sidebar: 252px; --home-gutter: 32px; }
#home { --sidebar-control-height: 44px; --sidebar-control-font: 13px; }
#home .main-menu-nav { gap: 20px; }
#home :is(.sidebar-group, .garage-card) { padding: 16px; }
#home .sidebar-heading { font-size: 12px; margin-bottom: 12px; }
#home .mode-selector { gap: var(--ui-space-group); }
#home .chase-setup .mode-selector button { padding-block: 6px; }
#home :is(.garage-card .garage-open, .more-group .menu-row, .more-group .free-drive) { min-height: 44px; }
#home .home-launch { width: clamp(320px, 29vw, 400px); bottom: 64px; }
#home .home-launch .play-button { min-height: 68px; height: auto; padding: 8px 20px; gap: 16px; }
#home .home-launch .play-button strong { font-size: 42px; padding-top: 4px; }
#home .home-launch .play-button > svg { width: 28px; height: 28px; }
#home .home-launch .menu-room-actions { gap: var(--ui-space-group); }
#home .home-launch .menu-room-actions .menu-row { min-height: 44px; height: auto; padding: 8px; font-size: 13px; gap: 8px; }
#home .home-launch .menu-row svg { width: 22px; height: 22px; }
#home .home-extra-actions { gap: var(--ui-space-group); }
#home .home-extra-actions button { width: 44px; min-height: 44px; padding: 8px; }
body:has(#home:not([hidden])) :is(.profile-chip, .top-right #menu-settings, .top-right #sound-toggle) { min-height: 44px; height: 44px; }
body:has(#home:not([hidden])) .top-right :is(#menu-settings, #sound-toggle) { width: 44px; padding: 8px; }
@media (min-width: 1101px) {
  #home .home-title { top: 88px; width: min(780px, calc(100vw - 380px)); }
  #home .home-title h1 { font-size: clamp(64px, 5.6vw, 92px); }
  #home .home-title p { font-size: 13px; margin-top: 16px; }
  #home .setup-panel { inset: 100px auto 64px var(--home-gutter); height: calc(100dvh - 164px); max-height: calc(100dvh - 164px); }
}
@media (orientation: landscape) and (max-width: 1100px) {
  body:has(#home:not([hidden])) {
    --compact-rail: clamp(144px, 20vw, 192px);
    --compact-gutter: max(16px, env(safe-area-inset-left), env(safe-area-inset-right));
    --compact-top: max(72px, calc(env(safe-area-inset-top) + 60px));
    --compact-bottom: max(20px, env(safe-area-inset-bottom));
    --compact-gap: 20px;
  }
  #home :is(.sidebar-group, .garage-card) { padding: 12px; }
  #home .chase-setup { height: auto; }
  #home .mode-selector { gap: 8px; }
  #home .pace-group { top: auto; bottom: var(--compact-bottom); height: auto; }
  #home .garage-card { height: auto; }
  #home .more-group { top: calc(var(--compact-top) + 88px); bottom: auto; height: auto; gap: 12px; }
  #home .home-title { top: 24px; }
  #home .home-title h1 { font-size: clamp(28px, 4vw, 48px); }
  #home .home-launch { bottom: var(--compact-bottom); width: min(340px, calc(100% - 2 * (var(--compact-rail) + var(--compact-gutter) + 16px))); }
  #home .home-launch .play-button { min-height: 52px; height: auto; padding: 6px 12px; }
  #home .home-launch .play-button strong { font-size: 30px; line-height: 1.1; }
  #home .home-launch .menu-room-actions .menu-row { min-height: 44px; height: auto; font-size: 12px; }
  #home .home-launch #practice-description { margin: 12px 0; }
  #home .home-extra-actions { gap: 8px; right: calc(var(--compact-gutter) + 52px); width: 96px; }
}
@media (orientation: landscape) and (min-height: 561px) and (max-width: 1100px) {
  #home .more-group { top: calc(var(--compact-top) + 160px); }
  #home .home-title { top: 76px; }
}

/* Small tasks stay small, with comfortable interior padding. */
html body :is(#join-dialog, #invite-dialog, #profile-dialog, #quit-dialog, #feel-settings, #home-guide, #game-menu) { padding: 24px; }
html body #join-dialog { width: min(440px, calc(100vw - 48px)); }
html body #invite-dialog { width: min(460px, calc(100vw - 48px)); }
html body #profile-dialog { width: min(540px, calc(100vw - 48px)); }
html body #feel-settings { width: min(520px, calc(100vw - 48px)); }
html body #home-guide { width: min(620px, calc(100vw - 48px)); }
html body #game-menu { width: min(440px, calc(100vw - 48px)); }
html body #quit-dialog { width: min(360px, calc(100vw - 48px)); }
html body :is(.menu-dialog-heading, .invite-heading, .home-guide-heading, .game-menu-heading) { margin-bottom: 20px; }
html body :is(.menu-dialog-heading, .invite-heading, .home-guide-heading, .game-menu-heading) h2 { font-size: 23px; }
html body #room-code { min-height: 48px; font-size: 24px; }
html body #join-form > button { min-height: 48px; font-size: 15px; }
html body #invite-url { min-height: 52px; font-size: 30px; }
html body #profile-dialog .profile-record { margin-block: 20px; padding: 16px; }
html body #profile-dialog .profile-record dl { gap: 12px; }
html body #profile-dialog .profile-record dd { font-size: 22px; }
html body #home-guide :is(p, li) { line-height: 1.55; }
html body #game-menu #game-menu-tools > .top-right { gap: 12px; }
html body #game-menu .game-menu-actions { gap: 12px; }
html body #quit-dialog h2 { font-size: 23px; }
html body #landscape-gate { width: min(400px, calc(100vw - 48px)); padding: 32px; }
html body #landscape-gate h2 { font-size: 23px; }

/* Settings: smaller shell, steady navigation, space between real field groups.
   Explanations remain beside their fields; the duplicate context rail recedes. */
html body #settings-dialog[open] { width: min(880px, calc(100vw - 64px)); height: min(600px, calc(100dvh - 64px)); }
html body #settings-dialog .settings-header { padding: 12px 24px; }
html body #settings-dialog .settings-header h2 { font-size: 24px; }
html body #settings-dialog .settings-body { grid-template-columns: minmax(0, 1fr); }
html body #settings-dialog .settings-scroll { padding: 20px 24px; }
html body #settings-dialog .settings-group { padding: 16px; margin-bottom: 24px; }
html body #settings-dialog .settings-row { gap: 24px; padding: 12px 0; }
html body #settings-dialog .settings-row-copy p { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
html body #settings-dialog .settings-group-help { margin-top: 8px; }
html body #settings-dialog .settings-group-help summary { display: list-item; min-height: 44px; padding: 12px 0; font-size: 13px; cursor: pointer; }
html body #settings-dialog .settings-group-help dl { margin: 12px 0 0; font-size: 13px; line-height: 1.5; }
html body #settings-dialog .settings-group-help dl > div + div { margin-top: 16px; }
html body #settings-dialog .settings-group-help dt { font-weight: 800; }
html body #settings-dialog .settings-group-help dd { margin: 4px 0 0; }
html body #settings-dialog .settings-footer { gap: 12px; padding: 12px 24px; }
html body #settings-dialog .settings-footer-actions { gap: 12px; }
html body #settings-dialog #settings-reset-confirm:not([hidden]) { inset: auto 16px 12px; padding: 16px; gap: 20px; }

/* Tutorials and loading: give the actual illustration and arena room. */
html body #race-tutorial[open] { width: min(860px, calc(100vw - 64px)); height: min(480px, calc(100dvh - 64px)); padding: 24px; gap: 20px; }
html body #race-tutorial .tutorial-copy h2 { font-size: 24px; margin-bottom: 16px; }
html body #race-tutorial .tutorial-copy p { line-height: 1.5; margin: 12px 0; }
html body #race-tutorial .tutorial-nav, html body #race-tutorial .tutorial-actions { gap: 12px; }
html body #match-loading .match-loading-card { width: min(360px, 100%); padding: 24px; }
html body #match-loading h2 { font-size: 23px; }
html body #welcome-screen .welcome-logo { font-size: clamp(48px, 8vw, 80px); margin-inline: auto; }
html body #welcome-screen .welcome-crown { width: 48px; height: 40px; }
html body #welcome-screen .welcome-loading { width: min(300px, 75vw); }
html body #welcome-screen .welcome-track { height: 20px; padding: 3px; }

/* Lobby / round / result panels keep all players and actions. */
html body #room.room[data-phase=lobby]:not([hidden]) { grid-template-columns: minmax(0, 440px) minmax(200px, 280px); gap: 32px; padding: 88px var(--ui-safe-r) 32px var(--ui-safe-l); }
html body #room.room[data-phase=lobby]:not([hidden]) .overlay-card { padding: 24px; }
html body #room.room[data-phase=lobby] #overlay-title { font-size: 26px; }
html body #room.room[data-phase=lobby] #lobby-room-code { font-size: 44px; padding: 12px; }
html body #room.room[data-phase=lobby] .score-panel { height: auto; align-self: start; padding: 20px; }
html body #room.room[data-phase=lobby] #lobby-invitation { gap: 12px; }
html body #room.room[data-phase=lobby] .overlay-card > :is(#start, #add-bot) { min-height: 44px; font-size: 16px; }
html body #room.room[data-phase=lobby] #lobby-copy-code { min-height: 44px; font-size: 16px; }
html body #room:is([data-phase=break], [data-phase=finished]) .match-panels { max-width: 960px; gap: 24px; padding: 24px; margin-inline: auto; }
html body #room:is([data-phase=break], [data-phase=finished]) #overlay-title { font-size: 30px; }
html body #room:is([data-phase=break], [data-phase=finished]) .result-stats { gap: 12px; padding: 12px; }
html body #room:is([data-phase=break], [data-phase=finished]) .result-stats dd { font-size: 28px; }
html body #room:is([data-phase=break], [data-phase=finished]) :is(#start, #back-to-lobby) { min-height: 44px; padding: 10px 16px; font-size: 17px; }
html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .victory-emblem { width: 96px; height: 54px; }
html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .result-stats dd { font-size: 28px; }
html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .result-stats dd::before { width: 22px; height: 22px; }
html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay :is(#start, #back-to-lobby) { min-height: 44px; height: 48px; padding: 8px 16px; font-size: 17px; }
html body #replay-panel:not([hidden]) { width: min(400px, calc(100vw - 64px)); padding: 16px; }
html body #replay-canvas { max-height: 30dvh; }
html body #drive-tip { width: min(400px, calc(100vw - 48px)); padding: 20px; }
html body #feedback { max-width: min(280px, 60vw); padding: 8px 12px; }
html body #connection-notice { width: min(360px, calc(100vw - 64px)); padding: 12px 16px; }

/* Short landscapes need outer breathing room, never smaller touch targets. */
@media (orientation: landscape) and (max-height: 560px) {
  html { --ui-safe-l: max(16px, env(safe-area-inset-left)); --ui-safe-r: max(16px, env(safe-area-inset-right)); --ui-safe-t: max(12px, env(safe-area-inset-top)); --ui-safe-b: max(12px, env(safe-area-inset-bottom)); }
  html body :is(#join-dialog, #invite-dialog, #profile-dialog, #quit-dialog, #feel-settings, #home-guide, #game-menu) { padding: 16px 20px; }
  html body #profile-dialog[open] { width: min(600px, calc(100vw - 64px)); gap: 12px 24px; }
  html body #profile-dialog .profile-record { margin: 0; padding: 12px; }
  html body #feel-settings[open] { width: min(580px, calc(100vw - 64px)); }
  html body :is(.menu-dialog-heading, .invite-heading, .home-guide-heading, .game-menu-heading) { margin-bottom: 12px; }
  html body #settings-dialog[open] { width: min(720px, calc(100vw - 64px)); height: calc(100dvh - 48px); }
  html body #settings-dialog .settings-header { min-height: 48px; padding: 6px 20px; }
  html body #settings-dialog .settings-scroll { padding: 12px 20px; }
  html body #settings-dialog .settings-group { padding: 12px; margin-bottom: 20px; }
  html body #settings-dialog .settings-row { gap: 16px; padding: 6px 0; }
  html body #settings-dialog .settings-footer { min-height: 52px; padding: 6px 16px; }
  html body #settings-dialog #settings-reset-confirm:not([hidden]) { gap: 12px; padding: 12px; }
  html body #race-tutorial[open] { width: min(760px, calc(100vw - 64px)); height: calc(100dvh - 48px); padding: 16px 20px; gap: 12px 20px; }
  html body #race-tutorial .tutorial-copy h2 { font-size: 22px; margin-bottom: 12px; }
  html body #race-tutorial .tutorial-copy p { font-size: 13px; margin: 8px 0; }
  html body #room.room[data-phase=lobby]:not([hidden]) { grid-template-columns: minmax(0, 360px) minmax(180px, 240px); gap: 24px; padding-top: 64px; padding-bottom: 24px; }
  html body #room.room[data-phase=lobby]:not([hidden]) .overlay-card { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 12px; padding: 16px 20px; }
  html body #room.room[data-phase=lobby] .overlay-card > :is(#overlay-title, #lobby-invitation, #team-choice) { grid-column: 1 / -1; }
  html body #room.room[data-phase=lobby] :is(.lobby-code-label, .lobby-join-help) { display: none; }
  html body #room.room[data-phase=lobby] #lobby-code-status { margin: 0; }
  html body #room.room[data-phase=lobby] .overlay-card > :is(#start, #add-bot) { margin: 0; padding: 8px; font-size: 13px; width: 100%; }
  html body #room.room[data-phase=lobby] .overlay-card > #start { grid-column: 1; }
  html body #room.room[data-phase=lobby] .overlay-card > #add-bot { grid-column: 2; }
  html body #room.room[data-phase=lobby] #lobby-copy-code { margin: 0; }
  html body #room.room[data-phase=lobby] #overlay-title { font-size: 22px; margin: 0; }
  html body #room.room[data-phase=lobby] #lobby-invitation { grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px; margin: 0; padding-bottom: 12px; }
  html body #room.room[data-phase=lobby] #lobby-invitation > :is(.lobby-wifi, #lobby-code-status) { grid-column: 1 / -1; margin: 0; }
  html body #room.room[data-phase=lobby] #lobby-room-code { font-size: 28px; letter-spacing: .1em; padding: 6px; }
  html body #room.room[data-phase=lobby] #lobby-copy-code { width: auto; padding: 8px; font-size: 13px; }
  html body #room.room[data-phase=lobby] .score-panel { padding: 16px; }
  html body #room:is([data-phase=break], [data-phase=finished]) .match-panels { inset: calc(var(--ui-safe-t) + 48px) calc(var(--ui-safe-r) + 16px) calc(var(--ui-safe-b) + 12px) calc(var(--ui-safe-l) + 16px) !important; padding: 16px !important; gap: 24px; }
  html body #room:is([data-phase=break], [data-phase=finished]) .result-stats dd { font-size: 22px; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .overlay-card { gap: 6px; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .victory-emblem { width: 48px; height: 28px; margin: 0 auto; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay #overlay-title { font-size: 22px; margin: 0; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .match-results { margin: 0; padding: 4px; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .result-stats { padding: 4px; margin: 0; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .result-stats dd { font-size: 22px; gap: 4px; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .result-stats dd::before { width: 16px; height: 16px; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay .result-stats > div { padding: 0; }
  html body #room:is([data-phase=break], [data-phase=finished]):not([hidden]) #overlay :is(#start, #back-to-lobby) { font-size: 16px; height: 44px; padding: 6px 10px; margin: 0 !important; }
  html body #game-menu #game-menu-tools > .top-right { gap: 12px; }
  html body #drive-tip { width: min(360px, calc(100vw - 64px)); padding: 16px; }
}

/* Race actions share a baseline and reserve separate space for charge meters. */
html body #room[data-phase=playing] #power-hint[hidden] { display: none; }
html body #room #feedback-detail[hidden] { display: none; }
@media (orientation: landscape) {
  html:not([data-touch-ui=true]) body #room[data-phase=playing] .driving-hud { gap: 12px; }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] :is(#dash, #nitro, #power) {
    width: 100%; min-width: 0; height: 84px; min-height: 0;
    gap: 4px; padding: 8px 8px 12px;
  }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] :is(#dash, #nitro) { padding-bottom: 16px; }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] :is(#dash, #nitro, #power) > svg { width: 20px; height: 20px; flex: none; }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] :is(#dash-label, #power-label, .nitro-copy > strong) { font-size: 15px; line-height: 1.1; letter-spacing: 0; }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] :is(#dash-hint, #nitro-hint, #power-hint) { font-size: 10px; line-height: 1.15; }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] :is(.nitro-track, .dash-recharge) { bottom: 6px; }
}
@media (orientation: landscape) and (max-height: 650px) {
  html:not([data-touch-ui=true]) body #room[data-phase=playing] :is(#dash, #nitro, #power) { height: 76px; }
}

html body #room[data-phase=playing] #round-clock[data-final-stretch="close"] {
  background: #9d3528; border-color: #ffac6b;
}
html body #room[data-phase=playing] #round-clock[data-final-stretch="close"] :is(#timer, #round-label) {
  color: #fff7e6;
}
html body #room[data-phase=playing] #round-clock[data-final-stretch="close"] .timer-line { background: #ffd298; }

@media (orientation: landscape) and (min-width: 1101px) {
  html:not([data-touch-ui=true]) body #room[data-phase=playing] .minimap-panel { width: 144px; padding: 8px; }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] #minimap { width: 124px; height: auto; }
  html:not([data-touch-ui=true]) body #room[data-phase=playing] .score-panel { width: 220px; padding: 12px; }
}

/* Credits retain every attribution and link, with a calmer reading measure. */
body > main:has(> .back) { max-width: 760px; padding: 32px max(24px, env(safe-area-inset-left)) 64px; }
body > main:has(> .back) h1 { font-size: clamp(28px, 4vw, 40px); margin: 24px 0 20px; }
body > main:has(> .back) article { padding: 24px; margin: 28px 0; }
body > main:has(> .back) p { line-height: 1.6; margin-block: 12px; }
@media (max-width: 700px) and (orientation: portrait) {
  html body #settings-dialog[open] { width: calc(100vw - 32px); height: calc(100dvh - 40px); }
  html body #settings-dialog .settings-row { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  html body #race-tutorial[open] { width: calc(100vw - 32px); height: calc(100dvh - 40px); padding: 16px; }
  html body #room.room[data-phase=lobby]:not([hidden]) { grid-template-columns: minmax(0, 1fr); }
}
