/* ============ THEME VARS ============ */
:root {
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 20px;
  --r-pill: 999px;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --font: 'Vazirmatn', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

html[data-theme="light"] {
  --bg: #fafafa;
  --bg-2: #ffffff;
  --bg-3: #f4f4f5;
  --bg-hover: #ededee;
  --text: #18181b;
  --text-2: #52525b;
  --text-3: #a1a1aa;
  --border: rgba(0,0,0,0.07);
  --border-2: rgba(0,0,0,0.12);
  --accent: #7c3aed;
  --accent-2: #8b5cf6;
  --accent-soft: rgba(124,58,237,0.08);
  --accent-text: #6d28d9;
  --nav-bg: rgba(250,250,250,0.8);
  --warn-bg: #fff8e5;
  --warn-text: #946200;
  --warn-border: rgba(255,159,10,0.3);
  --shadow: 0 4px 20px rgba(0,0,0,0.06);
}

html[data-theme="dark"] {
  --bg: #0a0a0c;
  --bg-2: #131316;
  --bg-3: #1a1a1f;
  --bg-hover: #1f1f25;
  --text: #f5f5f7;
  --text-2: #a1a1a6;
  --text-3: #6e6e73;
  --border: rgba(255,255,255,0.08);
  --border-2: rgba(255,255,255,0.14);
  --accent: #8b5cf6;
  --accent-2: #a78bfa;
  --accent-soft: rgba(139,92,246,0.14);
  --accent-text: #c4b5fd;
  --nav-bg: rgba(10,10,12,0.75);
  --warn-bg: rgba(255,159,10,0.1);
  --warn-text: #ffb340;
  --warn-border: rgba(255,159,10,0.25);
  --shadow: 0 4px 20px rgba(0,0,0,0.4);
}

/* ============ RESET ============ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}
*:focus { outline: none; }
*:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

html {
  scrollbar-width: none;
  -ms-overflow-style: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  overflow-x: hidden;
}
html::-webkit-scrollbar,
body::-webkit-scrollbar { width: 0; height: 0; display: none; }
* { scrollbar-width: none; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  letter-spacing: -0.011em;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-top: 58px;
  transition: background 0.3s var(--ease), color 0.3s var(--ease);
  overflow-x: hidden;
}
body.loading { overflow: hidden !important; height: 100vh !important; }

a { color: inherit; text-decoration: none; cursor: pointer; }

button {
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  letter-spacing: inherit;
  touch-action: manipulation;
}

img, svg { max-width: 100%; display: block; height: auto; }

.container {
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 16px;
}

/* ============ PRELOADER ============ */
.preloader {
  position: fixed; inset: 0; z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  transition: opacity 0.4s var(--ease), visibility 0.4s;
}
.preloader.hide { opacity: 0; visibility: hidden; pointer-events: none; }
.preloader-inner {
  display: flex; flex-direction: column; align-items: center; gap: 18px;
}
.preloader-logo-img {
  width: 48px; height: 48px; object-fit: contain;
  animation: preloaderPulse 1.8s ease-in-out infinite;
}
@keyframes preloaderPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
.preloader-bar {
  width: 90px; height: 2px; border-radius: 999px;
  background: var(--bg-3);
  overflow: hidden;
}
.preloader-bar-fill {
  height: 100%; width: 0%;
  background: var(--accent);
  animation: fillBar 1.3s var(--ease) forwards;
}
@keyframes fillBar {
  0% { width: 0%; } 100% { width: 100%; }
}

/* ============ HEADER ============ */
.site-header {
  position: fixed; top: 0; left: 0; right: 0;
  z-index: 50;
  height: 58px;
  background: var(--nav-bg);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid var(--border);
}
.site-header .header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 14px;
}

.logo { display: flex; align-items: center; flex-shrink: 0; }
.logo-text {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.nav {
  display: flex;
  gap: 0;
  align-items: center;
}
.nav a {
  color: var(--text-2);
  padding: 6px 8px;
  font-size: 0.76rem;
  font-weight: 500;
  border-radius: var(--r-md);
  transition: color 0.2s var(--ease), background 0.2s var(--ease);
  white-space: nowrap;
}
.nav a:hover { color: var(--text); background: var(--bg-3); }
.nav a.active { color: var(--text); background: var(--bg-3); }

.theme-toggle {
  width: 32px; height: 32px;
  margin-right: 2px;
  border-radius: var(--r-md);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-2);
  position: relative;
  flex-shrink: 0;
  transition: background 0.2s var(--ease), color 0.2s var(--ease);
}
.theme-toggle:hover { background: var(--bg-3); color: var(--text); }
.theme-toggle svg {
  position: absolute;
  width: 16px; height: 16px;
  transition: opacity 0.25s var(--ease), transform 0.35s var(--ease);
}
html[data-theme="dark"] .theme-toggle .icon-sun { opacity: 1; transform: rotate(0) scale(1); }
html[data-theme="dark"] .theme-toggle .icon-moon { opacity: 0; transform: rotate(-90deg) scale(0.6); }
html[data-theme="light"] .theme-toggle .icon-sun { opacity: 0; transform: rotate(90deg) scale(0.6); }
html[data-theme="light"] .theme-toggle .icon-moon { opacity: 1; transform: rotate(0) scale(1); }

/* ============ PAGE HEADER ============ */
.page-header {
  padding: 40px 0 24px;
  text-align: center;
}
.page-header h1 {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -0.035em;
  line-height: 1.2;
  margin-bottom: 8px;
  color: var(--text);
}
.page-header p {
  color: var(--text-2);
  font-size: 0.92rem;
}

.section { padding: 12px 0 60px; }

/* ============ FILTER ============ */
.filter-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 22px;
}
.filter-btn {
  padding: 7px 16px;
  border-radius: var(--r-pill);
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text-2);
  font-size: 0.82rem;
  font-weight: 500;
  transition: all 0.2s var(--ease);
}
.filter-btn:hover {
  border-color: var(--border-2);
  color: var(--text);
}
.filter-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ============ GRID ============ */
.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

/* ============ CARD ============ */
.card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 20px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  transition: all 0.25s var(--ease);
  position: relative;
  overflow: hidden;
}
.card:hover {
  border-color: var(--border-2);
  box-shadow: var(--shadow);
  transform: translateY(-2px);
}
.card::after {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  opacity: 0;
  transition: opacity 0.3s var(--ease);
}
.card:hover::after { opacity: 1; }

.card .card-icon {
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem;
  background: var(--accent-soft);
  border-radius: var(--r-lg);
  margin-bottom: 14px;
}
.card h3 {
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 6px;
  color: var(--text);
}
.card p {
  color: var(--text-2);
  font-size: 0.85rem;
  line-height: 1.6;
  margin-bottom: 14px;
  flex: 1;
}
.card .card-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text-3);
  font-size: 0.76rem;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  gap: 8px;
}
.card .card-meta .arrow {
  color: var(--accent-text);
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: gap 0.2s var(--ease);
  white-space: nowrap;
}
.card:hover .card-meta .arrow { gap: 8px; }

/* ============ BADGE ============ */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: var(--r-pill);
  font-size: 0.66rem;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent-text);
  white-space: nowrap;
}
.badge-green {
  background: rgba(34,197,94,0.12);
  color: #22c55e;
}
html[data-theme="light"] .badge-green { color: #16a34a; }

/* ============ EXERCISE PAGE ============ */
.container-narrow {
  max-width: 820px;
  margin: 0 auto;
}

.breadcrumb {
  color: var(--text-3);
  font-size: 0.76rem;
  margin: 16px 0 10px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px;
}
.breadcrumb a { transition: color 0.2s var(--ease); }
.breadcrumb a:hover { color: var(--text); }
.breadcrumb .sep { margin: 0 4px; opacity: 0.5; }

.exercise-title-block {
  text-align: right;
  padding: 10px 0 20px;
}
.exercise-title-block h1 {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.25;
  margin-bottom: 10px;
  color: var(--text);
}
.exercise-title-block p {
  color: var(--text-2);
  font-size: 0.92rem;
}

.exercise-block {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 22px;
  margin-bottom: 16px;
}
.exercise-block h2 {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 12px;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}
.exercise-block h2::before {
  content: '';
  width: 3px;
  height: 16px;
  background: var(--accent);
  border-radius: 2px;
}
.exercise-block p {
  color: var(--text-2);
  font-size: 0.92rem;
  line-height: 1.8;
  margin: 0;
  white-space: pre-line;
}

.io-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.io-box {
  background: var(--bg-3);
  border-radius: var(--r-lg);
  padding: 14px;
}
.io-label {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-3);
  margin-bottom: 6px;
  letter-spacing: 0.02em;
}
.io-value {
  font-family: var(--mono);
  font-size: 0.82rem;
  color: var(--text);
  direction: ltr;
  text-align: left;
  white-space: pre-wrap;
  line-height: 1.7;
}
@media (max-width: 500px) {
  .io-row { grid-template-columns: 1fr; }
}

.hints-list {
  padding: 0;
  list-style: none;
  counter-reset: hint;
}
.hints-list li {
  color: var(--text-2);
  font-size: 0.88rem;
  line-height: 1.7;
  padding: 10px 34px 10px 0;
  position: relative;
  counter-increment: hint;
}
.hints-list li::before {
  content: counter(hint);
  position: absolute;
  right: 0;
  top: 9px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 0.72rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.hints-list li:not(:last-child) {
  border-bottom: 1px solid var(--border);
}

.solution-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px 20px;
  border-radius: var(--r-lg);
  background: var(--accent);
  color: #fff;
  font-family: inherit;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s var(--ease);
  border: none;
}
.solution-toggle:hover { background: var(--accent-2); }
.solution-toggle.warning {
  background: var(--warn-bg);
  color: var(--warn-text);
  border: 1px solid var(--warn-border);
}
.solution-toggle.warning:hover { background: var(--warn-bg); }

.solution-wrapper {
  display: none;
  margin-top: 12px;
}
.solution-wrapper.show {
  display: block;
  animation: solutionIn 0.3s var(--ease);
}
@keyframes solutionIn {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

.code-wrapper {
  position: relative;
}
.code-copy {
  position: absolute;
  top: 12px;
  left: 12px;
  padding: 6px 14px;
  border-radius: var(--r-md);
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text-2);
  font-family: inherit;
  font-size: 0.74rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s var(--ease);
  z-index: 2;
}
.code-copy:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--accent-soft);
}
.code-block {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 50px 18px 18px 18px;
  overflow-x: auto;
  direction: ltr;
  text-align: left;
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.75;
  color: var(--text);
  margin: 0;
}
.code-block code {
  white-space: pre;
  display: block;
  font-family: inherit;
}

/* ============ EMPTY ============ */
.empty {
  text-align: center;
  padding: 50px 20px;
  color: var(--text-3);
}
.empty p { font-size: 0.9rem; }

/* ============ TOAST ============ */
.toast {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%) translateY(120px);
  padding: 10px 16px;
  border-radius: var(--r-md);
  background: var(--bg-2);
  border: 1px solid var(--border-2);
  color: var(--text);
  font-weight: 600;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 200;
  box-shadow: 0 12px 32px rgba(0,0,0,0.3);
  transition: transform 0.3s var(--ease);
  max-width: calc(100vw - 28px);
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast .dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
}

/* ============ FOOTER ============ */
.site-footer {
  margin-top: auto;
  padding: 24px 0;
  color: var(--text-3);
  text-align: center;
  font-size: 0.78rem;
  border-top: 1px solid var(--border);
}
.site-footer a { color: var(--text-2); transition: color 0.2s var(--ease); }
.site-footer a:hover { color: var(--accent-text); }

/* ============ RESPONSIVE ============ */
@media (min-width: 640px) {
  body { font-size: 16px; padding-top: 62px; }
  .site-header { height: 62px; }
  .site-header .header-inner { padding: 0 24px; }
  .logo-text { font-size: 1.12rem; }
  .nav a { padding: 8px 13px; font-size: 0.86rem; }
  .theme-toggle { width: 36px; height: 36px; margin-right: 4px; }
  .theme-toggle svg { width: 17px; height: 17px; }
  .container { padding: 0 24px; }

  .page-header { padding: 48px 0 28px; }
  .page-header h1 { font-size: 2rem; }
  .page-header p { font-size: 1rem; }

  .grid { grid-template-columns: repeat(2, 1fr); gap: 16px; }
  .card { padding: 24px; }
  .card .card-icon { width: 48px; height: 48px; font-size: 1.55rem; margin-bottom: 18px; }
  .card h3 { font-size: 1.06rem; }
  .card p { font-size: 0.88rem; }

  .exercise-title-block h1 { font-size: 2rem; }
}

@media (min-width: 860px) {
  body { padding-top: 68px; }
  .site-header { height: 68px; }
  .site-header .header-inner { padding: 0 32px; }
  .logo-text { font-size: 1.2rem; }
  .nav a { padding: 9px 15px; font-size: 0.9rem; }
  .theme-toggle { width: 38px; height: 38px; margin-right: 6px; }
  .theme-toggle svg { width: 18px; height: 18px; }
  .container { padding: 0 32px; }

  .page-header { padding: 60px 0 40px; }
  .page-header h1 { font-size: 2.4rem; }

  .grid { grid-template-columns: repeat(3, 1fr); gap: 20px; }
  .card { padding: 26px; }
  .card .card-icon { width: 52px; height: 52px; font-size: 1.7rem; margin-bottom: 20px; }
  .card h3 { font-size: 1.1rem; }
  .card p { font-size: 0.9rem; }

  .exercise-title-block h1 { font-size: 2.2rem; }
  .exercise-block { padding: 26px; }
}

@media (min-width: 1024px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
