/* ---------- Färger och mått ---------- */
:root {
  color-scheme: light;
  --bg: #f2f3f7;
  --surface: #ffffff;
  --surface-2: #f7f8fb;
  --text: #172033;
  --muted: #637087;
  --line: #b4bccb;
  --border: #d4d9e3;
  --divider: #e6e9f0;
  --accent: #c7951f;
  --accent-strong: #8c650c;
  --accent-soft: #fbf3dc;
  --header-bg: #14213d;
  --header-text: #f5f7fb;
  --header-muted: #b7c1d8;
  --primary: #1d3b72;
  --primary-hover: #16305e;
  --primary-text: #ffffff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --ok: #157f3b;
  --focus: #3b82f6;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --radius: 10px;
  --col-w: 236px;
  --gap: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0d1220;
    --surface: #161d2d;
    --surface-2: #1b2335;
    --text: #e6e9f0;
    --muted: #98a2b5;
    --line: #48546d;
    --border: #2d3750;
    --divider: #252e43;
    --accent: #e5b84f;
    --accent-strong: #f0c96a;
    --accent-soft: #2d2818;
    --header-bg: #090e19;
    --primary: #3d6cc0;
    --primary-hover: #4a7ad0;
    --danger: #f07b70;
    --danger-soft: #3a1d1d;
    --ok: #5cc985;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%);
  }
}

/* ---------- Grund ---------- */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

a { color: var(--primary); }
@media (prefers-color-scheme: dark) { a { color: #8fb3ff; } }

h1, h2, h3 { line-height: 1.2; margin: 0; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- Sidhuvud ---------- */
.site-header {
  background: var(--header-bg);
  color: var(--header-text);
  border-bottom: 3px solid var(--accent);
}

.header-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-right: auto;
  min-width: 0;
}

.brand-mark { font-size: 1.5rem; }

.header-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.header-tools select,
.header-tools .btn {
  background: rgb(255 255 255 / 8%);
  color: var(--header-text);
  border-color: rgb(255 255 255 / 22%);
}
.header-tools select option { color: #172033; background: #fff; }
.header-tools .btn:hover { background: rgb(255 255 255 / 16%); }
.header-tools form { margin: 0; }

/* ---------- Sidinnehåll ---------- */
.page {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px 16px 40px;
}

.intro {
  color: var(--muted);
  margin: 4px 0 16px;
  max-width: 70ch;
}

.status { margin: 12px 0; color: var(--muted); }
.status:empty { display: none; }

.site-footer {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 16px 32px;
  color: var(--muted);
  font-size: 0.85rem;
}
.site-footer a { color: inherit; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}

/* ---------- Spelträd ---------- */
.bracket-section h2 {
  font-size: 1.15rem;
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 8px;
}

.bracket-section h2 .sub {
  font-size: 0.85rem;
  font-weight: 400;
  color: var(--muted);
}

.bracket-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -16px;
  padding: 0 16px;
}

.bracket {
  display: flex;
  align-items: stretch;
  width: max-content;
  padding: 4px 0 8px;
}

.round {
  display: flex;
  flex-direction: column;
  width: var(--col-w);
  flex: none;
}

.round-title {
  text-align: center;
  font-size: 0.72rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  padding: 0 var(--gap) 6px;
}

.round-body {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.slot {
  flex: 1;
  display: flex;
  align-items: center;
  position: relative;
  padding: 13px var(--gap);
}

/* Linjerna mellan matcherna */
.slot::before,
.slot::after {
  content: "";
  position: absolute;
  border: 0 solid var(--line);
  pointer-events: none;
}

.round:not(:first-child) .slot::before {
  left: 0;
  top: 50%;
  width: var(--gap);
  border-top-width: 2px;
}

.round.is-feeding .slot:nth-child(odd)::after {
  right: 0;
  top: 50%;
  width: var(--gap);
  height: 50%;
  border-top-width: 2px;
  border-right-width: 2px;
}

.round.is-feeding .slot:nth-child(even)::after {
  right: 0;
  top: 0;
  width: var(--gap);
  height: calc(50% + 2px);
  border-bottom-width: 2px;
  border-right-width: 2px;
}

.round.is-final .slot::after {
  right: 0;
  top: 50%;
  width: var(--gap);
  border-top-width: 2px;
}

.match-wrap {
  position: relative;
  width: 100%;
}

.match {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 0;
  margin: 0;
  background: var(--surface);
  color: inherit;
  font: inherit;
  text-align: left;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.side {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 29px;
  padding: 4px 4px 4px 9px;
}

.side + .side { border-top: 1px solid var(--divider); }

.side .name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.side .hcp {
  flex: none;
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.side .score {
  flex: none;
  min-width: 26px;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.side .score:empty { min-width: 4px; }

.side.is-winner { background: var(--accent-soft); font-weight: 600; }
.side.is-winner .score { color: var(--accent-strong); }
.side.is-loser .name { color: var(--muted); }
.side.kind-bye .name { font-style: italic; color: var(--muted); }
.side.kind-pending .name { color: var(--muted); }

.match-wrap .sets {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  padding-top: 1px;
  font-size: 0.72rem;
  color: var(--muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}

.match.state-bye { box-shadow: none; background: var(--surface-2); }

.match.is-clickable { cursor: pointer; transition: border-color .15s, box-shadow .15s; }
.match.is-clickable:hover { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.match.is-clickable.state-ready { border-color: var(--accent); border-left-width: 4px; }

.champion {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  border: 2px solid var(--accent);
  border-radius: 10px;
  background: var(--accent-soft);
  font-weight: 700;
}
.champion .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.champion .hcp { font-size: 0.8rem; font-weight: 500; color: var(--muted); }
.champion.is-empty { border-style: dashed; border-color: var(--border); background: transparent; color: var(--muted); font-weight: 400; }
.trophy { font-size: 1.2rem; }

.third-place {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 8px 0 20px;
}
.third-place h3 { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.third-place .match-wrap { width: calc(var(--col-w) - 2 * var(--gap)); }

/* ---------- Mobilvy: en flik per omgång (visas bara på smala skärmar, se längst ned) ---------- */
.round-view { display: none; }

/* Val av visning på den publika sidan. På datorn visas alltid spelträdet, så valet syns bara på smala skärmar. */
.view-switch {
  display: none;
  flex-direction: column;
  gap: 4px;
  margin: 0 0 12px;
}
.view-switch-label { font-size: 0.8rem; color: var(--muted); }
.view-switch .round-tabs { margin: 0; }
/* Lite mindre än omgångsflikarna, så att det ser ut som en inställning och inte som en flik till. */
.view-switch .round-tab { min-height: 32px; padding: 4px 7px; font-size: 0.82rem; }

.round-tabs {
  display: flex;
  gap: 2px;
  padding: 4px;
  margin: 4px 0 14px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.round-tabs::-webkit-scrollbar { display: none; }

.round-tab {
  flex: 1 0 auto;
  min-height: 38px;
  padding: 6px 7px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 650;
  white-space: nowrap;
  cursor: pointer;
}
.round-tab:hover { color: var(--text); }
.round-tab[aria-pressed="true"] { background: var(--primary); color: var(--primary-text); }

.round-panel-title,
.round-extra h3 {
  font-size: 0.72rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  margin-bottom: 8px;
}

.round-list { display: flex; flex-direction: column; gap: 14px; }
.round-extra { margin-top: 22px; }

.round-view .side { min-height: 36px; padding-left: 11px; }
.round-view .side .name { white-space: normal; overflow-wrap: anywhere; }
.round-view .match-wrap .sets {
  position: static;
  padding: 3px 2px 0;
  text-align: left;
  white-space: normal;
}

/* ---------- Poolspel ---------- */
.pools-section h2,
.playoff-placeholder h2 {
  font-size: 1.15rem;
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 10px;
  margin-bottom: 6px;
}
.pools-section h2 .sub { font-size: 0.85rem; font-weight: 400; color: var(--muted); }

.pool-legend { margin: 0 0 14px; color: var(--muted); font-size: 0.85rem; }
.pool-legend .legend-a {
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--accent-soft);
  box-shadow: inset 3px 0 var(--accent);
  color: var(--text);
  font-weight: 600;
}

.pool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 380px), 1fr));
  gap: 16px;
  align-items: start;
}

.pool {
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}

.pool-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.pool-head h3 { font-size: 1rem; }
.pool-head .sub { margin-left: auto; font-size: 0.8rem; color: var(--muted); }

.pool-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}
.pool-table th {
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
  font-size: 0.72rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  text-align: center;
}
.pool-table th abbr { text-decoration: none; cursor: help; }
.pool-table td { padding: 6px 4px; border-top: 1px solid var(--divider); text-align: center; vertical-align: middle; }
.pool-table tbody tr:first-child td { border-top: 0; }
.pool-table .c-place { width: 2.2em; font-weight: 700; color: var(--muted); }
.pool-table .c-name { text-align: left; padding-left: 6px; }
.pool-table .c-num { width: 2.1em; }
.pool-table .c-set { width: 3.3em; white-space: nowrap; }
/* Bredd för t.ex. "(−13)–44" när en spelare med minushandikapp har förlorat set på minus. */
.pool-table .c-pts { width: 5.4em; white-space: nowrap; }
.pool-table .c-empty { text-align: left; color: var(--muted); font-style: italic; padding: 10px; }
.pool-table.is-started tr.to-a td { background: var(--accent-soft); }
.pool-table.is-started tr.to-a td:first-child { box-shadow: inset 3px 0 var(--accent); color: var(--accent-strong); }
.pool-table tr.is-tied .c-place { color: var(--danger); }

.pl { display: inline-flex; align-items: baseline; flex-wrap: wrap; gap: 0 6px; min-width: 0; }
.pl-name { overflow-wrap: anywhere; }
.pl-name.is-empty { color: var(--muted); font-style: italic; }
.pl .hcp { flex: none; font-size: 0.75rem; color: var(--muted); font-weight: 400; }

.pool-note { margin: 8px 0 0; font-size: 0.85rem; color: var(--danger); font-weight: 550; }

.pool-matches { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }

.pool-match {
  display: grid;
  grid-template-columns: 1.3em minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 1px 8px;
  width: 100%;
  margin: 0;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: inherit;
  font: inherit;
  font-size: 0.9rem;
  text-align: left;
}
.pool-match .pm-no { font-size: 0.72rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.pool-match .pm-top { text-align: right; }
.pool-match .pm-top .pl { justify-content: flex-end; }
.pool-match .pm-score {
  min-width: 2.8em;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--surface-2);
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.pool-match.state-done .pm-score { background: var(--accent-soft); color: var(--accent-strong); }
.pool-match .pm-side.is-winner { font-weight: 650; }
.pool-match .pm-side.is-loser .pl-name { color: var(--muted); }
.pool-match .pm-sets {
  grid-column: 2 / -1;
  text-align: center;
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.pool-match.is-clickable { cursor: pointer; transition: border-color .15s, box-shadow .15s; }
.pool-match.is-clickable:hover { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.pool-match.is-clickable.state-ready { border-color: var(--accent); border-left-width: 4px; }

.playoff-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 10px 16px; }
.playoff-bar p { flex: 1 1 320px; margin: 0; }
.playoff-bar .actions { margin-top: 0; }
.playoff-placeholder p { margin: 0; color: var(--muted); }

.tie-group + .tie-group { margin-top: 14px; }
.tie-group h3 { font-size: 0.9rem; margin-bottom: 6px; }
.tie-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.tie-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
}
.tie-list .pos { width: 1.6em; font-weight: 700; color: var(--muted); }
.tie-list .tie-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }

/* ---------- Knappar och formulär ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { min-height: 30px; padding: 4px 10px; font-size: 0.85rem; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
  min-height: 36px;
  min-width: 0;
}
textarea { width: 100%; resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .8; }
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

label { font-weight: 550; }

.field { display: flex; flex-direction: column; gap: 4px; }
.field .hint, .hint { color: var(--muted); font-size: 0.85rem; font-weight: 400; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.check input { min-height: 0; width: 18px; height: 18px; }

.error { color: var(--danger); font-weight: 550; }
.error:empty { display: none; }

/* ---------- Inloggning ---------- */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-card h1 { font-size: 1.4rem; }
.login-card .brand-mark { margin-right: 6px; }

/* ---------- Admin ---------- */
.admin-panel > summary,
.panel-title {
  font-size: 1.1rem;
  font-weight: 700;
}
.admin-panel > summary { cursor: pointer; list-style-position: inside; }
.admin-panel[open] > summary { margin-bottom: 12px; }
.admin-panel > summary .sub { font-size: 0.85rem; font-weight: 400; color: var(--muted); margin-left: 6px; }

.toolbar {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 10px 12px;
  margin-bottom: 12px;
}
.toolbar .spacer { flex: 1; }

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
  gap: 12px 16px;
  align-items: end;
  margin-bottom: 12px;
}

.draw-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 330px), 1fr));
  gap: 10px;
}

.draw-match {
  margin: 0;
  padding: 6px 10px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  min-width: 0;
}
.draw-match legend {
  padding: 0 4px;
  font-size: 0.75rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.draw-row {
  display: grid;
  grid-template-columns: 1.6rem minmax(0, 1fr) minmax(0, 1fr) 4rem;
  gap: 6px;
  align-items: center;
}
.draw-row.no-hcp { grid-template-columns: 1.6rem minmax(0, 1fr) minmax(0, 1fr); }
.draw-row + .draw-row { margin-top: 6px; }
.draw-row input { width: 100%; padding: 6px 8px; }
.draw-num { font-size: 0.8rem; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.draw-row.is-bye { grid-template-columns: 1.6rem 1fr; min-height: 36px; }
.draw-row.is-bye .bye-text { color: var(--muted); font-style: italic; font-size: 0.9rem; }

.actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
}
.actions .spacer { flex: 1; }

.empty-state { text-align: center; padding: 40px 16px; }
.empty-state p { color: var(--muted); }

/* ---------- Dialoger ---------- */
dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 0;
  border: none;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgb(0 0 0 / 30%);
}
dialog::backdrop { background: rgb(9 14 25 / 55%); }
dialog form { padding: 20px; display: flex; flex-direction: column; gap: 14px; margin: 0; }
dialog h2 { font-size: 1.15rem; }

.score-grid {
  display: grid;
  grid-template-rows: auto auto auto;
  grid-auto-flow: column;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-columns: 3.6rem;
  gap: 8px;
  align-items: center;
}
.score-grid .set-head { font-size: 0.75rem; color: var(--muted); text-align: center; font-weight: 600; }
.score-grid .player-cell { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.score-grid .player-cell .hcp { color: var(--muted); font-weight: 400; font-size: 0.85rem; margin-left: 4px; }
.score-grid input { width: 100%; text-align: center; font-size: 1.1rem; font-weight: 600; padding: 8px 2px; }

.summary { min-height: 1.4em; margin: 0; font-weight: 600; color: var(--ok); }
.summary.is-neutral { color: var(--muted); font-weight: 400; }

.wo-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 0.9rem; color: var(--muted); }

/* ---------- Meddelanden ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--header-bg);
  color: var(--header-text);
  box-shadow: 0 8px 24px rgb(0 0 0 / 25%);
  z-index: 10;
}
.toast.is-error { background: var(--danger); color: #fff; }

@media (max-width: 600px) {
  :root { --col-w: 222px; --gap: 12px; }
  .brand { font-size: 1.15rem; }

  .pool { padding: 10px; }
  .pool-table { font-size: 0.85rem; }
  .pool-table .c-place { width: 1.9em; }
  .pool-table .c-num { width: 1.8em; }
  .pool-match { gap: 1px 6px; padding: 6px; font-size: 0.85rem; }

  /* Besökarens eget val på den publika sidan (spelträd eller en flik per omgång). */
  .view-switch { display: flex; }

  /* Inställningen "En flik per omgång": flikarna ersätter trädet. */
  .bracket-section.has-round-view .bracket-scroll,
  .bracket-section.has-round-view > .third-place { display: none; }
  .bracket-section.has-round-view .round-view { display: block; }
}
