:root {
  --bg: #f2f3f7;
  --card: #ffffff;
  --card-2: #f7f8fb;
  --text: #1a1c24;
  --text-dim: #6b7180;
  --border: #e4e6ee;
  --primary: #4f46e5;
  --primary-dim: #eef0fe;
  --danger: #dc2626;
  --shadow: 0 2px 12px rgba(20, 22, 40, .07);
  --shadow-lg: 0 8px 40px rgba(20, 22, 40, .18);
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --card: #191c25;
    --card-2: #21242f;
    --text: #eceef4;
    --text-dim: #9aa0b0;
    --border: #2a2e3a;
    --primary: #7c78ff;
    --primary-dim: #23233a;
    --shadow: 0 2px 12px rgba(0, 0, 0, .3);
    --shadow-lg: 0 8px 40px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body { line-height: 1.4; }

button { font-family: inherit; cursor: pointer; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  padding: calc(var(--safe-top) + 12px) 16px 8px;
  border-bottom: 1px solid var(--border);
}
.header-row { display: flex; align-items: center; justify-content: space-between; }
.app-title { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -.02em; }
.header-actions { display: flex; align-items: center; gap: 6px; }

.icon-btn {
  border: none; background: var(--card); color: var(--text);
  width: 38px; height: 38px; border-radius: 12px; font-size: 20px;
  display: grid; place-items: center; box-shadow: var(--shadow);
}
.icon-btn:active { transform: scale(.94); }

.sync-badge {
  font-size: 26px; line-height: 1; width: 20px; text-align: center;
  color: #16a34a; transition: color .3s;
}
.sync-badge.pending { color: #f59e0b; }
.sync-badge.offline { color: #dc2626; }

.term-bar { margin-top: 10px; }
.term-select {
  width: 100%; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); font-size: 15px; font-weight: 600;
  appearance: none; box-shadow: var(--shadow);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b7180' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center;
}

/* ---------- Main ---------- */
.main {
  padding: 16px;
  max-width: 640px;
  margin: 0 auto;
}

/* Overview card */
.overview-card {
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  color: #fff;
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-lg);
  margin-bottom: 22px;
}
.overview-label { font-size: 13px; opacity: .85; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.overview-value { font-size: 52px; font-weight: 800; line-height: 1.05; margin: 4px 0 2px; letter-spacing: -.03em; }
.overview-meta { font-size: 13px; opacity: .9; }
.overview-bar { height: 6px; border-radius: 4px; background: rgba(255,255,255,.25); margin-top: 14px; overflow: hidden; }
.overview-bar-fill { height: 100%; width: 0; border-radius: 4px; background: #fff; transition: width .5s ease; }

.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section-head h2 { font-size: 17px; margin: 0; }
.text-btn { background: none; border: none; color: var(--primary); font-size: 14px; font-weight: 600; padding: 4px; }

/* Subject list */
.subject-list { display: flex; flex-direction: column; gap: 10px; }

.subject-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 14px;
  border-left: 5px solid var(--primary);
}
.subject-card:active { transform: scale(.99); }
.subject-info { flex: 1; min-width: 0; }
.subject-name { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.subject-sub { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.subject-avg {
  display: flex; flex-direction: column; align-items: flex-end; gap: 3px; min-width: 62px;
}
.avg-pill {
  font-size: 20px; font-weight: 800; padding: 3px 10px; border-radius: 10px; color: #fff; letter-spacing: -.01em;
}
.avg-decimal { font-size: 11px; color: var(--text-dim); }
.target-hint { font-size: 11px; font-weight: 600; }
.target-hint.ok { color: #16a34a; }
.target-hint.miss { color: #ea580c; }

/* Upcoming (planned Arbeiten/Tests) */
.upcoming-section { margin-bottom: 22px; }
.upcoming-list { display: flex; flex-direction: column; gap: 8px; }
.upcoming-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--card); border-radius: 14px; box-shadow: var(--shadow);
  border-left: 5px solid var(--primary);
}
.upcoming-item:active { transform: scale(.99); }
.upcoming-main { flex: 1; min-width: 0; }
.upcoming-title { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.upcoming-sub { font-size: 12.5px; color: var(--text-dim); margin-top: 2px; }
.upcoming-when {
  font-size: 12px; font-weight: 700; padding: 5px 9px; border-radius: 9px;
  white-space: nowrap; text-align: center; background: var(--primary-dim); color: var(--primary);
}
.upcoming-when.soon { background: #fef3c7; color: #b45309; }
.upcoming-when.today { background: #dcfce7; color: #15803d; }
.upcoming-when.over { background: #fee2e2; color: #b91c1c; }
@media (prefers-color-scheme: dark) {
  .upcoming-when.soon { background: #3a2f14; color: #fbbf24; }
  .upcoming-when.today { background: #16351f; color: #4ade80; }
  .upcoming-when.over { background: #3a1a1a; color: #f87171; }
}

/* Charts */
.chart-heading { font-size: 15px; margin: 6px 0 12px; }
.chart-bars { display: flex; flex-direction: column; gap: 10px; margin-bottom: 24px; }
.chart-bar-row { display: flex; align-items: center; gap: 10px; }
.chart-bar-label { width: 84px; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex-shrink: 0; }
.chart-bar-track { flex: 1; height: 22px; background: var(--card-2); border-radius: 7px; overflow: hidden; }
.chart-bar-fill { height: 100%; border-radius: 7px; transition: width .5s ease; min-width: 4px; }
.chart-bar-val { width: 34px; text-align: right; font-size: 13px; font-weight: 800; flex-shrink: 0; }
.chart-trend { width: 100%; overflow-x: auto; }
.chart-trend svg { display: block; width: 100%; height: auto; }
.chart-empty { text-align: center; color: var(--text-dim); padding: 20px 0; font-size: 14px; }

/* Empty */
.empty-hint { text-align: center; padding: 40px 20px; color: var(--text-dim); }
.empty-hint p { margin-bottom: 16px; }

.footer-space { height: 90px; }

/* FAB */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--safe-bottom) + 18px);
  width: 60px; height: 60px; border-radius: 20px;
  background: var(--primary); color: #fff; border: none;
  font-size: 34px; font-weight: 300; line-height: 1;
  box-shadow: 0 8px 24px rgba(79,70,229,.45);
  z-index: 30;
  display: grid; place-items: center;
}
.fab:active { transform: scale(.92); }

/* ---------- Sheets ---------- */
.backdrop {
  position: fixed; inset: 0; background: rgba(10,12,20,.5);
  z-index: 40; opacity: 0; transition: opacity .25s; backdrop-filter: blur(2px);
}
.backdrop.show { opacity: 1; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: var(--card); z-index: 50;
  border-radius: 24px 24px 0 0;
  padding: 10px 18px calc(var(--safe-bottom) + 22px);
  box-shadow: var(--shadow-lg);
  transform: translateY(100%);
  transition: transform .3s cubic-bezier(.2,.8,.2,1);
  max-height: 92vh; overflow-y: auto;
  max-width: 640px; margin: 0 auto;
}
.sheet.show { transform: translateY(0); }
.sheet-tall { min-height: 60vh; }
.sheet-handle { width: 40px; height: 4px; border-radius: 3px; background: var(--border); margin: 4px auto 14px; }
.sheet-title { font-size: 19px; margin: 0 0 16px; }

.list-action {
  display: block; width: 100%; text-align: left; background: none; border: none;
  padding: 15px 6px; font-size: 16px; color: var(--text); border-bottom: 1px solid var(--border);
}
.list-action:last-child { border-bottom: none; }
.list-action.sheet-close { color: var(--text-dim); text-align: center; margin-top: 6px; }

/* Forms */
.field { display: block; margin-bottom: 16px; }
.field > span { display: block; font-size: 13px; font-weight: 600; color: var(--text-dim); margin-bottom: 7px; }
.field em { font-style: normal; font-weight: 400; opacity: .7; }
.field input[type=text], .field input[type=date], .field select {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--card-2); color: var(--text); font-size: 16px; appearance: none;
}
.field input:focus, .field select:focus { outline: 2px solid var(--primary); border-color: transparent; }

.sheet-buttons { display: flex; gap: 10px; margin-top: 22px; align-items: center; }
.sheet-buttons .primary-btn { margin-left: auto; }
.primary-btn {
  background: var(--primary); color: #fff; border: none; padding: 13px 22px;
  border-radius: 12px; font-size: 15px; font-weight: 700;
}
.primary-btn:active { transform: scale(.97); }
.ghost-btn {
  background: var(--card-2); color: var(--text); border: 1px solid var(--border);
  padding: 13px 18px; border-radius: 12px; font-size: 15px; font-weight: 600;
}
.ghost-btn.danger, .ghost-btn#subject-delete, .ghost-btn#grade-delete { color: var(--danger); border-color: transparent; background: transparent; }

/* Grade picker */
.grade-picker { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.grade-col { display: flex; flex-direction: column; gap: 5px; }
.grade-btn {
  border: 1px solid var(--border); background: var(--card-2); color: var(--text);
  padding: 9px 0; border-radius: 10px; font-size: 15px; font-weight: 700; text-align: center;
}
.grade-btn.base { font-size: 17px; }
.grade-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.grade-preview { margin-top: 10px; font-size: 14px; color: var(--text-dim); font-weight: 600; }
.grade-clear-btn { display: inline-block; margin-top: 8px; padding: 4px 0; background: none; border: none; }

/* Segmented */
.seg { display: flex; gap: 6px; background: var(--card-2); padding: 4px; border-radius: 12px; }
.seg-item {
  flex: 1; border: none; background: transparent; color: var(--text-dim);
  padding: 10px 0; border-radius: 9px; font-size: 14px; font-weight: 600;
}
.seg-item.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* Color picker */
.color-picker { display: flex; gap: 10px; flex-wrap: wrap; }
.color-dot {
  width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent; cursor: pointer;
}
.color-dot.active { border-color: var(--text); transform: scale(1.08); }

/* Detail grade list */
.subject-detail-head { display: flex; align-items: center; justify-content: space-between; }
.subject-detail-avg { font-size: 14px; color: var(--text-dim); margin: -6px 0 14px; font-weight: 600; }
.detail-grade-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 4px; }
.detail-grade {
  display: flex; align-items: center; gap: 12px; padding: 11px 12px;
  background: var(--card-2); border-radius: 12px;
}
.detail-grade-badge {
  width: 42px; height: 42px; border-radius: 11px; color: #fff; font-weight: 800; font-size: 17px;
  display: grid; place-items: center; flex-shrink: 0;
}
.detail-grade-badge.pending { background: transparent !important; border: 2px dashed var(--border); color: var(--text-dim); }
.detail-grade.pending { border: 1px dashed var(--border); }
.detail-grade-main { flex: 1; min-width: 0; }
.detail-grade-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.detail-grade-meta { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.detail-grade-edit { background: none; border: none; color: var(--text-dim); font-size: 18px; padding: 6px; }
.detail-empty { text-align: center; color: var(--text-dim); padding: 24px 0; }

/* Manage lists */
.manage-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.manage-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--card-2); border-radius: 12px;
}
.manage-item .m-name { flex: 1; font-weight: 600; }
.manage-item .m-dot { width: 16px; height: 16px; border-radius: 50%; flex-shrink: 0; }
.manage-item .m-btn { background: none; border: none; color: var(--text-dim); font-size: 17px; padding: 6px; }
.manage-item .m-btn.del { color: var(--danger); }
.manage-item.active-term { outline: 2px solid var(--primary); }

.inline-add { display: flex; gap: 8px; margin-bottom: 6px; }
.inline-add input { flex: 1; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--card-2); color: var(--text); font-size: 15px; }
.inline-add .primary-btn { padding: 12px 18px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 90px); transform: translateX(-50%) translateY(20px);
  background: #1a1c24; color: #fff; padding: 11px 18px; border-radius: 12px; font-size: 14px; font-weight: 500;
  z-index: 100; box-shadow: var(--shadow-lg); opacity: 0; transition: opacity .25s, transform .25s; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (prefers-color-scheme: dark) { .toast { background: #2a2e3a; } }
