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

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface2: #242424;
  --border: #2e2e2e;
  --accent: #6ee7b7;
  --accent-dim: #34d39918;
  --text: #f0f0f0;
  --muted: #666;
  --radius: 12px;
  --slot: 96px; /* base bracket slot height */
  --conn: 24px; /* connector width */
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
#app { height: 100%; display: flex; flex-direction: column; }
.view { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.hidden { display: none !important; }
.logo { font-size: 1.25rem; font-weight: 700; letter-spacing: -0.5px; }
.dot { color: var(--accent); }

/* ── Chat view ─────────────────────────────── */
.chat-header {
  display: flex; align-items: center; justify-content: center;
  padding: 1.25rem; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.messages {
  flex: 1; overflow-y: auto; padding: 1.5rem 1rem;
  display: flex; flex-direction: column; gap: 1rem;
  max-width: 680px; width: 100%; margin: 0 auto;
}
.bubble {
  max-width: 85%; padding: 0.75rem 1rem; border-radius: var(--radius);
  line-height: 1.55; font-size: 0.95rem; white-space: pre-wrap;
}
.bubble.user  { align-self: flex-end; background: var(--accent); color: #0f0f0f; font-weight: 500; }
.bubble.ai    { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); }
.bubble.thinking { color: var(--muted); font-style: italic; }

.chat-form {
  display: flex; gap: 0.5rem; padding: 1rem;
  border-top: 1px solid var(--border);
  max-width: 680px; width: 100%; margin: 0 auto; flex-shrink: 0;
}
.chat-form input {
  flex: 1; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0.75rem 1rem;
  color: var(--text); font-size: 0.95rem; outline: none; transition: border-color 0.15s;
}
.chat-form input:focus { border-color: var(--accent); }
.chat-form input::placeholder { color: var(--muted); }
.chat-form button {
  background: var(--accent); border: none; border-radius: var(--radius);
  width: 44px; height: 44px; display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: #0f0f0f; flex-shrink: 0; transition: opacity 0.15s;
}
.chat-form button:disabled { opacity: 0.35; cursor: default; }
.chat-form button:not(:disabled):hover { opacity: 0.85; }

/* ── Recent tournaments ─────────────────────── */
.recents {
  max-width: 680px; width: 100%; margin: 0 auto;
  padding: 0 1rem 1rem; flex-shrink: 0;
}
.recents-label {
  font-size: 0.72rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.6px; color: var(--muted); margin-bottom: 0.5rem;
}
.recents-list { display: flex; flex-direction: column; gap: 2px; }
.recent-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.55rem 0.75rem; border-radius: 8px; background: var(--surface);
  border: 1px solid var(--border); cursor: pointer; gap: 0.5rem;
  transition: border-color 0.15s; text-align: left;
}
.recent-item:hover { border-color: var(--accent); }
.recent-item-left { flex: 1; min-width: 0; }
.recent-title { font-size: 0.88rem; font-weight: 600; color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.recent-meta  { font-size: 0.75rem; color: var(--muted); margin-top: 1px; }
.recent-delete {
  flex-shrink: 0; background: none; border: none; color: var(--muted);
  cursor: pointer; padding: 2px 4px; border-radius: 4px; line-height: 1;
  font-size: 1rem; transition: color 0.15s;
}
.recent-delete:hover { color: #f87171; }

/* ── Bracket view ─────────────────────────── */
.bracket-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.5rem; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.back-btn, .share-btn {
  display: flex; align-items: center; gap: 0.4rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.45rem 0.85rem;
  color: var(--text); font-size: 0.85rem; cursor: pointer; transition: border-color 0.15s;
}
.back-btn:hover, .share-btn:hover { border-color: var(--accent); }

.bracket-title-row {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 1.1rem 1.5rem 0.5rem; flex-shrink: 0;
}
.bracket-title-row h1 { font-size: 1.4rem; font-weight: 700; }
.format-badge {
  background: var(--accent-dim); color: var(--accent);
  border: 1px solid var(--accent); border-radius: 20px;
  padding: 0.2rem 0.65rem; font-size: 0.72rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.5px;
}

/* ── Bracket grid ─────────────────────────── */
.bracket-scroll { flex: 1; overflow: auto; }
.bracket {
  display: flex; align-items: flex-start;
  padding: 1.5rem; gap: 0; min-width: max-content;
}

.round { display: flex; flex-direction: column; }
.round:not(.round-final) { margin-right: var(--conn); }

.round-label {
  font-size: 0.72rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.6px; color: var(--muted);
  margin-bottom: 0.5rem; padding-left: 2px;
  min-width: 180px;
}

.matches-col { display: flex; flex-direction: column; }

.match-wrapper {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  /* height set by JS: var(--slot) * 2^(round-1) */
}

/* Vertical bracket — odd child goes DOWN from center, even goes UP */
/* Vertical sits at match card right edge; conn-left bridges the gap to the next round */
.round:not(.round-final) .matches-col .match-wrapper:not(:only-child):nth-child(odd)::before {
  content: '';
  position: absolute;
  left: 100%;
  top: 50%;
  width: 2px;
  height: 50%;
  background: var(--border);
  pointer-events: none;
}
.round:not(.round-final) .matches-col .match-wrapper:not(:only-child):nth-child(even)::before {
  content: '';
  position: absolute;
  left: 100%;
  bottom: 50%;
  width: 2px;
  height: 50%;
  background: var(--border);
  pointer-events: none;
}

/* Left horizontal connector (for rounds > 1, added as .conn-left div) */
.conn-left {
  position: absolute;
  right: 100%;
  top: 50%;
  width: var(--conn);
  height: 2px;
  background: var(--border);
  transform: translateY(-50%);
  pointer-events: none;
}

/* ── Match card ───────────────────────────── */
.match {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-width: 180px;
}

.team-slot {
  display: flex; align-items: center;
  padding: 0 0.85rem; gap: 0.5rem;
  font-size: 0.88rem; height: 42px;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.team-slot:last-child { border-bottom: none; }
.team-slot.clickable { cursor: pointer; }
.team-slot.clickable:hover { background: var(--accent-dim); }
.team-slot.winner { background: var(--accent-dim); color: var(--accent); font-weight: 600; }
.team-slot.loser  { color: #444; text-decoration: line-through; }
.team-slot.bye    { color: var(--muted); font-style: italic; }
.team-slot.empty  { color: #333; font-style: italic; }

.seed      { font-size: 0.72rem; color: var(--muted); min-width: 16px; flex-shrink: 0; }
.team-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.win-icon  { font-size: 0.8rem; flex-shrink: 0; }

/* ── Revert button (hover overlay, doesn't affect layout) ── */
.revert-btn {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: #1a1a1aee;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.72rem;
  padding: 3px 10px;
  cursor: pointer;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, color 0.15s, border-color 0.15s;
  z-index: 5;
}
.match-wrapper:hover .revert-btn {
  opacity: 1;
  pointer-events: auto;
}
.revert-btn:hover { color: #f87171; border-color: #f87171; }

/* ── Winner banner ────────────────────────── */
.winner-banner {
  display: flex; align-items: center; justify-content: center; gap: 0.6rem;
  padding: 0.85rem; background: var(--accent-dim); border-top: 1px solid var(--accent);
  font-size: 1.05rem; font-weight: 700; color: var(--accent); flex-shrink: 0;
}

/* ── Bracket AI chat ─────────────────────── */
.ai-fab {
  position: fixed; bottom: 1.5rem; right: 1.5rem;
  background: var(--accent); color: #0f0f0f;
  border: none; border-radius: 50%; width: 50px; height: 50px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 1.2rem; font-weight: 700;
  box-shadow: 0 4px 20px #00000060;
  transition: transform 0.15s, opacity 0.15s;
  z-index: 50;
}
.ai-fab:hover { transform: scale(1.08); }

.ai-panel {
  position: fixed; bottom: 0; right: 0;
  width: min(420px, 100vw); height: min(520px, 80vh);
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  display: flex; flex-direction: column;
  box-shadow: 0 -8px 40px #00000080;
  z-index: 49;
  transform: translateY(100%);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.ai-panel.open { transform: translateY(0); }

.ai-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.85rem 1rem; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.ai-panel-title { font-size: 0.9rem; font-weight: 600; }
.ai-panel-close {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 1.1rem; line-height: 1; padding: 2px;
}
.ai-panel-close:hover { color: var(--text); }

.ai-panel-messages {
  flex: 1; overflow-y: auto; padding: 0.75rem 1rem;
  display: flex; flex-direction: column; gap: 0.6rem;
}
.ai-panel-messages .bubble { font-size: 0.88rem; max-width: 90%; }

.ai-panel-form {
  display: flex; gap: 0.4rem; padding: 0.75rem;
  border-top: 1px solid var(--border); flex-shrink: 0;
}
.ai-panel-form input {
  flex: 1; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.55rem 0.85rem;
  color: var(--text); font-size: 0.88rem; outline: none;
}
.ai-panel-form input:focus { border-color: var(--accent); }
.ai-panel-form input::placeholder { color: var(--muted); }
.ai-panel-form button {
  background: var(--accent); border: none; border-radius: 8px;
  width: 36px; height: 36px; display: flex; align-items: center;
  justify-content: center; cursor: pointer; color: #0f0f0f;
  flex-shrink: 0; transition: opacity 0.15s;
}
.ai-panel-form button:disabled { opacity: 0.35; cursor: default; }

/* ── Toast ────────────────────────────────── */
.toast {
  position: fixed; bottom: 5rem; left: 50%; transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); padding: 0.6rem 1.1rem; border-radius: var(--radius);
  font-size: 0.88rem; z-index: 100; pointer-events: none;
  opacity: 1; transition: opacity 0.3s;
}
.toast.hidden { opacity: 0; }

/* ── Round-robin ──────────────────────────── */
.rr-view { padding: 1.5rem; display: flex; flex-direction: column; gap: 2rem; min-width: max-content; }

.standings { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; min-width: 340px; }
.standings table { width: 100%; border-collapse: collapse; }
.standings th { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); padding: 0.6rem 1rem; text-align: left; border-bottom: 1px solid var(--border); }
.standings th.num { text-align: right; }
.standings td { padding: 0.6rem 1rem; font-size: 0.88rem; border-bottom: 1px solid var(--border); }
.standings tr:last-child td { border-bottom: none; }
.standings td.num { text-align: right; color: var(--muted); }
.standings td.pts { color: var(--accent); font-weight: 600; }
.standings td.rank { color: var(--muted); font-size: 0.75rem; }
.standings tr.leader td { background: var(--accent-dim); }

.rr-matches { display: flex; flex-direction: column; gap: 4px; min-width: 340px; }
.rr-match {
  display: flex; align-items: center; gap: 0.75rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
}
.rr-team {
  flex: 1; display: flex; align-items: center; gap: 0.5rem;
  padding: 0.6rem 0.85rem; font-size: 0.88rem; cursor: pointer;
  transition: background 0.1s;
}
.rr-team:not(.winner):not(.loser):not(.draw):hover { background: var(--accent-dim); }
.rr-team.winner { background: var(--accent-dim); color: var(--accent); font-weight: 600; }
.rr-team.loser { color: #444; text-decoration: line-through; }
.rr-team.right { flex-direction: row-reverse; text-align: right; }
.rr-divider { width: 2px; height: 36px; background: var(--border); flex-shrink: 0; }
.rr-mid { display: flex; flex-direction: column; align-items: center; gap: 2px; flex-shrink: 0; }
.rr-vs { font-size: 0.72rem; color: var(--muted); padding: 0 0.5rem; flex-shrink: 0; }
.rr-draw-btn {
  background: none; border: 1px solid var(--border); border-radius: 4px;
  color: var(--muted); cursor: pointer; font-size: 0.78rem; font-weight: 700;
  padding: 1px 6px; flex-shrink: 0; transition: background 0.1s, color 0.1s, border-color 0.1s;
  line-height: 1.4;
}
.rr-draw-btn:hover { background: var(--surface-2); color: var(--text); border-color: var(--accent); }
.rr-team.draw { color: var(--muted); background: none; font-weight: normal; text-decoration: none; }
.draw-icon { font-size: 0.78rem; color: var(--muted); flex-shrink: 0; }
.rr-revert { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 0.75rem; padding: 0.5rem; flex-shrink: 0; transition: color 0.15s; }
.rr-revert:hover { color: #f87171; }

/* ── Double-elimination ───────────────────── */
.de-view { display: flex; flex-direction: column; gap: 2rem; padding: 1.5rem; min-width: max-content; }
.de-section { display: flex; flex-direction: column; gap: 0.5rem; }
.de-section-label { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); padding-left: 2px; }
.de-gf { min-width: 0 !important; }

/* ── Scrollbars ───────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Group-stage ──────────────────────────── */
.gs-view { display: flex; flex-direction: column; gap: 2rem; padding: 1.5rem; overflow: auto; }
.gs-groups { display: flex; flex-wrap: wrap; gap: 1.5rem; min-width: max-content; }
.gs-group { display: flex; flex-direction: column; gap: 0.75rem; min-width: 300px; }
.gs-group-label { font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.8px; color: var(--accent); }
.gs-standings { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.gs-standings th { font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); padding: 0.5rem 0.75rem; text-align: left; border-bottom: 1px solid var(--border); }
.gs-standings th.num { text-align: right; }
.gs-standings td { padding: 0.5rem 0.75rem; font-size: 0.85rem; border-bottom: 1px solid var(--border); }
.gs-standings tr:last-child td { border-bottom: none; }
.gs-standings td.num { text-align: right; color: var(--muted); }
.gs-standings td.pts { color: var(--accent); font-weight: 600; }
.gs-standings td.rank { color: var(--muted); font-size: 0.75rem; }
.gs-standings tr.advancing td { background: var(--accent-dim); }
.gs-standings tr.advancing.leader td { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.gs-match-list { display: flex; flex-direction: column; gap: 4px; }
.gs-playoffs { display: flex; flex-direction: column; gap: 1rem; }
.gs-playoffs-label { font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.8px; color: var(--accent); }
.ghost-match { opacity: 0.35; pointer-events: none; }
.generate-playoffs-btn { align-self: flex-start; background: var(--accent); color: #0f0f0f; border: none; border-radius: 8px; padding: 0.6rem 1.25rem; font-size: 0.88rem; font-weight: 700; cursor: pointer; transition: opacity 0.15s; }
.generate-playoffs-btn:hover { opacity: 0.85; }
.generate-playoffs-btn:disabled { opacity: 0.4; cursor: not-allowed; filter: grayscale(0.4); }

/* ── Placement bracket (3rd place, etc.) ─────────── */
.placement-view {
  display: flex; flex-direction: column; gap: 0.75rem;
  padding: 0 1.5rem 1.5rem; border-top: 1px solid var(--border);
  margin-top: 0.5rem;
}
.placement-section-label {
  font-size: 0.72rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.8px; color: var(--muted); padding-top: 1.25rem;
}

/* Consolation label in group stage */
.consolation-label { color: var(--muted); }

/* ── Quick-start chips ────────────────────────── */
.quick-starts { display: flex; flex-wrap: wrap; gap: 0.5rem; padding: 0.25rem 0 0.75rem; }
.quick-start-btn {
  background: var(--surface2); border: 1px solid var(--border); border-radius: 20px;
  color: var(--text); font-size: 0.8rem; padding: 0.4rem 0.85rem; cursor: pointer;
  transition: border-color 0.15s, background 0.15s; white-space: nowrap;
}
.quick-start-btn:hover { border-color: var(--accent); background: var(--accent-dim); }

/* ── File attachment ──────────────────────────── */
.chat-form { position: relative; flex-wrap: wrap; }
.attach-btn {
  display: flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; flex-shrink: 0; cursor: pointer;
  color: var(--accent); background: var(--accent-dim); border: 1px solid var(--border);
  border-radius: var(--radius); transition: border-color 0.15s, background 0.15s;
}
.attach-btn:hover { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, transparent); }
.attach-preview {
  display: flex; align-items: flex-end; flex-wrap: wrap; gap: 0.5rem;
  width: 100%; order: -1; padding: 0.5rem 0.5rem 0; background: none; border: none;
}
.attach-item {
  display: flex; align-items: flex-end; gap: 0.35rem; position: relative;
}
.attach-thumb {
  max-height: 140px; max-width: 220px; border-radius: 10px;
  object-fit: cover; border: 1px solid var(--border); display: block;
}
.attach-file-icon { font-size: 1.5rem; flex-shrink: 0; }
.attach-file-name {
  font-size: 0.82rem; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px;
}
.attach-remove {
  background: var(--surface2); border: 1px solid var(--border); color: var(--muted);
  cursor: pointer; font-size: 0.72rem; border-radius: 50%;
  width: 22px; height: 22px; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; align-self: flex-start; margin-left: 0.25rem;
  transition: color 0.15s, background 0.15s;
}
.attach-remove:hover { color: #f87171; background: #3a1a1a; border-color: #f87171; }

/* ── Chat form drag-and-drop ─────────────────────── */
.chat-form.drag-active {
  border-color: var(--accent);
  background: var(--accent-dim);
}
.chat-form.drag-active input { background: transparent; }

/* ── Drag and drop ────────────────────────────── */
.team-slot.draggable { cursor: grab; }
.team-slot.draggable:active { cursor: grabbing; }
.team-slot.dragging { opacity: 0.4; }
.team-slot.drag-over { background: var(--accent-dim); outline: 2px dashed var(--accent); border-radius: 6px; }
