/* ============================================================================
   RESET & BASE STYLES
   ============================================================================ */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #2196F3;
    --primary-dark: #1976D2;
    --secondary-color: #757575;
    --success-color: #4CAF50;
    --error-color: #f44336;
    --warning-color: #ff9800;
    --bg-color: #f5f5f5;
    --card-bg: #ffffff;
    --text-color: #333333;
    --text-light: #666666;
    --border-color: #e0e0e0;
    --shadow: 0 2px 4px rgba(0,0,0,0.1);
    --shadow-hover: 0 4px 8px rgba(0,0,0,0.15);
    --strom-color: #FFC107;
    --gas-color: #FF5722;
    --wasser-color: #2196F3;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ============================================================================
   NAVIGATION
   ============================================================================ */
.navbar {
    background: linear-gradient(135deg, #1976D2 0%, #2196F3 100%);
    color: white;
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 1000;
}

.nav-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.nav-brand {
    font-size: 1.5rem;
    font-weight: bold;
    color: white;
    text-decoration: none;
    padding: 15px 0;
}

.nav-menu {
    display: flex;
    list-style: none;
    gap: 5px;
}

.nav-menu a {
    color: white;
    text-decoration: none;
    padding: 15px 20px;
    display: block;
    border-radius: 5px;
    transition: background-color 0.3s;
}

.nav-menu a:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* ============================================================================
   CONTAINER & LAYOUT
   ============================================================================ */
.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 30px 20px;
    flex: 1;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    flex-wrap: wrap;
    gap: 15px;
}

.page-header h1 {
    font-size: 2rem;
    color: var(--text-color);
}

/* ============================================================================
   BUTTONS
   ============================================================================ */
.btn {
    display: inline-block;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    font-size: 1rem;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.3s;
    font-weight: 500;
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-dark);
    transform: translateY(-2px);
    box-shadow: var(--shadow-hover);
}

.btn-secondary {
    background-color: var(--secondary-color);
    color: white;
}

.btn-secondary:hover {
    background-color: #616161;
}

.btn-danger {
    background-color: var(--error-color);
    color: white;
}

.btn-danger:hover {
    background-color: #d32f2f;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 0.875rem;
}

.btn-block {
    display: block;
    width: 100%;
}

/* ============================================================================
   FORMS
   ============================================================================ */
.form-container {
    max-width: 600px;
    margin: 0 auto;
}

.form-card {
    background: var(--card-bg);
    padding: 30px;
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: var(--text-color);
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    font-size: 1rem;
    font-family: inherit;
    transition: border-color 0.3s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
}

.form-group textarea {
    resize: vertical;
    min-height: 80px;
}

.form-help {
    display: block;
    margin-top: 5px;
    font-size: 0.875rem;
    color: var(--text-light);
}

.form-actions {
    display: flex;
    gap: 10px;
    margin-top: 30px;
}

.form-group-inline {
    display: flex;
    align-items: center;
    gap: 15px;
}

.form-group-inline label {
    margin-bottom: 0;
    white-space: nowrap;
}

.form-group-inline select {
    flex: 1;
}

/* ============================================================================
   ALERTS
   ============================================================================ */
.alert {
    padding: 15px 20px;
    border-radius: 5px;
    margin-bottom: 20px;
}

.alert-success {
    background-color: #d4edda;
    border: 1px solid #c3e6cb;
    color: #155724;
}

.alert-error {
    background-color: #f8d7da;
    border: 1px solid #f5c6cb;
    color: #721c24;
}

.alert-warning {
    background-color: #fff3cd;
    border: 1px solid #ffeeba;
    color: #856404;
}

/* ============================================================================
   CARDS
   ============================================================================ */
.stat-card,
.meter-card,
.action-card,
.settings-card,
.chart-card {
    background: var(--card-bg);
    border-radius: 10px;
    padding: 20px;
    box-shadow: var(--shadow);
    transition: transform 0.3s, box-shadow 0.3s;
}

.stat-card:hover,
.meter-card:hover {
    transform: translateY(-5px);
    box-shadow: var(--shadow-hover);
}

/* ============================================================================
   STATS GRID
   ============================================================================ */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    display: flex;
    align-items: center;
    gap: 20px;
}

.stat-icon {
    font-size: 3rem;
    line-height: 1;
}

.stat-content {
    flex: 1;
}

.stat-value {
    font-size: 2rem;
    font-weight: bold;
    color: var(--primary-color);
}

.stat-label {
    color: var(--text-light);
    font-size: 0.875rem;
}

/* ============================================================================
   METERS GRID
   ============================================================================ */
.meters-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 20px;
}

.meter-card {
    display: flex;
    flex-direction: column;
}

.meter-header {
    display: flex;
    align-items: center;
    gap: 15px;
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--border-color);
}

.meter-type-icon {
    font-size: 2.5rem;
    line-height: 1;
}

.meter-header h3 {
    margin: 0;
    font-size: 1.25rem;
}

.meter-type {
    display: inline-block;
    padding: 3px 10px;
    background-color: var(--bg-color);
    border-radius: 15px;
    font-size: 0.75rem;
    text-transform: uppercase;
    font-weight: 500;
}

.meter-body {
    flex: 1;
    margin-bottom: 15px;
}

.meter-body p {
    margin-bottom: 8px;
    font-size: 0.9rem;
}

.last-reading {
    margin-top: 15px;
    padding: 10px;
    background-color: var(--bg-color);
    border-radius: 5px;
}

.meter-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

/* ============================================================================
   CONTENT GRID
   ============================================================================ */
.content-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 30px;
}

.recent-readings,
.quick-actions {
    background: var(--card-bg);
    padding: 25px;
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.recent-readings h2,
.quick-actions h2 {
    margin-bottom: 20px;
    font-size: 1.5rem;
}

.action-card {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 15px;
    margin-bottom: 10px;
    background: var(--bg-color);
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-color);
    transition: all 0.3s;
}

.action-card:hover {
    background-color: var(--primary-color);
    color: white;
    transform: translateX(5px);
}

.action-icon {
    font-size: 2rem;
}

.action-title {
    font-weight: 500;
}

/* ============================================================================
   TABLES
   ============================================================================ */
.data-table {
    width: 100%;
    border-collapse: collapse;
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: var(--shadow);
}

.data-table thead {
    background: linear-gradient(135deg, #1976D2 0%, #2196F3 100%);
    color: white;
}

.data-table th {
    padding: 15px;
    text-align: left;
    font-weight: 500;
}

.data-table td {
    padding: 12px 15px;
    border-bottom: 1px solid var(--border-color);
}

.data-table tbody tr:hover {
    background-color: var(--bg-color);
}

.data-table tbody tr:last-child td {
    border-bottom: none;
}

/* ============================================================================
   EMPTY STATES
   ============================================================================ */
.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-light);
}

.empty-state-large {
    text-align: center;
    padding: 60px 20px;
    background: var(--card-bg);
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.empty-icon {
    font-size: 4rem;
    margin-bottom: 20px;
}

.empty-state-large h2 {
    margin-bottom: 10px;
    color: var(--text-color);
}

.empty-state-large p {
    color: var(--text-light);
    margin-bottom: 25px;
}

/* ============================================================================
   LOGIN PAGE
   ============================================================================ */
.login-page {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}

.login-container {
    width: 100%;
    max-width: 400px;
    padding: 20px;
}

.login-box {
    background: white;
    padding: 40px;
    border-radius: 15px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}

.login-box h1 {
    text-align: center;
    margin-bottom: 10px;
    color: var(--primary-color);
}

.subtitle {
    text-align: center;
    color: var(--text-light);
    margin-bottom: 30px;
}

/* ============================================================================
   CHARTS
   ============================================================================ */
.charts-container {
    display: grid;
    gap: 30px;
}

.chart-card {
    background: var(--card-bg);
    padding: 25px;
    border-radius: 10px;
    box-shadow: var(--shadow);
}

.chart-card h2 {
    margin-bottom: 20px;
    font-size: 1.5rem;
}

.chart-card canvas {
    max-height: 400px;
}

/* ============================================================================
   FILTER SECTION
   ============================================================================ */
.filter-section {
    background: var(--card-bg);
    padding: 20px;
    border-radius: 10px;
    box-shadow: var(--shadow);
    margin-bottom: 30px;
}

.filter-form {
    display: flex;
    gap: 20px;
    align-items: center;
    flex-wrap: wrap;
}

/* ============================================================================
   INFO BOX
   ============================================================================ */
.info-box {
    background: #e3f2fd;
    border-left: 4px solid var(--primary-color);
    padding: 15px;
    margin-bottom: 20px;
    border-radius: 5px;
}

.meter-info-card {
    background: var(--card-bg);
    padding: 20px;
    border-radius: 10px;
    box-shadow: var(--shadow);
    margin-bottom: 30px;
}

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
}

/* ============================================================================
   SETTINGS
   ============================================================================ */
.settings-container {
    max-width: 800px;
    margin: 0 auto;
    display: grid;
    gap: 20px;
}

.settings-card h2 {
    margin-bottom: 20px;
    font-size: 1.5rem;
    color: var(--text-color);
}

.profile-info p {
    margin-bottom: 10px;
    padding: 10px;
    background: var(--bg-color);
    border-radius: 5px;
}

/* ============================================================================
   SECTION
   ============================================================================ */
.section {
    background: var(--card-bg);
    padding: 25px;
    border-radius: 10px;
    box-shadow: var(--shadow);
    margin-bottom: 30px;
}

.section h2 {
    margin-bottom: 20px;
    font-size: 1.5rem;
}

/* ============================================================================
   FOOTER
   ============================================================================ */
.footer {
    background: #333;
    color: white;
    text-align: center;
    padding: 20px;
    margin-top: auto;
}

/* ============================================================================
   RESPONSIVE
   ============================================================================ */
@media (max-width: 768px) {
    .nav-menu {
        flex-wrap: wrap;
        justify-content: center;
    }
    
    .nav-menu a {
        padding: 10px 15px;
        font-size: 0.9rem;
    }
    
    .content-grid {
        grid-template-columns: 1fr;
    }
    
    .stats-grid {
        grid-template-columns: 1fr;
    }
    
    .meters-grid {
        grid-template-columns: 1fr;
    }
    
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .form-actions .btn {
        width: 100%;
    }
    
    .info-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    .container {
        padding: 20px 15px;
    }
    
    .form-card {
        padding: 20px;
    }
    
    .login-box {
        padding: 30px 20px;
    }
}

/* ============================================================================
   UTILITIES
   ============================================================================ */
.text-center {
    text-align: center;
}

.mt-20 {
    margin-top: 20px;
}

.mb-20 {
    margin-bottom: 20px;
}

.hidden {
    display: none;
}

/* === Mobile improvements (added) ========================================= */

/* Prevent horizontal scroll leaks from long tables/images/charts */
html, body { overflow-x: hidden; }
img, canvas, video { max-width: 100%; height: auto; display: block; }
.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; }

/* Hamburger toggle (hidden on desktop) */
.nav-toggle {
    display: none;
    background: transparent;
    border: 0;
    font-size: 1.8rem;
    color: #fff;
    padding: 10px;
    margin-left: auto;
    cursor: pointer;
}

/* On small screens: collapse menu */
@media (max-width: 768px) {
    .nav-container {
        align-items: center;
        gap: 10px;
    }
    .nav-toggle { display: block; }
    .nav-menu {
        display: none;
        flex-direction: column;
        width: 100%;
        background: rgba(0,0,0,0.1);
        border-radius: 10px;
        margin-top: 8px;
    }
    .nav-menu.open { display: flex; }
    .nav-menu li { width: 100%; }
    .nav-menu a { display: block; width: 100%; padding: 12px 14px; }
    
    /* Make action buttons expand */
    .btn { width: 100%; text-align: center; }
    
    /* Cards and forms padding */
    .card, .form-card, .filter-section { padding: 16px; }
}

/* Avoid overly wide max-widths on very small devices */
@media (max-width: 360px) {
    .login-box { padding: 20px 16px; }
}

/* Tables: ensure cells wrap instead of forcing overflow */
/* Nur in cardified-Ansicht sollen Inhalte umbrechen, NICHT überall */
table td { word-break: normal; }
table th { word-break: normal; white-space: nowrap; }

/* Cardified-Mobile-Ansicht (nur <768px): hier dürfen Werte umbrechen */
@media (max-width: 768px) {
  table.cardified td { word-break: break-word; }
}

/* === Desktop Tabellen-Verbesserungen ==================================== */
.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
}

.table thead th {
  background: #f4f7fb;       /* dezente Kopfzeile */
  color: #2f3944;
  font-weight: 600;
  border-bottom: 1px solid var(--border-color);
  padding: 10px 12px;
}

.table tbody td {
  padding: 10px 12px;
  border-bottom: 1px solid #f0f0f0;
  vertical-align: middle;
}

.table tbody tr:nth-child(even) td {
  background: #fafbfc;
}

/* Zahlen-Spalten rechtsbündig & ohne Zeilenbruch */
.table th.num,
.table td.num {
  text-align: right;
  white-space: nowrap;
}

/* Aktionen immer in einer Zeile anzeigen */
.table td.actions {
  white-space: nowrap;
}
.table td.actions .btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  margin-right: 6px;
}
.table td.actions .btn:last-child { margin-right: 0; }

/* Thumbnail statt grauem Button (aus meiner letzten Antwort) */
.thumb {
  max-width: 90px;
  max-height: 90px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: block;
}
.thumb-link { display: inline-block; line-height: 0; }

/* Kopfbereich-Aktionen: Desktop nebeneinander, Mobile gestapelt */
.page-header .actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
@media (max-width: 768px) {
  .page-header .actions { flex-direction: column; }
  .page-header .actions .btn { width: 100%; }
}

/* Cardified bleibt wie gehabt NUR mobil aktiv */
@media (max-width: 768px) {
  table.cardified { border-spacing: 0 12px; }
  table.cardified thead { display: none; }
  table.cardified tr { display: block; background: var(--card-bg); border-radius: 12px; box-shadow: var(--shadow); padding: 12px; }
  table.cardified td { display: grid; grid-template-columns: 40% 1fr; gap: 8px; padding: 6px 0; border: 0 !important; }
  table.cardified td::before { content: attr(data-label); font-weight: 600; color: var(--text-light); }
  .thumb { max-width: 120px; max-height: 120px; }
}

/* === History as responsive grid cards =================================== */
.history-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.history-item {
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 14px;
}

.hi-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.hi-date {
  font-weight: 600;
}

.hi-actions {
  white-space: nowrap;
}
.hi-actions .btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  margin-left: 6px;
}
.hi-actions .btn:first-child { margin-left: 0; }

/* Body: Wert, Bild (optional), Notizen */
.hi-body {
  display: grid;
  grid-template-columns: 1fr auto; /* Wert + Bild (falls da), Notizen läuft unterhalb */
  gap: 12px;
  align-items: start;
}

.hi-label {
  color: var(--text-light);
  font-size: 12px;
  margin-bottom: 4px;
}

.hi-strong {
  font-weight: 700;
  font-size: 1.05rem;
  white-space: nowrap;
}

.hi-image img {
  display: block;
  max-width: 110px;
  max-height: 110px;
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.hi-notes {
  grid-column: 1 / -1; /* immer volle Breite unter Wert/Bild */
}

.hi-text {
  white-space: normal;
  word-break: break-word;
}

/* Tablet/Desktop: etwas dichteres Layout */
@media (min-width: 769px) {
  .history-grid {
    grid-template-columns: 1fr; /* eine Spalte – schön luftig */
  }
  .hi-body {
    grid-template-columns: 1fr auto 1fr; /* Wert | Bild | Notizen */
    align-items: center;
  }
}

/* Mobile Layout Fix: Bearbeiten/Löschen Buttons */
@media (max-width: 480px) {
  .hi-actions {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 6px;
  }
  .hi-actions .btn {
    width: 100%;
    text-align: center;
  }
}

/* === Lightbox ============================================================ */
.lb {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: none;
  z-index: 2000;
}
.lb.open { display: block; }

.lb-toolbar {
  position: absolute;
  top: env(safe-area-inset-top, 8px);
  left: 50%;
  transform: translateX(-50%);
  display: inline-flex;
  gap: 8px;
  background: rgba(255,255,255,0.12);
  padding: 6px;
  border-radius: 999px;
  backdrop-filter: blur(6px);
}
.lb-btn {
  border: 0;
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 18px;
  line-height: 1;
  color: #fff;
  background: rgba(255,255,255,0.18);
}
.lb-btn.lb-close { background: rgba(255,60,60,0.5); }

.lb-stage {
  position: absolute;
  inset: 0;
  top: 44px;                     /* Platz für Toolbar */
  overflow: hidden;
  touch-action: none;            /* wir handlen Touch selbst */
  cursor: grab;
}
.lb-stage.grabbing { cursor: grabbing; }

#lb-img {
  /* Wir skalieren/verschieben per CSS Custom Props */
  max-width: none;
  max-height: none;
  transform: translate(var(--tx, 0px), var(--ty, 0px)) scale(var(--scale, 1));
  transform-origin: center center;
  user-select: none;
  -webkit-user-drag: none;
  display: block;
  margin: 0 auto;               /* Start mittig */
}
