/* =========================================================
   Variables & reset
   ========================================================= */
:root {
  --green:   #2ecc71;
  --green-d: #27ae60;
  --gray:    #95a5a6;
  --gray-d:  #7f8c8d;
  --blue:    #3498db;
  --blue-d:  #2980b9;
  --red:     #e74c3c;
  --orange:  #f39c12;
  --bg:      #1a1a2e;
  --surface: #16213e;
  --card:    #0f3460;
  --text:    #ecf0f1;
  --muted:   #8892b0;
  --border:  #233554;
  --h:       56px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
select { font-family: inherit; }

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button { cursor: pointer; border: none; font-family: inherit; }
input  { font-family: inherit; }

/* =========================================================
   Écran de connexion
   ========================================================= */
#login-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99999;
}
#login-screen.hidden { display: none; }

.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2.5rem 2rem;
  width: 360px;
  max-width: 95vw;
  box-shadow: 0 30px 80px rgba(0,0,0,.6);
  text-align: center;
}
.login-logo    { font-size: 3rem; margin-bottom: 0.5rem; }
.login-box h1  { font-size: 1.4rem; margin-bottom: 0.2rem; }
.login-subtitle { font-size: 0.82rem; color: var(--muted); margin-bottom: 1.5rem; }

.login-error {
  background: rgba(231,76,60,.15);
  border: 1px solid var(--red);
  color: var(--red);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
  margin-bottom: 1rem;
}
.login-error.hidden { display: none; }

.login-box .form-group { text-align: left; }
.login-box .form-group input { margin-bottom: 0; }
.btn-block { width: 100%; margin-top: 1rem; padding: 0.65rem; font-size: 0.95rem; }

/* =========================================================
   Header
   ========================================================= */
#header {
  height: var(--h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  flex-shrink: 0;
  gap: 0.75rem;
  z-index: 1000;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.logo { font-size: 1.6rem; }
#header h1   { font-size: 1rem; font-weight: 700; }
#header small { font-size: 0.7rem; color: var(--muted); }

.header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.badges { display: flex; gap: 0.4rem; }
.badge {
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-active   { background: rgba(52,152,219,.15);  color: var(--blue);  border: 1px solid var(--blue); }
.badge-inactive { background: rgba(149,165,166,.15); color: var(--gray);  border: 1px solid var(--gray); }
.badge-conn     { background: rgba(52,152,219,.15);  color: var(--blue);  border: 1px solid var(--blue); }

.worker-block {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  color: var(--muted);
}
.worker-block strong { color: var(--text); }

.btn-sm {
  padding: 0.2rem 0.6rem;
  border-radius: 6px;
  background: var(--card);
  color: var(--muted);
  font-size: 0.75rem;
  transition: background .2s, color .2s;
}
.btn-sm:hover { background: var(--border); color: var(--text); }

/* Badge de rôle */
.role-badge {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: .05em;
}
.role-admin    { background: rgba(243,156,18,.2); color: var(--orange); border: 1px solid var(--orange); }
.role-operator { background: rgba(52,152,219,.2); color: var(--blue);   border: 1px solid var(--blue); }
.role-viewer   { background: rgba(149,165,166,.2);color: var(--gray);   border: 1px solid var(--gray); }

/* Bouton icône header */
.btn-header-icon {
  width: 34px; height: 34px;
  border-radius: 8px;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 1rem;
  transition: background .2s;
}
.btn-header-icon:hover { background: var(--border); }

/* Bouton mode édition */
.btn-edit-mode {
  padding: 0.3rem 0.75rem;
  border-radius: 8px;
  background: var(--card);
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  border: 1px solid var(--border);
  transition: all .2s;
}
.btn-edit-mode:hover     { background: var(--orange); color: #fff; border-color: var(--orange); }
.btn-edit-mode.is-active { background: var(--orange); color: #fff; border-color: var(--orange); }

.ws-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.ws-connected    { background: var(--green); box-shadow: 0 0 6px var(--green); }
.ws-disconnected { background: var(--red);   box-shadow: 0 0 6px var(--red); }

/* =========================================================
   Bandeau mode édition
   ========================================================= */
.edit-banner {
  background: rgba(243,156,18,.12);
  border-bottom: 1px solid var(--orange);
  color: var(--orange);
  font-size: 0.82rem;
  padding: 0.35rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-shrink: 0;
  flex-wrap: wrap;
  z-index: 999;
}
.edit-banner.hidden { display: none; }
.edit-banner .btn-sm {
  background: rgba(243,156,18,.2);
  color: var(--orange);
  border: 1px solid var(--orange);
}
.edit-banner .btn-sm:hover { background: var(--orange); color: #fff; }

.edit-sep { color: var(--border); }

/* Boutons sous-mode édition */
.btn-submode {
  padding: 0.22rem 0.7rem;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  background: rgba(243,156,18,.15);
  color: var(--orange);
  border: 1px solid var(--orange);
  transition: background .2s, color .2s;
}
.btn-submode:hover  { background: rgba(243,156,18,.35); }
.btn-submode.active { background: var(--orange); color: #fff; }

/* =========================================================
   Layout principal
   ========================================================= */
#layout {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* =========================================================
   Barre latérale
   ========================================================= */
#sidebar {
  width: 300px;
  min-width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width .25s ease, min-width .25s ease;
  flex-shrink: 0;
}

.sidebar-top {
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#search-input {
  width: 100%;
  padding: 0.4rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 0.85rem;
  outline: none;
  transition: border-color .2s;
}
#search-input:focus { border-color: var(--blue); }
#search-input::placeholder { color: var(--muted); }

.filter-row { display: flex; gap: 0.4rem; }
.filter-btn {
  flex: 1;
  padding: 0.3rem;
  border-radius: 6px;
  background: var(--card);
  color: var(--muted);
  font-size: 0.75rem;
  transition: background .2s, color .2s;
}
.filter-btn.active { background: var(--blue); color: #fff; }
.filter-btn:hover:not(.active) { background: var(--border); color: var(--text); }

#points-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0.75rem;
}
#points-list::-webkit-scrollbar { width: 4px; }
#points-list::-webkit-scrollbar-track { background: transparent; }
#points-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.sector-group { margin-bottom: 0.75rem; }
.sector-title {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  padding: 0.25rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.4rem;
}

.point-card {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.6rem;
  border-radius: 8px;
  margin-bottom: 0.3rem;
  cursor: pointer;
  background: var(--card);
  border: 1px solid transparent;
  transition: border-color .2s, background .2s;
}
.point-card:hover  { border-color: var(--blue); }
.point-card.selected { border-color: var(--blue); background: rgba(52,152,219,.12); }

.point-dot {
  width: 13px; height: 13px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-active   { background: var(--blue);  box-shadow: 0 0 5px var(--blue); }
.dot-inactive { background: var(--gray); }

.point-info { flex: 1; min-width: 0; }
.point-name { font-size: 0.86rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.point-meta { font-size: 0.68rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.point-actions { display: flex; gap: 0.3rem; flex-shrink: 0; }

.point-toggle-btn {
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  font-size: 0.72rem;
  font-weight: 600;
  transition: background .2s;
}
.btn-open  { background: rgba(52,152,219,.2);  color: var(--blue);  border: 1px solid var(--blue); }
.btn-open:hover  { background: var(--blue-d);  color: #fff; }
.btn-close { background: rgba(149,165,166,.2); color: var(--gray);  border: 1px solid var(--gray); }
.btn-close:hover { background: var(--gray-d); color: #fff; }

.btn-edit-point {
  padding: 0.2rem 0.4rem;
  border-radius: 6px;
  font-size: 0.72rem;
  background: rgba(52,152,219,.2);
  color: var(--blue);
  border: 1px solid var(--blue);
  display: none;
}
.btn-edit-point:hover { background: var(--blue-d); color: #fff; }

.btn-delete-point {
  padding: 0.2rem 0.4rem;
  border-radius: 6px;
  font-size: 0.72rem;
  background: rgba(231,76,60,.2);
  color: var(--red);
  border: 1px solid var(--red);
  display: none;
}
.btn-delete-point:hover { background: var(--red); color: #fff; }

/* En mode édition, afficher les boutons édit/suppression */
body.edit-mode .btn-edit-point,
body.edit-mode .btn-delete-point { display: inline-flex; }
body.edit-mode .point-toggle-btn { display: none; }

/* Curseur crosshair sur la carte en mode édition */
body.edit-mode #map { cursor: crosshair !important; }
body.edit-mode #map .leaflet-interactive { cursor: grab !important; }

/* =========================================================
   Carte Leaflet
   ========================================================= */
#map { flex: 1; z-index: 0; }

/* Popup */
.popup-title   { font-weight: 700; font-size: 0.95rem; margin-bottom: 3px; }
.popup-sector  { font-size: 0.75rem; color: #666; margin-bottom: 6px; }
.popup-status  { font-size: 0.85rem; margin-bottom: 5px; }
.popup-meta    { font-size: 0.72rem; color: #888; margin-bottom: 8px; }
.popup-coords  { font-size: 0.68rem; color: #aaa; margin-bottom: 8px; font-family: monospace; }
.popup-actions { display: flex; flex-direction: column; gap: 5px; }
.popup-btn {
  width: 100%;
  padding: 0.35rem;
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
  border: none;
}
.popup-btn-open   { background: #3498db; color: #fff; }
.popup-btn-close  { background: #e74c3c; color: #fff; }
.popup-btn-edit   { background: #3498db; color: #fff; display: none; }
.popup-btn-delete { background: #c0392b; color: #fff; display: none; }
body.edit-mode .popup-btn-edit,
body.edit-mode .popup-btn-delete { display: block; }
body.edit-mode .popup-btn-open,
body.edit-mode .popup-btn-close  { display: none; }

/* =========================================================
   Étiquettes de vignes sur la carte
   ========================================================= */
.vine-label-icon {
  background: none !important;
  border: none !important;
}
.vine-label-text {
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  text-shadow:
    -1px -1px 0 rgba(0,0,0,.7),
     1px -1px 0 rgba(0,0,0,.7),
    -1px  1px 0 rgba(0,0,0,.7),
     1px  1px 0 rgba(0,0,0,.7),
     0 0 6px rgba(0,0,0,.5);
  white-space: nowrap;
  cursor: default;
  user-select: none;
  letter-spacing: .03em;
  padding: 2px 4px;
  border-radius: 3px;
  background: rgba(0,0,0,.25);
}
/* En mode édition : curseur grab + contour pour montrer que c'est déplaçable */
body.edit-mode .vine-label-text {
  cursor: grab;
  outline: 1px dashed rgba(243,156,18,.6);
  outline-offset: 2px;
}

/* =========================================================
   Modales
   ========================================================= */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9000;
  backdrop-filter: blur(4px);
}
.modal-overlay.hidden { display: none; }

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.75rem;
  min-width: 320px;
  max-width: 90vw;
  width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.modal-box h2 { font-size: 1.1rem; margin-bottom: 0.5rem; text-align: center; }
.modal-box > p { font-size: 0.87rem; color: var(--muted); margin-bottom: 1rem; text-align: center; }

/* Formulaire modal */
.form-group { margin-bottom: 0.85rem; }
.form-group label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 0.3rem;
  text-transform: uppercase;
  letter-spacing: .05em;
}
.form-group input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  transition: border-color .2s;
}
.form-group input:focus { border-color: var(--blue); }
.form-group input::placeholder { color: var(--muted); }

.coords-row { display: flex; gap: 0.75rem; }
.coords-row > div { flex: 1; }

.form-hint { font-size: 0.75rem; color: var(--muted); margin-bottom: 1rem; text-align: center; }

/* Input centré pour modal worker */
#modal-worker .modal-box { text-align: center; }
#modal-worker input {
  width: 100%;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 1rem;
  outline: none;
  text-align: center;
}
#modal-worker input:focus { border-color: var(--blue); }

.modal-actions {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
  margin-top: 0.25rem;
}

.btn-primary {
  padding: 0.5rem 1.4rem;
  border-radius: 8px;
  background: var(--blue);
  color: #fff;
  font-weight: 600;
  font-size: 0.88rem;
  transition: opacity .2s;
}
.btn-primary:hover { opacity: 0.85; }

.btn-secondary {
  padding: 0.5rem 1.4rem;
  border-radius: 8px;
  background: var(--card);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.88rem;
  border: 1px solid var(--border);
  transition: background .2s, color .2s;
}
.btn-secondary:hover { background: var(--border); color: var(--text); }

.btn-danger {
  padding: 0.5rem 1.4rem;
  border-radius: 8px;
  background: var(--red);
  color: #fff;
  font-weight: 600;
  font-size: 0.88rem;
  transition: opacity .2s;
}
.btn-danger:hover { opacity: 0.85; }

/* =========================================================
   Toast
   ========================================================= */
#toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  pointer-events: none;
}
.toast {
  padding: 0.6rem 1rem;
  border-radius: 8px;
  font-size: 0.83rem;
  font-weight: 500;
  color: #fff;
  opacity: 0;
  transform: translateY(10px);
  animation: toast-in .25s ease forwards, toast-out .3s ease 2.7s forwards;
  max-width: 280px;
}
.toast-success { background: var(--green-d); }
.toast-info    { background: var(--blue-d); }
.toast-error   { background: var(--red); }
.toast-warning { background: var(--orange); }

@keyframes toast-in  { to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-6px); } }

/* =========================================================
   Gestion des utilisateurs
   ========================================================= */
.modal-box-wide { width: 600px; }
.modal-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}
.modal-header-row h2 { font-size: 1.1rem; }
.btn-icon {
  background: none; border: none;
  color: var(--muted); font-size: 1.1rem;
  padding: 0.2rem 0.4rem; border-radius: 4px;
  transition: color .2s, background .2s;
}
.btn-icon:hover { color: var(--text); background: var(--border); }

/* Table utilisateurs */
.users-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.users-table th {
  text-align: left;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--border);
}
.users-table td {
  padding: 0.55rem 0.6rem;
  border-bottom: 1px solid rgba(35,53,84,.5);
  vertical-align: middle;
}
.users-table tr:last-child td { border-bottom: none; }
.users-table tr:hover td { background: rgba(52,152,219,.05); }

.user-actions { display: flex; gap: 0.4rem; justify-content: flex-end; }
.btn-table-edit {
  padding: 0.2rem 0.55rem;
  border-radius: 5px;
  font-size: 0.75rem;
  background: rgba(52,152,219,.15);
  color: var(--blue);
  border: 1px solid var(--blue);
  transition: background .2s;
}
.btn-table-edit:hover { background: var(--blue-d); color: #fff; }

.btn-table-del {
  padding: 0.2rem 0.55rem;
  border-radius: 5px;
  font-size: 0.75rem;
  background: rgba(231,76,60,.15);
  color: var(--red);
  border: 1px solid var(--red);
  transition: background .2s;
}
.btn-table-del:hover { background: var(--red); color: #fff; }

/* Select dans les modales */
.form-group select {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  transition: border-color .2s;
  appearance: none;
  cursor: pointer;
}
.form-group select:focus { border-color: var(--blue); }

/* App principale masquée au départ */
#app.hidden { display: none; }
#app { display: flex; flex-direction: column; height: 100vh; }

/* =========================================================
   Barre de titre + bouton toggle sidebar
   ========================================================= */
.sidebar-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 0.4rem;
}

.sidebar-title {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
}

.btn-sidebar-toggle {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.75rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .2s, color .2s;
}
.btn-sidebar-toggle:hover { background: var(--border); color: var(--text); }

.btn-sidebar-open {
  position: absolute;
  top: 12px;
  left: 0;
  z-index: 5;
  padding: 0.4rem 0.55rem;
  border-radius: 0 6px 6px 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-left: none;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s, color .2s;
  white-space: nowrap;
  writing-mode: vertical-rl;
  letter-spacing: .05em;
}
.btn-sidebar-open:hover { background: var(--border); color: var(--text); }
.btn-sidebar-open.hidden { display: none; }

#layout { position: relative; }

/* Sidebar collapsed */
#sidebar.collapsed {
  width: 0 !important;
  min-width: 0 !important;
  overflow: hidden;
  border-right: none;
  flex-shrink: 0;
}

/* =========================================================
   Responsive
   ========================================================= */
@media (max-width: 768px) {
  #header h1   { font-size: 0.85rem; }
  #header small { display: none; }
  .badges      { display: none; }
  .worker-block { font-size: 0.72rem; }
  #sidebar { width: 240px; min-width: 200px; }
}

@media (max-width: 480px) {
  #sidebar { width: 200px; min-width: 180px; }
  .logo { font-size: 1.2rem; }
  #header h1 { font-size: 0.78rem; }
  .btn-sm { font-size: 0.68rem; padding: 0.15rem 0.45rem; }
  .role-badge { display: none; }
  #worker-display-header { display: none; }
}
