/*
 * Handedness is a peripheral-HUD composition, not only a control swap.
 * The stats/score/steering column stays opposite the map/drive-pad column,
 * while the centre of the road remains clear.
 */
:root {
  --turn-peripheral-left-edge: max(9px, env(safe-area-inset-left));
  --turn-peripheral-right-edge: max(9px, env(safe-area-inset-right));
  --turn-peripheral-stats-height: clamp(43.5px, 8vh, 49.5px);
  --turn-peripheral-column-gap: clamp(6px, 2vh, 12px);
  --turn-peripheral-score-control-gap: clamp(9px, 2vh, 12px);
  --turn-peripheral-score-flow-gap: 2.25px;
  --turn-peripheral-manual-bottom: max(51px, calc(env(safe-area-inset-bottom) + 42px));
  --turn-peripheral-manual-height: clamp(76.5px, 15vw, 112.5px);
  --turn-peripheral-score-top: calc(
    max(7.5px, env(safe-area-inset-top))
    + var(--turn-peripheral-stats-height)
    + var(--turn-peripheral-column-gap)
  );
  --turn-peripheral-score-clearance: calc(
    var(--turn-peripheral-manual-bottom)
    + var(--turn-peripheral-manual-height)
    + var(--turn-peripheral-score-control-gap)
  );
}

:root.turn-right-handed-controls .topbar {
  flex-direction: row;
}

:root.turn-left-handed-controls .topbar {
  flex-direction: row-reverse;
}

/*
 * Reserve the complete two-row DRIFT + FLOW footprint above steering. The
 * flex rows shrink together only when viewport height makes that necessary;
 * a single active row keeps its normal 104px instrument height.
 */
.score-feedback {
  top: var(--turn-peripheral-score-top);
  right: auto;
  left: var(--turn-peripheral-left-edge);
  display: flex;
  height: min(
    calc(
      var(--score-feedback-paper-height)
      + var(--score-feedback-paper-height)
      + var(--turn-peripheral-score-flow-gap)
    ),
    max(
      var(--score-feedback-paper-height),
      calc(
        var(--app-height)
        - var(--turn-peripheral-score-top)
        - var(--turn-peripheral-score-clearance)
      )
    )
  );
  flex-direction: column;
}

.score-feedback-row {
  min-height: 39px;
  flex: 0 1 var(--score-feedback-paper-height);
}

.score-feedback-flow-row {
  margin-top: var(--turn-peripheral-score-flow-gap);
}

.score-feedback-state {
  height: 100%;
}

.score-feedback-gauge-shell {
  height: calc(
    100%
    - var(--score-feedback-gauge-inset-y)
    - var(--score-feedback-gauge-inset-y)
  );
}

:root.turn-left-handed-controls .score-feedback {
  right: var(--turn-peripheral-right-edge);
  left: auto;
}

/* Default/right-handed gauges unfold from the left column into the playfield. */
:root.turn-right-handed-controls .score-feedback-gauge-shell,
:root:not(.turn-left-handed-controls) .score-feedback-gauge-shell {
  right: auto;
  left: calc(100% - var(--score-feedback-gauge-overlap));
  transform-origin: left center;
}

/* Left-handed mode mirrors the complete gauge, including its fill direction. */
:root.turn-left-handed-controls .score-feedback-gauge-shell {
  right: calc(100% - var(--score-feedback-gauge-overlap));
  left: auto;
  transform-origin: right center;
}

:root.turn-left-handed-controls .score-feedback-state {
  transform-origin: right center;
}

:root.turn-left-handed-controls .score-feedback-meter,
:root.turn-left-handed-controls .score-feedback-gauge {
  border-radius: 7.5px 0 0 7.5px;
  transform-origin: right center;
}

:root.turn-left-handed-controls .score-feedback-meter::before,
:root.turn-left-handed-controls .score-feedback-gauge::before {
  right: 3.75px;
  left: auto;
}

:root.turn-left-handed-controls .score-feedback-meter > i,
:root.turn-left-handed-controls .score-feedback-gauge > i {
  background: linear-gradient(
    to left,
    var(--score-feedback-gauge-fill-a),
    var(--score-feedback-gauge-fill-b)
  );
  transform-origin: right center;
}

:root.turn-left-handed-controls .score-feedback-meter > i::after,
:root.turn-left-handed-controls .score-feedback-gauge > i::after {
  right: auto;
  left: 0;
  transform: translateX(-35%) scaleY(.65);
}

:root.turn-left-handed-controls .score-feedback[data-gauge-heat="hot"] .score-feedback-meter > i::before,
:root.turn-left-handed-controls .score-feedback[data-flow-heat="hot"] .score-feedback-gauge[data-score-channel="flow"] > i::before,
:root.turn-left-handed-controls .score-feedback[data-gauge-heat="critical"] .score-feedback-meter > i::before,
:root.turn-left-handed-controls .score-feedback[data-flow-heat="critical"] .score-feedback-gauge[data-score-channel="flow"] > i::before {
  animation-name: turn-score-gauge-rush-mirrored;
}

:root.turn-left-handed-controls .score-feedback[data-gauge-heat="hot"] .score-feedback-meter > i::after,
:root.turn-left-handed-controls .score-feedback[data-flow-heat="hot"] .score-feedback-gauge[data-score-channel="flow"] > i::after,
:root.turn-left-handed-controls .score-feedback[data-gauge-heat="critical"] .score-feedback-meter > i::after,
:root.turn-left-handed-controls .score-feedback[data-flow-heat="critical"] .score-feedback-gauge[data-score-channel="flow"] > i::after {
  animation-name: turn-score-gauge-cap-mirrored;
}

@keyframes turn-score-gauge-rush-mirrored {
  from { transform: translateX(18%); }
  to { transform: translateX(-18%); }
}

@keyframes turn-score-gauge-cap-mirrored {
  0%, 100% { transform: translateX(-35%) scaleY(.65); }
  50% { transform: translateX(15%) scaleY(1.12); }
}

/*
 * Event callouts occupy the inboard slot beyond the gauge instead of growing
 * down into steering. The same rule stays clear of the opposite map column.
 */
.score-feedback-callout,
.score-feedback[data-score-layout="dual"] .score-feedback-callout {
  top: 0;
  right: auto;
  left: calc(100% + var(--score-feedback-gauge-width) + 6.75px);
  max-width: min(165px, 28vw);
  overflow: hidden;
  transform-origin: left top;
}

.score-feedback-callout strong,
.score-feedback-callout b {
  overflow: hidden;
  text-overflow: ellipsis;
}

:root.turn-left-handed-controls .score-feedback-callout,
:root.turn-left-handed-controls .score-feedback[data-score-layout="dual"] .score-feedback-callout {
  right: calc(100% + var(--score-feedback-gauge-width) + 6.75px);
  left: auto;
  transform-origin: right top;
}

@media (max-height: 420px) and (orientation: landscape) {
  :root {
    --turn-peripheral-stats-height: 43.5px;
    --turn-peripheral-column-gap: clamp(6px, 1.8vh, 7.5px);
    --turn-peripheral-score-control-gap: 9px;
    --turn-peripheral-manual-bottom: max(43.5px, calc(env(safe-area-inset-bottom) + 37.5px));
    --turn-peripheral-manual-height: clamp(72px, 14vw, 94.5px);
  }

  .score-feedback {
    --score-feedback-paper-width: clamp(111px, 21vw, 142.5px);
    --score-feedback-gauge-width: clamp(31.5px, 5.5vw, 39px);
    --score-feedback-gauge-inset-y: 3.75px;
    --score-feedback-gauge-overlap: 1.5px;
  }

  .score-feedback-state {
    padding: 4.5px 6px 5.25px;
    border-width: 1.5px;
    box-shadow: 2.25px 2.25px 0 var(--turn-shadow-color);
  }

  .score-feedback-heading,
  .score-feedback-lap {
    font-size: max(var(--turn-text-floor, 11px), clamp(0.3rem, 1vw, 0.405rem));
  }

  .score-feedback-values {
    margin-top: 0.75px;
  }

  .score-feedback-values strong {
    font-size: max(var(--turn-text-floor, 11px), clamp(0.81rem, 3.2vw, 1.29rem));
  }

  .score-feedback-values b {
    padding: 1.5px 3.75px 2.25px;
    font-size: max(var(--turn-text-floor, 11px), clamp(0.435rem, 1.4vw, 0.585rem));
  }

  .score-feedback-flow-state .score-feedback-values b {
    font-size: max(var(--turn-text-floor, 11px), clamp(0.48rem, 1.55vw, 0.645rem));
  }

  .score-feedback-techniques {
    font-size: max(var(--turn-text-floor, 11px), clamp(0.2325rem, .7vw, 0.3rem));
  }

  .score-feedback-meter,
  .score-feedback-gauge {
    border-width: 1.5px;
  }

  :root.turn-left-handed-controls .score-feedback-meter::before,
  :root.turn-left-handed-controls .score-feedback-gauge::before {
    right: 3px;
  }

  .score-feedback-callout,
  .score-feedback[data-score-layout="dual"] .score-feedback-callout {
    left: calc(100% + var(--score-feedback-gauge-width) + 5.25px);
    max-width: min(142.5px, 28vw);
    padding: 4.5px 6px 5.25px;
    border-width: 1.5px;
    box-shadow: 2.25px 2.25px 0 var(--turn-shadow-color);
  }

  :root.turn-left-handed-controls .score-feedback-callout,
  :root.turn-left-handed-controls .score-feedback[data-score-layout="dual"] .score-feedback-callout {
    right: calc(100% + var(--score-feedback-gauge-width) + 5.25px);
    left: auto;
  }

  .manual-steer {
    bottom: var(--turn-peripheral-manual-bottom);
    height: var(--turn-peripheral-manual-height);
  }
}

@media (max-height: 270px) and (orientation: landscape) {
  :root {
    --turn-peripheral-stats-height: 36px;
    --turn-peripheral-column-gap: 4.5px;
    --turn-peripheral-score-control-gap: 7.5px;
    --turn-peripheral-score-flow-gap: 1.5px;
    --turn-peripheral-manual-bottom: max(36px, calc(env(safe-area-inset-bottom) + 31.5px));
    --turn-peripheral-manual-height: 61.5px;
  }

  .stats {
    gap: 3px;
  }

  .stats .chip {
    height: 36px;
    padding: 3px 4.5px 3.75px;
    border-width: 1.5px;
    border-radius: 7.5px;
    box-shadow: 1.5px 1.5px 0 var(--turn-shadow-color);
    font-size: max(var(--turn-text-floor, 11px), 0.345rem);
  }

  .stats .chip strong,
  .stats .race-position-hud strong {
    font-size: max(var(--turn-text-floor, 11px), 0.585rem);
  }

  .stats .chip:nth-child(1) {
    width: 60px;
  }

  .stats .chip:nth-child(2),
  .stats .chip:nth-child(3) {
    width: 36px;
  }

  .stats .chip:nth-child(4),
  .stats .chip:nth-child(5) {
    width: 67.5px;
  }

  .map-wrap {
    width: min(16vw, 72px);
    padding: 2.25px;
    border-width: 1.5px;
    border-radius: 7.5px;
    box-shadow: 1.5px 1.5px 0 var(--turn-shadow-color);
  }

  .score-feedback {
    --score-feedback-paper-width: 94.5px;
    --score-feedback-gauge-width: 28.5px;
    --score-feedback-gauge-inset-y: 3px;
  }

  .score-feedback-state {
    padding: 3px 4.5px;
    border-radius: 7.5px;
  }

  .score-feedback-heading,
  .score-feedback-lap {
    gap: 3px;
    font-size: max(var(--turn-text-floor, 11px), 0.27rem);
  }

  .score-feedback-values strong {
    font-size: max(var(--turn-text-floor, 11px), clamp(0.705rem, 3.1vw, 0.84rem));
  }

  .score-feedback-values b,
  .score-feedback-flow-state .score-feedback-values b {
    padding: 0.75px 3px 1.5px;
    border-width: 0.75px;
    font-size: max(var(--turn-text-floor, 11px), 0.405rem);
  }

  .score-feedback-techniques {
    font-size: max(var(--turn-text-floor, 11px), 0.225rem);
  }

  .score-feedback-callout,
  .score-feedback[data-score-layout="dual"] .score-feedback-callout {
    left: calc(100% + var(--score-feedback-gauge-width) + 4.5px);
    max-width: min(112.5px, 25vw);
    padding: 3.75px 5.25px 4.5px;
    border-radius: 6.75px;
  }

  :root.turn-left-handed-controls .score-feedback-callout,
  :root.turn-left-handed-controls .score-feedback[data-score-layout="dual"] .score-feedback-callout {
    right: calc(100% + var(--score-feedback-gauge-width) + 4.5px);
    left: auto;
  }

  .manual-steer {
    width: 96px;
    border-width: 3px;
    border-radius: 16.5px;
    box-shadow: 3.75px 3.75px 0 var(--turn-shadow-color);
  }

  .manual-steer::before,
  .manual-steer::after {
    font-size: max(var(--turn-text-floor, 11px), 1.35rem);
  }

  .manual-steer-knob {
    width: 40.5px;
    border-width: 3px;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root.turn-left-handed-controls .score-feedback-meter > i::before,
  :root.turn-left-handed-controls .score-feedback-gauge > i::before,
  :root.turn-left-handed-controls .score-feedback-meter > i::after,
  :root.turn-left-handed-controls .score-feedback-gauge > i::after {
    animation: none !important;
  }
}
