:root {
  --bg: #f6f4fb;
  --surface: #ffffff;
  --text: #1e1b2e;
  --muted: #6b6880;
  --line: #e4e0ef;
  --primary: #5b3df5;
  --primary-ink: #ffffff;
  --primary-soft: #ece8ff;
  --danger: #d93a3a;
  --ok: #1f9d55;
  --warn: #e8a317;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(30, 20, 60, .06), 0 6px 20px rgba(30, 20, 60, .06);
  font-family: "Nunito", ui-rounded, system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 16px; line-height: 1.45; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 28px; margin: 0 0 8px; }
h2 { font-size: 20px; margin: 28px 0 12px; }
h3 { font-size: 17px; margin: 0 0 10px; }
code { background: #f1eef8; padding: 1px 6px; border-radius: 6px; font-size: .92em; }
hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.inline { display: inline; }

/* Topbar */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 10; flex-wrap: wrap; }
.brand { font-weight: 800; font-size: 19px; color: var(--text); display: flex; align-items: center; gap: 10px; }
.brand-logo { width: 32px; height: 32px; border-radius: 8px; box-shadow: 0 1px 3px rgba(30, 20, 60, .2); }
.topbar nav { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; }
.topbar nav a { color: var(--text); font-weight: 600; }
button.link { background: none; border: 0; color: var(--muted); font: inherit; cursor: pointer; padding: 0; }

.container { max-width: 1180px; margin: 0 auto; padding: 24px 16px 64px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 8px; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.back { display: inline-block; font-size: 14px; margin-bottom: 4px; }

/* Cards & layout */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.card.narrow { max-width: 460px; }
.two-col { display: grid; grid-template-columns: 1fr 340px; gap: 24px; align-items: start; }
.two-col.wide-left { grid-template-columns: 1fr 360px; }
section.two-col { margin-top: 24px; grid-template-columns: 1fr 1fr; }
@media (max-width: 860px) { .two-col, .two-col.wide-left, section.two-col { grid-template-columns: 1fr; } }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.stack-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

/* Forms */
label { display: block; font-size: 14px; font-weight: 600; margin: 12px 0 4px; }
label.check { display: flex; gap: 8px; align-items: center; font-weight: 500; }
input:not([type=checkbox]):not([type=radio]):not([type=color]):not([type=range]), select, textarea {
  width: 100%; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 10px; font: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
input[type=range] { width: 100%; accent-color: var(--primary); }
form .btn { margin-top: 12px; }
.card form input + input, form.card input + input { margin-top: 8px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 16px; border-radius: 10px; border: 1.5px solid var(--line); background: #fff; color: var(--text); font: inherit; font-weight: 700; cursor: pointer; text-decoration: none !important; white-space: nowrap; }
.btn:hover { border-color: #cfc8e6; background: #faf9fd; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: #4a2ee0; border-color: #4a2ee0; }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: #fdeeee; border-color: #f2c2c2; }
.btn-small { padding: 5px 10px; font-size: 14px; border-radius: 8px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.stack .btn { justify-content: flex-start; white-space: normal; text-align: left; line-height: 1.3; }
.stack form .btn, .project-actions form .btn, td form .btn, .row-actions .btn { margin-top: 0; }

.flash { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; font-weight: 600; }
.flash-ok { background: #e3f6ea; color: #14633a; }
.flash-error { background: #fdecea; color: #a52222; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; background: #eee; color: #555; vertical-align: middle; }
.badge-submitted { background: #fff1d1; color: #8a5a00; }
.badge-returned { background: #fde4e4; color: #a12b2b; }
.badge-done { background: #dff4e6; color: #186b3a; }
.badge-draft { background: #eeeaf8; color: #5b5675; }
.count { display: inline-block; background: var(--primary); color: #fff; border-radius: 999px; padding: 0 9px; font-size: 14px; vertical-align: middle; }

/* Tables & lists */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.table th, .table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { font-size: 13px; color: var(--muted); font-weight: 700; background: #faf9fd; }
.table tr:last-child td { border-bottom: 0; }
.table tr.inactive td { opacity: .5; }
@media (max-width: 700px) { .table { display: block; overflow-x: auto; } }
.mini-thumb { width: 44px; height: 44px; object-fit: contain; border-radius: 6px; background: #f3f1f8; }
.row-actions { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 12px; flex-wrap: wrap; }
.row-actions label { margin: 0; font-weight: 500; display: flex; gap: 6px; align-items: center; }
.filter { margin-bottom: 12px; max-width: 240px; }
.list { list-style: none; padding: 0; margin: 0 0 16px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.list li { padding: 12px 14px; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; }
.list li:last-child { border-bottom: 0; }

/* Dropdown */
.dropdown { position: relative; }
.dropdown summary { list-style: none; }
.dropdown summary::-webkit-details-marker { display: none; }
.dropdown-body { position: absolute; right: 0; top: calc(100% + 6px); width: 300px; background: #fff; border-radius: var(--radius); box-shadow: 0 10px 40px rgba(30, 20, 60, .2); padding: 14px; z-index: 20; }

/* Projects */
.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.project-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; }
.project-thumb { display: flex; align-items: center; justify-content: center; height: 180px; background: repeating-conic-gradient(#f2f0f7 0 25%, #fafafa 0 50%) 0 0 / 16px 16px; padding: 10px; }
.project-thumb img { max-width: 100%; max-height: 100%; box-shadow: 0 2px 8px rgba(0, 0, 0, .15); }
.thumb-empty { width: 100px; height: 140px; box-shadow: 0 2px 8px rgba(0, 0, 0, .15); }
.project-meta { padding: 10px 14px 4px; display: flex; flex-direction: column; gap: 3px; }
.project-meta .muted { font-size: 13px; }
.project-meta .badge { align-self: flex-start; }
.project-meta .note { font-size: 13px; background: #fdf3e6; padding: 6px 8px; border-radius: 8px; }
.project-actions { padding: 8px 14px 14px; display: flex; gap: 6px; flex-wrap: wrap; margin-top: auto; }

/* New project form */
.new-project { padding: 0; background: none; box-shadow: none; }
.new-project > summary { list-style: none; display: inline-flex; margin-bottom: 12px; }
.new-project > summary::-webkit-details-marker { display: none; }
.new-project[open] > summary { margin-bottom: 0; border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
.new-project-form { background: var(--surface); border-radius: 0 var(--radius) var(--radius) var(--radius); box-shadow: var(--shadow); padding: 4px 18px 18px; max-width: 640px; margin-bottom: 20px; }
.format-picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.format-option { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 6px; border: 1.5px solid var(--line); border-radius: 12px; cursor: pointer; font-size: 12px; font-weight: 600; text-align: center; }
.format-option input { display: none; }
.format-option:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.format-shape { display: block; border: 2px solid currentColor; border-radius: 3px; background: #fff; color: #9d97b8; }
.format-A6 { width: 18px; height: 25px; } .format-A5 { width: 24px; height: 34px; } .format-A4 { width: 30px; height: 42px; }
.format-A3 { width: 36px; height: 50px; } .format-Letter { width: 31px; height: 40px; } .format-custom { width: 36px; height: 26px; border-style: dashed; }
.custom-size { display: none; }
.new-project-form:has(input[value=custom]:checked) .custom-size { display: flex; }
.segmented { display: inline-flex; border: 1.5px solid var(--line); border-radius: 10px; overflow: hidden; }
.segmented label { margin: 0; cursor: pointer; }
.segmented input { display: none; }
.segmented span { display: block; padding: 8px 14px; font-weight: 600; }
.segmented input:checked + span { background: var(--primary); color: #fff; }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch { margin: 0; cursor: pointer; }
.swatch input[type=radio] { display: none; }
.swatch span { display: block; width: 34px; height: 34px; border-radius: 50%; background: var(--c); border: 2px solid var(--line); }
.swatch input:checked + span { box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--primary); }
.swatch-custom input { width: 34px; height: 34px; padding: 0; border: 2px solid var(--line); border-radius: 50%; overflow: hidden; cursor: pointer; }

/* Preview */
.preview { padding: 0; overflow: hidden; }
.preview iframe { width: 100%; height: 75vh; border: 0; display: block; }

/* Login */
body.bare { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: radial-gradient(circle at 20% 20%, #ffe3ef, transparent 40%), radial-gradient(circle at 80% 80%, #dfe9ff, transparent 40%), var(--bg); }
.login-wrap { width: 100%; padding: 16px; display: flex; justify-content: center; }
.login-card { background: #fff; border-radius: 22px; box-shadow: var(--shadow); padding: 32px 28px; width: 100%; max-width: 380px; }
.login-card h1 { text-align: center; margin-bottom: 16px; }

/* Library (library.ts) */
.lib-toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; }
/* Bild absolut positioniert: hohe/breite Bilder können die Kachel nie vergrössern (Safari) */
.lib-item { position: relative; min-width: 0; overflow: hidden; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); aspect-ratio: 1; display: flex; align-items: center; justify-content: center; }
.lib-item img, .lib-item canvas { position: absolute; inset: 10px; width: calc(100% - 20px); height: calc(100% - 20px); object-fit: contain; }
.lib-item .spinner { position: static; }
.lib-item .lib-actions { position: absolute; top: 6px; right: 6px; display: flex; gap: 4px; opacity: 0; transition: opacity .15s; }
.lib-item:hover .lib-actions, .lib-item:focus-within .lib-actions { opacity: 1; }
@media (hover: none) { .lib-item .lib-actions { opacity: 1; } }
.lib-item .lib-actions button { width: 30px; height: 30px; border-radius: 8px; border: 0; background: rgba(255, 255, 255, .92); box-shadow: 0 1px 4px rgba(0, 0, 0, .2); cursor: pointer; font-size: 15px; }
.lib-item .lib-status { position: absolute; bottom: 6px; left: 6px; right: 6px; font-size: 11px; text-align: center; background: rgba(255, 255, 255, .9); border-radius: 6px; padding: 2px; }
.lib-drop { border: 2px dashed #cfc8e6; border-radius: 12px; padding: 18px; text-align: center; color: var(--muted); margin-bottom: 12px; }
.lib-drop.over { border-color: var(--primary); background: var(--primary-soft); }
.spinner { width: 22px; height: 22px; border: 3px solid var(--primary-soft); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Modal (Kontur-Editor) */
.modal-backdrop { position: fixed; inset: 0; background: rgba(20, 14, 40, .55); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 12px; }
.modal { background: #fff; border-radius: 18px; width: min(1100px, 100%); max-height: calc(100vh - 24px); display: flex; flex-direction: column; overflow: hidden; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 18px; border-bottom: 1px solid var(--line); gap: 12px; }
.modal-head h3 { margin: 0; }
.modal-body { display: grid; grid-template-columns: 1fr 280px; min-height: 0; flex: 1; }
.modal-canvas-wrap { position: relative; background: repeating-conic-gradient(#e9e6f1 0 25%, #f8f7fb 0 50%) 0 0 / 20px 20px; min-height: 300px; overflow: hidden; touch-action: none; }
.modal-canvas-wrap canvas { position: absolute; top: 0; left: 0; }
.modal-side { padding: 14px 16px; border-left: 1px solid var(--line); overflow-y: auto; }
.modal-side label { margin-top: 14px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); }
.modal-foot .btn { margin-top: 0; }
.tool-row { display: flex; gap: 6px; flex-wrap: wrap; }
.tool-row .btn { margin-top: 0; }
.tool-row .btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.hint { font-size: 13px; color: var(--muted); background: #f7f5fc; border-radius: 8px; padding: 8px 10px; margin-top: 12px; }
@media (max-width: 760px) {
  .modal-body { grid-template-columns: 1fr; grid-template-rows: minmax(280px, 1fr) auto; }
  .modal-side { border-left: 0; border-top: 1px solid var(--line); max-height: 40vh; }
}
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1e1b2e; color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 200; font-weight: 600; box-shadow: var(--shadow); }
.toast.error { background: var(--danger); }

/* Bestätigungsdialog (ui.js / confirmDialog) */
.dialog-backdrop { position: fixed; inset: 0; z-index: 300; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(20, 14, 40, .45); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); animation: dlg-fade .15s ease-out; }
.dialog { background: #fff; border-radius: 20px; width: min(420px, 100%); padding: 26px 24px 20px; text-align: center; box-shadow: 0 24px 60px rgba(20, 14, 40, .3); animation: dlg-pop .18s cubic-bezier(.2, .9, .3, 1.2); }
.dialog-icon { width: 60px; height: 60px; margin: 0 auto 12px; border-radius: 18px; background: var(--primary-soft); display: flex; align-items: center; justify-content: center; font-size: 30px; transform: rotate(-4deg); }
.dialog h3 { font-size: 20px; margin: 0 0 8px; }
.dialog p { margin: 0 0 4px; color: var(--muted); line-height: 1.5; }
.dialog-actions { display: flex; gap: 10px; margin-top: 20px; }
.dialog-actions .btn { flex: 1; margin: 0; padding: 11px 14px; }
.btn-danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger-solid:hover { background: #c22f2f; border-color: #c22f2f; }
.dialog-backdrop.closing { animation: dlg-fade .12s ease-in reverse forwards; }
@keyframes dlg-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes dlg-pop { from { opacity: 0; transform: translateY(8px) scale(.96); } to { opacity: 1; transform: none; } }
@media (max-width: 420px) { .dialog-actions { flex-direction: column-reverse; } }

/* Abstand zwischen aufeinanderfolgenden Blöcken (z.B. Schülerliste → „Hinzufügen“) */
.table + .two-col, .table + .card, .two-col + .two-col { margin-top: 24px; }

.login-logo { display: block; margin: -8px auto 4px; width: 132px; height: 132px; }

.badge-format { background: var(--primary-soft); color: var(--primary); margin-top: 3px; }
.print-format { background: var(--primary-soft); border-radius: 10px; padding: 8px 12px; margin: 0 0 12px; font-size: 14px; }

.helper-card .btn-block { margin: 10px 0 4px; }
.helper-steps { padding-left: 18px; margin: 10px 0 0; color: var(--muted); line-height: 1.5; }
.helper-steps li + li { margin-top: 4px; }

/* Icons (Lucide) */
.icon { width: 1.15em; height: 1.15em; flex-shrink: 0; vertical-align: -0.2em; }
.btn .icon { width: 18px; height: 18px; vertical-align: 0; }
.btn-small .icon { width: 16px; height: 16px; }
.dialog-icon { color: var(--primary); }
.dialog-icon .icon { width: 30px; height: 30px; }
.dialog:has(.btn-danger-solid) .dialog-icon { color: var(--danger); background: #fdeeee; }
.lib-actions button { display: inline-flex; align-items: center; justify-content: center; }
.lib-actions .icon { width: 16px; height: 16px; }
.back { display: inline-flex; align-items: center; gap: 4px; }
.back .icon { width: 16px; height: 16px; }
.icon.spin { animation: spin 1s linear infinite; }
.lib-status { display: flex; align-items: center; justify-content: center; gap: 4px; }
.lib-status .icon { width: 12px; height: 12px; }
.lib-drop strong .icon { vertical-align: -0.2em; }
.tool-row .btn .icon { width: 16px; height: 16px; }
.empty-note .icon, p.muted > .icon { color: var(--primary); }

.project-actions { align-items: center; }
.project-actions form { display: flex; }
.btn-icon { padding: 5px 7px; }
.project-actions { gap: 5px; flex-wrap: nowrap; }
.project-actions .btn-small:not(.btn-icon) { padding: 5px 9px; }
.project-actions form:first-of-type { margin-left: auto; }
