:root{
  --bg:#0f1115;
  --card:#151924;
  --muted:#9aa4b2;
  --text:#e8ecf3;
  --border:#252b3a;
  --primary:#5b8cff;
  --danger:#ff5b6e;
  --sidebar:#0c0f18;
  --sidebarW: 288px;
  --topbarH: 56px;
}

*{ box-sizing:border-box; }
body{
  margin:0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background:var(--bg);
  color:var(--text);
}

/* Layout */
.layout{ display:flex; min-height:100vh; }

/* Sidebar */
.sidebar{
  width: var(--sidebarW);
  background: var(--sidebar);
  border-right:1px solid var(--border);
  padding:14px 12px;
  position:fixed;
  top:0;
  left:0;
  bottom:0;
  z-index:50;
  display:flex;
  flex-direction:column;
  gap:14px;
}
.sidebar-head{
  display:flex;
  justify-content:space-between;
  align-items:flex-start;
  gap:10px;
}
.brand-title{ font-weight:800; font-size:16px; }
.brand-subtitle{ color:var(--muted); font-size:12px; margin-top:4px; }

.sidebar-section{
  border:1px solid rgba(37,43,58,0.7);
  border-radius:14px;
  padding:12px;
  background: rgba(21,25,36,0.35);
}
.sidebar-label{ color:var(--muted); font-size:12px; margin-bottom:8px; }

.nav{
  display:flex;
  flex-direction:column;
  gap:8px;
}
.nav-item{
  border:1px solid var(--border);
  background:#0f1320;
  color:var(--text);
  padding:10px 10px;
  border-radius:12px;
  cursor:pointer;
  text-align:left;
  font-size:13px;
}
.nav-item[aria-current="step"]{
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(91,140,255,0.15) inset;
}
.nav-item:disabled{
  opacity:0.55;
  cursor:not-allowed;
}

.sidebar-actions{
  display:flex;
  flex-direction:column;
  gap:8px;
}

.sidebar-footer{
  margin-top:auto;
  padding:0 4px 8px;
}

.sidebar-overlay{
  position:fixed;
  inset:0;
  background: rgba(0,0,0,0.55);
  z-index:40;
}

/* Content */
.content{
  flex:1;
  margin-left: var(--sidebarW);
  min-width:0;
  display:flex;
  flex-direction:column;
  min-height:100vh;
}
.topbar{
  height: var(--topbarH);
  display:flex;
  align-items:center;
  gap:10px;
  padding:10px 14px;
  border-bottom:1px solid var(--border);
  background: rgba(15,17,21,0.92);
  backdrop-filter: blur(6px);
  position:sticky;
  top:0;
  z-index:10;
}
.topbar-title{ font-weight:800; font-size:14px; }
.topbar-subtitle{ color:var(--muted); font-size:12px; margin-top:2px; }

.container{
  padding:14px 18px 24px;
  max-width: 1800px;
  margin: 0 auto;
  width:100%;
}

/* Buttons & Inputs */
.icon-btn{
  border:1px solid var(--border);
  background:#0f1320;
  color:var(--text);
  width:40px;
  height:40px;
  border-radius:12px;
  cursor:pointer;
  font-size:16px;
  display:grid;
  place-items:center;
}
.icon-btn:hover{ border-color:#35405a; }

h2{ margin:0; font-size:16px; }
h3{ margin:10px 0 8px; font-size:14px; }

.row{
  display:flex;
  align-items:center;
  gap:8px;
  margin:10px 0;
  flex-wrap:wrap;
}

.label{ color:var(--muted); font-size:13px; }
.hint{ color:var(--muted); font-size:12px; }
.muted{ color:var(--muted); }

.input, .select, .textarea{
  width:100%;
  background:#0f1320;
  color:var(--text);
  border:1px solid var(--border);
  border-radius:10px;
  padding:9px 10px;
  outline:none;
}
.select{ width:auto; min-width: 200px; }
.textarea{ resize:vertical; }

.btn{
  border:1px solid var(--border);
  background:#0f1320;
  color:var(--text);
  padding:9px 10px;
  border-radius:10px;
  cursor:pointer;
  font-size:13px;
  white-space:nowrap;
}
.btn:hover{ border-color:#35405a; }
.btn:disabled{ opacity:0.55; cursor:not-allowed; }
.btn-primary{
  background:rgba(91,140,255,0.18);
  border-color: rgba(91,140,255,0.45);
}
.btn-danger{
  background:rgba(255,91,110,0.14);
  border-color: rgba(255,91,110,0.45);
}
.btn-order{
  background:rgba(255,255,255,0.04);
  border-color:rgba(255,255,255,0.12);
  padding:5px 8px;
  font-size:12px;
  line-height:1;
}
.item-order-btns{
  display:flex;
  flex-direction:column;
  gap:2px;
}

/* Cards & grids */
.phase{ margin-top:14px; }

.grid-3{
  display:grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap:12px;
}
.grid-3-scoring{
  display:grid;
  grid-template-columns: minmax(560px, 1.25fr) minmax(360px, 0.9fr) minmax(360px, 0.9fr);
  gap:12px;
  align-items:stretch;
}

@media (max-width: 1200px){
  .grid-3-scoring{
    grid-template-columns: 1fr;
  }
}

@media (max-width: 980px){
  .grid-3{ grid-template-columns: 1fr; }
}

/* Cards */
.card{
  background:var(--card);
  border:1px solid var(--border);
  border-radius:14px;
  padding:14px;
  min-width:0;
}
.card-head{
  display:flex;
  flex-direction:column;
  gap:6px;
  margin-bottom:10px;
}

.card-tall{
  display:flex;
  flex-direction:column;
  min-height: calc(100vh - var(--topbarH) - 70px);
}

/* Lists */
.list{
  list-style:none;
  padding:0;
  margin:10px 0 0;
  display:flex;
  flex-direction:column;
  gap:8px;
}
.item{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  padding:8px;
  border:1px solid var(--border);
  border-radius:12px;
  background:#0f1320;
}
.item-left{
  display:flex;
  align-items:center;
  gap:8px;
  flex:1;
}
.item input{
  background:transparent;
  border:1px solid transparent;
  color:var(--text);
  padding:6px 8px;
  border-radius:10px;
  width:100%;
}
.item input:focus{
  outline:none;
  border-color: #35405a;
  background:#0b1020;
}

/* Tables */
.table-wrap{ overflow:auto; border-radius:12px; border:1px solid var(--border); }
.table-wrap-tall{ flex:1; min-height: 0; }
.table{
  width:100%;
  border-collapse: collapse;
  min-width: 520px;
}
.table-compact{ min-width: 520px; }
.table th, .table td{
  border-bottom: 1px solid var(--border);
  padding:10px;
  text-align:left;
  vertical-align:middle;
}
.table th{
  color:var(--muted);
  font-weight:600;
  font-size:12px;
  background:#101524;
  position:sticky;
  top:0;
}
.table td input[type="number"]{
  width: 120px;
}

.footer-actions{
  display:flex;
  justify-content:flex-end;
  gap:8px;
  margin-top:12px;
}

.details summary{
  cursor:pointer;
  color: var(--muted);
  margin-top:8px;
}

.warning{
  border:1px solid rgba(255, 185, 90, 0.35);
  background: rgba(255, 185, 90, 0.12);
  padding:10px;
  border-radius:12px;
  margin:10px 0;
  color:#ffd9a1;
  font-size:13px;
}

.chips{
  display:flex;
  gap:8px;
  flex-wrap:wrap;
  margin-top:10px;
}
.chip{
  border:1px solid var(--border);
  background:#0f1320;
  padding:6px 8px;
  border-radius:999px;
  color:var(--muted);
  font-size:12px;
}

/* Scoreboard */
.score-card{
  border:1px solid var(--border);
  border-radius:14px;
  padding:12px;
  margin-bottom:10px;
  background:#0f1320;
}
.score-top{
  display:flex;
  justify-content:space-between;
  gap:10px;
  align-items:baseline;
}
.score-name{ font-weight:700; }
.score-total{ font-weight:800; font-size:18px; }
.small{
  color:var(--muted);
  font-size:12px;
  margin-top:6px;
}
.kv{
  display:flex;
  justify-content:space-between;
  gap:10px;
  padding:6px 0;
  border-top: 1px dashed rgba(37,43,58,0.9);
}
.kv:first-child{ border-top:none; }

.scroll-area{
  overflow:auto;
  flex:1;
  min-height:0;
}

/* Premade events */
.premade-list{ display:flex; flex-direction:column; gap:8px; margin-top:10px; }
.premade-row{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  padding:10px 12px;
  background:var(--bg);
  border:1px solid var(--border);
  border-radius:10px;
}
.premade-info{ flex:1; min-width:0; }
.premade-name{ font-weight:600; margin-bottom:2px; }

/* Leaderboard */
.leaderboard .rowline{
  display:flex;
  justify-content:space-between;
  gap:10px;
  padding:8px 10px;
  border:1px solid var(--border);
  border-radius:12px;
  margin-bottom:8px;
  background:#0f1320;
}
.leaderboard .rowline .name{ font-weight:600; }
.leaderboard .rowline .pts{ color:var(--muted); }

/* Footer */
.app-footer{
  padding:12px 18px;
  border-top:1px solid var(--border);
  margin-top:auto;
}

/* Sidebar toggle behavior */
body.sidebar-hidden .sidebar{
  transform: translateX(-102%);
}
body.sidebar-hidden .content{
  margin-left: 0;
}

/* Mobile behavior */
@media (max-width: 980px){
  .sidebar{
    transform: translateX(-102%);
  }
  body.sidebar-open .sidebar{
    transform: translateX(0);
  }
  .content{
    margin-left: 0;
  }
  body.sidebar-open #sidebarOverlay{
    display:block !important;
  }
}

/* ============================================================
   Chart Modal
   ============================================================ */

.chart-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.chart-modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 100%;
  max-width: 720px;
  max-height: 85vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 64px rgba(0,0,0,0.55);
}

.chart-modal-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}
.chart-modal-head h2 {
  flex: 1;
  min-width: 80px;
}

.toggle-group {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  flex-shrink: 0;
}
.toggle-btn {
  background: #0f1320;
  color: var(--muted);
  border: none;
  padding: 7px 14px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  transition: background 0.15s, color 0.15s;
}
.toggle-btn + .toggle-btn {
  border-left: 1px solid var(--border);
}
.toggle-btn.active {
  background: rgba(91,140,255,0.22);
  color: var(--text);
}
.toggle-btn:hover:not(.active) {
  background: #131826;
}

.chart-modal-body {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  flex: 1;
}

.chart-area {
  width: 100%;
  min-height: 80px;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 4px 0;
}
.chart-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text);
}
.chart-legend-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
}

.chart-athlete-filter {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  background: #0f1320;
}
.chart-filter-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
}
.chart-filter-team {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 10px 0 2px;
}
.chart-filter-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  padding: 3px 0;
  color: var(--text);
}
.chart-filter-label input[type="checkbox"] {
  accent-color: var(--primary);
  width: 14px;
  height: 14px;
  cursor: pointer;
}
.chart-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

.scoring-footer-actions {
  display: flex;
  justify-content: flex-end;
  padding: 12px 0 4px;
}

@media (max-width: 600px) {
  .chart-overlay {
    padding: 0;
    align-items: flex-end;
  }
  .chart-modal {
    max-width: 100%;
    max-height: 92vh;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }
}
