/* KDP Pro — App-Shell v3 (Overrides)
 * Wird NACH kdp.css und ui-kit.css geladen (includes/head-assets.php).
 * Enthält: ruhigere/schmalere Desktop-Sidebar, mobile Tab-Leiste, kompakte
 * Topbar mit sichtbarem Credit-Stand. Nur Tokens aus tokens.css; neue
 * Ableitungen via color-mix(). Gold bleibt dem einen Primär-CTA der Seite
 * vorbehalten — die Tab-Leiste benutzt es nicht als Fläche.
 */

:root {
  --sidebar-w: 240px;      /* war 272px (tokens.css) — .cta-bar/.app-content lesen die Variable */
  --kdp-tabbar-h: 0px;     /* Tab-Leiste nur mobil + :has()-Support (@supports unten) */
}

/* ── Desktop-Sidebar: ruhiger ─────────────────────────────────────────────── */
.sidebar {
  background: var(--bg-page);
}
.sidebar__brand { padding: 16px 18px 12px; }

.side-credits-card {
  margin: 10px 12px 4px;
  padding: 11px 13px;
  background: var(--bg-surface);
  border: 1px solid var(--border-hairline);
  box-shadow: var(--elev-1);
}
.side-credits-card:hover {
  background: var(--bg-surface);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
}

.side-nav { padding: 6px 8px; }
.sidebar__section { padding: 14px 12px 6px; }

.side-link {
  padding: 9px 12px;
  color: var(--neutral-700);
}
.side-link:hover {
  background: var(--state-hover);
  color: var(--text-primary);
}
.side-link.is-active {
  background: var(--bg-surface);
  border-color: var(--border-hairline);
  box-shadow: var(--elev-1);
  color: var(--text-primary);
}
.side-link.is-active .side-icon { color: var(--accent-700); }

.side-link:focus-visible,
.side-credits-card:focus-visible,
.sidebar__user-email:focus-visible,
.sidebar__legal a:focus-visible,
.kdp-tabbar__item:focus-visible {
  outline: 2px solid var(--accent-600);
  outline-offset: 2px;
}

.sidebar__footer { padding: 10px 12px 12px; }
.sidebar__user-email {
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
}
.sidebar__user-email:hover { color: var(--text-primary); text-decoration: underline; }

/* ── Topbar: Titel/Zurück-Link kürzen statt überlaufen (alle Breiten) ────── */
.topbar__title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar__project { min-width: 0; }
.topbar__credits { white-space: nowrap; flex-shrink: 0; }
.topbar__credits strong { color: var(--accent-700); }       /* war --gold3 (≈3,7:1) */
.topbar__credits-buy { color: var(--link-on-light); font-size: 13px; }
.kdp-topbar__back { min-width: 0; flex-shrink: 1; max-width: 45%; }
.kdp-topbar__back-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Tab-Leiste: Desktop unsichtbar ──────────────────────────────────────── */
.kdp-tabbar { display: none; }

/* Eingeklappte Desktop-Sidebar (liegt bei translateX(-100%)): Links sonst weiter per
   Tab erreichbar. visibility:hidden erst NACH der Slide-Transition, beim Ausklappen
   sofort sichtbar. #kdp-expand-tab ist Geschwister, nicht Kind, und bleibt fokussierbar. */
@media (min-width: 769px) {
  .sidebar {
    transition: transform .22s ease, visibility 0s;
  }
  body.sidebar-collapsed .sidebar {
    visibility: hidden;
    transition: transform .22s ease, visibility 0s linear .22s;
  }
}

@media (max-width: 768px) {
  /* Drawer: Schatten NUR geöffnet (vorher ragte er im geschlossenen Zustand
     als grauer Schleier in den Viewport); geschlossen auch nicht fokussierbar. */
  .sidebar {
    box-shadow: none;
    visibility: hidden;
    transition: transform .22s ease, visibility 0s linear .22s;
  }
  body.sidebar-open .sidebar {
    box-shadow: var(--elev-4);
    visibility: visible;
    transition: transform .22s ease, visibility 0s;
  }

  /* Topbar mobil: Credits wieder sichtbar (kdp.css blendet sie aus), kompakt */
  .topbar { gap: 8px; }
  .topbar__title { font-size: 16px; }
  .topbar__credits {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 3px 10px;
    font-size: 12px;
  }
  .topbar .btn--sm { padding-left: 12px; padding-right: 12px; white-space: nowrap; flex-shrink: 0; }
  .topbar .kdp-topbar__back { flex-shrink: 1; max-width: 32%; }   /* Titel behält Restbreite */
}

/* Schmale Telefone: Zurück-Link auf den Pfeil kürzen (Text bleibt im DOM/Accessible Name,
   Trefferfläche ≥44px), damit Titel + Credits + Sprach-Badge nicht den Titel verdrängen. */
@media (max-width: 480px) {
  .topbar .kdp-topbar__back {
    min-width: 44px;
    max-width: none;
    padding-left: 0;
    padding-right: 0;
    justify-content: center;
  }
  .topbar .kdp-topbar__back-label {
    display: inline-block;
    font-size: 0;
    overflow: visible;
  }
  .topbar .kdp-topbar__back-label::first-letter { font-size: 18px; }
}

/* ── Tab-Leiste aktivieren: NUR mit :has()-Unterstützung ─────────────────────
   Die Aktivierung hängt an „Seite hat keine .cta-bar" (:has). Ohne :has()
   (iOS <15.4, Firefox <121) läge die Leiste sonst über der .cta-bar und der
   Hamburger wäre weg → dort gilt das alte Verhalten: Hamburger sichtbar,
   keine Tab-Leiste, kein reservierter Platz. */
@supports selector(:has(*)) {
  @media (max-width: 768px) {
    /* Auf :root, damit auch html (scroll-padding) die Höhe lesen kann. */
    /* Alle Kopplungen verlangen zusätzlich, dass .kdp-tabbar WIRKLICH im DOM ist
       (altes sidebar.php + neues CSS / Seiten ohne Shell: Hamburger bleibt, kein Offset). */
    :root:has(.kdp-tabbar):not(:has(.cta-bar)) { --kdp-tabbar-h: calc(60px + env(safe-area-inset-bottom, 0px)); }

    /* Hamburger ersetzt durch Tab-Leiste (auf .cta-bar-Seiten bleibt er) */
    body:has(.kdp-tabbar):not(:has(.cta-bar)) #kdp-menu-btn { display: none !important; }

    .kdp-tabbar {
      display: grid;
      grid-template-columns: repeat(5, minmax(0, 1fr));
      position: fixed;
      left: 0; right: 0; bottom: 0;
      z-index: 70;                       /* unter Backdrop (79) und Drawer (90) */
      height: var(--kdp-tabbar-h);
      padding: 0 env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
      background: var(--bg-surface);     /* Fallback ohne color-mix() */
      background: color-mix(in srgb, var(--bg-surface) 94%, transparent);
      -webkit-backdrop-filter: blur(12px);
      backdrop-filter: blur(12px);
      border-top: 1px solid var(--border-hairline);
    }
    body:has(.cta-bar) .kdp-tabbar { display: none; }

    /* Platz für die Tab-Leiste: GLOBAL auf .app-content (nicht pro .page — Seiten
       wie upload_review.php haben weder .page noch .page-content). .app-content ist
       die Spalte, in der jede Shell-Seite ihren Inhalt hat; die Leiste ist fixed
       und nimmt keinen Platz im Fluss ein. Auf .cta-bar-Seiten ist die Variable 0px. */
    .app-content { padding-bottom: var(--kdp-tabbar-h); }

    /* WCAG 2.4.11: per Tastatur fokussierte Elemente nicht unter die Leiste scrollen */
    html { scroll-padding-bottom: var(--kdp-tabbar-h); }

    /* Andere fixe Elemente unten nicht hinter die Tab-Leiste */
    body:has(.kdp-tabbar):not(:has(.cta-bar)) #kdp-toast { bottom: calc(16px + var(--kdp-tabbar-h)); }
    body:has(.kdp-tabbar):not(:has(.cta-bar)) .kdp-toast-root { bottom: calc(16px + var(--kdp-tabbar-h)); }
    body:has(.kdp-tabbar):not(:has(.cta-bar)) .upload-toast { bottom: calc(16px + var(--kdp-tabbar-h)); }

    /* Zurück-Link zur Übersicht ist redundant zum Tab „Bücher" */
    body:has(.kdp-tabbar):not(:has(.cta-bar)) .kdp-topbar__back--home { display: none; }

    .kdp-tabbar__item {
      position: relative;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 3px;
      min-height: 44px;
      padding: 6px 2px;
      margin: 0;
      background: none;
      border: 0;
      border-radius: 0;
      font: inherit;
      color: var(--neutral-600);        /* ≥5,5:1 auf Weiß */
      text-decoration: none;
      cursor: pointer;
      -webkit-tap-highlight-color: transparent;
    }
    .kdp-tabbar__item:hover { text-decoration: none; color: var(--text-primary); }
    .kdp-tabbar__icon { width: 22px; height: 22px; flex-shrink: 0; }
    .kdp-tabbar__label {
      font-size: 11px;
      font-weight: 600;
      line-height: 1.1;
      max-width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .kdp-tabbar__item.is-active { color: var(--text-primary); }
    .kdp-tabbar__item.is-active:not(.kdp-tabbar__item--new)::before {
      content: '';
      position: absolute;
      top: 0; left: 28%; right: 28%;
      height: 3px;
      border-radius: 0 0 var(--radius-xs) var(--radius-xs);
      background: var(--accent-700);
    }

    /* Neues Buch: dunkler runder Plus-Button (nicht Gold — Gold = Primär-CTA der Seite) */
    .kdp-tabbar__plus {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 44px; height: 44px;
      margin-top: -16px;
      border-radius: var(--radius-round);
      background: var(--neutral-900);
      color: var(--neutral-0);
      border: 3px solid var(--bg-surface);
      box-shadow: var(--elev-3);
      transition: transform var(--dur-2) var(--ease-standard);
    }
    .kdp-tabbar__plus svg { width: 22px; height: 22px; }
    .kdp-tabbar__item--new .kdp-tabbar__label { margin-top: 1px; }
    .kdp-tabbar__item--new:active .kdp-tabbar__plus { transform: scale(.94); }
  }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar,
  .kdp-tabbar__plus { transition: none !important; }
}
