/* =========================================
   1. THEME & VARIABLES
   ========================================= */
:root {
  /* --- Colors: Backgrounds --- */
  --color-bg: #000000;
  --color-bg-main: #0a0a0a;
  --color-bg-card: #141414;
  --color-bg-raised: #222222;       /* Dialoge, Toasts */
  --color-bg-input: #101010;
  --color-bg-list-item: #111111;
  --color-bg-icon-btn: #191919;
  
  /* --- Colors: Actions & State --- */
  --color-primary: #1f8662;         /* Haupt-Grün */
  --color-primary-hover: #1c7a5a;
  
  --color-secondary-btn: #232323;
  --color-secondary-btn-hover: #2e2e2e;

  --color-danger: #b00020;
  --color-danger-hover: #b71c1c;
  --color-danger-bg: #2d0a0a;       /* Für Airbag Error */
  --color-danger-border: #ff4d4d;
  --color-danger-text-faded: #ffb4a9;
  
  --color-warning-bg: #2d240a;      /* Für Airbag Warnung */
  --color-warning-border: #ffcc00;
  --color-warning-text: #fff4d0;

  --color-info: #7c3aed;            /* Lila (Neu/Info) */
  --color-info-glow: rgba(124, 58, 237, 0.7);
  --color-info-bg-subtle: rgba(124, 58, 237, 0.1);

  /* --- Colors: Text & Borders --- */
  --color-accent: #28b080;
  --color-accent-text-light: #7fe0b7;
  
  --color-text: #e0e0e0;
  --color-text-light: #ffffff;
  --color-text-muted: #b7b7b7;

  --color-border: #1e1e1e;
  --color-border-dialog: #444444;
  --color-border-focus: var(--color-accent);
  --color-border-error: #d32f2f;

  /* --- Shadows --- */
  --shadow-sm: 0 1px 4px rgba(40,176,128,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.13);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.6);
  --shadow-toast: 0 4px 16px rgba(0,0,0,.25);

  /* --- Z-Indices (Layer Management) --- */
  --z-toast: 9999;
  --z-dialog: 10000;
  --z-airbag: 999999;
}

/* =========================================
   2. BASE & RESET
   ========================================= */
html { 
  -webkit-text-size-adjust: 100%; 
  text-size-adjust: 100%; 
  overscroll-behavior-y: none; /* Verhindert Gummiband-Effekt */
}

body {
  background: var(--color-bg);
  color: var(--color-text);
  font-family: 'Inter', 'Segoe UI', Arial, sans-serif;
  margin: 0;
  padding: 0;
  /* Verhindert Text-Markierung außer in Inputs */
  -webkit-user-select: none;
  user-select: none;
}

/* =========================================
   3. TYPOGRAPHY
   ========================================= */
h1 {
  font-size: 1em;
  font-weight: 800;
  margin: 0 0 10px 0;
  text-align: center;
  letter-spacing: 0.01em;
}

h2 {
  font-size: 0.95em;
  font-weight: 700;
  margin: 14px 0 6px 0;
  letter-spacing: 0.01em;
}

a {
  -webkit-tap-highlight-color: transparent;
}

/* =========================================
   4. LAYOUT & CONTAINERS
   ========================================= */
main {
  max-width: 410px;
  margin: 12px auto 0 auto;
  background: var(--color-bg-main);
  border-radius: 12px;
  padding: 7px 4px 18px 4px;
  box-shadow: var(--shadow-md);
}

section { 
  margin-bottom: 9px; 
}

/* Flexbox Row Utilities */
.button-row,
.custom-row,
.input-row,
.dialog-row,
.flex-row {
  display: flex;
  gap: 0.3em;
  align-items: center;
  margin-bottom: 0.4em;
}

/* Gleichmäßige Button-Verteilung */
.button-row button,
.custom-row button {
  flex: 1 1 0;
  margin: 0;
}

/* Header Bereich (Titel + Info Badge) */
.header-with-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 1rem;
}
.header-with-info h1 {
  margin: 0;
}

/* =========================================
   5. FORMS (INPUTS & BUTTONS)
   ========================================= */

/* --- Inputs --- */
input, select {
  background: var(--color-bg-input);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: 5px;
  padding: 5px 6px;
  font-size: 0.98em;
  margin-bottom: 0;
  outline: none;
  transition: border 0.15s;
  -webkit-tap-highlight-color: transparent;
  /* Textauswahl in Inputs erlauben */
  -webkit-user-select: text;
  user-select: text;
}

input:focus, select:focus { 
  border: 1.2px solid var(--color-border-focus); 
}
input:focus-visible {
  outline: 2px solid var(--color-border-focus); 
  outline-offset: 2px;
}

input[type="number"] { 
  touch-action: manipulation; /* iOS Scroll Fix */
}

input.input-error {
  border-color: var(--color-border-error);
  box-shadow: 0 0 0 1px rgba(211, 47, 47, 0.25);
}
input.input-error:focus {
  border-color: #ef5350;
}

.input-hint {
  font-size: 0.82em;
  color: var(--color-danger-text-faded);
  margin: -0.25em 0 0.4em 0.15em;
  min-height: 1.2em;
}

/* Input Layout Helpers */
.custom-row input {
  width: 3em;
  margin-right: 0;
}

.input-row label {
  flex: 1 1 0;
  margin-bottom: 0;
  font-size: 0.94em;
  font-weight: 500;
}
.input-row input {
  flex: 2 1 0;
  min-width: 0;
}

.input-with-buttons {
  display: flex;
  align-items: center;
  gap: 0.3em;
  flex: 2.11 1 0; 
  min-width: 0;
}
.input-with-buttons > input#rFactor {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
}

/* --- Buttons --- */
button, .btn {
  background: var(--color-primary);
  color: var(--color-text-light);
  border: none;
  border-radius: 8px;
  padding: 6px 0;
  font-size: 0.96em;
  font-weight: 600;
  box-shadow: var(--shadow-sm);
  transition: background 0.15s;
  cursor: pointer;
  min-width: 0;
  width: 100%;
  display: inline-block;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}

button:active, .btn:active { 
  background: var(--color-primary-hover); 
}

button:focus-visible, .btn:focus-visible { 
  outline: 2px solid var(--color-border-focus); 
  outline-offset: 2px; 
}

button:disabled, .btn:disabled {
  background: #2a2a2a;
  color: #777;
  cursor: not-allowed;
  box-shadow: none;
  opacity: 0.7;
}

/* Button Variants */
.btn-secondary {
  background: var(--color-secondary-btn);
  color: var(--color-text);
}
.btn-secondary:active {
  background: var(--color-secondary-btn-hover);
}

.btn-danger { 
  background: var(--color-danger); 
  color: var(--color-text-light);
}
.btn-danger:active { 
  background: var(--color-danger-hover); 
}

/* Icon Buttons (Round) */
.btn-icon {
  border-radius: 50%;
  padding: 0.32em 0.56em;
  font-size: 1em;
  background: var(--color-bg-icon-btn);
  color: var(--color-accent);
  min-width: unset;
  width: unset;
  height: unset;
  box-shadow: none;
  margin-left: 0.2em;
}
.btn-icon:active { 
  background: var(--color-accent); 
  color: var(--color-bg-input); 
}

/* Gender Toggle Buttons */
.btn-gender {
  font-size: 1.1em;
  width: 2.1em;
  height: 2.1em;
  border-radius: 50%;
  background: var(--color-secondary-btn);
  color: var(--color-text);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  border: 1px solid var(--color-secondary-btn-hover);
  transition: background 0.15s;
}
.btn-gender span {
  display: inline-block;
  transform: translateY(-2px);
}
.btn-gender:active {
  background: var(--color-accent);
  color: var(--color-bg-input);
}

/* Info Badge Button (Purple) */
.info-badge-btn {
  background: transparent;
  border: 1.5px solid var(--color-info);
  color: var(--color-info);
  border-radius: 50%;
  width: 22px;
  height: 22px;
  font-size: 15px;
  font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-weight: 600;
  font-style: italic;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.is-new {
  animation: pulse-purple 2s infinite;
  border-width: 2px; 
  background-color: var(--color-info-bg-subtle);
}

/* =========================================
   6. SPECIFIC COMPONENTS
   ========================================= */

/* BAC Output */
#bac {
  font-size: 1.1em;
  font-weight: bold;
  margin-top: 0.1em;
  margin-bottom: 0.15em;
  color: var(--color-accent);
  text-shadow: 0 1px 4px rgba(40,176,128,0.07);
}

#peakInfo {
  color: inherit;
  font-size: 0.92em;
  margin-top: 2px;
}

.muted-note {
  color: var(--color-text-muted);
  font-size: 0.85em;
  padding-left: 0.5em;
}

.perf-note {
  font-size: 0.75em;
  padding-left: 0;
  text-align: right;
  opacity: 0.7;
}

/* Drink List (#drinkEntries) */
#drinkEntries li {
  margin-bottom: 0.20em;
  background: var(--color-bg-list-item);
  padding: 4px 7px;
  border-radius: 5px;
  font-size: 0.97em;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  word-break: break-word;
  gap: 0.5em;
  /* Performance Optimization */
  content-visibility: auto;
  contain-intrinsic-size: 0 36px;
}

#drinkEntries li > span:first-child {
  flex: 1 1 auto;
  min-width: 0;
}

#drinkEntries button {
  border: none;
  font-size: 0.98em;
  padding: 0.19em 0.6em;
  border-radius: 6px;
  min-width: 1.8em;
  max-width: 2.5em;
  width: auto;
  height: 1.8em;
  cursor: pointer;
  transition: background 0.16s, color 0.16s;
  display: flex;
  align-items: center;
  justify-content: center;
}

#drinkEntries li button:first-of-type {
  background: var(--color-secondary-btn);
  color: var(--color-accent);
}
#drinkEntries li button:first-of-type:active {
  background: var(--color-accent);
  color: var(--color-bg-input);
}

#drinkEntries li button:last-of-type {
  background: var(--color-danger);
  color: var(--color-text-light);
}
#drinkEntries li button:last-of-type:active {
  background: var(--color-danger-hover);
}

/* Inline Edit Dialog inside List */
/* 1. Die Animation definieren */
@keyframes slideInVisible {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
    max-height: 0;
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
    max-height: 1000px; /* Groß genug für den Inhalt */
  }
}

/* 2. Den Zustand beim Anzeigen festlegen */
.dialog-visible {
  display: block !important;
  animation: slideInVisible 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
  overflow: hidden;
}

/* 3. Das Design des Inline-Dialogs (Fix für den Abbrechen-Button) */
.inline-edit {
  background-color: #1a1a1a !important; /* Etwas heller als der schwarze Hintergrund */
  border: 1px solid var(--color-primary); /* Ein feiner grüner Rahmen zur Abhebung */
  border-radius: 8px;
  margin: 10px 0;
  padding: 15px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.8);
  z-index: 100;
}

/* Sicherstellen, dass die Buttons im Dialog Kontrast haben */
.inline-edit .btn-secondary {
  background-color: #333 !important; /* Deutliches Grau für den Abbrechen-Button */
  border: 1px solid #444;
  color: white !important;
}


#drinkEntries .dialog.inline-edit {
  margin: 6px 8px 10px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--color-bg-card);
  box-shadow: 0 2px 8px rgba(0,0,0,.18);
  cursor: default; /* Verhindert Klicks auf das LI-Element dahinter */
}

#drinkEntries .dialog.inline-edit h2 { 
  margin: 2px 0 8px 0; 
  font-size: .95em; 
}

#drinkEntries .dialog.inline-edit .dialog-row { 
  display: flex; 
  gap: .5em; 
  align-items: center; 
  margin-bottom: .6em; 
}

#drinkEntries .dialog.inline-edit label { 
  flex: 1 1 0; 
  min-width: 0; 
}

#drinkEntries .dialog.inline-edit input { 
  width: 100%; 
}

/* Container für die Buttons */
#drinkEntries .dialog.inline-edit .dialog-row:last-child { 
  gap: .6em; 
  display: flex; 
  align-items: center;
  justify-content: flex-start; /* Linksbuendig oder space-between */
}

/* FIX: WICHTIG - Spezifischer Selektor (#drinkEntries vorangestellt) */
#drinkEntries .dialog.inline-edit .btn { 
  width: auto !important;       /* Erzwingt normale Breite */
  max-width: none !important;   /* HEBT DIE BEGRENZUNG (2.5em) AUF */
  min-width: 6.5em;             /* Mindestbreite damit Text passt */
  height: auto;                 /* Höhe resetten */
  padding: 6px 12px;
  margin-left: 0;
  margin-right: 0.5em;          /* Abstand zwischen den Buttons */
  font-weight: 600;
  border-radius: 8px;
  flex: 0 0 auto;               /* Verhindert Flex-Stauchung */
}

/* Letzter Button braucht keinen Margin rechts */
#drinkEntries .dialog.inline-edit .btn:last-child {
  margin-right: 0;
}

/* =========================================
   7. DIALOGS & MODALS
   ========================================= */
dialog {
  box-sizing: border-box;
  border: none;
  border-radius: 12px;
  background: #1a1a1a;
  color: white;
  width: 90%;
  max-width: 400px;
  margin: auto;
  padding: 1.5rem;
  z-index: var(--z-dialog);
}

dialog::backdrop {
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(3px);
}

dialog * {
  max-width: 100%;
  overflow-wrap: break-word;
}

/* -- Native Dialog Variants -- */

/* History Dialog */
#history-dialog {
  max-width: 95vw;
  width: 360px;
  padding: 0;
  background: var(--color-bg-card);
  border: 1px solid var(--color-border-dialog);
  box-shadow: var(--shadow-lg);
}
#history-dialog h3 {
  margin: 0;
  padding: 1rem;
  font-size: 1.1rem;
  border-bottom: 1px solid var(--color-border-dialog);
  background: #333;
}
.history-table { width: 100%; border-collapse: collapse; }
.history-table th, .history-table td {
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--color-border-dialog);
}
.history-table th { font-weight: 600; text-align: left; }
.history-actions {
  display: flex; gap: 0.5rem; padding: 0.75rem;
  border-top: 1px solid var(--color-border-dialog);
}
.history-actions button {
  flex: 1; border: none; border-radius: 6px; padding: 0.6rem; font-size: 0.9rem;
}
#history-export { background: #007bff; color: var(--color-text-light); }
#history-clear  { background: var(--color-danger); color: var(--color-text-light); }
#history-close  { background: var(--color-border-dialog); color: var(--color-text-light); }

/* Tips Dialog */
#tips-dialog {
  max-width: 92vw;
  width: min(400px, 92vw);
  padding: 1.1rem 1.25rem 1.2rem;
  background: var(--color-bg-card);
  border: 1px solid var(--color-border-dialog);
}
.tips-list { margin: 0; padding-left: 1.1rem; list-style: disc; font-size: 0.92rem; }
.tips-list li { margin-bottom: 0.55rem; }
.tips-list strong { color: var(--color-accent-text-light); font-weight: 600; }
.tips-actions { display: flex; justify-content: flex-end; margin-top: 1rem; }
.tips-actions .btn { width: auto; min-width: 0; padding: 0.55rem 1.15rem; }
#tips-dialog a { color: var(--color-accent-text-light); text-decoration: underline; }

/* News Dialog */
#news-dialog {
  background: var(--color-bg-card);
  box-shadow: 0 10px 25px rgba(0,0,0,0.5);
}
.news-content ul { padding-left: 20px; margin: 1rem 0; }
.news-content li { margin-bottom: 0.5rem; font-size: 0.95rem; line-height: 1.4; }

/* -- Legacy Dialogs (Div based) -- */
.dialog {
  background: var(--color-bg-card);
  padding: 0.45em 0.7em;
  border-radius: 7px;
  margin-top: 0.7em;
  box-shadow: 0 2px 8px rgba(0,0,0,0.10);
}
.dialog-hidden { display: none; }
.dialog-visible { display: block; }
.dlg-title{ margin-top:0; }
.dlg-text{ margin:0 0 12px; }

.app-dialog-bar {
  position:fixed; left:0; right:0; bottom:0;
  padding:1em;
  background: var(--color-bg-raised);
  color: var(--color-text-light);
  display:flex; justify-content:space-between; align-items:center;
  z-index: var(--z-dialog);
}

/* =========================================
   8. OVERLAYS, TOASTS & AIRBAGS
   ========================================= */

/* Toasts */
.toast { 
  position: fixed; right: 12px; top: 12px; 
  padding: .6rem .8rem; 
  z-index: var(--z-toast); 
  background: var(--color-bg-raised); 
  color: var(--color-text-light); 
  border-radius: .5rem; 
  box-shadow: var(--shadow-toast); 
  max-width: 80vw; font-size: .9rem;
}

.share-toast {
  position:fixed; left:50%; bottom:16px; transform:translateX(-50%);
  background: var(--color-bg-raised);
  color: var(--color-text-light);
  font-size:14px; padding:8px 10px; border-radius:8px;
  z-index: var(--z-toast); opacity:.96;
}

/* Update Prompt */
#__update_prompt {
  position:fixed; right:12px; bottom:12px; z-index: var(--z-toast);
  background: var(--color-bg-raised); 
  color: var(--color-text-light); 
  padding:12px 14px; border-radius:8px;
  box-shadow: 0 2px 10px rgba(0,0,0,.3); max-width:320px;
  font: 14px/1.4 system-ui,-apple-system,Segoe UI,Roboto,sans-serif;
}
#__update_prompt .btn-row { display:flex; gap:8px; justify-content:flex-end; flex-wrap:wrap; margin:0; }
#__update_prompt .btn { width: auto; background:#4caf50; padding:6px 10px; }
#__update_prompt .btn.btn-secondary { background: var(--color-border-dialog); }

/* Update Dot Indicator */
#refreshBacBtn.update-dot { position: relative; }
#refreshBacBtn.update-dot::after { 
  content:''; position:absolute; top:-2px; right:-2px; width:8px; height:8px; 
  border-radius:50%; background:#2ecc71; 
}

/* Airbag (Critical Errors) */
.airbag-wrap {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px); 
  left: calc(env(safe-area-inset-left, 0px) + 15px);
  right: calc(env(safe-area-inset-right, 0px) + 15px);
  display: flex; flex-direction: column; gap: 10px;
  z-index: var(--z-airbag);
  pointer-events: none;
}

.airbag-toast {
  pointer-events: auto;
  padding: 14px 18px;
  border-radius: 12px;
  font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px; line-height: 1.4;
  color: #ffffff;
  background: #1e1e1e;
  box-shadow: var(--shadow-lg);
  border-left: 6px solid #ccc;
  cursor: pointer;
  user-select: none;
  white-space: pre-wrap; word-break: break-word;
  animation: airbag-slide-in 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  transition: opacity 0.3s, transform 0.3s, filter 0.3s;
}

.airbag-toast:active { transform: scale(0.96); filter: brightness(1.2); }
.airbag-toast--error { background: var(--color-danger-bg); border-left-color: var(--color-danger-border); color: #ffdada; }
.airbag-toast--warn  { background: var(--color-warning-bg); border-left-color: var(--color-warning-border); color: var(--color-warning-text); }
.airbag-toast--closing { opacity: 0; transform: translateX(50px); }

/* =========================================
   9. ANIMATIONS
   ========================================= */
@keyframes pulse-purple {
  0% { transform: scale(1); box-shadow: 0 0 0 0 var(--color-info-glow); }
  70% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(168, 85, 247, 0); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(168, 85, 247, 0); }
}

@keyframes spin {
  to { transform: rotate(1turn); }
}
.spin {
  animation: spin .6s linear;
  will-change: transform;
  backface-visibility: hidden;
}

@keyframes airbag-slide-in {
  from { opacity: 0; transform: translateY(-30px) scale(0.9); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* =========================================
   10. UTILITIES
   ========================================= */
.u-m0 { margin:0 !important; }
.u-mt0 { margin-top:0 !important; }
.mt-8 { margin-top:8px !important; }
.mb-0, .no-mb { margin-bottom:0 !important; }
.gap-12 { gap:12px !important; }
.gap-8 { gap:8px !important; }
.grow { flex:1 1 0 !important; }
.minw-0 { min-width:0 !important; }
.no-shrink { flex-shrink:0 !important; }
.is-hidden { display:none !important; }
.ta-right { text-align:right !important; }
.ta-left { text-align:left !important; }
.wrap { flex-wrap: wrap !important; }
.justify-end { justify-content: flex-end !important; }

/* =========================================
   11. MEDIA QUERIES & DEVICE SUPPORT
   ========================================= */

/* Mobile Optimization */
@media (max-width: 410px) {
  main {
    padding: 3px 0 8px 0;
    border-radius: 0;
  }
  .button-row, .custom-row, .input-row, .dialog-row, .flex-row {
    gap: 0.13em;
  }
  input, .btn, #drinkEntries button, .btn-icon {
    font-size: 0.95em !important;
    min-height: 1.8em;
  }
  #bac { font-size: 0.97em !important; }
  
  .airbag-toast {
    font-size: 13px;
    padding: 12px 15px;
  }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
  .spin { animation: none !important; }
}

/* iOS Safe Area Support */
@supports(padding:max(0px)) {
  body {
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
}

/* iOS Focus Zoom Fix (Prevent standard zoom if input font < 16px) */
@supports (-webkit-touch-callout: none) {
  input, select, textarea, .input-sm { font-size: 16px !important; }
}
