:root {
  --blue: #1a56db;
  --blue-light: #e8f0fe;
  --green: #057a55;
  --green-light: #def7ec;
  --red: #c81e1e;
  --red-light: #fde8e8;
  --yellow: #c27803;
  --yellow-light: #fdf6b2;
  --gray: #6b7280;
  --gray-light: #f3f4f6;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.12);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.12);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #f9fafb;
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

/* Layout */
.header {
  background: var(--blue);
  color: white;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow-md);
}
.header h1 { font-size: 18px; font-weight: 700; }
.header .subtitle { font-size: 12px; opacity: 0.8; }

.main { max-width: 1200px; margin: 0 auto; padding: 24px 16px; }

.steps { display: flex; gap: 8px; margin-bottom: 24px; flex-wrap: wrap; }
.step-badge {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 20px;
  font-size: 12px; font-weight: 600;
  background: var(--gray-light); color: var(--gray);
  border: 2px solid transparent;
  cursor: default;
}
.step-badge.active { background: var(--blue-light); color: var(--blue); border-color: var(--blue); }
.step-badge.done { background: var(--green-light); color: var(--green); border-color: var(--green); }
.step-badge .num {
  width: 20px; height: 20px; border-radius: 50%;
  background: currentColor; color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px;
}
.step-badge.active .num, .step-badge.done .num { color: white; }

.card {
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card-title {
  font-size: 15px; font-weight: 700;
  margin-bottom: 16px;
  display: flex; align-items: center; gap: 8px;
}

/* Forms */
.form-group { margin-bottom: 14px; }
label { display: block; font-weight: 600; margin-bottom: 4px; font-size: 13px; }
input, select, textarea {
  width: 100%; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px; font-family: inherit;
  background: white;
}
input:focus, select:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(26,86,219,0.15); }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: 6px; border: none;
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.btn:hover { opacity: 0.88; }
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--blue); color: white; }
.btn-success { background: var(--green); color: white; }
.btn-danger { background: var(--red); color: white; }
.btn-outline { background: white; color: var(--text); border: 1px solid var(--border); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Upload zone */
.upload-zone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 40px; text-align: center; cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}
.upload-zone:hover, .upload-zone.drag { border-color: var(--blue); background: var(--blue-light); }
.upload-zone input[type=file] { display: none; }
.upload-zone .icon { font-size: 32px; margin-bottom: 8px; }
.upload-zone p { color: var(--text-muted); font-size: 13px; }

/* Teams table */
.teams-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.teams-table th, .teams-table td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; }
.teams-table th { background: var(--gray-light); font-weight: 700; }
.teams-table tr:hover td { background: #f9fafb; }
.div-badge {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 11px; font-weight: 700;
}
.div-B12 { background: #dbeafe; color: #1e40af; }
.div-G12 { background: #fce7f3; color: #9d174d; }
.div-B34 { background: #d1fae5; color: #065f46; }
.div-G34 { background: #fef3c7; color: #92400e; }
.div-B56 { background: #ede9fe; color: #4c1d95; }
.div-G56 { background: #fee2e2; color: #7f1d1d; }

/* Warnings */
.warning-box {
  background: var(--yellow-light); border: 1px solid #f6ad55;
  border-radius: var(--radius); padding: 12px 16px; margin-bottom: 12px;
}
.warning-box .warn-title { font-weight: 700; color: var(--yellow); margin-bottom: 6px; }
.warn-item { font-size: 12px; color: #78350f; margin-bottom: 2px; }

/* Conflict pairs */
.conflict-pair {
  display: flex; align-items: center; gap: 8px;
  padding: 8px; background: var(--gray-light); border-radius: 6px;
  margin-bottom: 6px;
}
.conflict-pair select { flex: 1; }
.conflict-pair .vs { font-size: 11px; font-weight: 700; color: var(--gray); }

/* G56 blocks */
.g56-block {
  display: flex; align-items: center; gap: 8px;
  padding: 6px; background: var(--gray-light); border-radius: 6px;
  margin-bottom: 6px;
}
.g56-block select { flex: 1; }

/* Progress */
.progress-wrap { margin: 12px 0; }
.progress-bar-bg { background: var(--gray-light); border-radius: 10px; height: 12px; overflow: hidden; }
.progress-bar { background: var(--blue); height: 100%; border-radius: 10px; transition: width 0.3s; }
.progress-label { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* Results comparison */
.results-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; margin-bottom: 20px; }
.result-card {
  border: 2px solid var(--border); border-radius: var(--radius); padding: 14px;
  cursor: pointer; transition: border-color 0.15s, box-shadow 0.15s;
}
.result-card:hover { border-color: var(--blue); box-shadow: var(--shadow-md); }
.result-card.selected { border-color: var(--blue); background: var(--blue-light); }
.result-card .rank { font-size: 11px; font-weight: 700; color: var(--gray); }
.result-card .score { font-size: 22px; font-weight: 800; color: var(--blue); }
.result-card .score-label { font-size: 11px; color: var(--text-muted); }
.metric-row { display: flex; justify-content: space-between; font-size: 12px; padding: 3px 0; border-bottom: 1px solid var(--border); }
.metric-row:last-child { border-bottom: none; }
.metric-val { font-weight: 600; }

/* Schedule grid */
.schedule-grid-wrap { overflow-x: auto; }
.schedule-grid { border-collapse: collapse; width: 100%; font-size: 12px; min-width: 800px; }
.schedule-grid th, .schedule-grid td {
  border: 1px solid var(--border); padding: 6px 8px;
  vertical-align: top; min-width: 110px;
}
.schedule-grid th { background: var(--gray-light); font-weight: 700; text-align: center; }
.schedule-grid .field-header { background: #e0e7ff; font-size: 11px; font-weight: 700; }
.schedule-grid .slot-label { background: var(--gray-light); font-size: 11px; color: var(--text-muted); white-space: nowrap; }

.game-cell {
  border-radius: 4px; padding: 4px 6px; margin-bottom: 3px; font-size: 11px;
}
.game-cell .teams { font-weight: 700; }
.game-cell .meta { color: var(--text-muted); font-size: 10px; }
.game-cell.pref-ok { background: var(--green-light); }
.game-cell.pref-violated { background: var(--red-light); border-left: 3px solid var(--red); }
.game-cell.external { background: #f3f4f6; color: var(--gray); font-style: italic; }
.game-cell.b2b { border-left: 3px solid var(--yellow); }

/* Degrade log */
.degrade-section { background: var(--yellow-light); border: 1px solid #f6ad55; border-radius: var(--radius); padding: 14px; margin-bottom: 16px; }
.degrade-section h3 { color: var(--yellow); margin-bottom: 8px; }
.degrade-step { font-size: 12px; margin-bottom: 4px; color: #78350f; }

/* Tabs */
.tab-bar { display: flex; gap: 2px; border-bottom: 2px solid var(--border); margin-bottom: 20px; }
.tab-btn {
  padding: 8px 16px; border: none; background: none;
  font-size: 14px; font-weight: 600; color: var(--text-muted);
  cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -2px;
}
.tab-btn.active { color: var(--blue); border-bottom-color: var(--blue); }

/* Sections */
.section { display: none; }
.section.active { display: block; }

/* Misc */
.row { display: flex; gap: 12px; align-items: flex-start; flex-wrap: wrap; }
.col { flex: 1; min-width: 200px; }
.text-muted { color: var(--text-muted); }
.text-small { font-size: 12px; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 700; }
.badge-green { background: var(--green-light); color: var(--green); }
.badge-red { background: var(--red-light); color: var(--red); }
.badge-blue { background: var(--blue-light); color: var(--blue); }
.badge-yellow { background: var(--yellow-light); color: var(--yellow); }

/* View switcher (Field / Team Stats) */
.view-switcher {
  display: flex; gap: 2px; margin-bottom: 16px;
  background: var(--gray-light); border-radius: 8px; padding: 3px; width: fit-content;
}
.view-btn {
  padding: 6px 14px; border: none; border-radius: 6px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  background: transparent; color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}
.view-btn.active { background: white; color: var(--text); box-shadow: var(--shadow); }

/* Team stats table */
.team-stats-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.team-stats-table th, .team-stats-table td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: center; }
.team-stats-table th { background: var(--gray-light); font-weight: 700; }
.team-stats-table td:first-child { text-align: left; font-weight: 600; }
.team-stats-table tr:hover td { background: #f9fafb; }
.slot-count { display: inline-block; min-width: 18px; text-align: center; }
.slot-count.hi { color: var(--red); font-weight: 800; } /* 3+ games in same slot */
.slot-count.zero { color: #d1d5db; }
.ha-split { font-weight: 700; }
.ha-ok { color: var(--green); }
.ha-warn { color: var(--yellow); }
.ha-bad { color: var(--red); }
.viol-count { color: var(--red); font-weight: 700; }
.viol-none { color: var(--text-muted); }
.demand-full { color: var(--green); font-weight: 700; }
.demand-mid  { color: var(--yellow); font-weight: 700; }
.demand-low  { color: var(--red); font-weight: 700; }

/* Failure state */
.fail-state { padding: 28px 24px; text-align: center; }
.fail-state h3 { font-size: 1.1rem; font-weight: 700; margin: 8px 0; }
.fail-state p  { font-size: 0.875rem; color: var(--text-muted); max-width: 520px; margin: 8px auto; }
.fail-icon { font-size: 2.5rem; margin-bottom: 4px; }
.fail-hint { font-style: italic; }
.fail-steps-wrap { text-align: left; background: var(--gray-light); border-radius: 8px; padding: 14px 16px; margin: 14px auto; max-width: 560px; font-size: 0.8rem; }
.fail-step { margin-bottom: 8px; line-height: 1.5; }
.fail-step:last-child { margin-bottom: 0; }
.fail-detail { color: var(--text-muted); font-size: 0.75rem; }

/* Weight sliders */
.weight-row {
  display: grid;
  grid-template-columns: 200px 1fr 52px 48px;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.weight-row:last-child { border-bottom: none; }
.weight-label { font-size: 13px; font-weight: 600; }
.weight-sublabel { font-size: 11px; color: var(--text-muted); font-weight: 400; }
input[type=range] {
  width: 100%; height: 6px; cursor: pointer;
  accent-color: var(--blue);
}
.weight-val {
  text-align: right; font-size: 13px; font-weight: 700;
  color: var(--blue); min-width: 40px;
}
.weight-default { font-size: 11px; color: var(--text-muted); text-align: center; }

/* Scoring guide table */
.teams-table td:last-child { font-size: 12px; color: var(--text-muted); line-height: 1.5; }

.btn-secondary { background: #6b7280; color: white; }

/* Diagnostic panel */
.diag-panel {
  position: fixed;
  top: 20px;
  right: 20px;
  width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  background: white;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.15);
  z-index: 100;
  padding: 20px;
}
.diag-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.diag-header h3 { font-size: 1rem; font-weight: 700; margin: 0; }
.diag-close { cursor: pointer; font-size: 1.1rem; color: var(--text-muted); padding: 4px 8px; border-radius: 4px; }
.diag-close:hover { background: var(--gray-light); }
.diag-summary { display: flex; gap: 20px; margin-bottom: 20px; padding: 12px; background: var(--gray-light); border-radius: 8px; }
.diag-stat { display: flex; gap: 10px; align-items: center; }
.diag-num { font-size: 1.8rem; font-weight: 800; }
.diag-lbl { font-size: 0.75rem; color: var(--text-muted); line-height: 1.4; }
.diag-section { margin-bottom: 16px; }
.diag-section h4 { font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); margin-bottom: 10px; }
.diag-section h4 small { text-transform: none; font-weight: 400; letter-spacing: 0; }
.diag-reason-row, .diag-pair-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 0.82rem; }
.diag-reason-label, .diag-pair-names { flex: 1; min-width: 0; }
.diag-bar-wrap { width: 200px; flex-shrink: 0; background: var(--gray-light); border-radius: 3px; height: 8px; }
.diag-bar { height: 8px; background: var(--blue); border-radius: 3px; transition: width 0.3s; }
.diag-bar-red { background: var(--red); }
.diag-reason-count { font-weight: 700; font-size: 0.8rem; text-align: right; min-width: 60px; }
.diag-pct { font-weight: 400; color: var(--text-muted); }
.diag-field-label { font-size: 0.78rem; font-weight: 600; color: var(--text-muted); margin: 8px 0 4px; }
.diag-heat-wrap { overflow-x: auto; margin-bottom: 8px; }
.diag-heat { border-collapse: collapse; font-size: 0.78rem; width: 100%; }
.diag-heat th, .diag-heat td { border: 1px solid var(--border); padding: 4px 8px; text-align: center; }
.diag-heat th { background: var(--gray-light); font-weight: 600; }
.heat-low  { background: #fef9c3; }
.heat-mid  { background: #fde68a; }
.heat-high { background: #fca5a5; font-weight: 700; }
.diag-loading, .diag-error { text-align: center; padding: 24px; color: var(--text-muted); }
.diag-pairs { max-height: 200px; overflow-y: auto; }
.vs { color: var(--text-muted); }

.hidden { display: none !important; }
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.4); border-top-color: white; border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state { text-align: center; color: var(--text-muted); padding: 32px; }
.empty-state .icon { font-size: 36px; margin-bottom: 8px; }

@media (max-width: 640px) {
  .main { padding: 12px 8px; }
  .results-grid { grid-template-columns: 1fr 1fr; }
}
