/* StructoLab -- Tablet-first, hell/dunkel nach Systemeinstellung oder Umschalter. Keine externen Ressourcen. */

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1d2433;
  --muted: #5f6b7f;
  --line: #d5dae2;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --hi: #f59e0b;        /* Leitung = 1 */
  --hi-glow: rgba(245, 158, 11, .35);
  --lo: #6b7a90;        /* Leitung = 0 */
  --unk: #b4bcc8;       /* undefiniert */
  --ok: #15803d;
  --ok-bg: #e8f6ec;
  --bad: #dc2626;
  --bad-bg: #fdecec;
  --gate-fill: #ffffff;
  --gate-stroke: #1d2433;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

/* Dunkles Schema: per Systemeinstellung oder per Umschalter (data-theme, theme.ts) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161f;
    --panel: #1b212d;
    --ink: #e6e9ef;
    --muted: #9aa5b7;
    --line: #2f3747;
    --accent: #5b8cff;
    --hi: #fbbf24;
    --hi-glow: rgba(251, 191, 36, .35);
    --lo: #7c8aa1;
    --unk: #4a5468;
    --ok: #4ade80;
    --ok-bg: #173323;
    --bad: #f87171;
    --bad-bg: #3a1c1c;
    --gate-fill: #232a38;
    --gate-stroke: #e6e9ef;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12161f;
  --panel: #1b212d;
  --ink: #e6e9ef;
  --muted: #9aa5b7;
  --line: #2f3747;
  --accent: #5b8cff;
  --hi: #fbbf24;
  --hi-glow: rgba(251, 191, 36, .35);
  --lo: #7c8aa1;
  --unk: #4a5468;
  --ok: #4ade80;
  --ok-bg: #173323;
  --bad: #f87171;
  --bad-bg: #3a1c1c;
  --gate-fill: #232a38;
  --gate-stroke: #e6e9ef;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { font-size: 16px; line-height: 1.4; -webkit-text-size-adjust: 100%; }
#app { padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
       max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
       max-width: 1500px; margin: 0 auto; }
button { font: inherit; color: inherit; }
a { color: var(--accent); }
.loading, .load-error { color: var(--muted); padding: 2rem 0; }

/* ---------------------------------------------------------------- Übersicht */

.overview { max-width: 720px; margin: 0 auto; }
.overview h1 { font-size: 2.2rem; margin: .5rem 0 .25rem; }
.lead { color: var(--muted); margin: 0 0 .5rem; }
.total { font-weight: 600; color: var(--hi); margin: 0 0 1rem; }
.task-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.task-card {
  display: grid; grid-template-columns: 2.4rem 1fr auto auto; align-items: center; gap: 12px;
  padding: 14px 16px; min-height: 64px; border-radius: var(--radius);
  background: var(--panel); border: 1px solid var(--line); color: var(--ink); text-decoration: none;
}
.task-card:active { transform: scale(.99); }
.task-card .num {
  width: 2.2rem; height: 2.2rem; border-radius: 50%; display: grid; place-items: center;
  background: var(--bg); font-weight: 700; color: var(--muted);
}
.task-card.done .num { background: var(--ok-bg); color: var(--ok); }
.task-card .title { font-weight: 600; font-size: 1.1rem; }
.task-card .parts { display: flex; gap: 2px; }
.task-card .parts svg { width: 28px; height: 24px; }
.task-card .stars { color: var(--hi); font-size: 1.2rem; letter-spacing: 1px; white-space: nowrap; }
.privacy { color: var(--muted); font-size: .85rem; margin-top: 1.5rem; }
.link-btn { background: none; border: 0; padding: 4px 0; margin-left: .4em; color: var(--accent);
  text-decoration: underline; cursor: pointer; font-size: inherit; }
.reset-confirm { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: .9rem;
  padding: 10px 12px; border: 1px solid var(--bad); border-radius: 8px; background: var(--bad-bg); }
.reset-confirm[hidden] { display: none; }
.reset-confirm button { padding: 8px 14px; border-radius: 8px; border: 1px solid var(--muted);
  background: var(--bg); cursor: pointer; }
.reset-confirm button.danger { background: var(--bad); border-color: var(--bad); color: #fff; }

/* ---------------------------------------------------------------- Aufgabenkopf */

.task-head { margin-bottom: 10px; }
.task-head .back { display: inline-block; padding: 6px 0; text-decoration: none; font-weight: 600; }
.task-head h1 { font-size: 1.5rem; margin: 2px 0 4px; }
.task-head p { margin: 0; color: var(--muted); max-width: 70ch; }

/* ---------------------------------------------------------------- Werkbank */

.workbench {
  display: grid; gap: 12px;
  grid-template-columns: 124px minmax(0, 1fr) 340px;
  align-items: start;
}


.palette, .side, .canvas-wrap {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
}
.palette { padding: 8px; display: flex; flex-direction: column; gap: 8px; position: sticky; top: 8px;
           transition: background .15s, border-color .15s; }
.palette-title { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em;
                 text-align: center; }
.part {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px 6px; min-height: 84px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); cursor: grab; touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.part svg { width: 84px; height: 40px; }
.part:disabled { opacity: .35; cursor: not-allowed; }
.part-name { font-size: .8rem; font-weight: 600; }
.part .count { position: absolute; top: 4px; right: 6px; font-size: .75rem; color: var(--muted); }
.trash-hint { display: none; font-size: .8rem; text-align: center; color: var(--bad); padding: 6px 2px; }
.palette.trash-hot { background: var(--bad-bg); border-color: var(--bad); }
.palette.trash-hot .trash-hint { display: block; }

.canvas-wrap { overflow: hidden; }
.canvas { display: block; width: 100%; height: auto; touch-action: none; user-select: none;
          -webkit-user-select: none; -webkit-touch-callout: none; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px; border-top: 1px solid var(--line); }
.toolbar .spacer { flex: 1; }
.toolbar button {
  min-height: 44px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); cursor: pointer;
}
.toolbar button:disabled { opacity: .4; cursor: default; }
.toolbar button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
                          font-weight: 700; padding: 0 22px; }

/* ---------------------------------------------------------------- Struktogramm (SVG) */

.blk { cursor: grab; }
.blk .own { fill: var(--gate-fill); stroke: var(--gate-stroke); stroke-width: 2; stroke-linejoin: miter; }
.blk .own.filler { stroke: none; }
.blk .tri { fill: none; stroke: var(--gate-stroke); stroke-width: 2; pointer-events: none; }
.blk-text { font-size: 19px; dominant-baseline: central; fill: var(--ink);
            font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; pointer-events: none; }
.blk-text .kw { fill: var(--accent); font-weight: 700; }
.blk-text.placeholder { fill: var(--muted); font-style: italic; opacity: .7; }
.branch { font-size: 15px; font-weight: 700; fill: var(--muted); dominant-baseline: auto; pointer-events: none; }
.branch.end { text-anchor: end; }
.branch.taken { fill: var(--hi); font-size: 17px; }
.rounds { font-size: 14px; font-weight: 700; fill: var(--hi); text-anchor: end; dominant-baseline: central;
          pointer-events: none; }
.blk.invalid > .own:not(.filler) { stroke: var(--bad); stroke-dasharray: 8 5; }
.blk.selected > .own { fill: color-mix(in srgb, var(--accent) 16%, var(--gate-fill)); }
.blk.error > .own:not(.filler) { stroke: var(--bad); stroke-width: 4; stroke-dasharray: none; }
.blk.error > .own { fill: var(--bad-bg); }
.blk.current > .own { fill: color-mix(in srgb, var(--hi) 40%, var(--gate-fill)); }
.blk.failed > .own { fill: var(--bad-bg); }
.blk.dragging { opacity: .35; }
.sel-outline { fill: none; stroke: var(--accent); stroke-width: 4; pointer-events: none; }
.frame { fill: none; stroke: var(--gate-stroke); stroke-width: 3; pointer-events: none; }

.slot { cursor: pointer; }
.slot-bg { fill: color-mix(in srgb, var(--bg) 60%, var(--gate-fill)); stroke: var(--gate-stroke); stroke-width: 2; }
.slot-text { font-size: 17px; text-anchor: middle; dominant-baseline: central; fill: var(--muted);
             font-style: italic; pointer-events: none; }
.slot.selected .slot-bg { fill: color-mix(in srgb, var(--accent) 16%, var(--gate-fill)); }
.slot.selected .slot-text { fill: var(--accent); font-style: normal; font-weight: 700; }
.drop-line { stroke: var(--accent); stroke-width: 7; stroke-linecap: round; pointer-events: none;
             filter: drop-shadow(0 0 4px color-mix(in srgb, var(--accent) 60%, transparent)); }
.drop-slot { fill: color-mix(in srgb, var(--accent) 20%, transparent); stroke: var(--accent); stroke-width: 3;
             stroke-dasharray: 8 5; pointer-events: none; }

.part .ns-icon { width: 84px; height: 40px; }
.ns-icon rect, .ns-icon path { fill: var(--gate-fill); stroke: var(--gate-stroke); stroke-width: 2; }

.pseudo { margin: 0; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--bg); overflow-x: auto;
          font: 16px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.pseudo[hidden] { display: none; }
.toolbar button.active { background: color-mix(in srgb, var(--accent) 16%, var(--bg)); border-color: var(--accent); }

/* ---------------------------------------------------------------- Block-Inhalt bearbeiten */

.edit-bar { border-top: 1px solid var(--line); padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.edit-bar[hidden] { display: none; }
.edit-row { display: flex; align-items: center; gap: 10px; }
.edit-kw { font: 700 18px ui-monospace, "SF Mono", Menlo, Consolas, monospace; color: var(--accent); }
.edit-type { font-size: .8rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
             min-width: 7.5em; }
.edit-row input { flex: 1; min-width: 0; min-height: 44px; padding: 0 12px; border-radius: 10px; border: 2px solid var(--line);
  background: var(--bg); color: var(--ink); font: 18px ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.edit-row input:focus { outline: none; border-color: var(--accent); }
.keys { display: flex; flex-wrap: wrap; gap: 4px; }
.keys button { min-width: 40px; min-height: 38px; padding: 0 8px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); cursor: pointer; font: 16px ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.edit-msg { margin: 0; font-size: .85rem; color: var(--muted); }
.edit-msg.ok { color: var(--ok); }
.edit-msg.bad { color: var(--bad); }

/* ---------------------------------------------------------------- Seitenleiste */

.side { padding: 12px; display: flex; flex-direction: column; gap: 14px; }
.side h2 { font-size: 1rem; margin: 0 0 6px; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.side h2 small { font-weight: 500; color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.muted { color: var(--muted); }
.case-table, .vars { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.case-table th, .case-table td, .vars th, .vars td { text-align: center; padding: 6px 4px; }
.case-table thead th, .vars thead th { font-size: .8rem; color: var(--muted); font-weight: 600;
                                       border-bottom: 2px solid var(--line); }
.case-table tbody tr { cursor: pointer; border-bottom: 1px solid var(--line); }
.case-table tbody td { height: 40px; }
.case-table .num { color: var(--muted); width: 2em; }
.case-table .soll { background: color-mix(in srgb, var(--bg) 70%, transparent); }
.case-table .ist { font-weight: 700; }
.case-table tr.current { outline: 3px solid var(--accent); outline-offset: -3px; }
.case-table tr.ok .mark { color: var(--ok); }
.case-table tr.bad { background: var(--bad-bg); }
.case-table tr.bad .mark, .case-table tr.bad .ist { color: var(--bad); }
.case-table .why { font-weight: 800; }

.stepper p { margin: 6px 0; }
.stepper p.ok { color: var(--ok); }
.stepper p.bad { color: var(--bad); }
.stepper .todo { margin: 4px 0 0; padding-left: 1.2em; color: var(--bad); font-size: .9rem; }
.run-buttons { display: flex; gap: 6px; }
.run-buttons button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px;
  min-width: 48px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); cursor: pointer; }
.run-buttons button[data-run="step"] { flex: 1; background: var(--accent); color: var(--accent-ink);
                                       border-color: var(--accent); font-weight: 700; }
.run-buttons button:disabled { opacity: .4; cursor: default; }
.vars { margin-top: 6px; }
.vars td { font-family: ui-monospace, Menlo, monospace; border-bottom: 1px solid var(--line); }
.vars tr.changed td { background: color-mix(in srgb, var(--hi) 25%, transparent); font-weight: 700; }
.console { margin-top: 8px; }
.console small { color: var(--muted); }
.console pre { margin: 2px 0 0; padding: 8px 10px; border-radius: 8px; background: var(--bg); white-space: pre-wrap;
               font: 15px ui-monospace, Menlo, monospace; }

.feedback { border-radius: 10px; padding: 10px 12px; }
.feedback ul { margin: 6px 0 0; padding-left: 1.2em; }
.feedback p { margin: 6px 0 0; }
.feedback.success { background: var(--ok-bg); color: var(--ok); }
.feedback.error { background: var(--bad-bg); color: var(--bad); }
.feedback .stars { font-size: 2rem; color: var(--hi); letter-spacing: 3px; }
.hints { background: var(--bg); border-radius: 10px; padding: 10px 12px; }
.hints ul { margin: 6px 0 0; padding-left: 1.2em; }

/* ---------------------------------------------------------------- Schmale Bildschirme */

@media (max-width: 1100px) {
  .workbench { grid-template-columns: 112px minmax(0, 1fr); }
  .side { grid-column: 1 / -1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
  .side .feedback { grid-column: 1 / -1; }
}

@media (max-width: 700px) {
  .workbench { grid-template-columns: minmax(0, 1fr); }
  .palette { position: static; flex-direction: row; overflow-x: auto; }
  .palette-title { display: none; }
  .part { min-width: 96px; flex: none; }
  .side { display: flex; }
  .toolbar button { padding: 0 10px; }
  .edit-type { display: none; }
}

/* ---------------------------------------------------------------- Hörsaal-Barometer */

.barometer { margin: 2px 0 6px !important; color: var(--hi) !important; font-weight: 600; }

/* ---------------------------------------------------------------- Kopfzeile mit Logo */

:root { --logo: #2a2068; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --logo: #c9c3f2; } }
:root[data-theme="dark"] { --logo: #c9c3f2; }

.site-header {
  display: flex; align-items: center; gap: 12px;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 8px
           max(16px, env(safe-area-inset-left));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.site-brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink);
              max-width: 1500px; width: 100%; margin: 0 auto; }
.theme-toggle { flex: none; display: grid; place-items: center; width: 44px; height: 44px;
  border: 1px solid var(--line); border-radius: 50%; background: var(--bg); color: var(--ink); cursor: pointer; }
.theme-toggle .icon { width: 22px; height: 22px; }
.hs-logo { height: 30px; width: auto; color: var(--logo); flex: none; }
.site-org { font-weight: 600; color: var(--logo); }
.site-name { font-weight: 800; padding-left: 10px; border-left: 2px solid var(--line); }
#app { padding-top: 12px; }

@media (max-width: 500px) {
  .site-org { display: none; }
  .site-name { padding-left: 0; border-left: 0; }
}

/* ---------------------------------------------------------------- Symbole */

.icon { width: 1.15em; height: 1.15em; vertical-align: -0.2em; flex: none; }
.toolbar button { display: inline-flex; align-items: center; gap: 6px; }

/* ---------------------------------------------------------------- Phase 4: Level, Badges, Effekte */

.overview h2 { font-size: 1.2rem; margin: 28px 0 10px; display: flex; gap: 10px; align-items: baseline; }
.overview h2 .muted { color: var(--muted); font-size: .9rem; font-weight: 500; }
.progress { display: flex; align-items: center; gap: 12px; margin: 0 0 16px; font-weight: 700; color: var(--hi); }
.progress .bar { flex: 1; height: 12px; border-radius: 999px; background: var(--line); overflow: hidden; }
.progress .bar div { height: 100%; background: var(--hi); border-radius: 999px; transition: width .6s ease; }

.level-path .task-card.done .num { background: var(--ok-bg); color: var(--ok); }
.level-path .task-card.next { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 35%, transparent); }
.level-path .task-card.next .num { background: var(--accent); color: var(--accent-ink); }
.level-path .task-card .title { display: flex; flex-direction: column; gap: 2px; }
.level-path .tag { align-self: flex-start; font-size: .75rem; font-weight: 700; color: var(--accent-ink);
                   background: var(--accent); border-radius: 999px; padding: 1px 8px; }

.badge-shelf { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px;
               grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.badge { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: var(--radius);
         background: var(--panel); border: 1px solid var(--line); opacity: .45; filter: grayscale(1); }
.badge.earned { opacity: 1; filter: none; }
.badge-icon { flex: none; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
              font-weight: 800; font-size: 1.1rem; color: #1d2433; background: var(--hi);
              box-shadow: inset 0 0 0 3px color-mix(in srgb, #fff 45%, transparent); }
.badge-text { display: flex; flex-direction: column; }
.badge-text small { color: var(--muted); }

.next-task { display: inline-flex; align-items: center; gap: 6px; margin-top: 10px; padding: 10px 14px;
             border-radius: 10px; background: var(--ok); color: var(--panel); font-weight: 700; text-decoration: none; }

.stars.pop { animation: pop .6s cubic-bezier(.2, 1.6, .4, 1); transform-origin: left center; }
@keyframes pop { 0% { transform: scale(.3); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }


.toast { position: fixed; left: 50%; transform: translateX(-50%); z-index: 50; display: flex; gap: 12px;
         align-items: center; padding: 10px 16px 10px 10px; border-radius: 999px; background: var(--panel);
         border: 1px solid var(--line); box-shadow: 0 8px 30px rgba(0, 0, 0, .25);
         animation: toast-in .4s ease; transition: opacity .5s, transform .5s; }
.toast small { display: block; color: var(--muted); font-size: .75rem; }
.toast.leave { opacity: 0; transform: translateX(-50%) translateY(20px); }
@keyframes toast-in { from { opacity: 0; transform: translateX(-50%) translateY(20px); } }

.confetti { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 40; }

@media (prefers-reduced-motion: reduce) {
  .stars.pop, .toast { animation: none; }
}

/* Werkzeugleiste: Rückgängig/Löschen nur als Symbol, damit „Prüfen“ in der Zeile bleibt */
@media (max-width: 1400px) {
  .toolbar button[data-act="undo"] span, .toolbar button[data-act="delete"] span { display: none; }
}
