:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --bg: #f8fafc;
    --surface: #ffffff;
    --text: #1e293b;
    --muted: #64748b;
    --border: #e2e8f0;
    --error: #dc2626;
    --error-bg: #fee2e2;
    --success: #16a34a;
    --success-bg: #dcfce7;
    --info: #2563eb;
    --info-bg: #dbeafe;
}

* { box-sizing: border-box; }

body {
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

/* Header-Layout: Titel links, Admin-Menü + Sprachumschalter rechts in einer
   Zeile. Das CSS-Grid übernimmt die Platzierung über die DOM-Ordnnung –
   so sind Sprachumschalter und Admin-Buttons garantiert auf derselben
   Höhe (keine "Treppe" durch Margin-Tops). */
.header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 1.5rem 1rem;
    position: sticky;
    top: 0;
    z-index: 10;
    display: grid;
    grid-template-columns: 1fr auto auto;
    column-gap: 1rem;
    row-gap: 0.75rem;
    align-items: center;
}
.header h1,
.header .subtitle {
    grid-column: 1;
    grid-row: 1;
    align-self: start;
}
.header .subtitle { grid-row: 2; }
.header .admin-nav {
    grid-column: 2;
    grid-row: 1;
    align-self: center;
    justify-self: end;
}
.header .lang-switch {
    grid-column: 3;
    grid-row: 1;
    align-self: center;
}

.header h1 { margin: 0; font-size: 1.6rem; }
.subtitle { margin: 0.25rem 0 0; color: var(--muted); }

/* Sprachumschalter: rechts im Header, in derselben Zeile wie das Admin-Menü.
   Dropdown ohne JS: versteckter Checkbox + <label> + absolute Positionierung.
   Damit bleibt das i18n-Routing (/lang/de, /lang/en) als normaler Link. */
.lang-switch {
    position: relative;
    display: inline-block;
    align-self: center;
}
.lang-switch input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
    margin: 0;
    padding: 0;
    border: none;
}
.lang-switch label {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    /* exakt gleiche Box wie .admin-nav .nav-link (Padding + Font + Line-Height),
       damit Sprachumschalter und Admin-Menü-Buttons identisch hoch sind */
    padding: 0.4rem 1rem;
    cursor: pointer;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.95rem;
    line-height: 1.5;
    background: var(--surface);
    transition: background 0.15s;
    user-select: none;
}
.lang-switch label::after {
    content: "▾";
    font-size: 0.75em;
    color: var(--muted);
}
.lang-switch label:hover { background: var(--bg); }
.lang-switch input:focus-visible + label {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.lang-switch ul {
    position: absolute;
    top: calc(100% + 0.25rem);
    right: 0;
    min-width: 100%;
    margin: 0;
    padding: 0.25rem;
    list-style: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
    z-index: 20;
    display: none;
}
.lang-switch input:checked ~ ul { display: block; }
.lang-switch input:checked + label { background: var(--bg); }
.lang-switch ul a {
    display: block;
    padding: 0.35rem 0.75rem;
    text-decoration: none;
    color: var(--text);
    border-radius: 4px;
    font-size: 0.85rem;
    transition: background 0.15s;
}
.lang-switch ul a:hover { background: var(--bg); }
.lang-switch ul a.active {
    background: var(--primary);
    color: white;
}

/* Admin-Menü in der Headerzeile (wie auf den öffentlichen Seiten oben) */
.admin-nav {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    justify-self: end;
}
.admin-nav .nav-link {
    display: inline-flex;
    align-items: center;
    padding: 0.4rem 1rem;
    line-height: 1.5; /* einheitliche Höhe wie .btn-Buttons */
    font-size: 0.95rem;
    text-decoration: none;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    transition: background 0.15s, color 0.15s;
}
.admin-nav .nav-link:hover { background: var(--bg); }
.admin-nav .nav-link.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}
.admin-nav .nav-link-danger { color: var(--error); border-color: var(--error); }
.admin-nav .nav-link-danger:hover { background: var(--error-bg); }

main {
    max-width: 900px;
    margin: 2rem auto;
    padding: 0 1rem;
}

.footer {
    text-align: center;
    color: var(--muted);
    font-size: 0.85rem;
    padding: 2rem 0;
}

/* Form */
.form {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1.5rem;
}
.form h2 { margin-top: 0; }

.row {
    display: flex;
    gap: 1rem;
}
.row .field { flex: 1; }

.field { margin-bottom: 1rem; }
.field label { display: block; font-weight: 600; margin-bottom: 0.25rem; }

input[type="number"],
input[type="text"],
input[type="password"],
textarea,
select {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 1rem;
    font-family: inherit;
    transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
textarea { resize: vertical; }

.hint { color: var(--muted); font-size: 0.9rem; margin: 0.25rem 0; }

fieldset {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem;
    margin: 0 0 1rem;
}
fieldset legend { font-weight: 600; padding: 0 0.5rem; }

.directions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.25rem 0.5rem;
}
.directions label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    cursor: pointer;
}
.directions input[type="checkbox"] { width: auto; }

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    cursor: pointer;
}
.checkbox-row input { width: auto; }

.actions {
    margin-top: 1.5rem;
    display: flex;
    gap: 0.75rem;
}
button,
.btn {
    padding: 0.6rem 1.5rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    text-decoration: none;
    font-size: 1rem;
    line-height: 1.5; /* einheitliche Höhe für <a> UND <button> (Button hat sonst line-height:normal) */
    font-family: inherit;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.1s;
}
button:hover,
.btn:hover { background: var(--primary-dark); }
button:active,
.btn:active { transform: translateY(1px); }
.btn { font-size: 0.95rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.alert {
    padding: 0.75rem 1rem;
    border-radius: 6px;
    margin: 1rem 0;
}
.alert-error { background: var(--error-bg); color: var(--error); }
.alert-info { background: var(--info-bg); color: var(--info); }

/* Result */
/* Statusblock: Größe · Wörter · Status links, dazugehörige Aktion rechts
   (temporär: „Speichern"-Button; gespeichert: Rätsel-URL). Der Block ist die
   „Quelle der Wahrheit" für den Zustand des Rätsels – die Button-Leiste
   darunter enthält nur reine Handlungs-Buttons (Ansicht, PDF, Neu, Zurück). */
.result-meta-block {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    flex-wrap: wrap;
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
}
.result-meta {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
    align-items: center;
    color: var(--muted);
    margin: 0;
}
.session-hint { color: var(--error); font-weight: 500; }
.saved-hint { color: var(--success); font-weight: 500; }
.result-meta-side {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}
.result-meta-side:empty { display: none; }

.result-actions {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
    align-items: center;
    margin-bottom: 1.5rem;
}
/* Der Regenerate-Button muss in einem <form> sitzen (POST). Das Form wird
   daher layout-transparent (kein Margin, kein Padding), damit der Button
   exakt wie die Nachbar-Links aussieht: gleiche Höhe, gleicher Abstand. */
.regenerate-form {
    display: inline-flex;
    margin: 0;
    padding: 0;
}
/* Das Save-Form sitzt im Statusblock rechts; hier braucht es nur, dass der
   Button wie ein normaler Button aussieht (kein Block-Layout). */
.save-form {
    display: inline-flex;
    margin: 0;
    padding: 0;
}

/* Rätsel-URL im Statusblock (nur bei dauerhaft gespeicherten Rätseln).
   Klick = kopieren (JS setzt .copied), Rechtsklick/Mittlere Taste = öffnen. */
.result-url {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin: 0;
    padding: 0.25rem 0.5rem;
    font-size: 0.85rem;
}
.result-url-label {
    color: var(--muted, #57606a);
    white-space: nowrap;
}
.url-copy {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    word-break: break-all;
    color: var(--primary);
    text-decoration: none;
    cursor: pointer;
}
.url-copy:hover { text-decoration: underline; }
.url-copy.copied {
    color: var(--success);
    text-decoration: none;
}

/* Segmentierte Ansichtsumschaltung (Rätsel / Rätsel+Lösung): zwei
   nebeneinanderliegende Optionen, die aktiv gewählte ist hervorgehoben. */
.segmented {
    display: inline-flex;
    flex-wrap: wrap; /* drei Tabs (Rätsel/Lösung/Spielen) dürfen umbrechen */
    border: 1px solid var(--border);
    border-radius: 4px;
    overflow: hidden;
}
.segment {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1.5; /* einheitliche Höhe wie die .btn-Buttons */
    padding: 0.55rem 1rem;
    font-size: 0.95rem;
    text-decoration: none;
    color: var(--text);
    background: var(--surface);
    transition: background 0.15s, color 0.15s;
    border-right: 1px solid var(--border);
}
.segment:last-child { border-right: none; }
.segment:hover { background: var(--bg); }
.segment:focus-visible {
    outline: none;
    box-shadow: inset 0 0 0 2px var(--primary);
}
.segment.active {
    background: var(--primary);
    color: white;
}
.segment.active:hover { background: var(--primary-dark); }

.grid-wrapper {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1rem;
}

table.grid {
    border-collapse: collapse;
    margin: 0 auto;
}
table.grid td {
    text-align: center;
    vertical-align: middle;
    font-family: 'Courier New', monospace;
    font-weight: bold;
    font-size: 1rem;
    border: 1px solid var(--border);
    background: var(--surface);
}
table.grid td.solution {
    background: #fef3c7;
    color: #92400e;
}

.word-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    list-style: none;
    padding: 0;
}
.word-list li {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.25rem 0.75rem;
    font-family: 'Courier New', monospace;
    font-weight: 600;
}
.word-list li.found {
    background: #bbf7d0;
    color: #166534;
    border-color: #86efac;
    text-decoration: line-through;
}

/* --- Spielen-Modus --- */
.game-toolbar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin: 0 0 0.75rem;
}
.game-progress {
    font-family: 'Courier New', monospace;
}
.game-progress strong {
    font-size: 1.1rem;
}

table.grid.game td {
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
}
table.grid.game td:hover {
    background: var(--bg);
}
table.grid.game td.picked {
    background: #fde047;
    color: #854d0e;
}
table.grid.game td.picked:hover {
    background: #facc15;
}
table.grid.game td.found {
    background: #bbf7d0;
    color: #166534;
    cursor: default;
}
table.grid.game td.found:hover {
    background: #bbf7d0;
}

.game-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: rgba(15, 23, 42, 0.55);
}
.game-overlay[hidden] {
    display: none;
}
.game-overlay-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 2rem;
    max-width: 28rem;
    width: 100%;
    text-align: center;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}
.game-overlay-card h2 {
    margin-top: 0;
}
.game-overlay-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    justify-content: center;
    margin-top: 1.5rem;
}

/* Puzzle-Liste im Admin */
table.puzzle-list {
    width: 100%;
    border-collapse: collapse;
    margin: 1.5rem 0;
}
table.puzzle-list th,
table.puzzle-list td {
    text-align: left;
    padding: 0.75rem;
    border-bottom: 1px solid var(--border);
}
table.puzzle-list th {
    background: var(--surface);
    font-weight: 600;
}
/* Sortierbare Spalten-Header: der Link füllt die ganze Zelle aus, damit
   der Klickbereich groß ist. Das aktive Feld wird hervorgehoben und zeigt
   einen Pfeil (▲ asc / ▼ desc). */
table.puzzle-list th .sort-link {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    color: var(--muted);
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
}
table.puzzle-list th .sort-link:hover {
    color: var(--primary);
    text-decoration: underline;
}
table.puzzle-list th .sort-link.active {
    color: var(--primary);
    font-weight: 700;
}
table.puzzle-list tr:hover {
    background: #f9fafb;
}
table.puzzle-list .actions {
    white-space: nowrap;
}
table.puzzle-list .actions .btn {
    margin-right: 0.25rem;
    margin-bottom: 0.25rem;
}
.btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
}
.btn-danger {
    background: #dc2626;
    color: white;
    border-color: #dc2626;
}
.btn-danger:hover {
    background: #b91c1c;
}

@media (max-width: 600px) {
    .row { flex-direction: column; gap: 0; }
    .directions { grid-template-columns: 1fr; }
    table.puzzle-list {
        display: block;
        overflow-x: auto;
    }
}
