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

:root {
  --bg: #dbeafe;
  --surface: #ffffff;
  --surface2: #eff6ff;
  --border: #bfdbfe;
  --panel-bg: #bfdbfe;
  --text: #1e293b;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --warning: #f59e0b;
  --success: #22c55e;
  --router-color: #3b82f6;
  --router-self: #f97316;
  --lan-color: #22c55e;
  --link-color: #93c5fd;
  --link-broken: #ef4444;
  --packet-color: #f59e0b;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

/* ===== LOBBY ===== */
.lobby-page {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  min-height: 100vh;
}

.lobby-container {
  max-width: 800px;
  width: 100%;
}

.logo {
  text-align: center;
  margin-bottom: 3rem;
}

.logo-icon { font-size: 4rem; margin-bottom: 0.5rem; }
.logo h1 { font-size: 2.5rem; font-weight: 700; color: var(--primary); }
.subtitle { color: var(--muted); font-size: 1.1rem; margin-top: 0.5rem; }

.card-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}

@media (max-width: 600px) { .card-row { grid-template-columns: 1fr; } }

.card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 2px 16px rgba(37, 99, 235, 0.08);
}

.card h2 { font-size: 1.2rem; margin-bottom: 0.75rem; color: var(--primary); }
.card p { color: var(--muted); font-size: 0.9rem; margin-bottom: 1.25rem; }

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 0.6rem 0.85rem;
  background: var(--surface2);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
  outline: none;
  transition: border-color 0.15s;
}
input:focus { border-color: var(--primary); }

.btn {
  display: inline-block;
  padding: 0.6rem 1.25rem;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: background 0.15s, opacity 0.15s;
  width: 100%;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { background: var(--success); color: #fff; }
.btn-primary:hover:not(:disabled) { background: #16a34a; }
.btn-secondary { background: var(--primary); color: #fff; }
.btn-secondary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-warning { background: var(--warning); color: #fff; }
.btn-sm { padding: 0.35rem 0.75rem; font-size: 0.85rem; width: auto; }

.error-banner {
  background: rgba(239, 68, 68, 0.08);
  border: 1.5px solid var(--danger);
  color: var(--danger);
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin-top: 1rem;
  text-align: center;
}

.hidden { display: none !important; }

/* ===== GAME LAYOUT ===== */
.game-layout {
  display: grid;
  grid-template-columns: 1fr 340px;
  grid-template-rows: auto 1fr;
  height: 100vh;
  overflow: hidden;
}

.game-header {
  grid-column: 1 / -1;
  background: var(--surface);
  border-bottom: 2px solid var(--border);
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.game-header h1 { font-size: 1.1rem; font-weight: 700; color: var(--primary); }
.header-info { display: flex; gap: 1.25rem; flex: 1; flex-wrap: wrap; }
.info-badge {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.25rem 0.75rem;
  font-size: 0.82rem;
}
.info-badge .label { color: var(--muted); }
.info-badge .value { font-weight: 600; font-family: monospace; color: var(--text); }
.score-badge .value { color: var(--success); }

.graph-area {
  background: var(--surface);
  overflow: hidden;
  position: relative;
  border: 2px solid var(--border);
  margin: 1rem;
  border-radius: 12px;
}

#networkGraph {
  width: 100%;
  height: 100%;
}

/* ===== SVG GRAPH ===== */
.link-line { stroke: var(--link-color); stroke-width: 2.5; opacity: 0.9; transition: all 0.3s; }
.link-line.broken { stroke: var(--link-broken); stroke-dasharray: 8 4; opacity: 0.5; }
.link-line.busy { stroke: #f97316; stroke-dasharray: 10 4; opacity: 0.8; stroke-width: 3; }
.link-line.packet-active { stroke: var(--packet-color); stroke-width: 4; opacity: 1; }

.link-busy-label {
  fill: #f97316;
  font-size: 9px;
  text-anchor: middle;
  font-weight: 700;
}

.router-node { cursor: default; }
.router-circle {
  fill: var(--router-color);
  stroke: none;
  transition: all 0.3s;
  filter: drop-shadow(0 2px 6px rgba(59,130,246,0.3));
}
.router-circle.self {
  fill: var(--router-self);
  filter: drop-shadow(0 2px 6px rgba(249,115,22,0.35));
}
.router-circle.disconnected { opacity: 0.35; filter: none; }
.router-circle.has-packet {
  fill: var(--packet-color);
  filter: drop-shadow(0 2px 8px rgba(245,158,11,0.4));
}

.router-icon { font-size: 16px; text-anchor: middle; dominant-baseline: middle; fill: #ffffff; }
.router-name { fill: var(--text); font-size: 11px; text-anchor: middle; font-weight: 700; }
.router-ip { fill: var(--muted); font-size: 10px; text-anchor: middle; font-family: monospace; }

.lan-node { cursor: default; }
.lan-circle {
  fill: var(--lan-color);
  stroke: none;
  filter: drop-shadow(0 2px 5px rgba(34,197,94,0.3));
}
.lan-label { fill: var(--text); font-size: 10px; text-anchor: middle; font-weight: 600; }
.lan-line { stroke: var(--lan-color); stroke-width: 1.5; stroke-dasharray: 5 3; opacity: 0.5; }

/* ===== SIDEBAR ===== */
.sidebar {
  background: var(--panel-bg);
  border-left: 2px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  background: var(--surface);
}
.tab-btn {
  flex: 1;
  padding: 0.65rem;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.82rem;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: all 0.15s;
}
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.tab-btn:hover:not(.active) { color: var(--text); background: var(--surface2); }

.tab-content { display: none; flex: 1; overflow-y: auto; padding: 0.75rem; }
.tab-content.active { display: flex; flex-direction: column; gap: 0.5rem; }

/* ===== PACKET PANEL ===== */
.packet-panel {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
}
.packet-panel h3 { color: var(--primary); font-size: 0.9rem; margin-bottom: 0.5rem; font-weight: 700; }
.packet-dest { font-family: monospace; font-size: 1rem; color: var(--text); margin-bottom: 0.75rem; }
.packet-hops { color: var(--muted); font-size: 0.8rem; margin-bottom: 0.75rem; }
.forward-buttons { display: flex; flex-direction: column; gap: 0.4rem; }
.forward-btn {
  background: var(--primary);
  border: none;
  color: #ffffff;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  text-align: left;
  transition: background 0.15s;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.forward-btn:hover { background: var(--primary-hover); }
.forward-btn.deliver { background: var(--success); }
.forward-btn.deliver:hover { background: #16a34a; }
.forward-btn .btn-ip { font-family: monospace; font-size: 0.8rem; opacity: 0.85; }

.no-packet-msg {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 2rem 0;
}

/* ===== ROUTING TABLE ===== */
.routing-table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border-radius: 10px;
  border: 1.5px solid var(--border);
}
.routing-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}
.routing-table th {
  background: var(--surface2);
  color: var(--text);
  padding: 0.5rem 0.6rem;
  text-align: left;
  font-weight: 700;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
.routing-table td {
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--border);
  font-family: monospace;
  color: var(--text);
}
.routing-table tr:last-child td { border-bottom: none; }
.routing-table tr.direct-route td { color: var(--success); }
.routing-table tr.unreachable td { color: var(--danger); opacity: 0.7; }
.routing-table tr.highlight td { background: rgba(245, 158, 11, 0.12); }
.metric-cell { color: var(--primary); font-weight: 600; }

/* ===== SCOREBOARD ===== */
.score-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  font-size: 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
}
.score-row.self { background: rgba(249, 115, 22, 0.08); border-color: rgba(249, 115, 22, 0.3); }
.score-row-rank { color: var(--muted); font-size: 0.75rem; min-width: 1.5rem; }
.score-row-name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-row-ip { font-family: monospace; font-size: 0.75rem; color: var(--muted); }
.score-row-pts { font-weight: 700; color: var(--success); min-width: 3rem; text-align: right; }
.score-row.disconnected { opacity: 0.4; }

/* ===== NOTIFICATIONS ===== */
.notification-stack {
  position: fixed;
  top: 4.5rem;
  right: 1rem;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
  max-width: 320px;
}
.notif {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem 1rem;
  font-size: 0.85rem;
  animation: slideIn 0.2s ease, fadeOut 0.3s ease 2.7s forwards;
  pointer-events: none;
  color: var(--text);
  box-shadow: 0 2px 12px rgba(37, 99, 235, 0.1);
}
.notif.success { border-color: var(--success); background: rgba(34, 197, 94, 0.08); }
.notif.warning { border-color: var(--warning); background: rgba(245, 158, 11, 0.08); }
.notif.danger { border-color: var(--danger); background: rgba(239, 68, 68, 0.08); }
.notif.info { border-color: var(--primary); background: rgba(37, 99, 235, 0.08); }

@keyframes slideIn { from { transform: translateX(120%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes fadeOut { from { opacity: 1; } to { opacity: 0; transform: translateX(120%); } }

/* ===== TEACHER PAGE ===== */
.teacher-layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  grid-template-rows: auto 1fr;
  height: 100vh;
  overflow: hidden;
}

.teacher-controls {
  background: var(--panel-bg);
  border-left: 2px solid var(--border);
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.control-section h3 {
  font-size: 0.8rem;
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.75rem;
  padding-bottom: 0.35rem;
  border-bottom: 2px solid var(--border);
  font-weight: 700;
}

.student-list { display: flex; flex-direction: column; gap: 0.35rem; max-height: 280px; overflow-y: auto; }
.student-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  font-size: 0.82rem;
  cursor: pointer;
  transition: border-color 0.15s;
}
.student-item:hover { border-color: var(--primary); }
.student-item.selected { border-color: var(--primary); background: rgba(37, 99, 235, 0.08); }
.student-item.disconnected { opacity: 0.4; }
.student-item input[type="checkbox"] { accent-color: var(--primary); }
.student-item-name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.student-item-ip { font-family: monospace; font-size: 0.75rem; color: var(--muted); }
.student-item-score { color: var(--success); font-size: 0.78rem; font-weight: 600; }
.queue-count {
  background: rgba(245,158,11,0.15);
  border: 1px solid var(--warning);
  color: #b45309;
  border-radius: 10px;
  padding: 0.1rem 0.4rem;
  font-size: 0.72rem;
  font-weight: 700;
  white-space: nowrap;
}

.busy-links-btn { background: var(--surface2); border: 1px solid var(--border); width: 100%; }

.sabotage-timer {
  font-size: 0.8rem;
  color: var(--danger);
  text-align: center;
  margin-top: 0.25rem;
  font-weight: 600;
}

.teacher-graph-area {
  background: var(--surface);
  overflow: hidden;
  position: relative;
  border: 2px solid var(--border);
  margin: 1rem;
  border-radius: 12px;
}

.teacher-log {
  max-height: 160px;
  overflow-y: auto;
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.log-entry { padding: 0.2rem 0; border-bottom: 1px solid var(--border); color: var(--text); }
.log-entry.success { color: var(--success); }
.log-entry.danger { color: var(--danger); }
.log-entry.warning { color: var(--warning); }

/* ===== LINK TIMER ===== */
.link-timer-label {
  fill: var(--danger);
  font-size: 10px;
  text-anchor: middle;
  font-weight: 700;
}

/* ===== WAITING STATE ===== */
.waiting-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.92);
  border-radius: 10px;
}
.waiting-overlay h2 { font-size: 1.2rem; color: var(--primary); font-weight: 700; }
.pulse {
  width: 60px; height: 60px;
  border-radius: 50%;
  border: 3px solid var(--primary);
  animation: pulsate 1.5s ease-in-out infinite;
}
@keyframes pulsate {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); opacity: 0.5; }
}

/* ===== QUEUE BADGE (header) ===== */
.queue-badge .value { color: var(--warning); font-weight: 700; }
.queue-badge { border-color: var(--warning); }

/* ===== SESSION CODE BADGE ===== */
.session-code-badge .value {
  color: var(--primary);
  font-size: 1rem;
  font-weight: 800;
  letter-spacing: 0.1em;
}
.session-code-badge { border-color: var(--primary); background: rgba(37,99,235,0.06); }

/* ===== PACKET PANEL HEADER ===== */
.packet-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.packet-panel-header h3 { margin-bottom: 0; }
.queue-indicator {
  background: rgba(245,158,11,0.15);
  border: 1.5px solid var(--warning);
  color: #b45309;
  border-radius: 20px;
  padding: 0.15rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}

/* ===== HEADER NOTIFICATION ===== */
.header-notif {
  padding: 0.3rem 1rem;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  animation: notifPop 0.2s ease;
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-notif.success { background: rgba(34,197,94,0.15); color: #16a34a; border: 1.5px solid #22c55e; }
.header-notif.warning { background: rgba(245,158,11,0.15); color: #b45309; border: 1.5px solid #f59e0b; }
.header-notif.danger  { background: rgba(239,68,68,0.15);  color: #dc2626; border: 1.5px solid #ef4444; }
.header-notif.info    { background: rgba(37,99,235,0.1);   color: #2563eb; border: 1.5px solid #bfdbfe; }
@keyframes notifPop { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ===== SABOTAGE BADGE STATES ===== */
.sabotage-badge.sabotage-ready  { border-color: #22c55e; }
.sabotage-badge.sabotage-ready .value  { color: #16a34a; }
.sabotage-badge.sabotage-active { border-color: #ef4444; animation: pulse-red 1s ease-in-out infinite; }
.sabotage-badge.sabotage-active .value { color: #dc2626; }
.sabotage-badge.sabotage-cooldown .value { color: var(--muted); }
@keyframes pulse-red {
  0%, 100% { background: rgba(239,68,68,0.05); }
  50%       { background: rgba(239,68,68,0.18); }
}

/* ===== LEADERBOARD PAGE ===== */
.leaderboard-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #1e40af 0%, #1d4ed8 40%, #2563eb 100%);
  color: #fff;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  padding: 2rem;
}

.lb-header {
  text-align: center;
  margin-bottom: 2.5rem;
}
.lb-header h1 {
  font-size: clamp(2rem, 5vw, 3.5rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  text-shadow: 0 2px 12px rgba(0,0,0,0.25);
}
.lb-header .lb-subtitle {
  font-size: clamp(1rem, 2.5vw, 1.5rem);
  opacity: 0.8;
  margin-top: 0.4rem;
  font-weight: 500;
}

.lb-grid {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

.lb-empty {
  text-align: center;
  opacity: 0.6;
  font-size: 1.2rem;
  padding: 3rem;
}

.lb-card {
  display: grid;
  grid-template-columns: 3.5rem 1fr auto;
  align-items: center;
  gap: 1rem;
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 14px;
  padding: 1rem 1.5rem;
  backdrop-filter: blur(6px);
  transition: transform 0.2s, background 0.2s;
}
.lb-card:hover { transform: translateX(4px); background: rgba(255,255,255,0.18); }
.lb-card.rank-1 { background: rgba(251,191,36,0.25); border-color: rgba(251,191,36,0.5); }
.lb-card.rank-2 { background: rgba(203,213,225,0.2); border-color: rgba(203,213,225,0.4); }
.lb-card.rank-3 { background: rgba(180,120,60,0.2);  border-color: rgba(180,120,60,0.4); }
.lb-card.disconnected { opacity: 0.45; }

.lb-rank {
  font-size: clamp(1.5rem, 3vw, 2.2rem);
  font-weight: 800;
  text-align: center;
  opacity: 0.9;
}
.lb-card.rank-1 .lb-rank,
.lb-card.rank-2 .lb-rank,
.lb-card.rank-3 .lb-rank { font-size: 0; }
.lb-card.rank-1 .lb-rank::before { content: '🥇'; font-size: 2.2rem; }
.lb-card.rank-2 .lb-rank::before { content: '🥈'; font-size: 2.2rem; }
.lb-card.rank-3 .lb-rank::before { content: '🥉'; font-size: 2.2rem; }

.lb-name {
  font-size: clamp(1.2rem, 2.5vw, 1.8rem);
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lb-ip {
  font-size: 0.75rem;
  opacity: 0.6;
  font-family: monospace;
  margin-top: 0.15rem;
}

.lb-score {
  font-size: clamp(1.5rem, 3vw, 2.5rem);
  font-weight: 800;
  text-align: right;
  color: #fde68a;
}
.lb-score::after { content: ' pts'; font-size: 0.5em; opacity: 0.7; font-weight: 500; }

/* ===== ZOOM CONTROLS ===== */
.zoom-controls {
  position: absolute;
  bottom: 12px;
  right: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 10;
}
.zoom-btn {
  width: 32px;
  height: 32px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(37,99,235,0.12);
  color: var(--text);
  transition: border-color 0.15s, background 0.15s, color 0.15s;
  line-height: 1;
  padding: 0;
}
.zoom-btn:hover {
  background: var(--surface2);
  border-color: var(--primary);
  color: var(--primary);
}

/* ===== SCROLLBARS ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
