/* ------------------------------------------------------------------------- *
 * iOS-flavoured styling: grouped inset lists on a grey canvas, a segmented
 * control for navigation, system colours and the system font stack.
 * ------------------------------------------------------------------------- */

:root {
  /* iOS system colours, light appearance */
  --canvas: #f2f2f7;
  --card: #ffffff;
  --separator: #c6c6c8;
  --label: #000000;
  --label-2: rgba(60, 60, 67, 0.6);
  --label-3: rgba(60, 60, 67, 0.3);
  --fill: rgba(118, 118, 128, 0.12);
  --fill-strong: rgba(118, 118, 128, 0.2);
  --blue: #007aff;
  --green: #34c759;
  --red: #ff3b30;
  --tint-red: rgba(255, 59, 48, 0.1);
  --radius-card: 10px;
  --radius-field: 8px;
  --row-pad: 1rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --canvas: #000000;
    --card: #1c1c1e;
    --separator: #38383a;
    --label: #ffffff;
    --label-2: rgba(235, 235, 245, 0.6);
    --label-3: rgba(235, 235, 245, 0.3);
    --fill: rgba(118, 118, 128, 0.24);
    --fill-strong: rgba(118, 118, 128, 0.36);
    --blue: #0a84ff;
    --green: #30d158;
    --red: #ff453a;
    --tint-red: rgba(255, 69, 58, 0.18);
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  background: var(--canvas);
}

body {
  margin: 0;
  background: var(--canvas);
  color: var(--label);
  /* 17px is the iOS body size and keeps Safari from zooming on input focus. */
  font: 17px / 1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui,
    "Segoe UI", "Helvetica Neue", sans-serif;
  font-variant-numeric: tabular-nums;
}

code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.88em;
}

/* ----------------------------------------------------------- app header -- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(0.6rem + env(safe-area-inset-top))
    max(1rem, env(safe-area-inset-right)) 0.6rem max(1rem, env(safe-area-inset-left));
  background: var(--canvas);
  background: color-mix(in srgb, var(--canvas) 80%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
}

.app-header > * {
  max-width: 42rem;
  margin-inline: auto;
}

.app-header h1 {
  margin: 0 auto 0.6rem;
  /* iOS "Large Title" */
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 0.37px;
  line-height: 1.2;
}

/* --------------------------------------------------- segmented control -- */

.segmented {
  display: flex;
  gap: 0;
  padding: 2px;
  background: var(--fill);
  border-radius: 9px;
}

.segmented button {
  flex: 1 1 0;
  min-width: 0;
  padding: 6px 8px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--label);
  font-size: 13px;
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -0.08px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.segmented button.active {
  background: var(--card);
  box-shadow:
    0 3px 8px rgba(0, 0, 0, 0.12),
    0 3px 1px rgba(0, 0, 0, 0.04);
}

/* ------------------------------------------------------------- layout --- */

main {
  max-width: 42rem;
  margin: 0 auto;
  padding: 1.25rem max(1rem, env(safe-area-inset-left))
    calc(2.5rem + env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-right));
}

.intro {
  margin: 0 0 1.5rem;
  color: var(--label-2);
  font-size: 15px;
}

.teams {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
}

@media (min-width: 40rem) {
  .teams {
    grid-template-columns: 1fr 1fr;
    gap: 0 1rem;
  }
}

/* -------------------------------------------------- grouped list cards -- */

.group {
  /* The name input would otherwise impose a min-content floor on the track. */
  min-width: 0;
  margin: 0 0 1.4rem;
}

.group-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0 var(--row-pad) 0.4rem;
}

.group-title,
.group-head-trailing {
  margin: 0;
  color: var(--label-2);
  font-size: 13px;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.group > .group-title {
  padding: 0 var(--row-pad) 0.4rem;
}

.group-head-trailing {
  flex: 0 0 auto;
  font-weight: 400;
  white-space: nowrap;
}

.group-footer {
  margin: 0.45rem 0 0;
  padding: 0 var(--row-pad);
  color: var(--label-2);
  font-size: 13px;
}

.card {
  background: var(--card);
  border-radius: var(--radius-card);
  overflow: hidden;
}

/* --------------------------------------------------------------- rows --- */

.list-row,
.player-row,
.score-row,
.row-add {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 44px;
  padding: 0.35rem var(--row-pad);
}

/* iOS hairline separators, inset to the text column. */
.list-row:not(:last-child)::after,
.player-row::after,
.score-row::after {
  content: "";
  position: absolute;
  left: var(--row-pad);
  right: 0;
  bottom: 0;
  height: 0.5px;
  background: var(--separator);
}

.card > *:last-child::after {
  display: none;
}

.row-label {
  flex: 1 1 auto;
  min-width: 0;
}

.row-detail {
  flex: 0 1 auto;
  color: var(--label-2);
  text-align: right;
}

.list-row.stacked,
.list-row.column {
  flex-direction: column;
  align-items: stretch;
  gap: 0.15rem;
  padding-top: 0.55rem;
  padding-bottom: 0.55rem;
}

.list-row.stacked .row-label {
  color: var(--label-2);
  font-size: 13px;
}

.list-row.column .row-detail {
  font-size: 13px;
  text-align: left;
}

/* ------------------------------------------------------------- fields --- */

input,
select,
button {
  font: inherit;
  color: inherit;
}

input[type="text"],
input[type="number"] {
  appearance: none;
  -webkit-appearance: none;
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--label);
  font-variant-numeric: tabular-nums;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input::placeholder {
  color: var(--label-3);
}

/* Numeric entry gets a tinted pill so it reads as editable. */
/* `input` is part of the selector so these beat the `input[type=number]`
   reset above, which is otherwise the more specific rule. */
input.row-value,
input.p-elo,
input.p-games,
.score-row input {
  flex: 0 0 auto;
  padding: 0.3rem 0.5rem;
  border-radius: var(--radius-field);
  background: var(--fill-strong);
  text-align: center;
}

.row-value {
  width: 5rem;
}

.p-elo {
  width: 4.5rem;
}

.p-games {
  width: 3.5rem;
}

.p-name {
  flex: 1 1 auto;
  min-width: 0;
}

.score-row input {
  width: 3.1rem;
}

.score-row .colon {
  color: var(--label-3);
}

.game-label {
  flex: 1 1 auto;
  min-width: 0;
}

.row-select {
  display: flex;
  align-items: center;
}

.row-select select {
  appearance: none;
  -webkit-appearance: none;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
  margin: 0;
  padding: 0 1.4rem 0 0;
  border: 0;
  border-radius: 0;
  background: none;
  color: var(--label);
  text-overflow: ellipsis;
}

.row-select::after {
  content: "";
  flex: 0 0 auto;
  width: 7px;
  height: 7px;
  margin-left: -1rem;
  border-right: 2px solid var(--label-3);
  border-bottom: 2px solid var(--label-3);
  border-radius: 1px;
  transform: rotate(45deg) translate(-2px, -2px);
  pointer-events: none;
}

:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ------------------------------------------------------------- switch --- */

.switch {
  position: relative;
  flex: 0 0 auto;
  width: 51px;
  height: 31px;
}

.switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.switch .track {
  position: absolute;
  inset: 0;
  border-radius: 15.5px;
  background: var(--fill-strong);
  transition: background 0.2s ease;
  pointer-events: none;
}

.switch .track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow:
    0 3px 8px rgba(0, 0, 0, 0.15),
    0 1px 1px rgba(0, 0, 0, 0.16);
  transition: transform 0.2s ease;
}

.switch input:checked + .track {
  background: var(--green);
}

.switch input:checked + .track::after {
  transform: translateX(20px);
}

.switch input:focus-visible + .track {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

/* ------------------------------------------------------- add / remove --- */

.row-add {
  width: 100%;
  border: 0;
  background: none;
  color: var(--blue);
  text-align: left;
  cursor: pointer;
}

.row-add-glyph {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background-color: var(--green);
  background-image: linear-gradient(#fff, #fff), linear-gradient(#fff, #fff);
  background-size:
    11px 2px,
    2px 11px;
  background-position: center;
  background-repeat: no-repeat;
}

.row-remove {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 2.6rem;
  height: 2.6rem;
  margin-right: -0.7rem;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}

.row-remove::before {
  content: "";
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background-color: var(--label-3);
  background-image: linear-gradient(var(--card), var(--card));
  background-size: 11px 2px;
  background-position: center;
  background-repeat: no-repeat;
}

/* ------------------------------------------------------------- button --- */

.btn-filled {
  display: block;
  width: 100%;
  min-height: 50px;
  margin: 1.75rem 0 0;
  border: 0;
  border-radius: 12px;
  background: var(--blue);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
}

.btn-filled:active {
  filter: brightness(0.9);
}

/* ------------------------------------------------------------- output --- */

.output:not(:empty) {
  margin-top: 2rem;
}

.card-pad {
  padding: 1.1rem var(--row-pad);
}

.headline {
  margin: 0;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 0.37px;
  line-height: 1.1;
}

.sub {
  display: block;
  margin-top: 0.35rem;
  color: var(--label-2);
  font-size: 15px;
  font-weight: 400;
  letter-spacing: normal;
}

.note {
  margin: 0;
  padding: 0.85rem var(--row-pad);
  color: var(--label-2);
  font-size: 13px;
  line-height: 1.5;
}

.note code {
  display: inline-block;
  padding: 0.15rem 0.35rem;
  border-radius: 5px;
  background: var(--fill);
  color: var(--label);
}

.note + .note {
  padding-top: 0;
}

.error {
  margin: 0;
  padding: 0.9rem var(--row-pad);
  border-radius: var(--radius-card);
  background: var(--tint-red);
  color: var(--red);
  font-size: 15px;
}

/* -------------------------------------------------------------- table --- */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table.result {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

table.result th {
  padding: 0.6rem 0.45rem;
  color: var(--label-2);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: 0.04em;
  text-align: right;
  text-transform: uppercase;
  white-space: nowrap;
}

table.result td {
  padding: 0.6rem 0.45rem;
  font-size: 15px;
  text-align: right;
  white-space: nowrap;
}

table.result th:first-child,
table.result td:first-child {
  padding-left: var(--row-pad);
  text-align: left;
}

table.result th:last-child,
table.result td:last-child {
  padding-right: var(--row-pad);
}

table.result thead th {
  border-bottom: 0.5px solid var(--separator);
}

table.result tbody tr + tr td {
  border-top: 0.5px solid var(--separator);
}

table.result tbody tr.separator td {
  border-top: 0.5px solid var(--label-3);
}

.gain {
  color: var(--green);
}

.loss {
  color: var(--red);
}

/* ---------------------------------------------------------- notation --- */

.notation {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.55rem 0.9rem;
  margin: 0;
  padding: 0.9rem var(--row-pad);
  align-items: baseline;
}

.notation dt {
  text-align: right;
  white-space: nowrap;
}

.notation dd {
  margin: 0;
  color: var(--label-2);
  font-size: 15px;
}

/* -------------------------------------------------------------- math --- */

/* MathML is native in Safari and Chrome, so the formulas need no library.
   The default math font is a serif face, which gives them the LaTeX look. */
math {
  font-size: 1.04em;
}

.math-block {
  padding: 1.1rem var(--row-pad);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.math-block > math {
  width: fit-content;
  margin: 0 auto;
  font-size: 1.3rem;
}

.math-block + .note {
  padding-top: 0;
}

/* ------------------------------------------------- narrow screens --- */

/* Below the two-column breakpoint the wide result tables drop their
   secondary columns instead of scrolling sideways. */
@media (max-width: 39.99rem) {
  .hide-sm {
    display: none;
  }

  table.result th,
  table.result td {
    padding: 0.55rem 0.35rem;
    font-size: 14px;
  }

  table.result th:first-child,
  table.result td:first-child {
    padding-left: 0.7rem;
  }

  table.result th:last-child,
  table.result td:last-child {
    padding-right: 0.7rem;
  }

  /* Let headings wrap rather than push the table into sideways scrolling. */
  table.result th {
    white-space: normal;
  }
}
