:root {
  --grid-size: 25px;
  --ink: #1a3a8f;
  --ink-light: #4d6bef;
  --bg: #f2f4fa;
  --card-shadow: 0 2px 10px rgba(26, 58, 143, 0.08);
  --card-shadow-hover: 0 8px 24px rgba(26, 58, 143, 0.14);
  --radius: 12px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: #222;
  margin: 0;
  padding: 0;
}

.app-layout {
  display: flex;
  min-height: 100vh;
}

/* Logo: gradientes Quadrat mit "√x"-Glyphe, in Sidebar/Login/Startbildschirm wiederverwendet. */
.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.5rem;
}

.logo-icon {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border-radius: 10px;
  box-shadow: var(--card-shadow);
}

.logo-icon-large {
  width: 56px;
  height: 56px;
  border-radius: 14px;
}

.brand h1 {
  margin: 0;
}

/* Login-Bildschirm: eigenstaendig ausserhalb von .app-layout, damit vor dem Einloggen nichts vom
   Rest der App (Sidebar, Aufgaben, Admin-Bereich) sichtbar oder erreichbar ist. */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--bg) 0%, #e3e8fb 100%);
}

.login-card {
  background: #fff;
  border-radius: var(--radius);
  padding: 2rem 2.5rem;
  width: min(360px, 90vw);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  box-sizing: border-box;
}

.login-card .brand h1 {
  margin: 0 0 0.25rem;
}

.login-card h2 {
  margin: 0 0 1rem;
  color: #555;
  font-size: 1.05rem;
}

.login-card label,
#screen-admin label {
  display: block;
  margin-top: 0.75rem;
  font-weight: 600;
}

.login-card input,
#screen-admin input {
  width: 100%;
  padding: 0.5rem 0.6rem;
  font-size: 1rem;
  box-sizing: border-box;
  margin-top: 0.25rem;
}

.login-error {
  color: #a30000;
  background: #fdecea;
  border: 1px solid #f3b3ab;
  border-radius: 6px;
  padding: 0.5rem 0.7rem;
  margin-top: 0.75rem;
  font-size: 0.9rem;
}

.logged-in-as {
  margin-top: auto;
  font-size: 0.85rem;
  color: #666;
  padding-top: 0.5rem;
  border-top: 1px solid #eee;
}

.admin-user-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid #eee;
}

.admin-user-row .admin-username {
  flex: 1;
  font-weight: 600;
}

.admin-user-row .admin-badge {
  font-size: 0.8rem;
  color: #b8860b;
}

.sidebar {
  flex: 0 0 240px;
  background: #fff;
  box-shadow: 2px 0 12px rgba(26, 58, 143, 0.06);
  padding: 1.5rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  position: sticky;
  top: 0;
  align-self: flex-start;
  height: 100vh;
}

.sidebar h1 {
  font-size: 1.1rem;
  margin: 0 0 0.5rem;
}

.nav-btn {
  text-align: left;
  padding: 0.65rem 0.8rem;
  font-size: 0.95rem;
  font-weight: 500;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

.nav-btn:hover {
  background: #eef1fc;
}

.nav-btn.active {
  background: var(--ink);
  color: #fff;
  box-shadow: var(--card-shadow);
}

main {
  flex: 1;
  max-width: 900px;
  padding: 2rem;
}

h1 {
  font-size: 1.4rem;
}

/* Startbildschirm: Logo + Begruessung, gefolgt von drei anklickbaren Karten statt nur Text. */
.hero {
  text-align: center;
  padding: 1.5rem 0 2rem;
}

.hero .logo-icon-large {
  margin-bottom: 0.75rem;
}

.hero h2 {
  margin: 0 0 0.25rem;
}

.hero-sub {
  color: #666;
  margin: 0;
}

.dashboard-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1.25rem;
}

.dashboard-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.4rem;
  text-align: left;
  background: #fff;
  border: 1px solid #e6e9f5;
  border-radius: var(--radius);
  padding: 1.5rem;
  cursor: pointer;
  box-shadow: var(--card-shadow);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.dashboard-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--card-shadow-hover);
}

.dashboard-card-icon {
  font-size: 1.8rem;
}

.dashboard-card-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink);
}

.dashboard-card-desc {
  font-size: 0.88rem;
  color: #666;
  line-height: 1.4;
}

.problem-text {
  font-size: 1.2rem;
  font-weight: 600;
  background: #fff;
  padding: 0.75rem 1rem;
  border-radius: 6px;
  border: 1px solid #ddd;
}

/* KaTeX's outer .katex box defaults to inline-block (shrink-to-fit width), so a long
   \text{...} sentence renders as one unbreakable line. Force it back into normal text flow
   so it wraps like any other paragraph. */
.problem-text .katex,
#result-latex-rendered .katex {
  display: inline;
  white-space: normal;
  overflow-wrap: anywhere;
}

.problem-images {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.problem-images img {
  width: 100%;
  max-width: 480px;
  max-height: 70vh;
  height: auto;
  object-fit: contain;
  border: 1px solid #ddd;
  border-radius: 6px;
}

.canvas-wrap {
  margin-top: 1rem;
}

/* Scrollbarer Rahmen um die Canvas-Ebenen: die Zeichenflaeche kann per "Mehr Platz"-Knopf
   ueber die Standardhoehe hinaus wachsen (fuer laengere Loesungswege), ohne dass Werkzeugleiste
   und Abgeben-Knopf aus dem sichtbaren Bereich rutschen - stattdessen scrollt nur dieser Rahmen. */
.notebook-scroll {
  max-height: 65vh;
  overflow-y: auto;
  border-radius: 4px;
}

/* Zwei uebereinanderliegende Canvas-Ebenen: #notebook speichert die fertige Tinte (Radierer,
   PNG-Export), #notebook-live zeichnet nur den gerade aktiven perfect-freehand-Strich, damit er
   bei jedem pointermove flimmerfrei neu berechnet werden kann, ohne die gesamte Zeichnung
   neu zu rendern. Beide behalten das 800x500-Seitenverhaeltnis bei, schrumpfen aber auf
   schmalen Bildschirmen proportional mit. */
.notebook-stack {
  position: relative;
  max-width: 800px;
}

.notebook-stack canvas {
  display: block;
  width: 100%;
  height: auto;
}

#notebook {
  background-color: #fff;
  background-image:
    linear-gradient(to right, #cfe0f5 1px, transparent 1px),
    linear-gradient(to bottom, #cfe0f5 1px, transparent 1px);
  background-size: var(--grid-size) var(--grid-size);
  border: 1px solid #999;
  border-radius: 4px;
}

#notebook-live {
  position: absolute;
  top: 0;
  left: 0;
  touch-action: none;
  cursor: crosshair;
}

.live-preview {
  margin-top: 0.5rem;
  background: #eef3ff;
  border: 1px solid #c7d6f5;
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.95rem;
}

.pencilkit-wrap {
  border: 1px dashed #999;
  border-radius: 4px;
  padding: 1rem;
  text-align: center;
}

.pencilkit-preview {
  display: block;
  max-width: 100%;
  margin: 0.75rem auto 0;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.toolbar {
  margin-top: 0.5rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

button {
  padding: 0.5rem 1rem;
  font-size: 1rem;
  border-radius: 4px;
  border: 1px solid #999;
  background: #fff;
  cursor: pointer;
}

#submit-btn {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.hint-btn {
  background: #fff8e1;
  border-color: #e0b400;
}

.hint-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.hint-box {
  margin-top: 0.5rem;
  background: #fff8e1;
  border: 1px solid #e0b400;
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
}

.result {
  margin-top: 1.5rem;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 1rem;
}

.result.hidden {
  display: none;
}

.status {
  min-height: 1.5rem;
  color: #666;
}

/* Impressum/AGB: eigenstaendige Seiten ausserhalb von .app-layout. */
.legal-page {
  max-width: 760px;
  margin: 0 auto;
  padding: 2rem 1.5rem 4rem;
  line-height: 1.6;
}

.legal-page h2 {
  margin-top: 2rem;
  color: var(--ink);
}

.legal-page .back-link {
  display: inline-block;
  margin-bottom: 1.5rem;
  color: var(--ink-light);
}

.legal-page .legal-note {
  color: #666;
  font-size: 0.85rem;
}

.sidebar-footer-links {
  padding-top: 0.75rem;
  border-top: 1px solid #eee;
  display: flex;
  gap: 0.75rem;
  font-size: 0.8rem;
}

.sidebar-footer-links a {
  color: #666;
}

.hidden {
  display: none !important;
}

.screen label {
  display: block;
  margin-top: 1rem;
  font-weight: 600;
}

.screen select,
.screen input[type="text"] {
  display: block;
  width: 100%;
  max-width: 320px;
  padding: 0.5rem;
  font-size: 1rem;
  margin-top: 0.25rem;
  border: 1px solid #999;
  border-radius: 4px;
  box-sizing: border-box;
}

.mode-buttons {
  margin-top: 1.5rem;
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.mode-buttons button {
  font-size: 1.1rem;
  padding: 0.75rem 1.25rem;
}

.variant-btn.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.tool-btn.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.btn-secondary {
  background: #eee;
}

.exam-submit-btn {
  background: #b3261e;
  color: #fff;
  border-color: #b3261e;
}

.exam-submit-btn:hover {
  background: #8f1d17;
}

.task-progress {
  font-weight: 600;
  color: #555;
}

.exam-timer {
  display: inline-block;
  background: #fff3cd;
  border: 2px solid #e0a800;
  border-radius: 8px;
  padding: 0.4rem 0.9rem;
  font-size: 1.2rem;
  font-weight: 700;
  color: #7a5400;
  margin-bottom: 0.5rem;
}

.exam-timer.exam-timer-expired {
  background: #f8d7da;
  border-color: #d33;
  color: #a30000;
}

.stars-total {
  font-size: 1.1rem;
  color: #b8860b;
}

.level-info {
  margin-top: 0.5rem;
  padding: 0.6rem 0.75rem;
  background: #f0f4ff;
  border: 1px solid #d3ddf7;
  border-radius: 6px;
}

.level-title {
  font-weight: 700;
  color: var(--ink);
  font-size: 0.95rem;
  margin: 0 0 0.4rem;
}

.level-progress-track {
  background: #dde5f9;
  border-radius: 4px;
  height: 8px;
  overflow: hidden;
}

.level-progress-fill {
  background: var(--ink);
  height: 100%;
  width: 0%;
  transition: width 0.3s ease;
}

.level-next {
  margin: 0.4rem 0 0;
  font-size: 0.8rem;
  color: #555;
}

.level-up-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  overflow: hidden;
  padding: 1rem;
  box-sizing: border-box;
}

.level-up-card {
  background: #fff;
  border-radius: 12px;
  padding: 2rem 2.5rem;
  text-align: center;
  width: min(420px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  z-index: 1;
  box-sizing: border-box;
}

.level-up-card h2 {
  margin-top: 0;
}

#level-up-message {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--ink);
}

.confetti-container {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 14px;
  opacity: 0.9;
  animation: confetti-fall linear forwards;
}

@keyframes confetti-fall {
  to {
    transform: translateY(110vh) rotate(360deg);
    opacity: 0.3;
  }
}

.minigame-timer {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--ink);
}

.minigame-timer.low {
  color: #d33;
}

.minigame-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin: 1rem 0;
}

.minigame-card {
  aspect-ratio: 1;
  font-size: 2rem;
  border-radius: 6px;
  border: 1px solid #999;
  background: var(--ink);
  color: transparent;
  cursor: pointer;
}

.minigame-card.flipped,
.minigame-card.matched {
  background: #fff;
  color: #222;
}

.minigame-card.matched {
  background: #d7f5d7;
  border-color: #4caf50;
  cursor: default;
}

.minigame-result {
  font-size: 1.1rem;
  font-weight: 600;
}

#shape-dialog-overlay .level-up-card p {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  text-align: left;
}

#shape-dialog-overlay input[type="number"] {
  width: 6rem;
  padding: 0.3rem 0.5rem;
  font-size: 1rem;
}

.bubblepop-score {
  font-weight: 600;
  color: var(--ink);
}

.bubblepop-area {
  position: relative;
  width: 100%;
  height: 260px;
  background: linear-gradient(180deg, #e3f2fd, #bbdefb);
  border-radius: 10px;
  overflow: hidden;
  margin: 0.5rem 0;
}

.bubble {
  position: absolute;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  box-shadow: inset -4px -4px 8px rgba(0, 0, 0, 0.15), inset 4px 4px 8px rgba(255, 255, 255, 0.4);
  animation: bubble-pop-in 0.15s ease-out;
}

@keyframes bubble-pop-in {
  from {
    transform: scale(0.3);
    opacity: 0.4;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.summary-score {
  font-size: 1.4rem;
  font-weight: 700;
}

.summary-grade {
  font-size: 1.2rem;
  font-weight: 600;
  color: var(--ink);
}

.summary-stars {
  font-size: 2rem;
  letter-spacing: 0.25rem;
}

.summary-save-error {
  margin-top: 0.75rem;
  background: #fdecea;
  border: 1px solid #f3b3ab;
  color: #8a1f11;
  border-radius: 6px;
  padding: 0.6rem 0.85rem;
  font-size: 0.9rem;
}

#history-list {
  overflow-x: auto;
}

.history-details-row td {
  padding: 0 0 0.75rem;
  border-top: none;
}

.history-details summary {
  cursor: pointer;
  color: var(--ink);
  font-weight: 600;
  padding: 0.3rem 0;
}

.history-problem-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

.history-problem {
  background: #f7f8fc;
  border: 1px solid #dde1ee;
  border-radius: 8px;
  padding: 0.75rem 1rem;
}

.history-problem-statement {
  font-weight: 600;
}

.history-problem-image {
  display: block;
  max-width: 100%;
  width: 300px;
  border: 1px solid #ccc;
  border-radius: 6px;
  margin: 0.5rem 0;
  background: #fff;
}

#history-list table {
  width: 100%;
  min-width: 480px;
  border-collapse: collapse;
  margin-top: 1rem;
  background: #fff;
}

#history-list th,
#history-list td {
  text-align: left;
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid #eee;
  font-size: 0.9rem;
}

.star-earned {
  display: inline-block;
  font-size: 2rem;
  animation: pop 0.4s ease-out;
}

@keyframes pop {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  60% {
    transform: scale(1.3);
    opacity: 1;
  }
  100% {
    transform: scale(1);
  }
}

/* Mobile (iPhone Safari etc.): Sidebar wird zur horizontalen Leiste oben statt fixer Spalte
   links, damit auf schmalen Bildschirmen nichts ueberlappt oder abgeschnitten wird. */
@media (max-width: 700px) {
  .app-layout {
    flex-direction: column;
  }

  .sidebar {
    position: static;
    height: auto;
    width: auto;
    flex: 0 0 auto;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid #ddd;
    padding: 0.75rem 1rem;
  }

  .sidebar .brand {
    width: 100%;
    margin-bottom: 0.25rem;
  }

  .nav-btn {
    flex: 1 1 auto;
    text-align: center;
    white-space: nowrap;
  }

  .stars-total,
  .level-info {
    width: 100%;
    order: 10;
  }

  .logged-in-as,
  #btn-logout {
    width: 100%;
    order: 11;
  }

  main {
    padding: 1rem;
    max-width: 100%;
    box-sizing: border-box;
  }

  .mode-buttons {
    flex-direction: column;
    align-items: stretch;
  }

  .screen select,
  .screen input[type="text"] {
    max-width: 100%;
  }
}
