/* Mobile-first layout: phone browsers are the primary target; wider screens get extra room. */
:root {
  --bg: #f5f6f8;
  --card: #fff;
  --text: #1f2933;
  --muted: #6b7785;
  --border: #dde2e8;
  --primary: #2563eb;
  --danger: #dc2626;
  --ok: #16a34a;
  --a: #2563eb;
  --b: #ea580c;
  --tap: 44px;
  --tabbar: 56px;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- top bar & navigation ---------- */

header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
header .brand { font-weight: 700; color: var(--text); text-decoration: none; white-space: nowrap; flex: 1; overflow: hidden; text-overflow: ellipsis; }
.save-status { font-size: 13px; color: var(--muted); white-space: nowrap; max-width: 45vw; overflow: hidden; text-overflow: ellipsis; }
.save-status.saved { color: var(--ok); }
.save-status.error { color: var(--danger); }
header button.link { white-space: nowrap; }

/* Phones: navigation is a bottom tab bar within thumb reach. */
header nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 5;
}
header nav a {
  flex: 1;
  height: var(--tabbar);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  text-decoration: none;
  font-size: 15px;
  white-space: nowrap;
}
header nav a.active { color: var(--primary); font-weight: 600; box-shadow: inset 0 3px 0 var(--primary); }

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 12px;
  padding-bottom: calc(var(--tabbar) + 16px + env(safe-area-inset-bottom));
}
main.no-tabbar { padding-bottom: 16px; }

h1 { font-size: 20px; margin: 0 0 12px; }
h2 { font-size: 17px; margin: 0 0 10px; }
h3 { font-size: 15px; margin: 16px 0 8px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); font-size: 13px; }
.error-text { color: var(--danger); }
.back { display: inline-flex; align-items: center; min-height: var(--tap); color: var(--primary); text-decoration: none; margin: -6px 0 4px; }

/* ---------- controls ---------- */

input, select, textarea, button { font: inherit; font-size: 16px; } /* 16px avoids iOS focus zoom */
input[type=text], input[type=password], input[type=number], select, textarea {
  min-height: var(--tap);
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  min-width: 0;
}
textarea { width: 100%; min-height: 120px; }
input:disabled, select:disabled { background: #f0f2f5; }

button {
  min-height: var(--tap);
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}
button:active { background: #eef1f5; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:active { filter: brightness(.92); }
button.danger { color: var(--danger); border-color: #f3b4b4; }
button.link { border: none; background: none; color: var(--muted); padding: 8px; }
button.small { min-height: 36px; padding: 4px 10px; font-size: 14px; }
.lang-select { min-height: 36px; padding: 4px 6px; }

.grow { flex: 1 1 160px; }
.full { width: 100%; }

/* ---------- tables ---------- */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { text-align: left; padding: 10px 4px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: normal; vertical-align: bottom; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.name { white-space: normal; overflow-wrap: anywhere; min-width: 5.5em; width: 40%; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 13px; background: #e5e7eb; white-space: nowrap; }
.badge.in_progress { background: #dbeafe; color: #1d4ed8; }
.badge.completed { background: #dcfce7; color: #15803d; }

/* ---------- home ---------- */

.create-form { display: flex; gap: 8px; }
.create-form input { flex: 1; }
.home-actions { margin-top: 10px; }
.home-actions button { width: 100%; }

.match-list a.match-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  padding: 12px 2px;
  border-bottom: 1px solid var(--border);
  color: inherit;
  text-decoration: none;
}
.match-list a.match-item:last-child { border-bottom: none; }
.match-list a.match-item:active { background: #f3f5f8; }
.match-item .name { font-weight: 600; word-break: break-word; }
.match-item .teams { color: var(--muted); font-size: 14px; word-break: break-word; }
.match-item .time { grid-column: 1 / -1; color: var(--muted); font-size: 13px; }
.match-item .score { font-weight: 700; font-size: 18px; font-variant-numeric: tabular-nums; text-align: right; }

/* ---------- match detail ---------- */

.match-name { font-size: 18px; font-weight: 600; flex: 1 1 200px; word-break: break-word; }
.match-actions { display: grid; grid-template-columns: 1fr; gap: 8px; margin-top: 10px; }
.match-actions:empty { display: none; }
.match-actions button { min-height: 48px; }

/* Scoreboard: both sides always side by side, even on narrow phones. */
.scoreboard { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.score-col { text-align: center; border-radius: 10px; padding: 10px 6px; background: #f8fafc; border-top: 4px solid var(--a); min-width: 0; }
.score-col.B { border-top-color: var(--b); }
.score-col.winner { box-shadow: 0 0 0 2px var(--ok); }
.score-col .side-name { font-weight: 700; }
.score-col .team { color: var(--muted); font-size: 14px; min-height: 20px; word-break: break-word; }
.score-value { display: block; font-size: 56px; font-weight: 700; line-height: 1.1; margin: 6px 0; font-variant-numeric: tabular-nums; }
.score-col input.score-input {
  width: 100%;
  font-size: 44px;
  font-weight: 700;
  text-align: center;
  padding: 2px;
  margin: 6px 0;
  min-height: 64px;
}
.score-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.score-buttons button { min-height: 60px; font-size: 30px; font-weight: 600; padding: 0; border-radius: 10px; }
.score-buttons button.plus { background: var(--primary); border-color: var(--primary); color: #fff; }
.score-col.B .score-buttons button.plus { background: var(--b); border-color: var(--b); }
.score-hint { text-align: center; margin-top: 8px; }

.shared-total { text-align: center; }
.shared-total b { font-size: 24px; }

.sides { display: grid; grid-template-columns: 1fr; gap: 0 12px; }
.side { border-top: 4px solid var(--a); }
.side.B { border-top-color: var(--b); }

/* Segmented tabs: switch between panels in one place instead of stacking them. */
.seg-tabs {
  position: sticky;
  top: calc(var(--header-h, 60px) + 6px);
  z-index: 2;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  margin-bottom: 8px;
  background: #e8ebf0;
  border-radius: 12px;
}
.seg-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  min-height: 52px;
  padding: 6px 4px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
}
.seg-tab-label { font-weight: 600; font-size: 16px; }
.seg-tab-meta { font-size: 12px; }
.seg-tab[aria-selected="true"] { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(15, 23, 42, .15); }
.seg-tab[aria-selected="true"] .seg-tab-label { color: var(--primary); }
.seg-tab.A[aria-selected="true"] .seg-tab-label { color: var(--a); }
.seg-tab.B[aria-selected="true"] .seg-tab-label { color: var(--b); }
.tab-panel:not(.active) { display: none; }
.side-title { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 4px 4px 4px 12px;
  border-radius: 999px;
  background: #eef2f7;
  margin: 0 6px 6px 0;
  min-height: 36px;
}
.chip button { min-height: 32px; min-width: 32px; border: none; background: none; padding: 0; color: var(--muted); }
.participant-select { width: 100%; }

.assist-list { list-style: none; margin: 0; padding: 0; }
.assist-item {
  display: grid;
  grid-template-columns: 1fr 96px 52px auto;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.assist-item.readonly { grid-template-columns: 1fr 64px 64px 64px; }
.settled { font-weight: 700; font-variant-numeric: tabular-nums; }
.settled.pos { color: var(--ok); }
.settled.neg { color: var(--danger); }
.assist-item .name { word-break: break-word; }
.assist-item input { width: 100%; text-align: right; }
.assist-item .eff { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.assist-item .label { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
.assist-item .val { font-variant-numeric: tabular-nums; text-align: right; }
.assist-head { font-size: 12px; color: var(--muted); padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.assist-head .eff { font-weight: 600; }

.add-assist { display: grid; grid-template-columns: 1fr 96px; gap: 8px; margin-top: 10px; }
.add-assist button { grid-column: 1 / -1; }
.totals { margin-top: 10px; font-size: 14px; display: flex; flex-wrap: wrap; gap: 4px 16px; }
.totals b { font-variant-numeric: tabular-nums; }

/* ---------- history ---------- */

.history-list { list-style: none; padding: 0; margin: 0; }
.history-list li { padding: 10px 0; border-bottom: 1px solid var(--border); word-break: break-word; }
.history-list li:last-child { border-bottom: none; }
.history-list .meta { display: block; font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.history-list .type { color: var(--primary); margin-left: 8px; }
.history-list details summary { min-height: 32px; display: flex; align-items: center; cursor: pointer; }
.history-list pre { background: #f6f8fa; padding: 8px; border-radius: 6px; font-size: 12px; overflow-x: auto; white-space: pre-wrap; margin: 4px 0 0; }

/* ---------- login ---------- */

.login { max-width: 380px; margin: 8vh auto; text-align: center; }
.login input { width: 100%; margin: 14px 0; }
.login button[type=submit] { width: 100%; min-height: 48px; }

/* ---------- members sheet ---------- */

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .45);
  display: flex; align-items: stretch; justify-content: center; z-index: 10;
}
.modal {
  background: var(--card);
  width: 100%;
  max-width: 560px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom));
}
.modal-header {
  position: sticky; top: 0; background: var(--card); z-index: 1;
  padding: calc(10px + env(safe-area-inset-top)) 0 10px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; }
.member-row { display: flex; gap: 8px; margin-bottom: 8px; }
.member-row input { flex: 1; }
.add-member { display: flex; gap: 8px; }
.add-member input { flex: 1; }

/* ---------- wider screens ---------- */

@media (min-width: 761px) {
  body { font-size: 15px; }
  header { padding: 10px 16px; gap: 16px; }
  header .brand { flex: 0 0 auto; }
  header nav {
    position: static;
    flex: 1;
    gap: 16px;
    border: none;
    padding: 0;
    background: none;
  }
  header nav a { flex: 0 0 auto; height: auto; padding: 4px 2px; }
  header nav a.active { box-shadow: inset 0 -2px 0 var(--primary); }
  main { padding: 16px; padding-bottom: 24px; }
  .card { padding: 16px; margin-bottom: 16px; }
  .home-actions { margin-top: 0; }
  .home-actions button { width: auto; }
  .home-top { display: flex; gap: 12px; align-items: center; }
  .home-top .create-form { flex: 1; max-width: 520px; }
  .match-actions { display: flex; justify-content: flex-end; }
  .scoreboard { gap: 16px; }
  .score-value { font-size: 64px; }
  .sides { grid-template-columns: 1fr 1fr; }
  /* Wide screens show both match sides at once; the leaderboard keeps its tabs. */
  .side-tabs { display: none; }
  .sides .tab-panel { display: block; }
  .side-title { display: block; }
  .modal-backdrop { align-items: flex-start; padding: 6vh 16px; overflow-y: auto; }
  .modal { border-radius: 12px; max-height: 88vh; }
  .modal-header { padding-top: 16px; }
}
