/* Workbenchy – Design „Arbeitsbank Dunkel" (WCAG 2.1 AA)
   Dunkles Slate-Design mit klaren Kanten (kaum Rundungen, sichtbare
   1-px-Grenzen). Alle Farben laufen über Design-Tokens: Weitere Themes
   (hell, Kontrast) überschreiben später nur die :root-Werte bzw.
   setzen z. B. [data-theme="hell"]. */
:root {
  color-scheme: dark;
  --bg: #0b1220;
  --bg-deep: #0a0f1c;
  --panel: #0f172a;
  --panel-raised: #16233b;
  --panel-hover: #1c2b4a;
  --border: #475569;
  --border-strong: #64748b;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-hover: #7dd3fc;
  --accent-contrast: #082f49;
  --accent-soft: #0c2d44;
  --ok: #4ade80;
  --warn: #fbbf24;
  --warn-soft: #2a2310;
  --error: #f87171;
  --error-bg: #2d1518;
  --error-text: #fecaca;
  --zebra: #0d1729;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* ---------- Basis ---------- */
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.55;
  margin: 0;
  padding: 1.25rem;
  background: var(--bg);
  color: var(--text);
}

::selection {
  background: var(--accent);
  color: var(--accent-contrast);
}

h1 {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0 0 0.25rem;
  display: flex;
  align-items: center;
}

/* Brand-Mark: kleines „DB"-Schild als CSS-only-Ornament */
h2 {
  font-size: 1.35rem;
  font-weight: 700;
}

h3 {
  font-size: 1.05rem;
  font-weight: 700;
}

a {
  color: var(--accent);
}

/* Sichtbarer Fokus: 2 px, hoher Kontrast */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Kopfzeile ---------- */
.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.brand-logo {
  width: 80px;
  height: 80px;
  flex-shrink: 0;
}

/* Titel + Untertitel als enge Spalte rechts neben dem Logo –
   der Header bleibt so hoch wie das Logo */
.brand-text {
  display: flex;
  flex-direction: column;
}

.brand-text p {
  margin: 0.1rem 0 0;
  color: var(--muted);
}

address {
  font-style: normal;
}

address p {
  margin-bottom: 0.5rem;
}

/* ---------- Navigation ---------- */
nav {
  display: flex;
  gap: 0.25rem;
  flex-wrap: wrap;
  margin: 1.1rem 0 1.25rem;
}

nav button {
  background: var(--panel);
  border: 1px solid var(--border);
  border-top: 3px solid transparent;
  border-radius: 0;
  color: var(--text);
  padding: 0.55rem 1.1rem;
  font-weight: 600;
  cursor: pointer;
}

nav button:hover:not([disabled]) {
  background: var(--panel-hover);
  border-color: var(--border-strong);
  border-top-color: var(--border-strong);
}

nav button[disabled] {
  color: var(--muted);
  background: var(--bg-deep);
  cursor: not-allowed;
}

nav button[aria-current="page"] {
  background: var(--panel-raised);
  border-top: 3px solid var(--accent);
  color: var(--accent);
}

/* ---------- Ansichten ---------- */
main > section {
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 1.25rem 1.5rem 1.5rem;
}

main > section > h2 {
  margin-top: 0;
}

main > section > h2:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Buttons ---------- */
button {
  font: inherit;
  font-size: 0.95rem;
  color: var(--text);
  background: var(--panel-raised);
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 0.45rem 0.95rem;
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: var(--panel-hover);
  border-color: var(--border-strong);
}

button:disabled {
  color: var(--muted);
  background: var(--bg-deep);
  cursor: not-allowed;
}

/* Primäraktionen: cyan mit dunklem Text */
#btn-run,
#btn-db-create,
button[type="submit"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 700;
}

#btn-run:hover:not(:disabled),
#btn-db-create:hover:not(:disabled),
button[type="submit"]:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* ---------- Formulare und Eingaben ---------- */
form {
  display: grid;
  gap: 0.65rem;
  max-width: 26rem;
}

form label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
}

input,
select,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: 0;
  padding: 0.5rem 0.65rem;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}

textarea {
  resize: vertical;
}

input::placeholder {
  color: var(--muted);
}

input[aria-invalid="true"] {
  border-color: var(--error);
}

select {
  padding: 0.4rem 0.6rem;
  cursor: pointer;
}

/* ---------- Tabellen ---------- */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  border: 1px solid var(--border-strong);
}

caption {
  caption-side: top;
  text-align: left;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.85rem;
  padding-bottom: 0.5rem;
}

th,
td {
  border: 1px solid var(--border);
  padding: 0.45rem 0.7rem;
  text-align: left;
  vertical-align: top;
  font-size: 0.95rem;
}

td {
  overflow-wrap: break-word;
}

th {
  background: var(--panel-raised);
  font-weight: 700;
}

/* Schlüssel-Markierung (PK/FK) und Typ-Anzeige in den Spaltenköpfen */
.key-badge {
  display: inline-block;
  margin-left: 0.45rem;
  padding: 0 0.35rem;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1.4;
  letter-spacing: 0.03em;
  border: 1px solid;
  vertical-align: baseline;
}

.key-pk {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-hover);
}

.key-fk {
  background: var(--warn-soft);
  border-color: var(--warn);
  color: var(--warn);
}

.col-type {
  display: block;
  font-family: var(--mono);
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--muted);
}

/* Schalter „Typen anzeigen“ über den Tabellen */
.toggle-types {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0.5rem 0 0;
  font-weight: 600;
  cursor: pointer;
}

.toggle-types input {
  margin: 0;
}

tbody tr:nth-child(even) td {
  background: var(--zebra);
}

tbody tr:hover td {
  background: rgba(56, 189, 248, 0.08);
}

/* ---------- Code ---------- */
code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--bg-deep);
  padding: 0.12em 0.35em;
  border-radius: 2px;
}

pre {
  background: var(--bg-deep);
  border: 1px solid var(--border);
  padding: 0.9rem 1rem;
  overflow-x: auto;
  line-height: 1.5;
  margin: 1rem 0;
}

pre code {
  background: none;
  padding: 0;
}

/* ---------- Dialoge ---------- */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 0;
  padding: 1.25rem 1.5rem;
  box-shadow: 0 10px 40px rgba(2, 6, 23, 0.65);
  max-width: min(40rem, calc(100vw - 3rem));
}

dialog::backdrop {
  background: rgba(2, 6, 23, 0.65);
}

dialog h3 {
  margin-top: 0;
}

dialog button {
  margin: 0.75rem 0.5rem 0 0;
}

/* ---------- Dialog „Neue Tabelle“ ---------- */
#table-dialog fieldset {
  border: 1px solid var(--border);
  margin: 0.75rem 0;
  padding: 0.25rem 0.9rem 0.75rem;
}

#table-dialog legend {
  font-weight: 600;
}

/* Eine Spalten-Zeile: Label/Steuerung-Grid, Nummern per aria-label
 * („Spalte 1 …“); das Längenfeld ist nur bei „Kurzer Text“ sichtbar. */
.nt-col {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 0.35rem 0.75rem;
  align-items: center;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--border);
}

.nt-col:last-of-type {
  border-bottom: none;
}

.nt-col input[type="text"],
.nt-col input[type="number"],
.nt-col select {
  width: 100%;
  max-width: 15rem;
}

.nt-pk {
  grid-column: 2;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.nt-pk input {
  margin: 0;
}

.nt-col-remove {
  grid-column: 2;
  justify-self: start;
  margin: 0;
}

.nt-col .field-error {
  grid-column: 1 / -1;
  margin: 0.2rem 0 0;
}

/* ---------- Ansicht „Datenbanken" ---------- */
#db-create-form {
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "lbl lbl"
    "input btn"
    "err err";
  max-width: 34rem;
  margin-bottom: 1.5rem;
}

#db-create-form label {
  grid-area: lbl;
}

#db-create-form input {
  grid-area: input;
  width: 100%;
}

#db-create-form button {
  grid-area: btn;
  align-self: center;
}

#db-create-form p[role="alert"] {
  grid-area: err;
  margin: 0;
}

p[role="alert"] {
  color: var(--error);
  font-size: 0.9rem;
}

#db-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.75rem;
}

#db-list li {
  background: var(--panel-raised);
  border: 1px solid var(--border);
  padding: 0.9rem 1.1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 1rem;
}

#db-list li strong {
  font-size: 1.05rem;
}

#db-list li span {
  color: var(--muted);
  font-size: 0.88rem;
  margin-right: auto;
}

#db-list li button {
  margin: 0;
}

/* Leere Liste: kein Karten-Look, nur Hinweis */
#db-list > li:not(.db-item) {
  background: none;
  border: none;
  padding: 0;
  color: var(--muted);
}

/* ---------- Ansicht „SQL-Konsole" ---------- */
#sql-input {
  width: 100%;
  box-sizing: border-box;
  min-height: 120px;
  font: 0.95rem/1.55 var(--mono);
  background: var(--bg-deep);
}

#sql-input-hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0.35rem 0 0.75rem;
}

#btn-import-sql {
  margin-left: 0.5rem;
}

#sql-result {
  margin-top: 1.5rem;
}

#sql-history-h {
  margin: 1.75rem 0 0.5rem;
}

#sql-history {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.4rem;
  max-width: 60rem;
}

#sql-history button {
  display: block;
  width: 100%;
  text-align: left;
  font: 0.85rem/1.45 var(--mono);
  padding: 0.45rem 0.7rem;
  background: var(--bg-deep);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#sql-help {
  margin-top: 2rem;
  border: 1px solid var(--border);
  background: var(--bg-deep);
  padding: 0.9rem 1.1rem;
  max-width: 65rem;
}

#sql-help summary {
  font-weight: 700;
  cursor: pointer;
}

#sql-help p {
  font-size: 0.9rem;
}

/* Hinweise (z. B. entfernte MySQL-Klauseln) */
.note {
  color: var(--muted);
  font-size: 0.9rem;
  border-left: 3px solid var(--warn);
  padding-left: 0.65rem;
  margin: 0.6rem 0;
}

/* Fehler-Block (SQL-Ergebnis) */
.error-block {
  background: var(--error-bg);
  border: 1px solid var(--error);
  border-left-width: 4px;
  padding: 0.8rem 1rem;
  margin: 1rem 0;
  color: var(--error-text);
}

.error-block h3 {
  margin: 0 0 0.25rem;
  color: var(--error-text);
}

.error-block p {
  margin: 0;
}

/* DDL-Ergebnis-Toast: Fixed-Position außerhalb des Flusses (schiebt die
   Panels nicht herunter). Schließbar (Schließen-Button/Esc) und mit
   Auto-Dismiss – Verhalten in modell-view.js (nur Erfolg: 10 s, pausiert
   bei Hover; Fehler bleiben sichtbar). */
#modell-ddl-ergebnis {
  position: fixed;
  right: 1rem;
  bottom: 3.5rem; /* über Footer/Statuszeile */
  z-index: 60;
  box-sizing: border-box;
  width: min(32rem, calc(100vw - 2rem));
  max-height: 60vh;
  overflow: auto;
  padding: 0.75rem 1rem;
  background: var(--panel-raised);
  border: 1px solid var(--border-strong);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.45);
}

#modell-ddl-ergebnis-schliessen {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  font-size: 0.8rem;
  padding: 0.15rem 0.5rem;
}

#modell-ddl-ergebnis .modell-ergebnis-inhalt {
  padding-right: 2.75rem;
}

/* Feld-Fehler in Formularen */
.field-error {
  color: var(--error);
  font-size: 0.85rem;
  margin: 0.2rem 0 0.6rem;
}

/* ---------- Ansicht „Tabellen und Daten" ---------- */
#view-tables h3 {
  margin: 1.75rem 0 0.25rem;
}

#view-tables h3:first-child {
  margin-top: 0;
}

/* Tabellen-Überschrift: Mülleimer hinter dem Namen (Tabelle löschen) */
h3 button.tabelle-loeschen {
  margin-left: 0.5rem;
  padding: 0.05rem 0.35rem;
  font-size: 0.85em;
  line-height: 1.2;
}

/* Zeilen-Aktionszelle: Stift und Mülleimer je Datenzeile (Icon-only) */
td.zeilen-aktionen {
  white-space: nowrap;
  text-align: right;
}

td.zeilen-aktionen button {
  padding: 0.15rem 0.4rem;
  margin: 0 0.1rem;
  font-size: 0.95rem;
  line-height: 1.3;
}

.form-region {
  margin: 1.25rem 0 2rem;
}

/* Seiten-Steuerung */
#view-tables [role="group"]:not(.form-region) {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin: 0.75rem 0;
}

#view-tables [role="group"]:not(.form-region) p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ---------- Ansicht „Modell" (#view-modell) ---------- */
#view-modell .modell-panels {
  display: grid;
  grid-template-columns: minmax(300px, 42%) 1fr;
  gap: 1rem;
}

@media (max-width: 800px) {
  #view-modell .modell-panels {
    grid-template-columns: 1fr;
  }
}

#view-modell .modell-panel {
  border: 1px solid var(--border);
  background: var(--bg-deep);
  padding: 1rem;
}

/* Toolbar */
#view-modell #modell-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.9rem;
}

#view-modell #modell-toolbar label {
  font-weight: 600;
}

/* Codefenster (Überschrift „Diagramm-Code" folgt dem h3-Panel-Stil) */
#view-modell #modell-code {
  display: block;
  width: 100%;
  box-sizing: border-box;
  min-height: 340px;
  padding: 0.75rem;
  font: 0.9rem/1.55 var(--mono);
  background: var(--bg-deep);
  border: 1px solid var(--border);
  color: var(--text);
}

/* Fehlerleiste */
#view-modell #modell-fehler {
  margin-top: 0.9rem;
  padding: 0.8rem 1rem;
  border: 1px solid var(--error);
  border-left-width: 4px;
  background: var(--error-bg);
  color: var(--error-text);
}

#view-modell #modell-fehler p {
  margin: 0;
  font-weight: 600;
}

#view-modell #modell-fehler ul {
  margin: 0.35rem 0 0;
  padding-left: 1.25rem;
}

/* Panel-Überschriften (h3 unter der h2-Ansicht) */
#view-modell .modell-panel > h3 {
  font-size: 1rem;
  margin: 1rem 0 0.5rem;
}

#view-modell .modell-panel > h3:first-child {
  margin-top: 0;
}

/* SVG-Diagramm: helles „Whiteboard" im dunklen Rahmen */
#view-modell #modell-svg {
  display: block;
  width: 100%;
  height: auto;
  max-height: 70vh;
  border: 1px solid var(--border-strong);
  overflow: auto;
  background: #f8fafc;
}

/* Diagramm-Baum */
#view-modell #modell-baum [role="tree"] {
  background: var(--bg-deep);
  border: 1px solid var(--border);
  padding: 0.5rem 0.75rem;
  max-height: 240px;
  overflow: auto;
}

#view-modell #modell-baum [role="treeitem"] {
  padding: 0.15rem 0.5rem;
  border-radius: 2px;
  font-size: 0.9rem;
  line-height: 1.5;
}

#view-modell #modell-baum [role="treeitem"]:focus {
  background: var(--accent-soft);
}

#view-modell #modell-baum [role="group"] {
  margin: 2px 0 2px 12px;
  padding-left: 10px;
  border-left: 2px solid var(--border);
}

#view-modell #modell-baum .baum-leer {
  padding: 4px 6px;
  font-size: 0.85rem;
  color: var(--muted);
}

/* Textansicht */
#view-modell #modell-textview {
  margin-top: 1rem;
}

#view-modell #modell-textview h3 {
  font-size: 0.9rem;
  font-weight: 700;
  margin: 0.75rem 0 0.4rem;
}

#view-modell #modell-textview h3:first-child {
  margin-top: 0;
}

#view-modell #modell-textview-inhalt table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.9rem;
  border: 1px solid var(--border-strong);
}

#view-modell #modell-textview-inhalt th,
#view-modell #modell-textview-inhalt td {
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 7px 10px;
  text-align: left;
}

#view-modell #modell-textview-inhalt th {
  background: var(--panel-raised);
  font-weight: 600;
}

#view-modell #modell-textview-inhalt tbody tr:last-child td {
  border-bottom: none;
}

#view-modell #modell-textview-inhalt tbody tr:nth-child(even) td {
  background: var(--zebra);
}

/* Ansicht-Schalter: SVG bzw. Textansicht ausblenden
   (data-ansicht liegt auf der Section; JS setzt zusätzlich „hidden") */
#view-modell[data-ansicht="svg"] #modell-textview {
  display: none;
}

#view-modell[data-ansicht="text"] #modell-svg {
  display: none;
}

#view-modell #modell-svg[hidden],
#view-modell #modell-textview[hidden] {
  display: none;
}

/* Hilfe-Dialog */
#modell-dialog-hilfe {
  max-width: 46rem;
}

#modell-dialog-hilfe .modell-hilfe-card {
  max-height: 80vh;
  overflow: auto;
  padding: 0.5rem 1rem 0.75rem;
}

#modell-dialog-hilfe h3 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
}

#modell-dialog-hilfe h4 {
  margin: 1.1rem 0 0.4rem;
  font-size: 0.95rem;
  font-weight: 700;
  padding-top: 0.7rem;
  border-top: 1px solid var(--border);
}

#modell-dialog-hilfe p,
#modell-dialog-hilfe li {
  font-size: 0.9rem;
  line-height: 1.55;
}

#modell-dialog-hilfe ul {
  margin: 0.4rem 0;
  padding-left: 1.25rem;
}

#modell-dialog-hilfe table {
  font-size: 0.85rem;
}

#modell-dialog-hilfe kbd {
  font-family: var(--mono);
  font-size: 0.8em;
  background: var(--panel-raised);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 2px;
  padding: 1px 6px;
}

#modell-hilfe-schliessen {
  margin-top: 1.1rem;
}

/* ---------- Fußzeile und Status ---------- */
footer {
  margin-top: 2rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

#live-status {
  min-height: 1.5rem;
  margin-top: 0;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--muted);
}

footer p {
  margin: 0.5rem 0 0;
  color: var(--muted);
}

/* ---------- PWA-Update-Banner ---------- */
#pwa-aktualisierung {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  background: var(--warn-soft);
  border: 1px solid var(--warn);
  padding: 0.65rem 1rem;
  margin: -1.25rem -1.25rem 1.25rem;
}

#pwa-aktualisierung p {
  margin: 0;
  font-weight: 600;
}

#pwa-aktualisierung button {
  margin: 0;
}

/* hidden muss immer gewinnen – auch gegen ID-Selektoren mit display
   (z. B. #pwa-aktualisierung { display: flex }), sonst bleibt ein per
   hidden ausgeblendetes Element sichtbar. */
[hidden] {
  display: none !important;
}

/* Screenreader-Text (visuell ausgeblendet, z. B. Tabellen-Captions) */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
