/* Layout and components. Every color, font and size comes from tokens.css. */

*, *::before, *::after { box-sizing: border-box; }
html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100%;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: var(--step-0);
  line-height: 1.5;
  /* Star-trail rings around a soft glow, like a long-exposure night sky */
  background:
    radial-gradient(1100px 520px at 50% -160px, var(--glow), transparent 70%),
    repeating-radial-gradient(circle at 50% -140px, var(--ring) 0 1px, transparent 2px 28px),
    var(--bg);
}

h1, h2, h3, h4 { margin: 0; text-wrap: balance; }
p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

.display {
  font-family: var(--font-display);
  font-variation-settings: var(--display-axes);
  font-weight: 700;
}

/* ---------- Masthead band ---------- */

.masthead {
  background: var(--band);
  color: var(--band-ink);
  border-top: 3px solid var(--band-line);
  border-bottom: 1px solid var(--line);
  margin-top: calc(-1 * env(safe-area-inset-top, 0px));
  padding-top: env(safe-area-inset-top, 0px);
}
.masthead-inner {
  max-width: var(--content-max);
  margin: 0 auto;
  padding-block: var(--space-4) var(--space-5);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: grid;
  gap: var(--space-4);
}
.brand-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3) var(--space-5);
}
.brand { display: flex; gap: var(--space-3); align-items: center; min-width: 0; }
.brand-mark { width: 52px; height: 52px; flex: none; }
.title {
  font-family: var(--font-display);
  font-variation-settings: var(--display-axes);
  font-weight: 800;
  font-size: var(--step-4);
  line-height: 1;
  letter-spacing: -0.01em;
}
/* One line on wide screens; wraps on phones and narrow windows. */
.tagline { color: var(--band-muted); font-size: var(--step--1); }
@media (min-width: 1100px) {
  .tagline { white-space: nowrap; }
}

.host-controls { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.masthead .btn { background: transparent; color: var(--band-ink); border-color: var(--line); }
.masthead .btn:hover { border-color: var(--gold); }
.masthead .btn-primary { background: var(--gold); color: var(--gold-ink); border-color: var(--gold); }
.pin-form { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.pin-form label { font-weight: 700; }
.pin-form input { width: 7.5em; background: var(--surface); color: var(--ink); }
.pin-note { flex-basis: 100%; font-size: var(--step--1); color: var(--band-muted); }

/* ---------- Page shell ---------- */

.shell {
  max-width: var(--content-max);
  margin: 0 auto;
  padding-block: var(--space-5) var(--space-7);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: grid;
  gap: var(--space-5);
}
#app { display: grid; gap: var(--space-5); min-width: 0; }

/* ---------- Controls ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 38px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-control);
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--gold); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { background: var(--gold); border-color: var(--gold); color: var(--gold-ink); }
.btn-primary:hover { filter: brightness(1.06); }
.btn-quiet { background: transparent; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: var(--danger-ink); }
.btn-small { min-height: 32px; padding: 0 var(--space-3); font-size: var(--step--1); }
.btn-big {
  min-height: 48px;
  padding: 0 var(--space-5);
  font-family: var(--font-display);
  font-variation-settings: var(--display-axes);
  font-size: var(--step-1);
}

input:not([type="checkbox"]), select, textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-control);
  padding: var(--space-2) var(--space-3);
  min-height: 40px;
}
input:focus, select:focus, textarea:focus { border-color: var(--gold); outline: none; box-shadow: 0 0 0 3px var(--gold-soft); }
textarea { resize: vertical; }

.field { display: grid; gap: var(--space-1); align-content: start; min-width: 0; border: 0; padding: 0; margin: 0; }
.field label, .field legend { font-weight: 700; font-size: var(--step--1); padding: 0; }
.field-row { display: flex; flex-wrap: wrap; gap: var(--space-3); }
.field-row > .field { flex: 1 1 150px; }
.field-row > .grow { flex: 3 1 240px; }

.segmented { display: inline-flex; justify-self: start; border: 1px solid var(--line); border-radius: var(--radius-control); overflow: hidden; }
.seg {
  min-width: 56px; min-height: 40px; border: 0; background: var(--surface);
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 700; font-size: var(--step-1); cursor: pointer;
}
.seg + .seg { border-left: 1px solid var(--line); }
.seg.on { background: var(--gold); color: var(--gold-ink); }

.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.chip {
  border: 1px solid var(--line);
  border-radius: var(--radius-control);
  padding: 2px var(--space-3);
  font-size: var(--step--1);
  background: var(--surface);
  font-variant-numeric: tabular-nums;
  line-height: 1.6;
}
.chip-flag { border-color: var(--teal); color: var(--teal); }
.chip-warn { border-color: var(--warn); color: var(--warn); background: var(--warn-soft); }

/* Small solid badges, like a "NEW" tag */
.pill {
  display: inline-block;
  border-radius: 4px;
  padding: 1px var(--space-2);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  line-height: 1.5;
}
.pill-live { background: var(--teal); color: var(--teal-ink); }
.pill-done { background: var(--surface-2); color: var(--muted); }
.pill-final { background: var(--gold); color: var(--gold-ink); }
.pill-host { background: var(--gold); color: var(--gold-ink); }

.tag {
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.03em;
  border: 1px solid currentColor; border-radius: 4px; padding: 0 5px;
  color: var(--muted); vertical-align: 1px;
}

.muted { color: var(--muted); }
.fine { color: var(--muted); font-size: var(--step--1); }
.error { color: var(--loss); font-weight: 700; }
.warn { color: var(--warn); background: var(--warn-soft); border-radius: var(--radius-control); padding: var(--space-2) var(--space-3); font-size: var(--step--1); }
.actions { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.flash {
  display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; justify-content: space-between;
  border: 1px solid var(--loss); border-radius: var(--radius-control);
  padding: var(--space-2) var(--space-3); color: var(--loss); background: var(--surface);
}

/* ---------- Panels ---------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-panel);
  padding: var(--space-5);
  display: grid;
  gap: var(--space-4);
  align-content: start;
  min-width: 0;
  box-shadow: var(--shadow);
}
.section-title {
  font-family: var(--font-display);
  font-variation-settings: var(--display-axes);
  font-weight: 700;
  font-size: var(--step-2);
  line-height: 1.1;
}
.mini-title { font-size: var(--step--1); font-weight: 700; }
.empty { justify-items: start; }

/* ---------- Setup ---------- */

.setup-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr);
  gap: var(--space-5);
  align-items: start;
}
.setup-side { display: grid; gap: var(--space-4); align-content: start; }
.field-players { gap: var(--space-2); }
.field-players textarea { font-size: var(--step-1); line-height: 1.6; min-height: 17rem; }

/* ---------- Event head and host toolbar ---------- */

.event-head { display: grid; gap: var(--space-3); }
.event-title-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-3); }
.event-title {
  font-family: var(--font-display);
  font-variation-settings: var(--display-axes);
  font-weight: 800;
  font-size: var(--step-3);
  line-height: 1.05;
}
.event-desc { color: var(--muted); max-width: 70ch; }
.host-bar {
  display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center;
}
.host-bar-label { font-size: var(--step--1); color: var(--muted); margin-right: var(--space-1); }

/* ---------- Board: pairings + standings ---------- */

.board { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: var(--space-5); align-items: start; }
.board > .standings { position: sticky; top: calc(env(safe-area-inset-top, 0px) + var(--space-4)); }

.tabs { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.tab {
  min-width: 40px; min-height: 40px;
  border-radius: var(--radius-control);
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 700; font-size: var(--step-1);
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.tab-done { color: var(--muted); }
.tab-live { border-color: var(--teal); color: var(--teal); background: transparent; }
.tab-future { border-style: dashed; background: transparent; color: var(--muted); cursor: default; opacity: 0.7; }
.tab.on { background: var(--gold); border-color: var(--gold); color: var(--gold-ink); }

.round-head { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3); }
.round-title {
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 700; font-size: var(--step-2);
}
.round-count { font-size: var(--step--1); font-variant-numeric: tabular-nums; }
.round-tools { margin-left: auto; }
.round-next { display: grid; gap: var(--space-2); justify-items: start; }
.round-next:empty { display: none; }
.waiting {
  border: 1px solid var(--teal); border-radius: var(--radius-control);
  padding: var(--space-2) var(--space-3); color: var(--teal); font-weight: 700;
}

.tables { display: grid; gap: var(--space-3); }

/* The table strip: two seats facing each other, results underneath. */
.table-card {
  border: 1px solid var(--line);
  border-radius: var(--radius-panel);
  background: var(--bg);
  overflow: hidden;
}
.table-card.reported { border-color: var(--surface-2); }
.table-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: var(--space-2) var(--space-4);
  background: var(--surface-2);
  font-size: var(--step--1);
}
.table-no {
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 700; font-size: var(--step-0); color: var(--gold-text);
}
.table-status { color: var(--muted); font-weight: 700; }
.seats {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4);
}
.seat { display: grid; gap: 2px; min-width: 0; }
.seat-b { text-align: right; }
.seat-name {
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 700;
  font-size: var(--step-2);
  line-height: 1.1;
  overflow-wrap: anywhere;
}
.seat-rec { font-size: var(--step--1); color: var(--muted); font-variant-numeric: tabular-nums; }
.seat-flag { font-size: var(--step--1); font-weight: 900; }
.seat.won .seat-flag { color: var(--win); }
.seat.drew .seat-flag { color: var(--draw); }
.seat.lost .seat-name { color: var(--muted); }
.vs { font-family: var(--font-display); font-style: italic; color: var(--muted); font-size: var(--step-0); }

.results {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: var(--space-3);
  padding: 0 var(--space-4) var(--space-4);
}
.res-side { display: grid; gap: var(--space-1); min-width: 0; align-content: start; }
.res-b { justify-items: end; }
.res-mid { justify-items: center; }
.res-cap { font-size: 0.75rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.res-row { display: flex; flex-wrap: wrap; gap: var(--space-1); }
.res-b .res-row { justify-content: flex-end; }
.res {
  min-width: 52px; min-height: 40px;
  border-radius: var(--radius-control);
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 900;
  font-size: var(--step-0);
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.res:hover:not(:disabled) { border-color: var(--gold); }
.res.on { background: var(--gold); border-color: var(--gold); color: var(--gold-ink); }
.res-mid .res.on { background: var(--draw); border-color: var(--draw); color: var(--draw-ink); }
.res:disabled { cursor: default; opacity: 0.55; }
.res.on:disabled { opacity: 1; }

.bye-card {
  border: 1px dashed var(--line);
  border-radius: var(--radius-panel);
  padding: var(--space-3) var(--space-4);
}
.bye-card .seat-name { font-size: var(--step-1); }
.progress { font-variant-numeric: tabular-nums; }

/* ---------- Pairing editor ---------- */

.editor { display: grid; gap: var(--space-3); }
.editor > .btn-small { justify-self: start; }
.edit-intro {
  border: 1px solid var(--gold); border-radius: var(--radius-control);
  background: var(--gold-soft); padding: var(--space-3); font-size: var(--step--1);
}
.edit-table {
  display: grid;
  grid-template-columns: 5.5rem minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: var(--space-2);
}
.seat-btn {
  min-height: 44px;
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-control);
  border: 1px solid var(--line);
  background: var(--surface);
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 700;
  font-size: var(--step-1);
  text-align: left;
  cursor: pointer;
  overflow-wrap: anywhere;
}
.seat-btn.empty { border-style: dashed; color: var(--muted); font-weight: 400; font-style: italic; }
.seat-btn.selected { border-color: var(--gold); background: var(--gold-soft); box-shadow: 0 0 0 2px var(--gold); }
.edit-out { display: grid; gap: var(--space-2); padding-top: var(--space-3); border-top: 1px solid var(--line); }
.chip-row { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.edit-actions {
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-control);
  background: var(--gold-soft);
}

/* ---------- Standings ---------- */

.standings-head { display: flex; align-items: center; gap: var(--space-3); }
.table-wrap { overflow-x: auto; margin-inline: calc(-1 * var(--space-2)); }
.stand { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.stand th {
  font-size: 0.72rem; font-weight: 900; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted);
  text-align: left; padding: var(--space-2) 6px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
.stand td { padding: var(--space-2) 6px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.stand .num { text-align: right; }
.stand .player { font-weight: 700; white-space: normal; min-width: 6rem; }
.stand .rank, .stand .pts {
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-size: var(--step-1); font-weight: 700;
}
.stand .rank { color: var(--muted); }
.stand tr.first .rank { color: var(--gold-text); }
.stand tr.dropped td { color: var(--muted); }
.stand .w { color: var(--win); }
.stand .l { color: var(--loss); }
.stand .d { color: var(--draw); }
.tie { display: block; font-size: 0.72rem; font-weight: 400; color: var(--muted); }
.legend { font-size: var(--step--1); color: var(--muted); max-width: 70ch; }

/* ---------- Final placings ---------- */

.final { display: grid; gap: var(--space-3); }
.podium {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: var(--space-3);
}
.place {
  display: grid; gap: 2px;
  padding: var(--space-4);
  border-radius: var(--radius-panel);
  border: 1px solid var(--line);
  background: var(--surface);
}
.place-1 { border-color: var(--gold); background: var(--gold-soft); box-shadow: var(--shadow); }
.place-rank { font-weight: 900; font-size: var(--step--1); letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.place-1 .place-rank { color: var(--gold-text); }
.place-name {
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 800; font-size: var(--step-3); line-height: 1.05; overflow-wrap: anywhere;
}
.place-meta { font-size: var(--step--1); color: var(--muted); font-variant-numeric: tabular-nums; }

.save-form { border-color: var(--gold); }

/* ---------- History ---------- */

.history { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-2); }
.history li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: var(--space-2); align-items: stretch; }
.history-row {
  width: 100%;
  display: grid;
  grid-template-columns: 8rem minmax(0, 1fr) auto;
  grid-template-areas: "date title win" "date meta win";
  column-gap: var(--space-4);
  align-items: center;
  text-align: left;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-control);
  border: 1px solid var(--line);
  background: var(--bg);
  cursor: pointer;
}
.history-row:hover { border-color: var(--gold); }
.history-del { display: flex; flex-direction: column; gap: var(--space-1); justify-content: center; }
.h-date { grid-area: date; color: var(--muted); font-variant-numeric: tabular-nums; }
.h-title {
  grid-area: title;
  font-family: var(--font-display); font-variation-settings: var(--display-axes);
  font-weight: 700; font-size: var(--step-1);
}
.h-meta { grid-area: meta; color: var(--muted); font-size: var(--step--1); }
.h-win { grid-area: win; font-weight: 700; color: var(--gold-text); }
.back { justify-self: start; }
.history-rounds { margin: 0; padding-left: var(--space-5); display: grid; gap: var(--space-3); }
.round-list { list-style: none; padding: 0; margin: var(--space-1) 0 0; display: grid; gap: 2px; font-variant-numeric: tabular-nums; }

/* ---------- Footer ---------- */

.foot { display: flex; flex-wrap: wrap; gap: var(--space-3); justify-content: space-between; align-items: center; color: var(--muted); font-size: var(--step--1); border-top: 1px solid var(--line); padding-top: var(--space-4); }
.foot p { max-width: 70ch; }
.foot-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }

/* ---------- Narrow screens ---------- */

@media (max-width: 900px) {
  .board { grid-template-columns: minmax(0, 1fr); }
  .board > .standings { position: static; }
  .setup-grid { grid-template-columns: minmax(0, 1fr); }
  .field-players textarea { min-height: 12rem; }
}

@media (max-width: 560px) {
  .panel { padding: var(--space-4); }
  .brand-mark { width: 40px; height: 40px; }
  .seat-name { font-size: var(--step-1); }
  .results { grid-template-columns: minmax(0, 1fr); gap: var(--space-2); }
  .res-side { grid-template-columns: 7.5rem minmax(0, 1fr); align-items: center; justify-items: start; }
  .res-b .res-row { justify-content: flex-start; }
  .res-b, .res-mid { justify-items: start; }
  .res { min-width: 48px; }
  .edit-table { grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); }
  .edit-table .table-no { grid-column: 1 / -1; }
  .history li { grid-template-columns: minmax(0, 1fr); }
  .history-del { flex-direction: row; }
  .history-row { grid-template-columns: minmax(0, 1fr); grid-template-areas: "title" "date" "meta" "win"; row-gap: 2px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
