/* ============================
   CSS Variables — Light & Friendly (default)
   ============================ */
:root {
  --bg: #f0f2f5;
  --bg2: #ffffff;
  --bg3: #e8eaed;
  --border: #d1d5db;
  --text: #1f2937;
  --text2: #4b5563;
  --text3: #9ca3af;
  --accent: #4f46e5;
  --accent-h: #4338ca;
  --accent-dim: rgba(79,70,229,0.1);
  --success: #16a34a;
  --success-dim: rgba(22,163,74,0.1);
  --error: #dc2626;
  --error-dim: rgba(220,38,38,0.1);
  --warn: #d97706;
  --warn-dim: rgba(217,119,6,0.1);
  --info: #2563eb;
  --info-dim: rgba(37,99,235,0.1);
  --easy: #16a34a;
  --medium: #d97706;
  --hard: #dc2626;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
}

/* ============================
   Dark Mode Variables
   ============================ */
html.dark {
  --bg: #0d1117;
  --bg2: #161b22;
  --bg3: #21262d;
  --border: #30363d;
  --text: #e6edf3;
  --text2: #8b949e;
  --text3: #484f58;
  --accent: #58a6ff;
  --accent-h: #388bfd;
  --accent-dim: rgba(88,166,255,0.12);
  --success: #3fb950;
  --success-dim: rgba(63,185,80,0.12);
  --error: #f85149;
  --error-dim: rgba(248,81,73,0.12);
  --warn: #d29922;
  --warn-dim: rgba(210,153,34,0.12);
  --info: #79c0ff;
  --info-dim: rgba(121,192,255,0.12);
  --easy: #3fb950;
  --medium: #d29922;
  --hard: #f85149;
  --shadow: 0 4px 24px rgba(0,0,0,0.5);
}

/* Dark mode: select arrow in light color */
html.dark select.form-control {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%238b949e' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
}

/* Tree page dark mode */
html.dark #tree-wrap    { background: radial-gradient(ellipse at 40% 60%, #0a1628 0%, #0d1117 55%); }
html.dark #canvasWrap   { background: radial-gradient(ellipse at 40% 40%, #0a1628 0%, #0d1117 65%); }
html.dark #hud, html.dark #progress-badge { background: rgba(13,17,23,0.9); border-color: var(--border); }
html.dark #tooltip      { background: var(--bg2); border-color: var(--border); color: var(--text); }
html.dark .ctrl-btn     { background: rgba(13,17,23,0.9); border-color: var(--border); color: var(--text); }
html.dark .hud-item     { color: var(--text2); }

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

html { scroll-behavior: smooth; }

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

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-h); }

img { max-width: 100%; }

input, textarea, select, button { font-family: inherit; }

/* ============================
   Typography
   ============================ */
h1 { font-size: 2rem; font-weight: 700; }
h2 { font-size: 1.5rem; font-weight: 600; }
h3 { font-size: 1.2rem; font-weight: 600; }
h4 { font-size: 1rem; font-weight: 600; }

.text-muted { color: var(--text2); }
.text-sm { font-size: 0.875rem; }
.text-xs { font-size: 0.75rem; }

/* ============================
   Layout
   ============================ */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1.5rem;
}

.page { padding: 2rem 0 4rem; }

/* ============================
   Navigation
   ============================ */
.navbar {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.navbar .container {
  display: flex;
  align-items: center;
  gap: 2rem;
  height: 60px;
}

.logo {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text) !important;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  white-space: nowrap;
}

.logo .logo-icon { font-size: 1.4rem; }

.nav-links {
  display: flex;
  gap: 0.25rem;
  flex: 1;
}

.nav-links a {
  color: var(--text2);
  padding: 0.4rem 0.75rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  transition: all 0.15s;
}

.nav-links a:hover,
.nav-links a.active {
  color: var(--text);
  background: var(--accent-dim);
}

.nav-user {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-left: auto;
}

.nav-username {
  color: var(--text2);
  font-size: 0.9rem;
}

.nav-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0.25rem;
  margin-left: auto;
}

/* ============================
   Buttons
   ============================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-h); border-color: var(--accent-h); color: white; }

.btn-outline {
  background: transparent;
  color: var(--text2);
  border-color: var(--border);
}
.btn-outline:hover:not(:disabled) { color: var(--text); border-color: var(--text3); background: var(--bg3); }

.btn-danger {
  background: var(--error);
  color: white;
  border-color: var(--error);
}
.btn-danger:hover:not(:disabled) { opacity: 0.85; color: white; }

.btn-success {
  background: var(--success);
  color: white;
  border-color: var(--success);
}
.btn-success:hover:not(:disabled) { opacity: 0.85; color: white; }

.btn-sm { padding: 0.35rem 0.8rem; font-size: 0.8rem; }
.btn-lg { padding: 0.75rem 1.75rem; font-size: 1rem; }
.btn-full { width: 100%; justify-content: center; }

/* ============================
   Cards
   ============================ */
.card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}

.card-sm { padding: 1rem; }

/* ============================
   Forms
   ============================ */
.form-group { margin-bottom: 1.25rem; }

.form-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text2);
  margin-bottom: 0.4rem;
}

.form-control {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0.6rem 0.9rem;
  font-size: 0.9rem;
  transition: border-color 0.15s;
}

.form-control:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.form-control::placeholder { color: var(--text3); }

textarea.form-control { min-height: 120px; resize: vertical; }

.form-error {
  font-size: 0.8rem;
  color: var(--error);
  margin-top: 0.3rem;
}

select.form-control {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 12px;
  padding-right: 2.5rem;
}

/* ============================
   Badges
   ============================ */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge-easy { background: var(--success-dim); color: var(--success); }
.badge-medium { background: var(--warn-dim); color: var(--warn); }
.badge-hard { background: var(--error-dim); color: var(--error); }
.badge-success { background: var(--success-dim); color: var(--success); }
.badge-error { background: var(--error-dim); color: var(--error); }
.badge-warning { background: var(--warn-dim); color: var(--warn); }
.badge-info { background: var(--info-dim); color: var(--info); }
.badge-muted { background: rgba(148,163,184,0.1); color: var(--text3); }
.badge-accent { background: var(--accent-dim); color: var(--accent); }

/* ============================
   Tables
   ============================ */
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

thead tr { background: var(--bg3); }

th {
  text-align: left;
  padding: 0.75rem 1rem;
  font-weight: 600;
  color: var(--text2);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg3); }
tbody tr.highlight { background: var(--accent-dim); }

/* ============================
   Stats Grid
   ============================ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
}

.stat-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
}

.stat-card .stat-value {
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
}

.stat-card .stat-label {
  font-size: 0.8rem;
  color: var(--text2);
  margin-top: 0.25rem;
}

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

.auth-box {
  width: 100%;
  max-width: 420px;
}

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

.auth-logo .logo-text {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text);
}

.auth-logo .logo-sub {
  color: var(--text2);
  font-size: 0.9rem;
  margin-top: 0.25rem;
}

.auth-tabs {
  display: flex;
  background: var(--bg3);
  border-radius: var(--radius-sm);
  padding: 0.25rem;
  margin-bottom: 1.5rem;
  gap: 0.25rem;
}

.auth-tab {
  flex: 1;
  padding: 0.5rem;
  text-align: center;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text2);
  transition: all 0.15s;
  border: none;
  background: transparent;
}

.auth-tab.active {
  background: var(--accent);
  color: white;
}

.auth-form { display: none; }
.auth-form.active { display: block; }

/* ============================
   Question List
   ============================ */
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.5rem;
}

.search-input {
  flex: 1;
  min-width: 200px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0.6rem 1rem;
  font-size: 0.9rem;
}

.search-input:focus { outline: none; border-color: var(--accent); }

.filter-btn {
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text2);
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.15s;
}

.filter-btn:hover, .filter-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.question-row td:first-child { font-weight: 500; }
.question-row:hover { cursor: pointer; }

/* ============================
   Question Detail
   ============================ */
.question-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  align-items: start;
}

.question-panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.panel-header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg3);
}

.panel-header h4 { font-size: 0.875rem; color: var(--text2); font-weight: 500; }

.panel-body { padding: 1.25rem; }

.question-meta {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
  margin: 0.75rem 0 1.25rem;
}

.question-description {
  font-size: 0.9rem;
  line-height: 1.7;
  color: var(--text);
}

.question-description pre {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem;
  overflow-x: auto;
  font-size: 0.85rem;
  margin: 0.75rem 0;
}

.question-description h3,
.question-description h4 { margin: 1rem 0 0.5rem; }

.example-box {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem;
  margin: 0.5rem 0;
}

.example-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text3);
  margin-bottom: 0.4rem;
}

.example-box pre {
  font-family: 'Fira Code', 'Courier New', monospace;
  font-size: 0.875rem;
  color: var(--text);
  white-space: pre-wrap;
  margin: 0;
}

/* Code Editor */
#editor-container {
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
}

#editor { height: 420px; }

.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  background: var(--bg3);
  border-bottom: 1px solid var(--border);
  gap: 0.5rem;
}

/* Result Panel */
.result-panel { margin-top: 1rem; }

.result-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.result-progress {
  background: var(--bg3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  padding: 1rem;
  font-size: 0.875rem;
}

.result-bar-track {
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  margin-top: 0.5rem;
  overflow: hidden;
}

.result-bar-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--success);
  transition: width 0.3s;
}

.result-error {
  background: var(--error-dim);
  border: 1px solid var(--error);
  border-radius: var(--radius-sm);
  padding: 1rem;
  font-family: monospace;
  font-size: 0.8rem;
  color: var(--error);
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
}

/* ============================
   Leaderboard
   ============================ */
.rank-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  font-weight: 700;
  font-size: 0.8rem;
}

.rank-1 { background: #ffd700; color: #000; }
.rank-2 { background: #c0c0c0; color: #000; }
.rank-3 { background: #cd7f32; color: #fff; }
.rank-other { background: var(--bg3); color: var(--text2); }

/* ============================
   Profile
   ============================ */
.profile-header {
  display: flex;
  gap: 1.5rem;
  align-items: center;
  margin-bottom: 2rem;
}

.avatar-circle {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.75rem;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.profile-info h2 { font-size: 1.4rem; }
.profile-info .email { color: var(--text2); font-size: 0.875rem; }
.profile-info .join-date { color: var(--text3); font-size: 0.8rem; margin-top: 0.2rem; }

/* ============================
   Admin
   ============================ */
.admin-grid {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 1.5rem;
  align-items: start;
}

.admin-sidebar {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.admin-nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 1rem;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text2);
  transition: all 0.15s;
  border-bottom: 1px solid var(--border);
}

.admin-nav-item:last-child { border-bottom: none; }
.admin-nav-item:hover, .admin-nav-item.active { background: var(--accent-dim); color: var(--accent); }

.admin-section { display: none; }
.admin-section.active { display: block; }

/* ============================
   Toast Notifications
   ============================ */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
  z-index: 9999;
  transform: translateY(100px);
  opacity: 0;
  transition: all 0.3s;
  max-width: 320px;
}

.toast.show { transform: translateY(0); opacity: 1; }
.toast-success { background: var(--success); color: white; }
.toast-error { background: var(--error); color: white; }
.toast-info { background: var(--accent); color: white; }
.toast-warning { background: var(--warn); color: #000; }

/* ============================
   Modal
   ============================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

.modal {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  font-size: 1.25rem;
  padding: 0.25rem;
  line-height: 1;
}

.modal-close:hover { color: var(--text); }

/* ============================
   Pagination
   ============================ */
.pagination {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  justify-content: center;
  margin-top: 1.5rem;
}

.page-btn {
  padding: 0.4rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text2);
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.15s;
}

.page-btn:hover { border-color: var(--accent); color: var(--accent); }
.page-btn.active { background: var(--accent); border-color: var(--accent); color: white; }
.page-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ============================
   Loading / Empty States
   ============================ */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  color: var(--text2);
  gap: 0.75rem;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text2);
}

.empty-state .empty-icon { font-size: 2.5rem; margin-bottom: 0.75rem; }
.empty-state p { font-size: 0.9rem; }

/* ============================
   Section headers
   ============================ */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  gap: 1rem;
}

.section-header h2 { font-size: 1.25rem; }

/* ============================
   Tabs (within pages)
   ============================ */
.tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.5rem;
}

.tab-btn {
  padding: 0.65rem 1.25rem;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text2);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: all 0.15s;
  margin-bottom: -1px;
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

.tab-content { display: none; }
.tab-content.active { display: block; }

/* ============================
   Responsive
   ============================ */
@media (max-width: 768px) {
  .question-layout { grid-template-columns: 1fr; }
  .admin-grid { grid-template-columns: 1fr; }

  .nav-links {
    display: none;
    position: absolute;
    top: 60px;
    left: 0;
    right: 0;
    background: var(--bg2);
    flex-direction: column;
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    gap: 0.25rem;
  }

  .nav-links.open { display: flex; }
  .nav-toggle { display: block; }

  .stats-grid { grid-template-columns: repeat(2, 1fr); }

  .filter-bar { flex-direction: column; align-items: stretch; }

  .profile-header { flex-direction: column; text-align: center; }

  h1 { font-size: 1.5rem; }
  .editor-layout { flex-direction: column; }

  #editor { height: 300px; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .container { padding: 0 1rem; }
}

/* ============================
   Contest Cards
   ============================ */
.contest-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}

/* ============================
   Editor Fullscreen
   ============================ */
.editor-fullscreen {
  position: fixed !important;
  inset: 0 !important;
  z-index: 500 !important;
  border-radius: 0 !important;
  display: flex !important;
  flex-direction: column !important;
  max-height: 100vh !important;
  overflow: hidden !important;
}

.editor-fullscreen #editor-container {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.editor-fullscreen #editor {
  height: 100% !important;
}

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

/* ============================
   Code / Preformatted
   ============================ */
code {
  font-family: 'Fira Code', 'Courier New', monospace;
  background: var(--bg3);
  padding: 0.1em 0.4em;
  border-radius: 4px;
  font-size: 0.875em;
}

pre code { background: none; padding: 0; }
