:root {
  color-scheme: light dark;

  /* Lean Professional Palette */
  --bg-main: light-dark(#ffffff, #0a0a0a);
  --bg-card: light-dark(#f9fafb, #151515);
  --accent: light-dark(#2563eb, #3b82f6);
  --text-main: light-dark(#111827, #f3f4f6);
  --text-muted: light-dark(#6b7280, #9ca3af);
  --border: light-dark(#e5e7eb, #262626);

  --radius-sm: 8px; /* Etwas runder für den App-Look */
  --radius-md: 12px;
}

/* Verhindert Pull-to-Refresh und "Gummiband"-Effekt auf iOS (App-Feel) */
html, body {
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent; /* Entfernt das blaue Flackern beim Tippen auf Handys */
}

@scope (body) {
  :scope {
    font-family: "Inter", system-ui, -apple-system, sans-serif;
    background-color: var(--bg-main);
    color: var(--text-main);
    max-width: 800px; /* Noch etwas kompakter für Fokus */
    margin: 0 auto;
    /* Safe-Area für iPhone-Notches */
    padding: max(1.5rem, env(safe-area-inset-top)) max(1.5rem, env(safe-area-inset-right)) 2rem max(1.5rem, env(safe-area-inset-left));
    letter-spacing: -0.01em;
    
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  /* =========================================
     1. NAVIGATION (Responsive Top/Bottom Bar)
     ========================================= */
  
  /* --- DESKTOP (Default) --- */
  header {
    position: sticky;
    top: 0;
    background: color-mix(in srgb, var(--bg-main) 85%, transparent);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    z-index: 50;
    margin: calc(-1 * max(1.5rem, env(safe-area-inset-top))) -1.5rem 2rem -1.5rem;
    padding: max(1.5rem, env(safe-area-inset-top)) 1.5rem 0 1.5rem;
  }

  header nav {
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--border);
    gap: 1.5rem;
    padding-bottom: 0.75rem;
    view-transition-name: main-nav;
    user-select: none;
  }

  .nav-item {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    text-decoration: none;
    color: var(--text-muted);
    font-size: 0.95rem;
    font-weight: 600;
    padding: 0.25rem 0;
    position: relative;
    transition: color 0.2s ease;
    background: none; 
    border: none; 
    cursor: pointer;
    font-family: inherit;
  }

  /* Desktop Icon Styling */
  .nav-icon { display: flex; align-items: center; }
  .nav-icon svg { width: 1.1rem; height: 1.1rem; stroke-width: 2.5; }

  /* Aktiver Tab auf Desktop (Linie unten) */
  .nav-item[aria-current="page"] { color: var(--text-main); }
  .nav-item[aria-current="page"]::after {
    content: ""; position: absolute; bottom: -0.85rem; left: 0;
    width: 100%; height: 2px; background: var(--accent);
    border-radius: 2px 2px 0 0;
  }

  .nav-item:hover:not([aria-current="page"]) { color: var(--text-main); }

  /* Rechtsbündige Buttons auf Desktop */
  .logout-btn, .install-btn {
    margin-left: auto;
    color: var(--text-main);
  }


  /* --- MOBILE (Bottom Bar ab 640px) --- */
  @media (max-width: 640px) {
    /* Platz am Ende der Seite machen, damit Text nicht unter der Leiste verschwindet */
    :scope {
      padding-bottom: calc(6rem + env(safe-area-inset-bottom));
    }

    header {
      /* Vom oberen Rand lösen und unten fixieren */
      position: fixed;
      top: auto;
      bottom: 0;
      left: 0;
      right: 0;
      margin: 0; 
      padding: 0;
      border-top: 1px solid var(--border);
    }

    header nav {
      /* Gleichmäßige Verteilung der Icons */
      border-bottom: none;
      padding: 0.75rem max(1rem, env(safe-area-inset-right)) max(0.75rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
      justify-content: space-around;
      gap: 0;
    }

    .nav-item {
      /* Auf Mobile: Icon oben, winziger Text unten */
      flex-direction: column;
      gap: 0.25rem;
      margin-left: 0 !important; /* Überschreibt das auto-margin der Desktop-Buttons */
      padding: 0;
    }

    /* Größere Icons & kleinerer Text für Daumen-freundliche Bedienung */
    .nav-icon svg { width: 1.5rem; height: 1.5rem; stroke-width: 2; }
    .nav-label { font-size: 0.65rem; font-weight: 500; }

    /* Aktiver Tab auf Mobile (Linie wandert nach oben an den Rand der Bar) */
    .nav-item[aria-current="page"]::after {
      bottom: auto;
      top: -0.85rem; /* exakt auf dem border-top der Navigation */
      height: 3px;
      border-radius: 0 0 3px 3px;
    }
  }
  /* 2. GRID (Clean Cards) */
  .snippet-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;

    & article.snippet-card {
      background: var(--bg-card);
      padding: 1.25rem;
      border-radius: var(--radius-md);
      border: 1px solid var(--border);
      transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
      contain: layout;
      display: flex;
      flex-direction: column;
      position: relative;

      /* "Tap"-Feedback für Handys */
      &:active {
        transform: scale(0.98);
        background: var(--border);
      }

      & h3 {
        margin: 0 0 0.5rem 0;
        line-height: 1.3;
        font-size: 1.1rem;
        font-weight: 600;
        
        & a {
          color: var(--text-main);
          text-decoration: none;
          &::before { content: ""; position: absolute; inset: 0; }
        }
      }

      & a.btn-edit {
        position: relative;
        z-index: 2; /* Hebt den Button über den Klickbereich der Karte */
        margin-top: auto;
        padding-top: 1rem;
        font-size: 0.85rem;
        font-weight: 600;
        color: var(--accent);
        text-decoration: none;
      }
    }
  }

  /* 3. FORMULARE & EDITOR (Vollbild-Feel) */
  form {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    height: calc(100vh - 150px); /* Nutzt den Bildschirm optimal aus */

    & input[type="text"],
    & input[type="password"],
    & textarea {
      background: var(--bg-card);
      border: 1px solid var(--border);
      border-radius: var(--radius-sm);
      padding: 1rem;
      color: var(--text-main);
      font-size: 1rem; /* 1rem verhindert Auto-Zoom auf iOS */
      font-family: inherit;
      transition: border-color 0.2s ease;
      -webkit-appearance: none; /* Entfernt iOS Default-Styling */

      &::placeholder { color: var(--text-muted); }
      &:focus { outline: none; border-color: var(--accent); }
    }
    
    & textarea {
      flex-grow: 1; /* Nimmt den restlichen Platz ein */
      line-height: 1.6;
      resize: none; /* App-Editoren zieht man nicht manuell auf */
    }

    /* Native Toggle Switch statt Checkbox */
    & label.toggle-label {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      font-weight: 500;
      cursor: pointer;
      user-select: none;

      & input[type="checkbox"] {
        appearance: none;
        -webkit-appearance: none;
        width: 3rem;
        height: 1.5rem;
        background: var(--border);
        border-radius: 999px;
        position: relative;
        cursor: pointer;
        transition: background 0.3s ease;

        &::after {
          content: '';
          position: absolute;
          top: 2px; left: 2px;
          width: calc(1.5rem - 4px);
          height: calc(1.5rem - 4px);
          background: var(--bg-main);
          border-radius: 50%;
          transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
          box-shadow: 0 2px 4px rgba(0,0,0,0.2);
        }

        &:checked { background: var(--accent); }
        &:checked::after { transform: translateX(1.5rem); }
      }
    }

    & button {
      background: var(--text-main);
      color: var(--bg-main);
      border: none;
      padding: 1rem;
      border-radius: var(--radius-sm);
      font-weight: 600;
      font-size: 1rem;
      cursor: pointer;
      transition: opacity 0.2s, transform 0.1s;
      user-select: none;
      touch-action: manipulation; /* Schnelleres Klick-Feedback */

      &:active { transform: scale(0.98); }
    }
  }

  /* 4. MARKDOWN VIEW */
  article.markdown-body {
    contain: layout;
    padding-bottom: 4rem;

    & h1 { font-size: 2rem; font-weight: 700; margin-bottom: 2rem; }
    
    & section {
      line-height: 1.7;
      & h2, h3 { margin-top: 2rem; margin-bottom: 1rem; }
      & p { margin-bottom: 1.25rem; }
      & a { color: var(--accent); text-decoration: none; }
      
      & pre {
        background: light-dark(#111827, #1e1e1e);
        color: #f3f4f6;
        padding: 1.25rem;
        border-radius: var(--radius-sm);
        overflow-x: auto;
        font-family: "JetBrains Mono", ui-monospace, monospace;
        font-size: 0.9rem;
        margin: 1.5rem 0;
      }
      & code {
        font-family: "JetBrains Mono", ui-monospace, monospace;
        font-size: 0.9em;
        background: var(--bg-card);
        padding: 0.2em 0.4em;
        border-radius: 4px;
      }
    }
  }
}

/* Transitions bleiben identisch */
::view-transition-old(root), ::view-transition-new(root) {
  animation-duration: 0.25s;
  animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
@keyframes slide-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
::view-transition-new(root) { animation-name: slide-in; }
::view-transition-old(active-snippet), ::view-transition-new(active-snippet) {
  animation-duration: 0.3s;
  animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  mix-blend-mode: normal;
  height: 100%;
}