*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --green: #2e7d32;
  --green-light: #e8f5e9;
  --green-bg: #43a047;
  --red: #c62828;
  --red-light: #ffebee;
  --red-bg: #e53935;
  --orange: #e65100;
  --orange-light: #fff3e0;
  --gray: #616161;
  --gray-light: #f5f5f5;
  --gray-border: #e0e0e0;
  --text: #212121;
  --white: #ffffff;
  --bg: #f0f0f0;
  --card: #ffffff;
  --header-bg: #1a237e;
  --accent: #1a237e;
  --accent-dark: #0d1457;
  --input-bg: #ffffff;
  --input-border: #e0e0e0;
  --input-focus: #1a237e;
  --radius: 8px;
}

html.dark {
  --green: #4caf50;
  --green-light: #1b3a1d;
  --green-bg: #2e7d32;
  --red: #ef5350;
  --red-light: #3b1a1a;
  --red-bg: #c62828;
  --orange: #ff9800;
  --orange-light: #3b2a10;
  --gray: #9e9e9e;
  --gray-light: #2a2a2a;
  --gray-border: #444;
  --text: #e0e0e0;
  --white: #1e1e1e;
  --bg: #121212;
  --card: #1e1e1e;
  --header-bg: #0d1457;
  --accent: #5c6bc0;
  --accent-dark: #3949ab;
  --input-bg: #2a2a2a;
  --input-border: #555;
  --input-focus: #7986cb;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

/* ===================== HEADER ===================== */
header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--header-bg);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 100;
}

header h1 {
  font-size: 1rem;
  font-weight: 700;
  white-space: nowrap;
  flex-shrink: 0;
}

.header-center {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

.batch-select-header {
  flex: 1;
  min-width: 0;
  height: 34px;
  padding: 4px 8px;
  font-size: 0.85rem;
  font-weight: 600;
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.1);
  color: #fff;
  appearance: auto;
}

.btn-hdr-sm {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  background: rgba(255,255,255,0.1);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: var(--radius);
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  flex-shrink: 0;
}

.btn-hdr-sm:active {
  background: rgba(255,255,255,0.25);
}

.btn-hdr-danger {
  color: #ff8a80;
  border-color: rgba(255,138,128,0.4);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn-header {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  background: rgba(255,255,255,0.15);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  height: 34px;
}

.btn-header:active {
  background: rgba(255,255,255,0.3);
}

#btn-dark-mode {
  font-size: 1rem;
  padding: 6px 8px;
}

.status-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #f44336;
  flex-shrink: 0;
  transition: background 0.3s;
}

.status-dot.connected { background: #4caf50; }
.status-dot.reconnecting { background: #ff9800; animation: pulse 1s infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ===================== LAYOUT ===================== */
.layout {
  display: flex;
  min-height: calc(100vh - 50px);
}

main {
  flex: 1;
  min-width: 0;
  max-width: 700px;
  margin: 0 auto;
  padding: 12px;
}

/* ===================== ON-FIELD PANEL ===================== */
.on-field-panel {
  width: 260px;
  flex-shrink: 0;
  background: var(--card);
  border-left: 1px solid var(--gray-border);
  padding: 12px;
  overflow-y: auto;
  position: sticky;
  top: 50px;
  height: calc(100vh - 50px);
}

.panel-title {
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--gray);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--green);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 800;
}

.on-field-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.on-field-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--green-light);
  border: 2px solid var(--green);
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  transition: transform 0.1s;
}

.on-field-item:active {
  transform: scale(0.97);
}

.on-field-info {
  min-width: 0;
}

.on-field-name {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text);
}

.on-field-plate {
  font-size: 0.8rem;
  color: var(--gray);
}

.on-field-meta {
  font-size: 0.75rem;
  color: var(--gray);
  margin-top: 2px;
}

.on-field-exit {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--red);
  background: var(--red-light);
  border: 1px solid var(--red);
  border-radius: 4px;
  padding: 4px 10px;
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: 8px;
}

.on-field-empty {
  text-align: center;
  padding: 20px 10px;
  color: var(--gray);
  font-size: 0.85rem;
}

/* Mobile: panel below main */
@media (max-width: 899px) {
  .layout {
    flex-direction: column;
  }

  .on-field-panel {
    width: 100%;
    position: static;
    height: auto;
    border-left: none;
    border-top: 1px solid var(--gray-border);
    max-height: none;
    order: -1;
  }

  .on-field-list {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .on-field-item {
    flex: 1 1 auto;
    min-width: 160px;
  }
}

/* ===================== MODAL ===================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

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

.modal {
  background: var(--card);
  border-radius: 12px;
  padding: 24px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}

.modal h2 {
  font-size: 1.1rem;
  margin-bottom: 16px;
  color: var(--text);
}

.modal-input {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  font-size: 1.1rem;
  border: 2px solid var(--input-border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--text);
  outline: none;
  margin-bottom: 16px;
}

.modal-input:focus {
  border-color: var(--input-focus);
  box-shadow: 0 0 0 3px rgba(92,107,192,0.2);
}

.modal-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.modal-buttons .btn-abbrechen,
.modal-buttons .btn-speichern {
  min-height: 48px;
  font-size: 1rem;
}

/* ===================== DIRECTION BUTTONS ===================== */
.richtung-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 12px;
}

.richtung-container.hidden { display: none; }

.btn-richtung {
  height: 90px;
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 1px;
  border: 3px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  transition: transform 0.1s, box-shadow 0.1s;
}

.btn-richtung:active { transform: scale(0.97); }

.btn-ein { background: var(--green-light); color: var(--green); border-color: var(--green); }
.btn-ein.active { background: var(--green-bg); color: #fff; box-shadow: 0 4px 12px rgba(46,125,50,0.4); }
.btn-aus { background: var(--red-light); color: var(--red); border-color: var(--red); }
.btn-aus.active { background: var(--red-bg); color: #fff; box-shadow: 0 4px 12px rgba(198,40,40,0.4); }

/* ===================== FORM ===================== */
.eingabe-form {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

.eingabe-form.hidden { display: none; }
.eingabe-form.form-ein { border-top: 4px solid var(--green); }
.eingabe-form.form-aus { border-top: 4px solid var(--red); }

.form-row { display: grid; gap: 12px; }
.form-row-zeit { grid-template-columns: 1fr 1fr; }
.form-group { margin-bottom: 12px; }

.form-group label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--gray);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group input[type="date"],
.form-group input[type="time"] {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  font-size: 1.1rem;
  border: 2px solid var(--input-border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  -webkit-appearance: none;
  appearance: none;
}

.form-group input:focus {
  border-color: var(--input-focus);
  box-shadow: 0 0 0 3px rgba(92,107,192,0.2);
}

/* Autocomplete */
.autocomplete-wrap { position: relative; }

.autocomplete-list {
  display: none;
  position: absolute;
  top: 100%; left: 0; right: 0;
  z-index: 50;
  background: var(--card);
  border: 2px solid var(--input-focus);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  max-height: 220px;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  -webkit-overflow-scrolling: touch;
}

.autocomplete-list.visible { display: block; }

.autocomplete-item {
  padding: 12px 14px;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  border-bottom: 1px solid var(--gray-border);
  color: var(--text);
}

.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:active,
.autocomplete-item.highlighted { background: var(--gray-light); }
.autocomplete-item .ac-plate { color: var(--gray); font-size: 0.85rem; margin-left: 8px; }

/* Patient toggle */
.patient-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.btn-patient {
  min-height: 48px;
  font-size: 1.1rem;
  font-weight: 700;
  border: 2px solid var(--gray-border);
  border-radius: var(--radius);
  background: var(--gray-light);
  color: var(--gray);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  transition: all 0.15s;
}

#btn-patient-ja.active { background: var(--orange); color: #fff; border-color: var(--orange); }
#btn-patient-nein.active { background: #455a64; color: #fff; border-color: #455a64; }

/* Form buttons */
.form-buttons { display: grid; grid-template-columns: auto 1fr; gap: 10px; margin-top: 4px; }

.btn-abbrechen {
  min-height: 56px;
  padding: 0 24px;
  font-size: 1rem;
  font-weight: 700;
  background: var(--gray-light);
  color: var(--gray);
  border: 2px solid var(--gray-border);
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  transition: transform 0.1s;
}

.btn-abbrechen:active { transform: scale(0.98); opacity: 0.7; }

.btn-speichern {
  min-height: 56px;
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  transition: transform 0.1s;
}

.btn-speichern:active { transform: scale(0.98); background: var(--accent-dark); }

/* ===================== LOG ===================== */
.log-container { display: flex; flex-direction: column; gap: 8px; }

.log-entry {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 14px;
  border-left: 5px solid var(--gray-border);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  position: relative;
}

.log-entry.ein { border-left-color: var(--green); }
.log-entry.aus { border-left-color: var(--red); }

.log-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.log-richtung {
  font-weight: 800;
  font-size: 0.85rem;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: uppercase;
}

.ein .log-richtung { background: var(--green-light); color: var(--green); }
.aus .log-richtung { background: var(--red-light); color: var(--red); }

.log-zeit { font-weight: 600; font-size: 0.95rem; color: var(--text); }
.log-fahrzeug { font-weight: 700; font-size: 1rem; }
.log-kennzeichen { font-size: 0.9rem; color: var(--gray); }

.log-bottom {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 6px;
  flex-wrap: wrap;
  font-size: 0.85rem;
  color: var(--gray);
}

.log-patient-ja {
  background: var(--orange-light);
  color: var(--orange);
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
}

.log-notizen { font-style: italic; color: var(--gray); }

.log-actions { display: none; margin-top: 8px; gap: 8px; }
.log-entry.expanded .log-actions { display: flex; }

.btn-log-action {
  padding: 6px 14px;
  font-size: 0.8rem;
  font-weight: 600;
  border: 1px solid var(--gray-border);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}

.btn-log-action.edit { color: var(--accent); border-color: var(--accent); }
.btn-log-action.delete { color: var(--red); border-color: var(--red); }

.log-empty, .no-batch {
  text-align: center;
  padding: 40px 20px;
  color: var(--gray);
  font-size: 1rem;
}

/* ===================== RESPONSIVE ===================== */
@media (min-width: 900px) {
  main { padding: 20px; }
  header h1 { font-size: 1.1rem; }
  .btn-richtung { height: 100px; font-size: 1.6rem; }
}

@media (max-width: 599px) {
  header h1 { font-size: 0.85rem; }
  .header-center { gap: 4px; }
  .btn-hdr-sm { width: 30px; height: 30px; font-size: 0.85rem; }
  .batch-select-header { height: 30px; font-size: 0.8rem; }
  .btn-header { padding: 4px 8px; font-size: 0.75rem; height: 30px; }
  #btn-dark-mode { padding: 4px 6px; }
}
