:root {
  --color-primary: #2563eb;
  --color-primary-dark: #1d4ed8;
  --color-primary-light: #dbeafe;
  --color-success: #16a34a;
  --color-success-light: #dcfce7;
  --color-danger: #dc2626;
  --color-danger-light: #fee2e2;
  --color-warning: #f59e0b;
  --color-warning-light: #fef3c7;
  --color-bg: #f1f5f9;
  --color-surface: #ffffff;
  --color-text: #1e293b;
  --color-text-muted: #64748b;
  --color-border: #e2e8f0;
  --sidebar-width: 280px;
  --header-height: 60px;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --font: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --transition: 0.2s ease;
}

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

body {
  font-family: var(--font);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 100vh;
}

/* ---- LOGIN ---- */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%);
  padding: 20px;
  position: relative;
  z-index: 200;
}

#login-screen[hidden] { display: none; }

.login-box {
  background: var(--color-surface);
  padding: 48px 40px;
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  text-align: center;
  max-width: 400px;
  width: 100%;
}

.login-icon { font-size: 48px; margin-bottom: 12px; }

.login-box h1 {
  font-size: 1.5rem;
  margin-bottom: 4px;
  color: var(--color-text);
}

.login-box > p {
  color: var(--color-text-muted);
  font-size: 0.9rem;
  margin-bottom: 24px;
}

#login-form input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 1rem;
  font-family: var(--font);
  outline: none;
  transition: border-color var(--transition);
  margin-bottom: 12px;
}

#login-form input:focus { border-color: var(--color-primary); }

#login-form button {
  width: 100%;
  padding: 12px;
  background: var(--color-primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
}

#login-form button:hover { background: var(--color-primary-dark); }

.error-msg {
  color: var(--color-danger);
  font-size: 0.85rem;
  margin-top: 12px;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}
.shake { animation: shake 0.4s ease; }

/* ---- HEADER ---- */
#site-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 100;
  box-shadow: var(--shadow);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-left h1 { font-size: 1.1rem; }
.header-left h1 a { color: var(--color-text); text-decoration: none; }

.menu-btn {
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  color: var(--color-text);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  display: none;
}

#header-nav { display: flex; gap: 8px; }

#header-nav a {
  text-decoration: none;
  color: var(--color-text-muted);
  font-size: 0.9rem;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
}

#header-nav a:hover, #header-nav a.active {
  background: var(--color-primary-light);
  color: var(--color-primary);
}

/* ---- SIDEBAR ---- */
#sidebar {
  position: fixed;
  top: var(--header-height);
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  z-index: 90;
  transition: transform 0.3s ease;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 8px;
}

.sidebar-header h2 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--color-text-muted); }

#sidebar-close { display: none; }

#sidebar-nav { padding: 0 12px 20px; }

.sidebar-category {
  margin-bottom: 4px;
}

.sidebar-category-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: none;
  border: none;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--color-text);
  cursor: pointer;
  border-radius: var(--radius-sm);
  text-align: left;
  transition: background var(--transition);
  font-family: var(--font);
}

.sidebar-category-btn:hover { background: var(--color-bg); }

.sidebar-category-btn .arrow {
  margin-left: auto;
  font-size: 0.7rem;
  transition: transform var(--transition);
}

.sidebar-category-btn.open .arrow { transform: rotate(90deg); }

.sidebar-topics {
  display: none;
  padding-left: 12px;
}

.sidebar-topics.open { display: block; }

.sidebar-topic-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  font-size: 0.85rem;
  color: var(--color-text-muted);
  text-decoration: none;
  border-radius: var(--radius-sm);
  transition: background var(--transition), color var(--transition);
}

.sidebar-topic-link:hover { background: var(--color-bg); color: var(--color-text); }
.sidebar-topic-link.active { background: var(--color-primary-light); color: var(--color-primary); font-weight: 600; }

.sidebar-topic-link .badge {
  margin-left: auto;
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 10px;
  font-weight: 600;
}

.badge-review { background: var(--color-danger-light); color: var(--color-danger); }
.badge-ok { background: var(--color-success-light); color: var(--color-success); }

#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.3);
  z-index: 80;
}

/* ---- MAIN CONTENT ---- */
#content {
  margin-left: var(--sidebar-width);
  margin-top: var(--header-height);
  padding: 32px;
  max-width: 900px;
}

/* ---- HOME VIEW ---- */
.home-hero {
  text-align: center;
  margin-bottom: 36px;
}

.home-hero h2 { font-size: 1.6rem; margin-bottom: 8px; }
.home-hero p { color: var(--color-text-muted); font-size: 1rem; }

.home-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 20px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--transition), transform 0.1s;
  font-family: var(--font);
}

.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--color-primary); color: white; }
.btn-primary:hover { background: var(--color-primary-dark); }

.btn-outline {
  background: var(--color-surface);
  color: var(--color-primary);
  border: 2px solid var(--color-primary);
}
.btn-outline:hover { background: var(--color-primary-light); }

.btn-success { background: var(--color-success); color: white; }
.btn-danger { background: var(--color-danger); color: white; }

.btn-sm { padding: 6px 14px; font-size: 0.85rem; }

.category-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.category-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: box-shadow var(--transition), transform 0.15s;
  text-decoration: none;
  color: var(--color-text);
  display: block;
}

.category-card:hover { box-shadow: var(--shadow-lg); transform: translateY(-2px); }

.category-card .emoji { font-size: 2rem; margin-bottom: 8px; display: block; }
.category-card h3 { font-size: 1.05rem; margin-bottom: 4px; }
.category-card .topic-count { font-size: 0.85rem; color: var(--color-text-muted); }

/* ---- QUIZ VIEW ---- */
.quiz-container {
  max-width: 640px;
  margin: 0 auto;
}

.quiz-header {
  text-align: center;
  margin-bottom: 32px;
}

.quiz-header h2 { font-size: 1.4rem; margin-bottom: 8px; }
.quiz-header p { color: var(--color-text-muted); }

.quiz-progress {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
  font-size: 0.85rem;
  color: var(--color-text-muted);
}

.progress-bar {
  flex: 1;
  height: 8px;
  background: var(--color-border);
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--color-primary);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.quiz-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 32px;
  box-shadow: var(--shadow);
}

.quiz-question {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 20px;
  line-height: 1.5;
}

.quiz-choices { display: flex; flex-direction: column; gap: 10px; }

.quiz-choice {
  display: block;
  width: 100%;
  padding: 14px 16px;
  background: var(--color-bg);
  border: 2px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  cursor: pointer;
  text-align: left;
  transition: border-color var(--transition), background var(--transition);
  font-family: var(--font);
  line-height: 1.4;
}

.quiz-choice:hover { border-color: var(--color-primary); background: var(--color-primary-light); }
.quiz-choice.selected { border-color: var(--color-primary); background: var(--color-primary-light); font-weight: 600; }

.quiz-nav {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
  gap: 12px;
}

/* ---- RESULTS VIEW ---- */
.results-container { max-width: 700px; margin: 0 auto; }

.results-header {
  text-align: center;
  margin-bottom: 32px;
}

.results-header h2 { font-size: 1.5rem; margin-bottom: 4px; }

.score-circle {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 20px auto;
  font-weight: 700;
  font-size: 1.8rem;
  border: 6px solid;
}

.score-circle .label { font-size: 0.75rem; font-weight: 500; color: var(--color-text-muted); }

.results-section-title {
  font-size: 1rem;
  font-weight: 700;
  margin: 24px 0 12px;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.result-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}

.result-row .topic-name { flex: 1; font-size: 0.95rem; font-weight: 500; }

.result-bar-wrap {
  width: 100px;
  height: 8px;
  background: var(--color-border);
  border-radius: 4px;
  overflow: hidden;
}

.result-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.5s ease;
}

.result-score {
  font-size: 0.85rem;
  font-weight: 600;
  min-width: 36px;
  text-align: right;
}

.result-row .btn { white-space: nowrap; }

/* ---- STUDY VIEW ---- */
.study-container { max-width: 760px; }

.study-header {
  margin-bottom: 24px;
}

.study-header h2 { font-size: 1.4rem; margin-bottom: 2px; }
.study-header .category-label { font-size: 0.85rem; color: var(--color-text-muted); }

.study-tabs {
  display: flex;
  gap: 4px;
  background: var(--color-border);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: 24px;
}

.study-tab {
  flex: 1;
  padding: 8px 16px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
  font-family: var(--font);
  color: var(--color-text-muted);
}

.study-tab.active { background: var(--color-surface); color: var(--color-text); box-shadow: var(--shadow); }

/* Study Guide */
.study-guide-section {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 24px 28px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.study-guide-section h3 {
  font-size: 1.05rem;
  margin-bottom: 10px;
  color: var(--color-primary-dark);
}

.study-guide-section p {
  font-size: 0.95rem;
  line-height: 1.7;
  margin-bottom: 10px;
}

.study-guide-section p:last-child { margin-bottom: 0; }

/* Flashcards */
.flashcard-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.flashcard-counter { font-size: 0.9rem; color: var(--color-text-muted); font-weight: 500; }

.flashcard {
  width: 100%;
  max-width: 500px;
  min-height: 220px;
  perspective: 800px;
  cursor: pointer;
}

.flashcard-inner {
  position: relative;
  width: 100%;
  min-height: 220px;
  transition: transform 0.5s;
  transform-style: preserve-3d;
}

.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }

.flashcard-front, .flashcard-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.flashcard-front {
  background: var(--color-surface);
  border: 2px solid var(--color-border);
}

.flashcard-front .term { font-size: 1.3rem; font-weight: 700; }
.flashcard-front .hint { font-size: 0.8rem; color: var(--color-text-muted); margin-top: 12px; }

.flashcard-back {
  background: var(--color-primary);
  color: white;
  transform: rotateY(180deg);
}

.flashcard-back .definition { font-size: 1.05rem; line-height: 1.6; }

.flashcard-nav {
  display: flex;
  gap: 12px;
  align-items: center;
}

/* Matching */
.matching-container { max-width: 600px; margin: 0 auto; }

.matching-instructions {
  text-align: center;
  color: var(--color-text-muted);
  font-size: 0.9rem;
  margin-bottom: 20px;
}

.matching-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 24px;
}

.matching-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.matching-col-header {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  padding-bottom: 8px;
  border-bottom: 2px solid var(--color-border);
  margin-bottom: 4px;
}

.matching-item {
  padding: 10px 14px;
  border: 2px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
  line-height: 1.4;
  user-select: none;
}

.matching-item:hover { border-color: var(--color-primary); }
.matching-item.selected { border-color: var(--color-primary); background: var(--color-primary-light); }
.matching-item.matched { border-color: var(--color-success); background: var(--color-success-light); cursor: default; opacity: 0.7; }
.matching-item.wrong { border-color: var(--color-danger); background: var(--color-danger-light); }

.matching-result {
  text-align: center;
  margin-top: 20px;
  font-weight: 600;
  font-size: 1.1rem;
}

/* ---- RESPONSIVE ---- */
@media (max-width: 768px) {
  .menu-btn { display: block; }
  #sidebar-close { display: block; }

  #sidebar {
    transform: translateX(-100%);
  }

  #sidebar.open { transform: translateX(0); }
  #sidebar.open ~ #sidebar-overlay { display: block; }

  #content {
    margin-left: 0;
    padding: 24px 16px;
  }

  .category-grid { grid-template-columns: 1fr; }

  .home-actions { flex-direction: column; align-items: stretch; }

  .quiz-card { padding: 24px 20px; }

  .matching-grid { grid-template-columns: 1fr; }
  .matching-col:nth-child(2) { margin-top: 16px; }

  .result-row { flex-wrap: wrap; }
  .result-bar-wrap { width: 80px; }
}

@media (min-width: 769px) {
  #menu-toggle { display: none; }
}
