:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --text: #1a2233;
  --muted: #6b7686;
  --border: #e2e6ed;
  --primary: #2f5fff;
  --primary-dark: #1f42c9;
  --danger: #e14b4b;
  --good: #1f9d55;
  --bad: #e14b4b;
  --warn: #d99a1f;
  --shadow: 0 1px 3px rgba(20, 30, 60, 0.08), 0 1px 2px rgba(20, 30, 60, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10131a;
    --surface: #1a1f2b;
    --text: #eef1f6;
    --muted: #97a2b5;
    --border: #2a3040;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* {
  box-sizing: border-box;
}

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

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}
.small {
  font-size: 0.85em;
}

/* Auth screen */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
}
.auth-card h1 {
  margin: 0 0 4px;
  font-size: 1.4rem;
}
.auth-card form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 16px;
}
.auth-buttons {
  display: flex;
  gap: 10px;
  margin-top: 6px;
}
.auth-buttons .btn {
  flex: 1;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--muted);
}

input,
select {
  font: inherit;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
input:focus,
select:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.btn {
  font: inherit;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 600;
}
.btn.primary {
  background: var(--primary);
  color: #fff;
}
.btn.primary:hover {
  background: var(--primary-dark);
}
.btn.secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}
.btn.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}
.btn.danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger);
}
.btn.small {
  padding: 6px 10px;
  font-size: 0.85rem;
}
.btn.icon {
  padding: 4px 8px;
  font-size: 0.95rem;
  line-height: 1;
}

.error {
  background: rgba(225, 75, 75, 0.12);
  color: var(--bad);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 0.85rem;
}

.success {
  background: rgba(31, 157, 85, 0.12);
  color: var(--good);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 0.85rem;
}

.btn-link {
  background: none;
  border: none;
  padding: 4px 0;
  color: var(--primary);
  font: inherit;
  font-size: 0.85rem;
  text-decoration: underline;
  cursor: pointer;
  align-self: center;
}
.btn-link:hover {
  color: var(--primary-dark);
}

/* App shell */
.app {
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
  flex-wrap: wrap;
}
.brand {
  font-weight: 700;
  font-size: 1.1rem;
}
.overall-average {
  display: flex;
  align-items: center;
  gap: 8px;
}
.overall-average .label {
  color: var(--muted);
  font-size: 0.85rem;
}
.topbar-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 12px;
}

.pill {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 999px;
  font-weight: 700;
  background: var(--border);
}
.pill.good {
  background: rgba(31, 157, 85, 0.15);
  color: var(--good);
}
.pill.bad {
  background: rgba(225, 75, 75, 0.15);
  color: var(--bad);
}

.content {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
}

/* Solange noch geladen wird, bleiben die eigentlichen Inhalte verborgen und
   nur der Ladezustand ist sichtbar - verhindert eine leere Seite zwischen
   Login und dem ersten Firestore-Snapshot. */
.content.is-loading #empty-state,
.content.is-loading #subjects-grid,
.content.is-loading .toolbar {
  display: none;
}
.loading-state {
  display: none;
}
.content.is-loading .loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 60px 20px;
  color: var(--muted);
}

.spinner {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.subjects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 18px;
  margin-bottom: 24px;
}

.subject-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.subject-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.drag-handle {
  cursor: grab;
  touch-action: none;
  user-select: none;
  color: var(--muted);
  font-size: 1.2rem;
  line-height: 1;
  padding: 4px 2px;
  flex-shrink: 0;
}

.subject-card.dragging {
  opacity: 0.5;
}
.subject-card.dragging .drag-handle {
  cursor: grabbing;
}
.subject-titles {
  display: flex;
  flex-direction: column;
}
.subject-shortname {
  font-weight: 700;
  font-size: 1.05rem;
}
.subject-longname {
  color: var(--muted);
  font-size: 0.85rem;
}
.subject-actions {
  display: flex;
  gap: 4px;
}

.subject-meta {
  display: flex;
  align-items: center;
  gap: 10px;
}
.subject-meta .weight-tag {
  font-size: 0.75rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 8px;
}

.grades-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.grades-table th,
.grades-table td {
  text-align: left;
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
}
.grades-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.grades-table tr:last-child td {
  border-bottom: none;
}
.grade-value {
  font-weight: 700;
}
.grade-value.good {
  color: var(--good);
}
.grade-value.bad {
  color: var(--bad);
}
.grade-row-actions {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
}

.no-grades {
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
}

.toolbar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

/* Modal */
.modal {
  border: none;
  border-radius: 16px;
  padding: 24px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 380px;
}
.modal::backdrop {
  background: rgba(10, 14, 24, 0.5);
}
.modal form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal h2 {
  margin: 0;
  font-size: 1.15rem;
}
.modal-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}
/* Löschen steht bewusst als letztes Element im DOM (nach Speichern), damit
   Tab-/Fokus-Reihenfolge und visuelle Reihenfolge übereinstimmen (WCAG
   2.4.3) und man beim Formular-Tabben nicht versehentlich auf der
   destruktiven Aktion landet. Der zusätzliche Abstand grenzt sie zusätzlich
   optisch von Abbrechen/Speichern ab. */
.modal-buttons .btn.danger {
  margin-left: 16px;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 0.9rem;
  z-index: 50;
  box-shadow: var(--shadow);
}
