:root {
  --color-primary: #1e5631;
  --color-primary-dark: #143d22;
  --color-primary-light: #e8f3ec;
  --color-accent: #2fa84f;
  --color-bg: #f3f5f4;
  --color-surface: #ffffff;
  --color-border: #e1e6e3;
  --color-text: #1f2a24;
  --color-muted: #7a8a82;
  --radius: 8px;
  --radius-sm: 5px;
  --shadow-sm: 0 1px 3px rgba(20, 40, 30, 0.06);
  --shadow-md: 0 4px 16px rgba(20, 40, 30, 0.10);
  --shadow-lg: 0 12px 40px rgba(20, 40, 30, 0.18);
  --transition: 0.15s ease;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
}

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: linear-gradient(160deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
}

.login-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}

.login-logo {
  height: 48px;
  width: auto;
}

.login-box {
  background: var(--color-surface);
  padding: 2.5rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 340px;
  max-width: 90vw;
}

.login-box h1 {
  font-size: 1.25rem;
  margin: 0 0 1.5rem;
  color: var(--color-primary);
  text-align: center;
  letter-spacing: 0.01em;
}

.login-box label {
  display: block;
  margin: 0.85rem 0 0.3rem;
  font-size: 0.85rem;
  color: var(--color-muted);
  font-weight: 600;
}

.login-box input {
  width: 100%;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.login-box input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.login-box button {
  margin-top: 1.5rem;
  width: 100%;
  font-weight: 600;
  padding: 0.65rem 1rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  color: #fff;
  padding: 0.85rem 1.5rem;
  box-shadow: var(--shadow-sm);
  flex-wrap: wrap;
  gap: 0.5rem;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-logo {
  height: 32px;
  width: auto;
}

.brand-copyright {
  font-size: 0.62rem;
  color: var(--color-muted);
  opacity: 0.7;
  align-self: flex-end;
  margin-bottom: 3px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.modal-admin {
  max-width: 1100px;
  width: 95vw;
}

.admin-section {
  border-top: 1px solid var(--color-border);
  padding-top: 1rem;
  margin-top: 1rem;
}

.admin-section h3 {
  margin: 0 0 0.6rem;
  font-size: 0.95rem;
  font-weight: 600;
}

.admin-info-grid {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.25rem 1rem;
  font-size: 0.875rem;
  margin-bottom: 0.6rem;
}

.admin-info-label {
  color: var(--color-muted);
  font-weight: 500;
}

.admin-sql-result {
  margin-top: 0.75rem;
  overflow-x: auto;
  font-size: 0.82rem;
}

.admin-sql-table {
  border-collapse: collapse;
  width: 100%;
}

.admin-sql-table th,
.admin-sql-table td {
  border: 1px solid var(--color-border);
  padding: 0.3rem 0.5rem;
  text-align: left;
}

.admin-sql-table th {
  background: var(--color-primary-light);
  font-weight: 600;
}

.topbar h1 {
  font-size: 1.25rem;
  margin: 0;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.topbar-user {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.4rem 0.6rem;
  font-size: 0.9rem;
}

#user-info {
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  margin-right: 0.25rem;
}

main {
  padding: 1.5rem;
  max-width: 1900px;
  margin: 0 auto;
}

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

.toolbar input[type="search"] {
  flex: 1;
  min-width: 240px;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  background: var(--color-surface);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.toolbar input[type="search"]:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.toolbar select {
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  font-size: 0.9rem;
  cursor: pointer;
}

button {
  cursor: pointer;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.55rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background var(--transition), transform var(--transition), box-shadow var(--transition);
  font-family: inherit;
}

button:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

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

.btn-secondary {
  background: #eef1ef;
  color: var(--color-text);
}

.btn-secondary:hover {
  background: #e0e5e2;
}

.btn-link {
  background: transparent;
  color: rgba(255, 255, 255, 0.92);
  padding: 0.3rem 0.55rem;
  border-radius: var(--radius-sm);
  font-weight: 500;
}

.btn-link:hover {
  background: rgba(255, 255, 255, 0.14);
}

.btn-small {
  padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
  margin-right: 0.25rem;
}

.table-wrapper {
  background: var(--color-surface);
  border-radius: var(--radius);
  overflow-x: auto;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  table-layout: fixed;
}

th, td {
  text-align: center;
  vertical-align: middle;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--color-border);
  white-space: normal;
  word-break: break-word;
  overflow-wrap: anywhere;
}

#bestellungen-table th:nth-child(1) { width: 2.5rem; }
#bestellungen-table th:nth-child(2) { width: 6.5rem; }
#bestellungen-table th:nth-child(3) { width: 14rem; }
#bestellungen-table th:nth-child(4) { width: 4rem; }
#bestellungen-table th:nth-child(5) { width: 7rem; }
#bestellungen-table th:nth-child(6) { width: 8rem; }
#bestellungen-table th:nth-child(7) { width: 7rem; }
#bestellungen-table th:nth-child(8) { width: 7rem; }
#bestellungen-table th:nth-child(9) { width: 6rem; }
#bestellungen-table th:nth-child(10) { width: 8rem; }
#bestellungen-table th:nth-child(11) { width: 9rem; }
#bestellungen-table th:nth-child(12) { width: 6rem; }
#bestellungen-table th:nth-child(13) { width: 7rem; }
#bestellungen-table th:nth-child(14) { width: 9rem; }

th {
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  position: sticky;
  top: 0;
}

tbody tr {
  transition: background var(--transition);
}

tbody tr:hover {
  background: var(--color-primary-light);
}

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

tbody tr.row-status-angefragt { background: #f3effa; }
tbody tr.row-status-offen { background: #f4f5f4; }
tbody tr.row-status-genehmigt { background: #eaf2fc; }
tbody tr.row-status-abgelehnt { background: #fbeceb; }
tbody tr.row-status-angekommen { background: #e8f3ec; }

tbody tr.row-status-angefragt:hover { background: #e6dcf3; }
tbody tr.row-status-offen:hover { background: #e6e9e7; }
tbody tr.row-status-genehmigt:hover { background: #d6e7fa; }
tbody tr.row-status-abgelehnt:hover { background: #f6d8d5; }
tbody tr.row-status-angekommen:hover { background: #d6ebdd; }

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.empty-state {
  padding: 2rem;
  text-align: center;
  color: var(--color-muted);
  font-size: 0.95rem;
}

.status-badge {
  display: inline-block;
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: #fff;
  white-space: nowrap;
}

.status-angefragt { background: #8e6fc7; }
.status-offen { background: #9aa5a0; }
.status-genehmigt { background: #3a7bd5; }
.status-abgelehnt { background: #c0392b; }
.status-angekommen { background: #1e5631; }

tbody tr.row-duplicate td:first-child {
  border-left: 3px solid #c0392b;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 30, 22, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fade-in 0.15s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pop-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 1.75rem;
  width: 720px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: pop-in 0.18s ease;
}

.modal-small {
  width: 380px;
}

.modal h2 {
  margin-top: 0;
  margin-bottom: 0.5rem;
  color: var(--color-primary-dark);
  font-size: 1.2rem;
}

.modal h3 {
  margin-top: 1.5rem;
  margin-bottom: 0.75rem;
  color: var(--color-primary-dark);
  font-size: 1rem;
  border-top: 1px solid var(--color-border);
  padding-top: 1rem;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.85rem;
}

.form-field {
  display: flex;
  flex-direction: column;
}

.form-field.span-2 {
  grid-column: span 2;
}

.form-field.span-3 {
  grid-column: span 3;
}

.form-field label {
  font-size: 0.78rem;
  margin-bottom: 0.3rem;
  color: var(--color-muted);
  font-weight: 600;
}

.form-field input,
.form-field select,
.form-field textarea {
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 0.9rem;
  background: var(--color-surface);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border);
}

.error-text {
  color: #c0392b;
  font-size: 0.85rem;
  min-height: 1.2em;
  margin: 0.5rem 0 0;
}

.hint-text {
  color: var(--color-muted);
  font-size: 0.85rem;
  margin-top: -0.25rem;
  margin-bottom: 1rem;
}

.modal-lookup {
  width: 700px;
}

.modal-lookup input[type="search"] {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.modal-lookup input[type="search"]:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.lookup-table-wrapper {
  max-height: 380px;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
}

.lookup-table-wrapper td input {
  width: 100%;
  padding: 0.15rem 0.3rem;
  font-size: 0.85rem;
  border: 1px solid var(--color-primary);
  border-radius: 3px;
  box-sizing: border-box;
}

.lookup-table-wrapper table {
  width: 100%;
  table-layout: auto;
}

.lookup-table-wrapper th,
.lookup-table-wrapper td {
  white-space: nowrap;
  word-break: normal;
  overflow-wrap: normal;
}

.lookup-table-wrapper th:last-child,
.lookup-table-wrapper td:last-child {
  width: 2.5rem;
  text-align: right;
}

.lookup-table-wrapper th {
  position: sticky;
  top: 0;
}

.lookup-form {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.75rem;
}

.lookup-form[hidden] {
  display: none;
}

.lookup-form input {
  flex: 1;
  min-width: 0;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.lookup-form input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

/* ===== Tablet (<=900px) ===== */
@media (max-width: 900px) {
  .topbar {
    padding: 0.6rem 1rem;
  }

  .topbar-brand h1 {
    font-size: 1.05rem;
  }

  .form-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .form-field.span-3 {
    grid-column: span 2;
  }
}

/* ===== Handy (<=640px) ===== */
@media (max-width: 640px) {
  /* Topbar */
  .topbar-brand,
  .topbar-user {
    width: 100%;
  }

  .topbar-user {
    justify-content: flex-start;
  }

  .brand-copyright {
    display: none;
  }

  .btn-link {
    min-height: 44px;
    padding: 0.6rem 0.85rem;
    display: inline-flex;
    align-items: center;
  }

  .btn-link[hidden] {
    display: none;
  }

  /* Touch-Ziele global */
  button {
    min-height: 44px;
    padding: 0.65rem 1.1rem;
  }

  .btn-small {
    min-height: 44px;
    padding: 0.55rem 0.85rem;
    font-size: 0.85rem;
  }

  .form-field input,
  .form-field select,
  .form-field textarea,
  .toolbar input[type="search"],
  .toolbar select {
    min-height: 44px;
  }

  /* Toolbar */
  .toolbar {
    flex-direction: column;
    align-items: stretch;
  }

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

  /* Lookup-Formulare (Kürzel/Kostenstellen/Abteilungen/Lagerorte) */
  .lookup-form {
    flex-direction: column;
  }

  .lookup-form input,
  .lookup-form button {
    width: 100%;
  }

  /* Formular-Grid */
  .form-grid {
    grid-template-columns: 1fr;
  }

  .form-field.span-2,
  .form-field.span-3 {
    grid-column: span 1;
  }

  /* Modals */
  .modal {
    padding: 1.1rem;
    max-height: 95vh;
  }

  .modal h2 {
    font-size: 1.05rem;
  }

  .modal-lookup {
    width: 95vw;
  }

  .modal-actions {
    flex-direction: column-reverse;
  }

  .modal-actions button {
    width: 100%;
  }

  /* Tabelle -> Karten */
  .table-wrapper {
    overflow-x: visible;
  }

  #bestellungen-table thead {
    display: none;
  }

  #bestellungen-table,
  #bestellungen-table tbody,
  #bestellungen-table tr,
  #bestellungen-table td {
    display: block;
    width: 100%;
  }

  #bestellungen-table tbody tr {
    margin-bottom: 0.75rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    box-shadow: var(--shadow-sm);
  }

  #bestellungen-table tbody tr.row-duplicate {
    border-top: 3px solid #c0392b;
  }

  #bestellungen-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    text-align: right;
    border-bottom: 1px solid var(--color-border);
    padding: 0.4rem 0;
  }

  #bestellungen-table td:last-child {
    border-bottom: none;
  }

  #bestellungen-table td::before {
    content: '';
    font-weight: 600;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--color-muted);
    flex-shrink: 0;
    text-align: left;
  }

  #bestellungen-table td:nth-child(1)::before { content: 'ID'; }
  #bestellungen-table td:nth-child(2)::before { content: 'Abas Nr.'; }
  #bestellungen-table td:nth-child(3)::before { content: 'Bezeichnung'; }
  #bestellungen-table td:nth-child(4)::before { content: 'Stk'; }
  #bestellungen-table td:nth-child(5)::before { content: 'Hersteller'; }
  #bestellungen-table td:nth-child(6)::before { content: 'Kostenstelle'; }
  #bestellungen-table td:nth-child(7)::before { content: 'Zeichen'; }
  #bestellungen-table td:nth-child(8)::before { content: 'Abteilung'; }
  #bestellungen-table td:nth-child(9)::before { content: 'Lagerort'; }
  #bestellungen-table td:nth-child(10)::before { content: 'Status'; }
  #bestellungen-table td:nth-child(11)::before { content: 'Termine'; }
  #bestellungen-table td:nth-child(12)::before { content: 'Kommentar'; }
  #bestellungen-table td:nth-child(13)::before { content: 'Registriert'; }

  /* Sekundäre Spalten auf dem Handy ausblenden (im Bearbeiten-Modal weiter sichtbar) */
  #bestellungen-table td:nth-child(1),
  #bestellungen-table td:nth-child(5),
  #bestellungen-table td:nth-child(7),
  #bestellungen-table td:nth-child(8),
  #bestellungen-table td:nth-child(9),
  #bestellungen-table td:nth-child(12),
  #bestellungen-table td:nth-child(13) {
    display: none;
  }

  /* Aktionen-Zelle: Label oben, Buttons untereinander */
  #bestellungen-table td:nth-child(14) {
    display: block;
    text-align: left;
  }

  #bestellungen-table td:nth-child(14)::before {
    content: 'Aktionen';
    display: block;
    margin-bottom: 0.4rem;
  }

  .row-actions {
    flex-direction: column;
    gap: 0.4rem;
  }

  .row-actions button {
    width: 100%;
  }
}
