/* ============ CIS Basketball Scoring App ============ */
:root {
  --navy: #0d1b2a;
  --navy2: #16283c;
  --navy3: #1f3550;
  --orange: #e8590c;
  --orange2: #ff7a2e;
  --ink: #eef3f8;
  --muted: #8fa3b8;
  --line: #2c4258;
  --good: #2f9e44;
  --warn: #f59f00;
  --bad: #e03131;
  --paper: #fdfcf7;
  --paper-ink: #1c1c1c;
  --paper-line: #9a9a9a;
  --p1: #1971c2;
  --p2: #e03131;
  --p3: #2f9e44;
  --p4: #9c36b5;
  --pE: #e8590c;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--navy);
  color: var(--ink);
  font-size: 15px;
  padding-bottom: 74px;
  min-height: 100vh;
}
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 15px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  background: linear-gradient(180deg, var(--navy2), var(--navy));
  border-bottom: 2px solid var(--orange);
  padding: 10px 14px;
  display: flex; align-items: center; gap: 10px;
}
.topbar .logo {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--orange);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 11px; color: #fff; flex-shrink: 0;
  border: 2px solid #00000033;
}
.topbar h1 { font-size: 15px; margin: 0; font-weight: 700; letter-spacing: .3px; }
.topbar .sub { font-size: 11px; color: var(--muted); margin: 0; }
.topbar .net {
  margin-left: auto; font-size: 11px; font-weight: 700;
  padding: 4px 9px; border-radius: 20px; flex-shrink: 0;
}
.net.online { background: #2f9e4433; color: #69db7c; }
.net.offline { background: #e0313133; color: #ffa8a8; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  display: flex;
  background: var(--navy2);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  flex: 1; background: none; border: none; color: var(--muted);
  padding: 9px 2px 8px; font-size: 10.5px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tabbar button .ico { font-size: 19px; line-height: 1; }
.tabbar button.active { color: var(--orange2); }
.tabbar button:disabled { opacity: .35; }

/* ---------- Layout ---------- */
.view { display: none; padding: 14px; max-width: 760px; margin: 0 auto; }
.view.active { display: block; }
h2.section { font-size: 13px; text-transform: uppercase; letter-spacing: 1.2px; color: var(--orange2); margin: 20px 0 8px; }
h2.section:first-child { margin-top: 4px; }
.card {
  background: var(--navy2); border: 1px solid var(--line);
  border-radius: 12px; padding: 14px; margin-bottom: 12px;
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.field label { display: block; font-size: 11px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: .6px; margin-bottom: 4px; }
.field input, .field select {
  width: 100%; background: var(--navy3); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px;
}
.field input:focus { outline: 2px solid var(--orange); border-color: transparent; }

.btn {
  border: none; border-radius: 10px; padding: 11px 16px;
  font-weight: 700; font-size: 14px; color: #fff; background: var(--navy3);
}
.btn.primary { background: var(--orange); }
.btn.green { background: var(--good); }
.btn.red { background: var(--bad); }
.btn.ghost { background: transparent; border: 1px solid var(--line); color: var(--muted); }
.btn.block { width: 100%; }
.btn.small { padding: 7px 10px; font-size: 12.5px; border-radius: 8px; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.row { display: flex; gap: 10px; align-items: center; }
.row .grow { flex: 1; }

/* ---------- Games list ---------- */
.game-item {
  background: var(--navy2); border: 1px solid var(--line); border-radius: 12px;
  padding: 13px 14px; margin-bottom: 10px; display: block; width: 100%;
  text-align: left; color: var(--ink);
}
.game-item .gi-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.game-item .gi-teams { font-weight: 800; font-size: 15px; }
.game-item .gi-score { font-weight: 800; color: var(--orange2); font-size: 16px; white-space: nowrap; }
.game-item .gi-meta { color: var(--muted); font-size: 12px; margin-top: 4px; display: flex; gap: 8px; flex-wrap: wrap; }
.badge { font-size: 10px; font-weight: 800; text-transform: uppercase; padding: 2px 7px; border-radius: 10px; letter-spacing: .5px; }
.badge.live { background: #e0313133; color: #ffa8a8; }
.badge.final { background: #2f9e4433; color: #69db7c; }
.badge.setup { background: #f59f0033; color: #ffd43b; }
.badge.uploaded { background: #1971c233; color: #74c0fc; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; font-size: 14px; }

/* ---------- Tournaments ---------- */
.tourn-item {
  background: var(--navy2); border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 14px; margin-bottom: 8px; display: flex; align-items: center; gap: 10px;
}
.tourn-item .ti-name { font-weight: 800; font-size: 14px; }
.tourn-item .ti-meta { color: var(--muted); font-size: 11.5px; margin-top: 2px; }
.tourn-item .grow { flex: 1; }
.tourn-item .del-t { background: none; border: none; color: var(--bad); font-size: 17px; padding: 4px 6px; }
.sched-item {
  display: block; width: 100%; text-align: left; background: var(--navy3);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px;
  margin-bottom: 6px; color: var(--ink); font-size: 13px;
}
.sched-item .sm { color: var(--muted); font-size: 11.5px; }
.sched-item.used { opacity: .55; }
.field select {
  width: 100%; background: var(--navy3); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; font-size: 15px;
}

/* ---------- Roster editor ---------- */
.roster-table { width: 100%; border-collapse: collapse; }
.roster-table th { font-size: 10px; color: var(--muted); text-transform: uppercase; text-align: left; padding: 4px 4px; letter-spacing: .5px; }
.roster-table td { padding: 3px 3px; }
.roster-table input {
  width: 100%; background: var(--navy3); color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px; padding: 7px 7px; font-size: 14px;
}
.roster-table .col-num { width: 52px; }
.roster-table .col-lic { width: 76px; }
.roster-table .col-del { width: 34px; }
.roster-table .del-p { background: none; border: none; color: var(--bad); font-size: 16px; padding: 4px; }

.team-tabs { display: flex; gap: 8px; margin-bottom: 12px; }
.team-tabs button {
  flex: 1; padding: 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--navy2); color: var(--muted); font-weight: 800; font-size: 14px;
}
.team-tabs button.active { background: var(--orange); color: #fff; border-color: var(--orange); }

/* ---------- Live scoring ---------- */
.scoreboard {
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: center;
  background: var(--navy2); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px 10px; margin-bottom: 12px; text-align: center;
}
.scoreboard .tname { font-size: 12px; font-weight: 700; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scoreboard .tscore { font-size: 44px; font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }
.scoreboard .mid { font-size: 12px; color: var(--muted); font-weight: 700; }
.scoreboard .period-label { color: var(--orange2); font-size: 15px; font-weight: 900; }
.sb-fouls { font-size: 11px; margin-top: 5px; color: var(--muted); }
.sb-fouls b { color: var(--ink); }
.bonus { color: var(--bad); font-weight: 800; }

.period-ctrl { display: flex; gap: 6px; margin-bottom: 12px; }
.period-ctrl button {
  flex: 1; padding: 9px 2px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--navy2); color: var(--muted); font-weight: 800; font-size: 13px;
}
.period-ctrl button.active { background: var(--orange); border-color: var(--orange); color: #fff; }
.period-ctrl button.done { color: var(--ink); background: var(--navy3); }

.score-panes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.score-pane { background: var(--navy2); border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.score-pane h3 { margin: 0 0 8px; font-size: 13px; text-align: center; color: var(--orange2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; justify-content: center; }
.pchip {
  min-width: 44px; padding: 9px 4px; border-radius: 9px; font-weight: 800; font-size: 15px;
  border: 1px solid var(--line); background: var(--navy3); color: var(--ink); position: relative;
}
.pchip.sel { background: var(--orange); border-color: var(--orange2); }
.pchip.fouled-out { opacity: .35; text-decoration: line-through; }
.pchip .pf { position: absolute; top: -6px; right: -5px; background: var(--bad); color: #fff; font-size: 9px; min-width: 15px; height: 15px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-weight: 800; }
.act-btns { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.act-btns .btn { padding: 13px 2px; font-size: 16px; }
.act-btns2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
.act-btns2 .btn { padding: 10px 2px; font-size: 12.5px; }
.pane-hint { text-align: center; color: var(--muted); font-size: 11px; margin-top: 8px; min-height: 14px; }

.undo-bar { margin-top: 12px; }
.log-list { margin-top: 10px; max-height: 220px; overflow-y: auto; font-size: 12.5px; }
.log-item { padding: 6px 8px; border-bottom: 1px solid var(--line); color: var(--muted); display: flex; gap: 8px; }
.log-item b { color: var(--ink); }
.log-item .lp { color: var(--orange2); font-weight: 700; min-width: 26px; }

/* ---------- Paper scoresheet rendering ---------- */
.sheet-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.sheet {
  background: var(--paper); color: var(--paper-ink);
  border-radius: 8px; padding: 14px; min-width: 660px;
  font-family: Georgia, 'Times New Roman', serif; font-size: 12px;
  box-shadow: 0 4px 18px #00000066;
}
.sheet * { box-sizing: border-box; }
.sheet .sh-head { text-align: center; border-bottom: 3px double #333; padding-bottom: 8px; margin-bottom: 8px; }
.sheet .sh-head .assoc { font-weight: 700; font-size: 15px; letter-spacing: 1px; }
.sheet .sh-head .title { font-weight: 700; font-size: 13px; letter-spacing: 2px; }
.sheet .kv-line { display: flex; flex-wrap: wrap; gap: 4px 18px; padding: 3px 0; }
.sheet .kv { white-space: nowrap; }
.sheet .kv b { font-weight: 700; }
.sheet .kv u { text-decoration: none; border-bottom: 1px solid #444; padding: 0 6px; min-width: 60px; display: inline-block; }
.sheet table { border-collapse: collapse; width: 100%; }
.sheet .ptable th, .sheet .ptable td { border: 1px solid var(--paper-line); padding: 2.5px 4px; font-size: 11px; }
.sheet .ptable th { background: #ececec; font-weight: 700; }
.sheet .ptable td.c { text-align: center; }
.sheet .foul-cell { width: 17px; text-align: center; font-weight: 700; }
.sheet .foul-mark { color: #b02020; }
.sheet .teamhdr { background: #333; color: #fff; font-weight: 700; padding: 4px 8px; font-size: 12.5px; margin-top: 10px; display: flex; justify-content: space-between; }
.sheet .subrow { display: flex; gap: 16px; flex-wrap: wrap; padding: 4px 0; align-items: center; }
.sheet .tbox { display: inline-block; width: 15px; height: 15px; border: 1.5px solid #333; text-align: center; line-height: 14px; font-size: 10px; font-weight: 700; vertical-align: middle; margin: 0 1px; }
.sheet .tbox.marked { background: #333; color: #fff; }
.sheet .tf-num { display: inline-block; width: 16px; height: 16px; border: 1px solid #333; text-align: center; line-height: 15px; font-size: 10px; font-weight: 700; margin: 0 1px; position: relative; }
.sheet .tf-num.marked::after { content: '✗'; position: absolute; inset: 0; color: #b02020; font-size: 12px; line-height: 15px; }
.sheet .rs-title { text-align: center; font-weight: 700; letter-spacing: 2px; border-top: 2px solid #333; border-bottom: 1px solid #333; padding: 3px; margin-top: 10px; background: #ececec; }
.sheet .rs-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0; border: 1px solid #333; }
.sheet .rs-col { border-right: 1px solid #333; }
.sheet .rs-col:last-child { border-right: none; }
.sheet .rs-hdr { display: grid; grid-template-columns: 1fr 1fr; background: #ececec; border-bottom: 1px solid #333; font-weight: 700; text-align: center; font-size: 10px; }
.sheet .rs-row { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid #ddd; }
.sheet .rs-cell { text-align: center; font-size: 9.5px; padding: 1px 0; border-right: 1px dotted #ccc; position: relative; min-height: 14px; }
.sheet .rs-cell:last-child { border-right: none; }
.sheet .rs-cell .n { color: #555; }
.sheet .rs-cell.scored .n { text-decoration: line-through; font-weight: 700; color: #111; }
.sheet .rs-cell.scored { }
.sheet .rs-cell .who { position: absolute; right: 1px; top: -1px; font-size: 7.5px; font-weight: 700; }
.sheet .rs-cell.p1 { color: var(--p1); } .sheet .rs-cell.p1 .n, .sheet .rs-cell.p1 .who { color: var(--p1); }
.sheet .rs-cell.p2 { color: var(--p2); } .sheet .rs-cell.p2 .n, .sheet .rs-cell.p2 .who { color: var(--p2); }
.sheet .rs-cell.p3 { color: var(--p3); } .sheet .rs-cell.p3 .n, .sheet .rs-cell.p3 .who { color: var(--p3); }
.sheet .rs-cell.p4 { color: var(--p4); } .sheet .rs-cell.p4 .n, .sheet .rs-cell.p4 .who { color: var(--p4); }
.sheet .rs-cell.pE { color: var(--pE); } .sheet .rs-cell.pE .n, .sheet .rs-cell.pE .who { color: var(--pE); }
.sheet .rs-cell.period-end .n { outline: 1.5px solid currentColor; border-radius: 50%; padding: 0 2px; }
.sheet .sh-bottom { display: grid; grid-template-columns: 1.2fr 1fr; gap: 14px; margin-top: 10px; border-top: 2px solid #333; padding-top: 8px; }
.sheet .sig-line { border-bottom: 1px solid #444; min-height: 18px; display: inline-block; min-width: 130px; vertical-align: bottom; padding: 0 4px; }
.sheet .legend { margin-top: 8px; font-size: 9.5px; color: #555; display: flex; gap: 12px; flex-wrap: wrap; }
.sheet .legend i { font-style: normal; font-weight: 700; }
.sheet .legend .lp1 { color: var(--p1); } .sheet .legend .lp2 { color: var(--p2); }
.sheet .legend .lp3 { color: var(--p3); } .sheet .legend .lp4 { color: var(--p4); }
.sheet .legend .lpE { color: var(--pE); }

/* ---------- Finalize / signature ---------- */
.sigpad { background: #fff; border-radius: 10px; border: 1px dashed #999; width: 100%; height: 130px; touch-action: none; display: block; }
.sig-label { font-size: 11px; color: var(--muted); margin: 6px 0; }

/* ---------- Upload view ---------- */
.up-status { font-size: 13px; padding: 10px 12px; border-radius: 10px; margin-bottom: 10px; }
.up-status.ok { background: #2f9e4426; color: #8ce99a; }
.up-status.err { background: #e0313126; color: #ffa8a8; }
.up-status.info { background: #1971c226; color: #a5d8ff; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 84px; left: 50%; transform: translateX(-50%);
  background: #000c; color: #fff; padding: 10px 18px; border-radius: 24px;
  font-size: 13.5px; font-weight: 600; z-index: 100; opacity: 0; pointer-events: none;
  transition: opacity .25s; max-width: 88vw; text-align: center;
}
#toast.show { opacity: 1; }

/* ---------- Modal ---------- */
.modal-bg { position: fixed; inset: 0; background: #000a; z-index: 90; display: none; align-items: flex-end; justify-content: center; }
.modal-bg.show { display: flex; }
.modal {
  background: var(--navy2); border-radius: 16px 16px 0 0; width: 100%; max-width: 560px;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); border-top: 2px solid var(--orange);
  max-height: 82vh; overflow-y: auto;
}
.modal h3 { margin: 0 0 12px; font-size: 16px; }

@media (min-width: 700px) {
  .tabbar button { font-size: 12px; }
}
@media print {
  body { background: #fff; padding: 0; }
  .topbar, .tabbar, .no-print { display: none !important; }
  .view { display: none; }
  #view-sheet { display: block !important; padding: 0; max-width: none; }
  .sheet { box-shadow: none; border-radius: 0; min-width: 0; }
}
