/* ===========================================================================
   Dashboard "Ankauf Gebrauchtbatterien"

   Aufbau nach der bewährten Vorgängerversion: helle Seite, eine breite
   Tabelle, alles Weitere in Popups. Tabellenzellen sind oben ausgerichtet und
   enthalten je Batterie einen Block — dadurch wächst die Zeilenhöhe mit dem
   Inhalt und eine Anfrage ist vollständig lesbar, ohne sie zu öffnen.
   =========================================================================== */

:root {
  --primary: #061F40;
  --accent:  #F25C05;
  --link:    #D1271B;
  --bg:      #F2F2F2;
  --text:    #0D0D0D;
  --muted:   #6B7280;
  --border:  #E5E7EB;
  --ok:      #059669;
  --warn:    #D97706;
  --error:   #DC2626;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.hidden { display: none !important; }

#loading {
  position: fixed; inset: 0; z-index: 999;
  background: #fff;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 16px;
}
.spinner {
  width: 38px; height: 38px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 0 auto;
}
.spinner.big { width: 54px; height: 54px; border-width: 5px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

/* --- Kopfzeile ----------------------------------------------------------- */
header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; flex-wrap: wrap;
  padding: 26px 32px 14px;
}
header h1 { margin: 0; font-size: 24px; font-weight: 700; }
header .user { font-size: 12px; color: var(--muted); margin-top: 4px; }

.head-links { display: flex; gap: 22px; align-items: center; }
.hlink {
  border: none; background: none; padding: 0;
  color: var(--link);
  font-family: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.hlink:hover { text-decoration: underline; }

#globalMsg { padding: 0 32px; }

/* --- Tabelle ------------------------------------------------------------- */
.table-card {
  margin: 8px 32px 40px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0,0,0,.1);
}
.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }

thead th {
  padding: 12px 18px;
  background: var(--bg);
  border-bottom: 2px solid var(--border);
  text-align: left;
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em;
  white-space: nowrap;
  position: relative;
}
thead th.center { text-align: center; }
thead th.sortable { cursor: pointer; user-select: none; }
thead th .arrow { color: #9CA3AF; margin-left: 4px; }
thead th.active .arrow { color: var(--text); }

/* Feldauswahl für die gebündelte Spalte "Batterie-Daten" */
.sort-menu {
  display: none;
  position: absolute; left: 8px; top: 100%; z-index: 20;
  min-width: 160px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 6px 16px rgba(0,0,0,.12);
  padding: 4px 0;
}
thead th:hover .sort-menu { display: block; }
.sort-menu button {
  display: block; width: 100%;
  padding: 7px 12px;
  border: none; background: none;
  text-align: left; text-transform: none; letter-spacing: 0;
  font-family: inherit; font-size: 12px; font-weight: 400;
  cursor: pointer;
}
.sort-menu button:hover { background: var(--bg); }

tbody td {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  /* Der Kern der Anordnung: alles oben bündig, Zeile wächst mit dem Inhalt */
  vertical-align: top;
  font-size: 13px;
}
tbody td.center { text-align: center; }
tbody td.nowrap { white-space: nowrap; }
tbody tr:last-child td { border-bottom: none; }

.sub { color: var(--muted); font-size: 12px; }
.grow { flex: 1; }
.center { text-align: center; }
.right { text-align: right; }
a { color: var(--primary); }

/* Ein Block je Batterie, durch eine feine Linie getrennt */
.bat-block {
  padding-bottom: 10px; margin-bottom: 10px;
  border-bottom: 1px solid #F3F4F6;
}
.bat-block:last-child { border-bottom: none; padding-bottom: 0; margin-bottom: 0; }
.bat-nr { font-weight: 700; font-size: 11px; color: var(--primary); margin-bottom: 3px; }
.bat-name { font-weight: 700; }
.note { color: var(--muted); font-size: 12px; font-style: italic; margin-top: 3px; }

/* Korrigierte Werte: was die KI las, durchgestrichen — daneben der gültige Wert */
.was { color: var(--error); text-decoration: line-through; opacity: .6; }
.now { color: #2563EB; font-weight: 700; }

.tag {
  display: inline-block; margin-top: 5px;
  padding: 2px 8px; border-radius: 4px;
  background: rgba(242,92,5,.12); color: var(--accent);
  font-size: 11px; font-weight: 600;
}
.tag.warnish { background: #FEF3C7; color: #92400E; }

/* Nachträglich geänderter Wert: der Stift steht direkt daneben, der Tooltip
   nennt Zeitpunkt, Person und den vorherigen Wert. */
.edited {
  color: #92400E; font-size: 12px;
  cursor: help;
}

.pot {
  display: inline-block;
  width: 26px; height: 26px; line-height: 26px;
  border-radius: 50%;
  font-weight: 700; font-size: 12px; color: #fff;
  background: var(--muted);
}
.pot-5 { background: var(--ok); }
.pot-4 { background: #65A30D; }
.pot-3 { background: var(--warn); }
.pot-2 { background: #EA580C; }
.pot-1 { background: var(--error); }

.calc-sum { margin-top: 8px; }
.calc-sum strong { color: var(--accent); font-size: 16px; }

.empty { padding: 40px; text-align: center; color: var(--muted); }

/* --- Knöpfe -------------------------------------------------------------- */
.act {
  padding: 8px 14px;
  border: none; border-radius: 6px;
  font-family: inherit; font-size: 13px; font-weight: 600;
  cursor: pointer;
}
.act.small { padding: 5px 10px; font-size: 12px; }
.act.big   { padding: 13px 28px; font-size: 15px; }
.act.block { display: block; width: 100%; white-space: nowrap; }
.act.dark   { background: var(--primary); color: #fff; }
.act.accent { background: var(--accent); color: #fff; }
.act.primary{ background: var(--primary); color: #fff; }
.act.soft   { background: rgba(6,31,64,.08); color: var(--primary); }
.act.ghost  { background: #E5E7EB; color: var(--text); }
.act.danger { background: var(--error); color: #fff; }
.act:disabled { background: #D1D5DB; color: #6B7280; cursor: not-allowed; }

.icon-btn {
  border: none; background: none; cursor: pointer;
  font-size: 17px; color: #2563EB; padding: 2px 6px;
}
.icon-btn.danger { color: var(--error); }

.linkish {
  border: none; background: none; padding: 0;
  color: var(--primary); font-family: inherit; font-size: 13px;
  text-decoration: underline; cursor: pointer;
}

input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--primary); }

/* --- Popups --------------------------------------------------------------
   Sie schließen ausschließlich über ihre Knöpfe. Kein Outside-Click, kein
   Escape — sonst gehen Eingaben durch einen Fehlklick verloren (CLAUDE.md).
   ------------------------------------------------------------------------- */
.modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.box {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
  width: 100%; max-width: 680px;
  max-height: 92vh;
  display: flex; flex-direction: column;
}
.box.wide   { max-width: 940px; }
.box.narrow { max-width: 420px; }

.box-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 12px;
  padding: 22px 24px 14px;
}
.box-head h3 { margin: 0; font-size: 19px; color: var(--primary); }
.box-head h3.danger { color: var(--error); }
.box-sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.x {
  border: none; background: none; cursor: pointer;
  font-size: 26px; line-height: 1; color: var(--muted); padding: 0 4px;
}
.x:hover { color: var(--text); }
.x.small { font-size: 18px; }

.box-body { padding: 0 24px 8px; overflow-y: auto; flex: 1; }
.box-body.center { text-align: center; }
.box-body img { max-width: 100%; border-radius: 6px; }

.box-foot {
  display: flex; justify-content: flex-end; gap: 12px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
}

.section-head {
  margin: 22px 0 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.hint { color: var(--muted); font-size: 12px; line-height: 1.5; }
.lead { font-size: 15px; margin: 18px 0; }

/* --- Formularfelder in den Popups ---------------------------------------- */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

.field-row { margin-bottom: 12px; }
.field-row label {
  display: block; margin-bottom: 4px;
  font-size: 13px; font-weight: 700;
}
.field-row input, .field-row select, .field-row textarea,
.inline-form input, .inline-form select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: inherit; font-size: 14px;
  background: #fff;
}
.field-row textarea { resize: vertical; }
.orig { font-size: 11px; color: var(--error); margin-top: 3px; }

.edit-section {
  background: #F9FAFB;
  border-radius: 8px;
  padding: 16px 16px 4px;
  margin-bottom: 16px;
}
.edit-section .section-head { margin-top: 0; }

.inline-form {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  margin: 12px 0;
}
.inline-form input, .inline-form select { width: auto; flex: 1; min-width: 140px; }
.inline-form button { flex: none; }

/* --- Vollständigkeitsprüfung vor der Kalkulation ------------------------- */
.check-block {
  background: #F9FAFB;
  border-radius: 8px;
  padding: 14px 16px;
  margin-bottom: 12px;
}
.check-title { font-weight: 700; margin-bottom: 8px; }
.checks { list-style: none; margin: 0; padding: 0; }
.checks li {
  position: relative;
  padding-left: 24px; margin-bottom: 6px;
  font-size: 13px; line-height: 1.45;
}
.checks li::before {
  position: absolute; left: 0; top: 0;
  font-weight: 700;
}
.checks li.ok::before   { content: '✓'; color: var(--ok); }
.checks li.warn::before { content: '⚠'; color: var(--warn); }
.checks li.bad::before  { content: '✕'; color: var(--error); }
.checks li.warn { color: #92400E; }
.checks li.bad  { color: #991B1B; font-weight: 600; }

.busy { padding: 50px 0; }
.busy p { margin: 14px 0 0; }

/* --- Kalkulationsergebnis ------------------------------------------------ */
.result-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; flex-wrap: wrap;
  background: #ECFDF5;
  border: 1px solid #A7F3D0;
  border-radius: 8px;
  padding: 16px 18px;
  margin: 16px 0;
}
.result-head .total { font-size: 18px; font-weight: 700; color: #065F46; }

.panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  margin-bottom: 14px;
}
.panel .section-head { margin-top: 0; }
.panel textarea {
  width: 100%; padding: 10px;
  border: 1px solid var(--border); border-radius: 6px;
  font-family: inherit; font-size: 13px; resize: vertical;
}

table.breakdown { width: 100%; border-collapse: collapse; }
table.breakdown th {
  padding: 6px 0; border-bottom: 1px solid var(--border);
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .04em; text-align: left;
}
table.breakdown th.right, table.breakdown td.right { text-align: right; }
table.breakdown td { padding: 7px 0; font-size: 13px; }
table.breakdown tfoot td { border-top: 1px solid var(--border); padding-top: 10px; }

.rec {
  display: inline-block;
  padding: 2px 9px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
}
.rec-gut             { background: #D1FAE5; color: #065F46; }
.rec-durchschnittlich{ background: #FEF3C7; color: #92400E; }
.rec-schlecht        { background: #FEE2E2; color: #991B1B; }

/* --- Kalkulationsbasis --------------------------------------------------- */
.base-box {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  background: #EFF6FF;
  border-radius: 8px;
  padding: 16px 18px;
  margin: 16px 0;
}
.base-label { font-weight: 700; font-size: 15px; }

.unit-field { position: relative; display: inline-block; width: 130px; }
.unit-field.small { width: 84px; }
.unit-field input {
  width: 100%;
  padding: 8px 26px 8px 10px;
  border: 1px solid var(--border); border-radius: 6px;
  font-family: inherit; font-size: 14px; text-align: right;
}
.unit-field .unit {
  position: absolute; right: 9px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-size: 13px; pointer-events: none;
}

.criteria-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.criteria-group {
  background: #F9FAFB;
  border-radius: 8px;
  padding: 12px 14px;
}
.criteria-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.criteria-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 6px;
}
.opt-label { font-size: 13px; }

.history-row {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.type-row {
  display: flex; gap: 12px; align-items: center;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.type-row strong { min-width: 90px; }

/* Typ-Kategorie als Pille — im runden Potenzial-Kreis hätte „Kat. 1" keinen Platz */
.cat {
  display: inline-block;
  padding: 3px 10px; border-radius: 999px;
  font-size: 11px; font-weight: 700; white-space: nowrap;
}
.cat-1 { background: #D1FAE5; color: #065F46; }
.cat-2 { background: #FEF3C7; color: #92400E; }
.cat-3 { background: #F3F4F6; color: var(--muted); }

/* --- Galerien ------------------------------------------------------------ */
.gallery { display: flex; flex-wrap: wrap; gap: 12px; }
.gallery figure {
  margin: 0; width: 190px;
  border: 1px solid var(--border); border-radius: 8px;
  overflow: hidden; background: #fff;
}
.gallery img {
  width: 100%; height: 140px; object-fit: cover;
  display: block; cursor: zoom-in;
  background: #F3F4F6;
}
.gallery img.broken {
  cursor: default;
  font-size: 12px; color: var(--muted);
}
.gallery figcaption {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 9px; font-size: 12px;
}
.gallery figcaption button {
  border: none; background: none; color: var(--error);
  cursor: pointer; font-size: 13px;
}

/* Kennzeichnung für Bilder, die nicht aus dem Formular des Kunden stammen,
   sondern später im Dashboard nachgereicht wurden. */
.late-note {
  padding: 4px 9px;
  background: #FEF3C7; color: #92400E;
  font-size: 11px; font-weight: 600;
}

/* --- Meldungen ----------------------------------------------------------- */
.msg {
  padding: 11px 14px;
  border-radius: 6px;
  margin: 10px 0;
  font-size: 13px; line-height: 1.5;
}
.msg.error { background: #FEE2E2; color: #991B1B; }
.msg.warn  { background: #FEF3C7; color: #92400E; }
.msg.info  { background: #DBEAFE; color: #1E40AF; }
.msg.ok    { background: #D1FAE5; color: #065F46; }

@media (max-width: 720px) {
  header, #globalMsg { padding-left: 16px; padding-right: 16px; }
  .table-card { margin-left: 8px; margin-right: 8px; }
  .two-col, .criteria-grid { grid-template-columns: 1fr; }
}
