:root {
  --sidebar-bg: #1c2530;
  --sidebar-bg-hover: #2a3646;
  --accent: #3b82f6;
  --accent-dark: #2563eb;
  --bg: #f4f6f9;
  --card-bg: #ffffff;
  --border: #e3e7ed;
  --text: #1f2733;
  --text-muted: #6b7684;
  --danger: #e0384f;
  --success: #16a34a;
  --warning: #d97706;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

/* ---------- Login ---------- */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1c2530, #33445c);
  padding: 20px;
}
.login-box {
  background: var(--card-bg);
  padding: 44px 40px 36px;
  border-radius: 16px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 25px 60px rgba(0,0,0,0.3);
}
.login-logo {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  display: block;
  margin: 0 auto 18px;
  box-shadow: 0 8px 20px rgba(59,130,246,0.35);
}
.login-box h1 {
  font-size: 23px;
  margin: 0 0 4px;
  text-align: center;
}
.login-box p.sub {
  color: var(--text-muted);
  margin: 0 0 28px;
  font-size: 13.5px;
  text-align: center;
}
.login-box .field { margin-bottom: 18px; }
.login-box .field label { font-size: 12.5px; margin-bottom: 6px; }
.login-box input {
  padding: 11px 14px;
  font-size: 14.5px;
  border-radius: 9px;
}
.login-box input:focus {
  border-color: var(--accent);
}
.login-box button.btn {
  padding: 12px 16px;
  font-size: 14.5px;
  border-radius: 9px;
  margin-top: 6px;
}
.login-box .error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
  min-height: 16px;
  text-align: center;
}

/* ---------- App Shell ---------- */
#app-shell {
  min-height: 100vh;
}

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 230px;
  background: var(--sidebar-bg);
  color: #cfd6df;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  z-index: 100;
}
.brand {
  font-weight: 700;
  font-size: 17px;
  color: #fff;
  padding: 0 20px 20px;
  border-bottom: 1px solid #2c3a4c;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand-logo {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  flex-shrink: 0;
}
#sidebar-close {
  display: none;
  margin-left: auto;
  background: none;
  border: none;
  color: #cfd6df;
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
}
#sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 28, 0.55);
  z-index: 95;
}
#menu-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text);
  padding: 4px 8px;
  margin-right: 4px;
}
.nav-item {
  padding: 12px 20px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background 0.15s;
  border-left: 3px solid transparent;
}
.nav-item:hover { background: var(--sidebar-bg-hover); }
.nav-item.active {
  background: var(--sidebar-bg-hover);
  border-left-color: var(--accent);
  color: #fff;
}
.nav-section-label {
  padding: 16px 20px 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6b7684;
}
.sidebar-footer {
  margin-top: auto;
  padding: 14px 20px;
  border-top: 1px solid #2c3a4c;
  font-size: 12.5px;
}
.sidebar-footer button {
  background: none;
  border: none;
  color: #cfd6df;
  cursor: pointer;
  font-size: 12.5px;
  padding: 0;
  margin-top: 6px;
}
.sidebar-footer button:hover { color: #fff; }
.credit-link {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #2c3a4c;
  color: #6b7684;
  font-size: 11.5px;
  text-decoration: none;
}
.credit-link:hover { color: #cfd6df; }
.credit-link svg { flex-shrink: 0; }

#main {
  margin-left: 230px;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 100vh;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
}
.company-switch { position: relative; }
.company-switch button {
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}
.company-switch img.logo-thumb {
  width: 20px; height: 20px; object-fit: contain; border-radius: 4px;
}
.company-dropdown {
  position: absolute;
  top: 42px;
  left: 0;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.1);
  min-width: 220px;
  z-index: 50;
  overflow: hidden;
}
.company-dropdown .item {
  padding: 10px 14px;
  cursor: pointer;
  font-size: 14px;
}
.company-dropdown .item:hover { background: var(--bg); }
.company-dropdown .item.active { font-weight: 700; color: var(--accent); }

#content {
  padding: 28px;
  overflow-y: auto;
  flex: 1;
}

h2.page-title {
  margin: 0 0 20px;
  font-size: 22px;
}

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}
.btn:hover { background: var(--accent-dark); }
.btn.secondary { background: #eef1f5; color: var(--text); }
.btn.secondary:hover { background: #e3e7ed; }
.btn.danger { background: var(--danger); }
.btn.danger:hover { background: #c92e43; }
.btn.small { padding: 5px 10px; font-size: 12.5px; border-radius: 6px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

input, select, textarea {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { font-size: 12.5px; color: var(--text-muted); font-weight: 600; display: block; margin-bottom: 4px; }
.field { margin-bottom: 14px; }
.field input:not([type="checkbox"]):not([type="radio"]),
.field select,
.field textarea { width: 100%; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }

/* ---------- Reiter (z.B. Einstellungen) ---------- */
.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
  /* Reiter bleiben auf schmalen Displays horizontal scrollbar, der Scrollbalken
     selbst wird jedoch ausgeblendet. overflow-y bleibt sichtbar ausgeschaltet,
     damit der negative margin-bottom der Reiter keine vertikale Leiste ausloest. */
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs .tab {
  appearance: none;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
}
.tabs .tab:hover { color: var(--text); }
.tabs .tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  overflow-x: auto;
}

table { width: 100%; border-collapse: collapse; font-size: 14px; }
table th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  color: var(--text-muted);
  border-bottom: 2px solid var(--border);
  padding: 10px 8px;
}
table td { padding: 10px 8px; border-bottom: 1px solid var(--border); }
table tr:hover td { background: #fafbfc; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}
.badge.entwurf { background: #e9ecef; color: #6b7684; }
.badge.gesendet-unpaid { background: #fde8ea; color: var(--danger); }
.badge.bezahlt { background: #e6f4ea; color: var(--success); }
.badge.overdue { background: var(--danger); color: #fff; }
.badge.gesendet { background: #e3edfc; color: var(--accent-dark); }

tr.row-entwurf { color: #97a0ab; background: #fafbfc; }
tr.row-unpaid td { color: var(--danger); }
tr.row-bezahlt td { color: var(--text); }
tr.row-overdue td { color: var(--danger); background: #fbe1e4; font-weight: 600; }
tr.row-overdue:hover td { background: #f8d3d7; }

.icon-btn {
  background: none; border: none; cursor: pointer; color: var(--text-muted); padding: 4px 6px;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { color: var(--text); }
.icon-btn.danger-hover:hover { color: var(--danger); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,20,28,0.5);
  display: flex; align-items: flex-start; justify-content: center;
  z-index: 200; overflow-y: auto; padding: 40px 20px;
}
.modal {
  background: var(--card-bg);
  border-radius: var(--radius);
  width: 100%;
  max-width: 640px;
  padding: 26px 28px;
  box-shadow: 0 30px 80px rgba(0,0,0,0.3);
  overflow-x: auto;
}
.modal.wide { max-width: 920px; }
.modal h3 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* Kurzer "Wackel"-Effekt, wenn versucht wird, das Fenster durch einen Klick
   ausserhalb (oder versehentliches Markieren ueber den Rand hinaus) zu
   schliessen - signalisiert, dass nur "Abbrechen" das Fenster schliesst. */
@keyframes modalAttentionPulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.025); }
  100% { transform: scale(1); }
}
.modal.modal-attention {
  animation: modalAttentionPulse 0.22s ease-out;
}

.small-confirm .modal { max-width: 380px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: #eef1f5; border-radius: 20px; padding: 4px 10px 4px 12px; font-size: 13px; margin: 3px 4px 3px 0;
}
.chip button { background: none; border: none; cursor: pointer; color: var(--text-muted); font-size: 13px; }

.toast {
  position: fixed; bottom: 24px; right: 24px; background: #1f2733; color: #fff;
  padding: 12px 18px; border-radius: 8px; font-size: 14px; z-index: 999;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

.empty-state { color: var(--text-muted); padding: 30px; text-align: center; font-size: 14px; }
.field-hint { color: var(--text-muted); font-size: 12.5px; margin-top: 4px; }

.stat-row { display: flex; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.stat-card {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 20px; min-width: 180px; flex: 1;
}
.stat-card .label { font-size: 12.5px; color: var(--text-muted); margin-bottom: 6px; }
.stat-card .value { font-size: 22px; font-weight: 700; }
.stat-card.income .value { color: var(--success); }
.stat-card.expense .value { color: var(--danger); }

.logo-preview {
  width: 90px; height: 90px; object-fit: contain; border: 1px solid var(--border); border-radius: 8px; background: #fff; padding: 6px;
}

.items-table input { width: 100%; }
.items-table td { padding: 6px 4px; }
.items-table th { padding: 6px 4px; }

.generated-password-box {
  background: #eef7ee; border: 1px solid #bfe3bf; padding: 12px 14px; border-radius: 8px; font-size: 14px; margin-top: 10px;
}
.generated-password-box code { font-size: 16px; font-weight: 700; }

.search-input { width: 260px; }

.file-picker {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}
.file-picker-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #eef1f5;
  color: var(--text);
  border: 1px dashed var(--border);
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s;
}
.file-picker:hover .file-picker-btn {
  background: #e3e7ed;
  border-color: var(--accent);
}
.file-picker-name {
  font-size: 13px;
  color: var(--text-muted);
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Ladeoverlay ---------- */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 28, 0.68);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 500;
  color: #fff;
  font-size: 14.5px;
}
.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(255, 255, 255, 0.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- Autocomplete ---------- */
.autocomplete-wrap { position: relative; }
.autocomplete-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(0,0,0,0.14);
  max-height: 240px;
  overflow-y: auto;
  z-index: 80;
}
.autocomplete-list .ac-item {
  padding: 9px 12px;
  cursor: pointer;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}
.autocomplete-list .ac-item:last-child { border-bottom: none; }
.autocomplete-list .ac-item .ac-sub {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}
.autocomplete-list .ac-item:hover { background: var(--bg); }
.autocomplete-list .ac-empty { padding: 10px 12px; font-size: 13px; color: var(--text-muted); }

/* ---------- Responsive / Mobilgeräte ---------- */
@media (max-width: 880px) {
  #sidebar {
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    width: 260px;
    max-width: 82vw;
    box-shadow: 0 0 50px rgba(0,0,0,0.35);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-close { display: block; }
  #sidebar-backdrop.visible { display: block; }

  #main { margin-left: 0; }

  #menu-toggle { display: inline-flex; align-items: center; justify-content: center; }

  #topbar { padding: 12px 16px; gap: 8px; }
  #content { padding: 16px; }

  h2.page-title { font-size: 19px; }

  .field-row { flex-direction: column; gap: 0; }

  .stat-row { flex-direction: column; }
  .stat-card { min-width: 0; }

  .modal-backdrop { padding: 0; align-items: stretch; }
  .modal {
    max-width: 100%;
    width: 100%;
    min-height: 100vh;
    border-radius: 0;
    padding: 20px 16px 90px;
  }
  .modal.wide { max-width: 100%; }

  .search-input { width: 100%; }

  table { font-size: 13px; }
  table th, table td { padding: 8px 6px; }

  .toolbar { gap: 8px; }
  .toolbar .btn, .toolbar .btn.secondary { flex: 1 1 auto; }

  .company-switch button span#company-switch-label {
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
    vertical-align: middle;
  }
}

@media (min-width: 881px) {
  #menu-toggle { display: none; }
  #sidebar-close { display: none; }
}

/* ---------- PWA: Installation ---------- */
#install-app-btn {
  display: block;
  color: #9ec5ff;
  font-weight: 600;
}
#install-app-btn:hover { color: #fff; }

.pwa-banner {
  position: fixed;
  z-index: 4000;
  left: 16px;
  right: 16px;
  bottom: 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 18px 45px rgba(15, 23, 35, 0.28);
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.pwa-banner.hidden {
  display: flex !important;
  opacity: 0;
  transform: translateY(14px);
  pointer-events: none;
}
.pwa-banner-icon {
  width: 44px;
  height: 44px;
  border-radius: 11px;
  flex-shrink: 0;
}
.pwa-banner-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}
.pwa-banner-text strong { font-size: 14px; }
.pwa-banner-text span {
  font-size: 12.5px;
  color: var(--text-muted);
}
.pwa-banner-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.pwa-banner-actions button {
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 9px 14px;
  border: 1px solid transparent;
}
.pwa-banner-later {
  background: none;
  border-color: var(--border);
  color: var(--text-muted);
}
.pwa-banner-later:hover { color: var(--text); }
.pwa-banner-install {
  background: var(--accent);
  color: #fff;
}
.pwa-banner-install:hover { background: var(--accent-dark); }

@media (min-width: 861px) {
  .pwa-banner {
    left: auto;
    right: 22px;
    bottom: 22px;
    width: 500px;
    max-width: calc(100vw - 44px);
  }
}
@media (max-width: 480px) {
  .pwa-banner { flex-wrap: wrap; }
  .pwa-banner-actions { width: 100%; }
  .pwa-banner-actions button { flex: 1; }
}

/* Im installierten Fenster ist auf iOS Platz fuer die Statusleiste noetig */
@media all and (display-mode: standalone) {
  #topbar { padding-top: max(14px, env(safe-area-inset-top)); }
  #sidebar { padding-top: env(safe-area-inset-top); }
}
