:root {
  --red: #c9282b;
  --red-dark: #a9171b;
  --ink: #242424;
  --muted: #747474;
  --line: #e7e7e7;
  --paper: #fff;
  --shadow: 0 12px 32px rgba(34, 22, 22, 0.08);

  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  background: #f3f4f5;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-width: 320px;
  min-height: 100%;
  margin: 0;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  background: #f3f4f5;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: min(100%, 680px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.06);
}

/* Header */
.brandbar {
  position: sticky;
  top: 0;
  z-index: 25;
  flex: 0 0 76px;
  height: 76px;
  padding: 0 16px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  background: var(--red);
}

.brand-logo {
  grid-column: 2;
  justify-self: center;
  display: block;
  width: 58px;
  height: 58px;
  object-fit: contain;
  border-radius: 50%;
}

.header-nav-button {
  position: relative;
  z-index: 1;
  width: 52px;
  height: 56px;
  padding: 0;
  display: grid;
  place-items: center;
  color: #fff;
  background: transparent;
  border: 0;
  font-size: 56px;
  font-weight: 400;
  line-height: 1;
}

.header-nav-back {
  grid-column: 1;
  justify-self: start;
}

.header-nav-next {
  grid-column: 3;
  justify-self: end;
}

.header-nav-button:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
  border-radius: 8px;
}

.header-nav-button:active {
  transform: scale(0.98);
}

.app-shell:has(#screen-setup.active) .header-nav-button,
.app-shell:has(#screen-output.active) #lineup-next {
  display: none !important;
}

/* Main and screen visibility */
main {
  flex: 1 0 auto;
  padding: 28px 22px 40px;
}

.screen {
  display: none;
}

.screen.active {
  display: block;
  animation: appear 180ms ease-out;
}

[hidden] {
  display: none !important;
}

@keyframes appear {
  from {
    opacity: 0;
    transform: translateY(5px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Shared elements */
.screen-heading {
  margin: 4px 0 22px;
}

.screen-heading h1 {
  margin: 4px 0 6px;
  font-size: clamp(25px, 7vw, 34px);
  letter-spacing: -0.04em;
}

.eyebrow {
  margin: 0;
  color: var(--red);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.muted {
  color: var(--muted);
}

.screen-heading .muted {
  margin: 0;
  font-size: 14px;
}

.card {
  padding: 19px;
  margin: 0 0 16px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 15px;
  box-shadow: var(--shadow);
}

.button {
  min-height: 46px;
  padding: 0 17px;
  border: 1px solid transparent;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 750;
  transition: transform 120ms ease, background 120ms ease;
}

.button:active,
.icon-button:active {
  transform: scale(0.98);
}

.primary {
  color: #fff;
  background: var(--red);
}

.primary:hover {
  background: var(--red-dark);
}

.secondary {
  color: var(--ink);
  background: #fff;
  border-color: #d8d8d8;
}

.danger {
  color: #fff;
  background: #333;
}

.hidden {
  display: none !important;
}

/* Screen 1: setup */
.form-card {
  display: grid;
  gap: 10px;
}

label {
  margin-top: 8px;
  font-size: 13px;
  font-weight: 700;
}

input,
select {
  width: 100%;
  min-height: 48px;
  padding: 0 13px;
  color: var(--ink);
  background: #fff;
  border: 1px solid #d7d7d7;
  border-radius: 9px;
  outline: none;
}

#squads,
#coach {
  padding-left: 18px;
}

input:focus,
select:focus {
  border-color: var(--red);
  box-shadow: 0 0 0 3px rgba(201, 40, 43, 0.11);
}

#coach-field:not(.hidden) {
  display: grid !important;
  gap: 10px;
}

#screen-setup .full-width {
  width: auto;
  min-width: 150px;
  justify-self: center;
  margin-top: 14px;
}

#screen-setup .screen-heading h1 {
  font-size: clamp(23px, 6vw, 30px);
}

#screen-setup .eyebrow {
  font-size: 12px;
}

/* Screen 2: lineup */
#screen-lineup {
  margin-top: -14px;
}

#screen-lineup .screen-heading {
  position: relative;
  padding-right: 105px;
}

#screen-lineup .screen-heading h1 {
  font-size: 95%;
}

#screen-lineup .screen-heading .muted {
  display: inline-block;
}

#screen-lineup .screen-heading::after {
  position: absolute;
  right: 0;
  bottom: 0;
  color: var(--muted);
  content: attr(data-selected-count);
  font-size: 14px;
  white-space: nowrap;
}

#screen-lineup #player-search,
#screen-lineup label[for="player-search"] {
  display: none !important;
}

#screen-lineup .selection-summary > span:first-child {
  display: none;
}

#screen-lineup .selection-summary {
  margin: 0;
  padding: 0;
}

#screen-lineup .selection-summary strong {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.player-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 9px;
}

#screen-lineup .player-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.player-button {
  min-width: 0;
  min-height: 47px;
  padding: 7px 4px;
  color: #454545;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 9px;
  font-size: 12px;
  overflow-wrap: anywhere;
}

.player-button.selected {
  color: #fff;
  background: var(--red);
  border-color: var(--red);
}

#screen-lineup .player-button {
  padding-right: 4px;
  padding-left: 4px;
  font-size: 13px;
}

.button-row {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.button-row .button {
  flex: 1;
}

/* Screen 3: fixed scoreboard, scrollable scorer list */
#screen-match.active {
  position: fixed;
  z-index: 20;
  top: 78px;
  right: max(0px, calc((100vw - 680px) / 2));
  bottom: 0;
  left: max(0px, calc((100vw - 680px) / 2));
  width: min(100%, 680px);
  height: auto;
  min-height: 0;
  margin: 0 auto;
  padding: 4px 22px calc(20px + env(safe-area-inset-bottom));
  display: grid;

  /* Keep the scoreboard and opponent bar fixed in the screen layout.
     The scorer card gets the remaining height and scrolls internally. */
  grid-template-rows: 132px minmax(0, 1fr) 58px;
  gap: 8px;
  overflow: hidden;
  background: #f3f4f5;
}

#screen-match .scoreboard {
  position: relative;
  width: 100%;
  height: 122px;
  min-height: 122px;
  padding: 10px 12px 44px;
  color: #fff;
  background: var(--red);
  border-radius: 14px;
}

#screen-match .match-teams-row {
  position: absolute;
  top: 12px;
  right: 12px;
  left: 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: start;
  gap: 6px;
}

#screen-match .match-teams-row > .team:first-child,
#screen-match .match-teams-row > .team:last-child {
  width: 100%;
  justify-self: stretch;
  text-align: center;
}

#screen-match .match-teams-row > .score {
  justify-self: center;
}

#screen-match .team {
  min-width: 0;
  padding: 9px 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

#screen-match .team-name {
  font-size: clamp(11px, 3.5vw, 17px);
  font-weight: 700;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

#screen-match .team-coach {
  margin-top: 4px;
  font-size: 11px;
  line-height: 1.15;
}

#screen-match .team-heading {
  position: relative;
  width: max-content;
  max-width: 100%;
  padding-bottom: 17px;
}

#screen-match .team-heading .team-name {
  text-align: center;
}

#screen-match .team-heading .team-coach {
  position: absolute;
  top: calc(100% - 13px);
  left: 0;
  width: max-content;
  margin: 0;
  text-align: left;
}


#screen-match .team-coach[hidden] {
  display: none !important;
}

#screen-match .score {
  min-width: max-content;
  padding: 0 2px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: clamp(24px, 7vw, 36px);
  font-weight: 800;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#screen-match .score-dash {
  opacity: 0.65;
  font-size: 0.75em;
}

#screen-match .match-toolbar {
  position: absolute;
  right: 12px;
  bottom: 8px;
  left: 12px;
  min-height: 38px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}

#screen-match .match-clock-button,
#screen-match .team-reverse-button {
  width: 42px;
  height: 38px;
  min-width: 42px;
  min-height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  color: #fff;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 10px;
  font-size: 22px;
  transition: background 120ms ease, transform 120ms ease;
}

#screen-match .match-clock-button {
  justify-self: start;
}

#screen-match .match-timer {
  color: #fff;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}

#screen-match .team-reverse-button {
  justify-self: end;
}

#screen-match .match-clock-button:active,
#screen-match .team-reverse-button:active {
  background: rgba(255, 255, 255, 0.28);
  transform: scale(0.9);
}

#screen-match > .scorer-card {
  width: 100%;
  height: auto;
  min-height: 0;
  padding: 0 6px;
  margin: 0;
  overflow: hidden;
  border-radius: 14px;
}

#screen-match #scorer-list {
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 0 4px;
  margin: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

#screen-match .scorer-row {
  min-height: 52px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 40px 40px;
  align-items: center;
  gap: 8px;
  border-top: 1px solid #ededed;
}

#screen-match .scorer-name {
  min-width: 0;
  font-size: 14px;
  overflow-wrap: anywhere;
}

#screen-match .scorer-count {
  min-width: 20px;
  text-align: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

#screen-match .goal-button {
  width: 40px;
  height: 40px;
  color: var(--red);
  background: #fff;
  border: 1px solid #e4e4e4;
  border-radius: 9px;
  font-size: 25px;
}

#screen-match .goal-button.minus {
  color: #555;
  font-size: 22px;
}

#screen-match .goal-button:disabled {
  opacity: 0.45;
  cursor: default;
}

#screen-match .opponent-dock {
  position: static;
  z-index: auto;
  width: calc(100% - 24px);
  height: 58px;
  min-height: 58px;
  justify-self: center;
  align-self: start;
  padding: 7px 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 28px 40px 40px;
  align-items: center;
  gap: 8px;
  transform: none;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

#screen-match .opponent-dock-name {
  min-width: 0;
  font-size: 14px;
  font-weight: 650;
  overflow-wrap: anywhere;
}

#screen-match .opponent-dock-count {
  text-align: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

#screen-match .opponent-dock .goal-button {
  width: 40px;
  height: 40px;
  color: var(--red);
  background: #fff;
}

#screen-match .opponent-dock .goal-button.minus {
  color: #555;
}

#screen-match:not(.active) .opponent-dock {
  display: none;
}

/* Screen 4: poster */
.output-heading {
  text-align: center;
}

.match-poster {
  overflow: hidden;
  background: #fff;
  border: 1px solid #e7e7e7;
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.poster-header {
  min-height: 52px;
  padding: 12px 16px 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: var(--red);
}

.poster-meta {
  width: 100%;
  display: flex;
  justify-content: center;
}

#poster-date {
  font-size: 14px;
  font-weight: 700;
  text-align: center;
}

.poster-score {
  min-height: 122px;
  padding: 6px 12px 16px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  color: #fff;
  text-align: center;
  background: var(--red);
}

.poster-team-block {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.poster-team-heading {
  position: relative;
  width: max-content;
  max-width: 100%;
  padding-bottom: 18px;
  text-align: center;
}

.poster-team-heading .poster-team {
  display: block;
}

.poster-team-heading .poster-coach {
  position: absolute;
  top: calc(100% - 14px);
  left: 0;
  width: max-content;
  margin: 0;
  text-align: left;
}


.poster-team {
  font-size: clamp(13px, 3.5vw, 17px);
  font-weight: 700;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.poster-coach {
  font-size: 11px;
  line-height: 1.2;
}

.poster-coach:empty {
  display: none;
}

.poster-score strong {
  font-size: 36px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.poster-colon {
  font-size: 26px;
  font-weight: 700;
  opacity: 0.75;
}

#screen-output .poster-colon {
  position: relative;
  top: 14px;
}

.poster-goals,
.poster-lineup {
  padding: 17px;
}

.poster-goals h2,
.poster-lineup h2 {
  margin: 0 0 11px;
  color: var(--red);
  font-size: 13px;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

.poster-goals p {
  margin: 0;
  font-size: 13px;
  line-height: 1.6;
}

.poster-goals p.scorer-summary {
  overflow-wrap: normal;
  word-break: normal;
}

.scorer-summary-item {
  white-space: nowrap;
}

.poster-lineup {
  background: #f8f8f8;
}

.poster-player-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.poster-player {
  min-height: 66px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 5px;
  text-align: center;
  font-size: 10px;
  overflow-wrap: anywhere;
}

.shirt-icon {
  width: 29px;
  height: 27px;
  color: var(--red);
}

.poster-footer {
  padding: 12px;
  color: #777;
  text-align: center;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.13em;
}

.output-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-top: 16px;
}

/* Footer and toast */
.app-footer {
  flex: 0 0 auto;
  margin-top: auto;
  padding: 17px;
  color: #999;
  text-align: center;
  font-size: 10px;
  border-top: 1px solid #eee;
}

.toast {
  position: fixed;
  z-index: 30;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  left: 16px;
  width: fit-content;
  max-width: min(420px, calc(100% - 32px));
  margin: auto;
  padding: 12px 16px;
  color: #fff;
  background: #242424;
  border-radius: 10px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 180ms, transform 180ms;
  font-size: 13px;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Responsive adjustments */
@media (max-width: 380px) {
  main {
    padding-right: 14px;
    padding-left: 14px;
  }

  .card {
    padding: 13px;
  }

  .player-grid {
    gap: 7px;
  }

  .player-button {
    font-size: 11px;
  }

  .lineup-selected-count {
    font-size: 12px;
  }

  #screen-match.active {
    padding-right: 14px;
    padding-left: 14px;
  }

  #screen-match .match-teams-row {
    right: 8px;
    left: 8px;
    gap: 4px;
  }

  #screen-match .team-name {
    font-size: 11px;
  }

  #screen-match .team-coach {
    font-size: 10px;
  }

  #screen-match .score {
    gap: 3px;
    font-size: 24px;
  }

  #screen-match .opponent-dock {
    width: calc(100% - 16px);
  }

  .poster-score {
    min-height: 132px;
    padding-right: 8px;
    padding-left: 8px;
    gap: 4px;
  }

  .poster-team {
    font-size: 12px;
  }

  .poster-score strong {
    font-size: 32px;
  }
}

.poster-player .shirt-icon {
  display: block;
  width: 52px;
  height: 52px;
  object-fit: contain;
}

