:root {
  --green-900: #17392a;
  --green-700: #235c3f;
  --green-600: #2f7a53;
  --green-500: #3f9765;
  --green-100: #e7f3ec;
  --gray-900: #1f2a24;
  --gray-600: #5b675f;
  --gray-300: #d9e2dc;
  --gray-100: #f5f8f6;
  --white: #ffffff;
  --danger: #b3432b;
  --warning: #b8860b;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(23, 57, 42, 0.08), 0 1px 2px rgba(23, 57, 42, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--gray-100);
  color: var(--gray-900);
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--green-700); text-decoration: none; }
a:hover { text-decoration: underline; }

.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 220px;
  background: var(--green-900);
  color: var(--white);
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  flex-shrink: 0;
}

.sidebar .logo-area {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 20px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.15);
  margin-bottom: 16px;
}

.sidebar .logo-area.has-logo { background: var(--white); border-radius: 10px; margin: 0 14px 18px; padding: 10px 12px; border-bottom: none; }
.sidebar .logo-area.has-logo a { display: block; line-height: 0; }
.sidebar .logo-area img { display: block; width: 100%; height: auto; max-width: 150px; }
.login-logo { display: block; max-width: 170px; height: auto; margin: 0 auto 14px; }
.sidebar .logo-area .brand { font-weight: 700; font-size: 16px; letter-spacing: 0.02em; }

.sidebar nav { display: flex; flex-direction: column; gap: 2px; }

.sidebar nav a {
  color: rgba(255,255,255,0.85);
  padding: 10px 20px;
  font-size: 14px;
  border-left: 3px solid transparent;
}

.sidebar nav a:hover, .sidebar nav a.active {
  background: rgba(255,255,255,0.08);
  color: var(--white);
  text-decoration: none;
  border-left-color: var(--green-500);
}

.sidebar .spacer { flex: 1; }

.sidebar .user-area {
  padding: 12px 20px 0;
  border-top: 1px solid rgba(255,255,255,0.15);
  font-size: 13px;
  color: rgba(255,255,255,0.7);
}
.sidebar .user-area a { color: rgba(255,255,255,0.85); }

.main { flex: 1; padding: 28px 36px; max-width: 1200px; min-width: 0; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.page-header h1 { margin: 0; font-size: 22px; color: var(--green-900); }

.card {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 22px;
  margin-bottom: 20px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--green-600);
  color: var(--white);
  border: none;
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}
.btn:hover { background: var(--green-700); text-decoration: none; }
.btn.secondary { background: var(--white); color: var(--green-700); border: 1px solid var(--gray-300); }
.btn.secondary:hover { background: var(--green-100); }
.btn.danger { background: var(--white); color: var(--danger); border: 1px solid var(--gray-300); }
.btn.danger:hover { background: #fbeae5; }
.btn.small { padding: 5px 10px; font-size: 13px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--gray-300); font-size: 14px; }
th { color: var(--gray-600); font-weight: 600; font-size: 13px; text-transform: uppercase; letter-spacing: 0.03em; }
tr:hover td { background: var(--gray-100); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px 20px; }
.form-grid.single { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: var(--gray-600); }
input, select, textarea {
  padding: 9px 11px;
  border: 1px solid var(--gray-300);
  border-radius: 7px;
  font-size: 14px;
  font-family: inherit;
  background: var(--white);
  color: var(--gray-900);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--green-500); outline-offset: 0; border-color: var(--green-500); }
textarea { resize: vertical; }

.search-bar { display: flex; gap: 10px; margin-bottom: 16px; }
.search-bar input[type=search] { flex: 1; max-width: 360px; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge.entwurf { background: var(--gray-300); color: var(--gray-900); }
.badge.versendet { background: #fff3cd; color: var(--warning); }
.badge.angenommen { background: var(--green-100); color: var(--green-700); }
.badge.abgelehnt { background: #fbeae5; color: var(--danger); }

.totals { margin-left: auto; width: 320px; }
.totals .row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14px; }
.totals .row.grand { font-weight: 700; font-size: 17px; border-top: 2px solid var(--gray-300); padding-top: 10px; margin-top: 4px; color: var(--green-900); }

.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--green-900); }
.login-card { background: var(--white); border-radius: var(--radius); padding: 36px 40px; width: 360px; box-shadow: var(--shadow); }
.login-card h1 { text-align: center; color: var(--green-900); font-size: 20px; margin-bottom: 6px; }
.login-card p.subtitle { text-align: center; color: var(--gray-600); font-size: 13px; margin-bottom: 20px; }
.login-card .field { margin-bottom: 14px; }
.login-card .btn { width: 100%; justify-content: center; margin-top: 6px; }

.error-msg { background: #fbeae5; color: var(--danger); padding: 10px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 14px; }
.success-msg { background: var(--green-100); color: var(--green-700); padding: 10px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 14px; }
.hint { color: var(--gray-600); font-size: 12.5px; margin-top: 2px; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 20px; }
.stat-card { background: var(--white); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.stat-card .value { font-size: 28px; font-weight: 700; color: var(--green-700); }
.stat-card .label { font-size: 13px; color: var(--gray-600); }

.items-table input { width: 100%; min-width: 60px; }
.items-table td { vertical-align: top; }
.items-table .desc-cell textarea { width: 100%; min-height: 44px; }
.autocomplete { position: relative; }
.autocomplete-results {
  position: absolute; top: 100%; left: 0; right: 0; background: var(--white);
  border: 1px solid var(--gray-300); border-radius: 8px; box-shadow: var(--shadow);
  max-height: 240px; overflow-y: auto; z-index: 20; display: none;
}
.autocomplete-results.open { display: block; }
.autocomplete-results .result-item { padding: 8px 12px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--gray-100); }
.autocomplete-results .result-item:hover { background: var(--green-100); }
.autocomplete-results .result-item .meta { color: var(--gray-600); font-size: 12px; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--gray-600); }

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: space-between;
             padding: 6px 12px; position: static; }
  .sidebar .logo-area.has-logo { margin: 0; padding: 4px 8px; }
  .sidebar .logo-area img { max-width: 74px; }
  .sidebar .logo-area { border-bottom: none; margin-bottom: 0; padding: 0 4px; }
  .sidebar .spacer { display: none; }
  .menu-toggle { display: inline-flex; background: rgba(255,255,255,0.12); color: var(--white); border: none; border-radius: 8px;
                 padding: 10px 16px; font-size: 16px; font-weight: 600; cursor: pointer; }
  .sidebar nav { display: none; width: 100%; flex-direction: column; margin-top: 8px; }
  .sidebar.open nav { display: flex; }
  .sidebar nav a { padding: 14px 16px; font-size: 16px; }
  .sidebar nav a.mobile-only { display: block; border-top: 1px solid rgba(255,255,255,0.15); margin-top: 6px; }
  .sidebar .user-area { display: none; }
  .main { padding: 18px; }
  .form-grid { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: 1fr; }
}

/* Mitlaufende Speicherleiste (savebar.js) */
.save-bar { position: sticky; bottom: 12px; z-index: 30; display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  margin: 18px 0 0; padding: 10px 14px; background: var(--white); border: 1px solid var(--gray-300); border-radius: 12px;
  box-shadow: 0 4px 18px rgba(0,0,0,.16); }
.save-bar .save-state { margin-left: auto; font-size: 13px; color: var(--gray-600); }
.save-bar.dirty { border-color: #d99a00; box-shadow: 0 4px 18px rgba(217,154,0,.28); }
.save-bar.dirty .save-state { color: #a05a00; font-weight: 700; }
@media (max-width: 700px) {
  .save-bar { bottom: 8px; padding: 8px 10px; }
  .save-bar .btn:first-child { flex: 1 1 auto; justify-content: center; padding: 12px 18px; font-size: 16px; }
  .save-bar .save-state { flex: 1 1 100%; margin-left: 0; text-align: center; font-size: 12px; }
}

/* Schnell-Dialog (Kunde anlegen) */
dialog.modal { border: none; border-radius: 12px; padding: 22px 24px; width: min(520px, 94vw); box-shadow: 0 12px 40px rgba(0,0,0,.28); }
dialog.modal::backdrop { background: rgba(23, 57, 42, .45); }
.select-row { display: flex; gap: 8px; align-items: stretch; }
.select-row select { flex: 1; min-width: 0; }
.select-row .btn { white-space: nowrap; }

/* Suchfeld statt Dropdown (searchable_select.js) */
.ss { position: relative; flex: 1; min-width: 0; }
.ss-native { position: absolute; left: 0; bottom: 0; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.ss-input { width: 100%; }
.ss-input.invalid { border-color: var(--danger); }
.ss-list { position: absolute; top: calc(100% + 2px); left: 0; right: 0; background: var(--white); border: 1px solid var(--gray-300);
  border-radius: 8px; box-shadow: var(--shadow); max-height: 280px; overflow-y: auto; z-index: 40; display: none; }
.ss-list.open { display: block; }
.ss-item { padding: 8px 12px; cursor: pointer; font-size: 14px; border-bottom: 1px solid var(--gray-100); }
.ss-item.active { background: var(--green-100); }
.ss-item.current { font-weight: 700; }
.ss-empty { padding: 8px 12px; font-size: 13px; color: var(--gray-600); }

.menu-toggle, .sidebar nav a.mobile-only { display: none; }
@media (max-width: 900px) {
  .menu-toggle { display: inline-flex; }
  .sidebar nav a.mobile-only { display: block; }
}

/* Status direkt in der Liste ändern */
select.status-select { border: none; border-radius: 999px; padding: 4px 26px 4px 12px; font-size: 12.5px; font-weight: 600;
  cursor: pointer; width: auto; min-width: 0; }
select.status-select:focus { outline: 2px solid var(--green-500); }
select.status-select.saving { opacity: .55; }
select.status-select.error { outline: 2px solid var(--danger); }

/* Handy: Tabellen als Karten, nichts läuft mehr über den Rand */
@media (max-width: 700px) {
  .main { min-width: 0; width: 100%; padding: 14px 12px; overflow-x: clip; }
  .card { overflow-x: auto; padding: 14px 12px; }
  table.stack-mobile, table.stack-mobile tbody, table.stack-mobile tr, table.stack-mobile td { display: block; width: 100%; }
  table.stack-mobile thead { display: none; }
  table.stack-mobile tr { border: 1px solid var(--gray-300); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; background: var(--white); }
  table.stack-mobile td { display: flex; justify-content: space-between; align-items: center; gap: 12px; border: none; padding: 5px 0;
                          text-align: right; font-size: 15px; }
  table.stack-mobile td::before { content: attr(data-label); flex: none; font-size: 12px; font-weight: 600; text-transform: uppercase;
                                  color: var(--gray-600); text-align: left; }
  table.stack-mobile td[data-label=""]::before { content: none; }
  table.stack-mobile td[data-label=""] { justify-content: flex-end; padding-top: 8px; }
  table.stack-mobile td[data-label=""] a { background: var(--green-100); color: var(--green-700); font-weight: 600; padding: 10px 18px; border-radius: 8px; }
  table.stack-mobile td:first-child { font-weight: 700; font-size: 16px; }
  table.stack-mobile td.num { text-align: right; }
}

@media (max-width: 900px) {
  .autocomplete-results { max-height: 36vh; overflow-y: auto; }
  .autocomplete-results .result-item { padding: 12px 14px; font-size: 15px; }
}

/* Schnellzugriff auf dem Dashboard */
.quick-actions { display: flex; gap: 10px; flex-wrap: wrap; }
@media (max-width: 700px) {
  .quick-actions { width: 100%; flex-direction: column; }
  .quick-actions .btn { width: 100%; justify-content: center; padding: 16px 18px; font-size: 17px; }
}

@media (max-width: 700px) {
  .stat-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .stat-card { padding: 12px 10px; }
  .stat-card .value { font-size: 22px; }
  .stat-card .label { font-size: 12px; }
}

.ss-item.custom { color: var(--green-700); font-weight: 600; border-bottom: 1px solid var(--gray-300); }
