:root {
  --color-bg: #f4f5f7;
  --color-surface: #ffffff;
  --color-border: #dde1e6;
  --color-text: #1b1f24;
  --color-muted: #667085;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-danger: #dc2626;
  --color-success-bg: #ecfdf3;
  --color-success-text: #027a48;
  --color-error-bg: #fef3f2;
  --color-error-text: #b42318;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #14171a;
    --color-surface: #1c2024;
    --color-border: #333a42;
    --color-text: #e6e9ec;
    --color-muted: #9aa4af;
    --color-primary: #3b82f6;
    --color-primary-hover: #60a5fa;
    --color-danger: #f87171;
    --color-success-bg: #0f2b1e;
    --color-success-text: #6ee7b7;
    --color-error-bg: #2b1414;
    --color-error-text: #fca5a5;
  }
}

* { box-sizing: border-box; }

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

a { color: var(--color-primary); }

.container {
  width: 100%;
  /* Grosszuegige Obergrenze statt fester Rahmenbreite: auf jedem normalen
     Bildschirm (bis ~27"/1600px) faktisch voll ausgenutzt, verhindert nur
     auf sehr breiten 4K/5K-Monitoren unlesbar lange Zeilen. */
  max-width: 1600px;
  margin: 0 auto;
  padding: 1.5rem clamp(1rem, 3vw, 2.5rem) 4rem;
}

.topbar {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.nav {
  width: 100%;
  max-width: 1600px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: 0.75rem clamp(1rem, 3vw, 2.5rem);
  flex-wrap: wrap;
  row-gap: 0.5rem;
}

.nav .brand {
  font-weight: 700;
  text-decoration: none;
  color: var(--color-text);
}

.nav a {
  text-decoration: none;
  color: var(--color-text);
}

.nav a:hover { color: var(--color-primary); }

.nav-spacer { flex: 1; }

.nav-user { color: var(--color-muted); font-size: 0.9rem; }

.inline-form { display: inline; }

.link-button {
  background: none;
  border: none;
  padding: 0;
  color: var(--color-primary);
  cursor: pointer;
  font: inherit;
  text-decoration: underline;
}

.link-danger { color: var(--color-danger); }

.flash-container { margin-bottom: 1rem; display: flex; flex-direction: column; gap: 0.5rem; }

.flash {
  padding: 0.65rem 1rem;
  border-radius: var(--radius);
  font-size: 0.95rem;
}

.flash-success { background: var(--color-success-bg); color: var(--color-success-text); }
.flash-error { background: var(--color-error-bg); color: var(--color-error-text); }

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

.page-header h1 { margin: 0; }

.page-header-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }

.toolbar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.toolbar input[type="text"] {
  flex: 1;
  min-width: 220px;
  padding: 0.65rem 0.9rem;
  font-size: 1.05rem;
  background: var(--color-surface);
  border: 2px solid var(--color-primary);
  box-shadow: 0 1px 4px rgba(37, 99, 235, 0.15);
}

.toolbar input[type="text"]:focus {
  outline: none;
  border-color: var(--color-primary-hover);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.25);
}

.toolbar-shop-filter {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: 6px;
}

.checkbox-inline {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  white-space: nowrap;
}

.button-primary,
.button-secondary {
  display: inline-block;
  padding: 0.55rem 1rem;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font: inherit;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}

.button-primary {
  background: var(--color-primary);
  color: #fff;
}
.button-primary:hover { background: var(--color-primary-hover); }

.button-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
}

.button-primary:disabled,
.button-secondary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.table-scroll { overflow-x: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  border-radius: var(--radius);
  overflow: hidden;
}

.data-table th,
.data-table td {
  padding: 0.2rem 0.5rem;
  border-bottom: 1px solid var(--color-border);
  text-align: left;
  white-space: nowrap;
  line-height: 1.2;
}

.data-table th { color: var(--color-muted); font-weight: 600; font-size: 0.85rem; }
.data-table th a { color: inherit; text-decoration: none; }
.data-table th a:hover { color: var(--color-primary); }

.data-table td.center { text-align: center; }

.data-table td.col-produktid,
.data-table th.col-produktid {
  font-size: 0.75rem;
}

.data-table td.col-titel {
  max-width: 300px;
  white-space: normal;
  font-size: 1.05rem;
}
.data-table td.col-titel a {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  max-width: 100%;
  overflow-wrap: break-word;
  word-break: break-word;
}

.thumb {
  display: block;
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--color-bg);
}

.table-scroll .thumb { width: 56px; height: 56px; }

.hover-zoom {
  cursor: zoom-in;
}

.hover-zoom-popup {
  position: absolute;
  z-index: 2000;
  pointer-events: none;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  padding: 6px;
}

.hover-zoom-popup img {
  display: block;
  max-width: min(70vw, 500px);
  max-height: min(70vh, 500px);
  width: auto;
  height: auto;
}

/* Kachelansicht der Produktliste fuer schmale Bildschirme - auf dem Desktop
   nicht sichtbar, dort bleibt die Tabelle (.table-scroll) die einzige Ansicht. */
.produkte-cards { display: none; }

.inline-edit-number { width: 80px; }

select.inline-edit { max-width: 80px; }

.data-table input.inline-edit,
.data-table select.inline-edit {
  padding: 0.2rem 0.4rem;
  line-height: 1.2;
}

.inline-edit.inline-saved {
  outline: 0px solid var(--color-success-text);
  outline-offset: 1px;
}
.inline-edit.inline-error {
  outline: 0px solid var(--color-danger);
  outline-offset: 1px;
}

.inline-edit-clear-date {
  width: 1.5rem;
  height: 1.5rem;
  line-height: 1;
  padding: 0;
  margin-left: 0.25rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: var(--color-surface);
  color: var(--color-muted);
  cursor: pointer;
}

.row-actions { display: flex; 
    flex-direction: column; /* Richtet die Elemente untereinander aus */
   /* align-items: flex-start; /* Verhindert, dass die Buttons sich über die ganze Breite strecken */
    gap: 1px; /* Abstand zwischen den Elementen */
  }

.badge {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  background: var(--color-border);
  font-size: 0.8rem;
}
.badge-active { background: #dbeafe; color: #1d4ed8; }
.badge-done { background: var(--color-success-bg); color: var(--color-success-text); }

.pagination { display: flex; align-items: center; gap: 0.35rem; margin-top: 1rem; flex-wrap: wrap; }
.page-link {
  padding: 0.35rem 0.7rem;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  text-decoration: none;
  color: var(--color-text);
}
.page-link.active { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }
.page-link.disabled { opacity: 0.4; cursor: default; }
.page-nav { font-weight: 600; }
.page-ellipsis { padding: 0 0.2rem; color: var(--color-muted); }
.page-info { margin-left: 0.5rem; color: var(--color-muted); font-size: 0.85rem; }

.auth-box {
  max-width: 380px;
  margin: 3rem auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 2rem;
}

.form fieldset {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
  padding: 1rem 1.25rem 1.25rem;
}

.form legend {
  padding: 0 0.4rem;
  font-weight: 600;
  color: var(--color-muted);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.9rem;
}

.form-field { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.9rem; }
.form fieldset > .form-field:last-child { margin-bottom: 0; }

.form-field-checkbox { flex-direction: row; align-items: center; }
.form-field-checkbox label { display: flex; align-items: center; gap: 0.5rem; }

label { font-size: 0.9rem; color: var(--color-muted); }

input[type="text"],
input[type="number"],
input[type="password"],
input[type="datetime-local"],
input[type="file"],
select,
textarea {
  padding: 0.5rem 0.6rem;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  color: var(--color-text);
  font: inherit;
}

textarea { resize: vertical; }

.readonly-text {
  padding: 0.5rem 0.6rem;
  border-radius: var(--radius);
  border: 1px dashed var(--color-border);
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 4.5rem;
  white-space: pre-wrap;
}

.field-error {
  color: var(--color-danger);
  font-size: 0.85rem;
}

.form-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
}

.subtable {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 0.75rem;
}
.subtable th, .subtable td { padding: 0.4rem; text-align: left; }
.subtable input[type="text"] { width: 100%; }

.image-gallery-section {
  margin-bottom: 1.5rem;
}
.image-gallery-section h2 {
  font-size: 1rem;
  color: var(--color-muted);
  margin: 0 0 0.6rem;
}

.image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.image-item {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 0.5rem;
  text-align: center;
  background: var(--color-bg);
  /* cursor: grab gilt nur fuer die Maus - Drag&Drop ist bewusst auf Desktop
     beschraenkt (siehe image-position.js), touch-action bleibt hier daher
     auf dem Standardwert, damit natives Scrollen/Zoomen auf dem
     iPhone/iPad normal funktioniert. */
  cursor: grab;
}
.image-item.dragging {
  opacity: 0.4;
}
.image-item img {
  max-width: 100%;
  max-height: 140px;
  object-fit: contain;
  display: block;
  margin: 0 auto 0.5rem;
}
.image-position-label {
  display: block;
  font-size: 0.85rem;
  color: var(--color-muted);
  margin-bottom: 0.4rem;
}
.image-position-input {
  width: 4rem;
  margin-left: 0.3rem;
}

.image-preview-name {
  font-size: 0.75rem;
  color: var(--color-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#images-preview:not(:empty) {
  margin-top: 0.75rem;
}

.muted { color: var(--color-muted); }
.ebay-nummer { font-size: 0.8rem; }

.record-count { margin: -0.25rem 0 0.75rem; font-size: 0.9rem; }
.export-select-all-label { margin-left: 0.75rem; cursor: pointer; }
.export-select-all-label input { margin-right: 0.35rem; }

.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.loading-box {
  background: var(--color-surface);
  color: var(--color-text);
  padding: 2rem 2.5rem;
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  text-align: center;
}

.spinner {
  width: 42px;
  height: 42px;
  border: 4px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ------------------------------------------------------------------ */
/* Zwischenstufe fuer schmalere Desktop-/Laptop-Fenster: Tabelle bleibt   */
/* erhalten (Kachelansicht erst ab 640px), aber kompakter, damit sie      */
/* beim Verkleinern des Fensters nicht schlagartig horizontal scrollt.    */
/* ------------------------------------------------------------------ */

@media (max-width: 1100px) {
  .data-table th,
  .data-table td {
    padding: 0.35rem 0.35rem;
    font-size: 0.78rem;
  }

  .table-scroll .thumb { width: 56px; height: 56px; }

  .inline-edit-number { width: 56px; }
  select.inline-edit { max-width: 76px; font-size: 0.75rem; }
  .data-table input[type="date"] { width: 96px; font-size: 0.72rem; padding: 0.2rem; }
  .inline-edit-clear-date { width: 1.1rem; height: 1.1rem; font-size: 0.7rem; }

  /* Titel bekommt so viel Platz wie moeglich, alle anderen Spalten werden auf
     ein Minimum geschrumpft (siehe nth-child-Regeln unten). Bei sehr langen
     Werten (z.B. eBay-Nummer) scrollt .table-scroll notfalls horizontal,
     statt die Spalten wieder aufzublaehen. */
  .data-table td.col-titel {
    max-width: none;
    width: 45%;
    font-size: 1.05rem;
  }

  .data-table th:nth-child(1), .data-table td:nth-child(1) { width: 20px; }
  .data-table th:nth-child(2), .data-table td:nth-child(2) { width: 46px; }
  .data-table th:nth-child(3), .data-table td:nth-child(3) { width: 40px; }
  .data-table th:nth-child(5), .data-table td:nth-child(5) { width: 60px; }
  .data-table th:nth-child(8), .data-table td:nth-child(8) { width: 32px; }
}

/* ------------------------------------------------------------------ */
/* Responsive Anpassungen fuer kleinere Bildschirme (Tablet/Smartphone) */
/* ------------------------------------------------------------------ */

@media (max-width: 640px) {
  .container { padding: 1rem 1rem 3rem; }
  .nav { padding: 0.6rem 1rem; gap: 0.75rem; }
  .nav-user { display: none; }

  .page-header { gap: 0.5rem; }
  .page-header-actions { width: 100%; }
  .page-header .button-primary,
  .page-header .button-secondary { flex: 1 1 100%; }

  .toolbar select,
  .toolbar input[type="text"] {
    flex: 1 1 100%;
    min-width: 0;
  }

  .form-grid { grid-template-columns: 1fr; }

  .form-actions { flex-wrap: wrap; }
  .form-actions .button-primary,
  .form-actions .button-secondary { flex: 1 1 auto; }

  .auth-box {
    margin: 1.5rem auto;
    padding: 1.5rem;
    width: calc(100% - 2rem);
  }

  /* Produktliste: Tabelle ausblenden, stattdessen Kachelansicht zeigen. */
  .table-scroll { display: none; }

  .produkte-cards {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .produkt-card {
    display: flex;
    gap: 1rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 1rem;
  }

  .produkt-card-media { flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; gap: 0.35rem; }
  .produkt-card-media .thumb { width: 72px; height: 72px; }
  .produkt-card-select { display: flex; }

  .produkt-card-body {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }

  .produkt-card-title {
    font-weight: 600;
    text-decoration: none;
    color: var(--color-text);
  }

  .produkt-card-meta { font-size: 0.8rem; color: var(--color-muted); }

  .produkt-card-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
  }
  .produkt-card-row label { flex: 0 0 auto; margin: 0; }
  .produkt-card-row input[type="number"],
  .produkt-card-row select {
    flex: 1 1 auto;
    min-width: 0;
    max-width: 60%;
  }

  .produkt-card-checkboxes {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.3rem;
  }
  .produkt-card-checkboxes label {
    display: flex;
    align-items: center;
    gap: 0.4rem;
  }

  .produkt-card-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.25rem;
  }
}
