/**
 * Interface utilisateur de l'application MarkDown Reader & Styler
 */

:root {
  --ui-bg: #f8fafc;
  --ui-surface: #ffffff;
  --ui-border: #e2e8f0;
  --ui-border-hover: #cbd5e1;
  --ui-text: #0f172a;
  --ui-text-muted: #64748b;
  --ui-primary: #2563eb;
  --ui-primary-hover: #1d4ed8;
  --ui-primary-light: #eff6ff;
  --ui-radius: 8px;
  --ui-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --ui-shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
  --ui-shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
  --ui-header-height: 56px;
}

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

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

/* ==============================================================
   EN-TÊTE DE L'APPLICATION
   ============================================================== */
.app-header {
  height: var(--ui-header-height);
  background: var(--ui-surface);
  border-bottom: 1px solid var(--ui-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 20;
  flex-shrink: 0;
  user-select: none;
}

.header-left, .header-center, .header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--ui-text);
}

.logo-badge {
  background: var(--ui-primary);
  color: #fff;
  font-size: 0.72rem;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 800;
  letter-spacing: 0.5px;
}

/* Boutons & Contrôles */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--ui-text);
  background: var(--ui-surface);
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius);
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.btn:hover {
  background: #f1f5f9;
  border-color: var(--ui-border-hover);
}

.btn-primary {
  background: var(--ui-primary);
  color: #fff;
  border-color: var(--ui-primary);
}

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

.btn-icon {
  padding: 6px 8px;
}

.btn-group {
  display: flex;
  background: #f1f5f9;
  padding: 3px;
  border-radius: var(--ui-radius);
  border: 1px solid var(--ui-border);
}

.btn-group .btn-mode {
  padding: 4px 10px;
  font-size: 0.82rem;
  border: none;
  background: transparent;
  color: var(--ui-text-muted);
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: all 0.15s;
}

.btn-group .btn-mode.active {
  background: #fff;
  color: var(--ui-text);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* Sélecteur de Thème */
.theme-picker-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  color: var(--ui-text-muted);
}

.theme-picker-wrapper select {
  padding: 6px 10px;
  border-radius: var(--ui-radius);
  border: 1px solid var(--ui-border);
  background: var(--ui-surface);
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--ui-text);
  cursor: pointer;
  outline: none;
}

.theme-picker-wrapper select:focus {
  border-color: var(--ui-primary);
}

/* ==============================================================
   DISPOSITION PRINCIPALE (SPLIT VIEW)
   ============================================================== */
.app-layout {
  flex: 1;
  display: flex;
  position: relative;
  overflow: hidden;
}

/* Volet Éditeur */
.editor-pane {
  width: 48%;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-right: 1px solid var(--ui-border);
  height: 100%;
  position: relative;
  min-width: 250px;
}

/* Barre d'outils de l'éditeur */
.quick-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  background: #f8fafc;
  border-bottom: 1px solid var(--ui-border);
  flex-wrap: wrap;
  user-select: none;
}

.tb-btn {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 0.78rem;
  color: var(--ui-text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 26px;
}

.tb-btn:hover {
  background: #e2e8f0;
  color: var(--ui-text);
}

.tb-separator {
  width: 1px;
  height: 18px;
  background: var(--ui-border);
  margin: 0 4px;
}

/* Zone de texte de l'éditeur */
.editor-wrapper {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.markdown-input {
  width: 100%;
  height: 100%;
  padding: 16px 20px;
  border: none;
  resize: none;
  font-family: Consolas, "Fira Code", Monaco, "Courier New", monospace;
  font-size: 14.5px;
  line-height: 1.6;
  color: #1e293b;
  background: #ffffff;
  outline: none;
  overflow-y: auto;
}

.markdown-input::placeholder {
  color: #94a3b8;
}

/* Barre d'état sous l'éditeur */
.editor-statusbar {
  height: 28px;
  background: #f8fafc;
  border-top: 1px solid var(--ui-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  font-size: 0.75rem;
  color: var(--ui-text-muted);
}

/* Séparateur Redimensionnable */
.resizer-divider {
  width: 6px;
  background: #e2e8f0;
  cursor: col-resize;
  position: relative;
  transition: background 0.15s;
  flex-shrink: 0;
  z-index: 10;
}

.resizer-divider:hover,
.resizer-divider.dragging {
  background: var(--ui-primary);
}

/* Volet Rendu / Lecture */
.preview-pane {
  flex: 1;
  height: 100%;
  overflow-y: auto;
  background: var(--md-bg-color, #f6f8fa);
  transition: background-color 0.2s;
  padding: 24px;
  display: flex;
  justify-content: center;
}

/* Modes d'affichage */
body[data-view="reader"] .editor-pane,
body[data-view="reader"] .resizer-divider {
  display: none !important;
}

body[data-view="editor"] .preview-pane,
body[data-view="editor"] .resizer-divider {
  display: none !important;
}

body[data-view="editor"] .editor-pane {
  width: 100% !important;
  border-right: none;
}

/* Zone de glisser-déposer de fichier */
.dropzone-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(37, 99, 235, 0.08);
  border: 2px dashed var(--ui-primary);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--ui-primary);
  font-weight: 600;
  z-index: 50;
  pointer-events: none;
}

.editor-wrapper.dragover .dropzone-overlay {
  display: flex;
}

/* ==============================================================
   TIROIR DE PERSONNALISATION DU STYLE (STYLE DRAWER)
   ============================================================== */
.style-drawer {
  position: absolute;
  top: 0;
  right: 0;
  width: 380px;
  height: 100%;
  background: #ffffff;
  border-left: 1px solid var(--ui-border);
  box-shadow: var(--ui-shadow-lg);
  z-index: 40;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.style-drawer.open {
  transform: translateX(0);
}

.drawer-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--ui-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #f8fafc;
}

.drawer-header h3 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--ui-text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.style-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
  border-bottom: 1px solid #f1f5f9;
  padding-bottom: 16px;
}

.style-section:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.style-section-title {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--ui-text-muted);
  letter-spacing: 0.5px;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Contrôles de formulaires du tiroir */
.control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.control-label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--ui-text);
  flex: 1;
}

.control-input {
  display: flex;
  align-items: center;
  gap: 8px;
}

.control-input select,
.control-input input[type="text"],
.control-input input[type="number"] {
  padding: 5px 8px;
  font-size: 0.82rem;
  border: 1px solid var(--ui-border);
  border-radius: 6px;
  outline: none;
  background: #fff;
  color: var(--ui-text);
}

.control-input input[type="range"] {
  width: 110px;
  accent-color: var(--ui-primary);
  cursor: pointer;
}

.control-input input[type="color"] {
  -webkit-appearance: none;
  border: 1px solid var(--ui-border);
  width: 32px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  padding: 2px;
  background: #fff;
}

.control-input input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

.control-input input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 4px;
}

.range-val {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--ui-text-muted);
  min-width: 36px;
  text-align: right;
}

.drawer-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--ui-border);
  background: #f8fafc;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ==============================================================
   VISIONNEUSE PLEIN ÉCRAN D'IMAGES (LIGHTBOX)
   ============================================================== */
.lightbox-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.88);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 30px;
  cursor: zoom-out;
}

.lightbox-modal.open {
  display: flex;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.lightbox-content {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: default;
}

.lightbox-img {
  max-width: 100%;
  max-height: 80vh;
  object-fit: contain;
  border-radius: 6px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  background: #fff;
}

.lightbox-caption {
  color: #f1f5f9;
  font-size: 0.95rem;
  margin-top: 14px;
  text-align: center;
  font-style: italic;
  max-width: 700px;
  line-height: 1.4;
}

.lightbox-close {
  position: absolute;
  top: -40px;
  right: 0;
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: #fff;
  font-size: 1.5rem;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.lightbox-close:hover {
  background: rgba(255, 255, 255, 0.4);
}

/* ==============================================================
   TOASTS DE NOTIFICATION
   ============================================================== */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  background: #0f172a;
  color: #ffffff;
  padding: 10px 18px;
  border-radius: var(--ui-radius);
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: var(--ui-shadow-lg);
  display: flex;
  align-items: center;
  gap: 10px;
  pointer-events: auto;
  animation: slideUp 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  transition: opacity 0.2s, transform 0.2s;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.toast.toast-success {
  border-left: 4px solid #10b981;
}

.toast.toast-info {
  border-left: 4px solid #3b82f6;
}

/* Menu déroulant Export */
.dropdown-wrapper {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 6px;
  background: #fff;
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius);
  box-shadow: var(--ui-shadow-lg);
  display: none;
  flex-direction: column;
  min-width: 190px;
  padding: 6px 0;
  z-index: 60;
}

.dropdown-menu.show {
  display: flex;
}

.dropdown-item {
  padding: 8px 14px;
  font-size: 0.84rem;
  color: var(--ui-text);
  background: transparent;
  border: none;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.12s;
}

.dropdown-item:hover {
  background: #f1f5f9;
  color: var(--ui-primary);
}

.dropdown-separator {
  height: 1px;
  background: var(--ui-border);
  margin: 4px 0;
}

/* ==============================================================
   BOUTON FLOTTANT FAB SUR SMARTPHONE (BASCULE RAPIDE)
   ============================================================== */
.mobile-toggle-fab {
  display: none;
}

@media (max-width: 768px) {
  .mobile-toggle-fab {
    display: flex;
    align-items: center;
    gap: 7px;
    position: fixed;
    bottom: 20px;
    right: 18px;
    background: var(--ui-primary);
    color: #ffffff;
    border: none;
    border-radius: 50px;
    padding: 10px 16px;
    font-size: 0.88rem;
    font-weight: 600;
    box-shadow: 0 4px 16px rgba(37, 99, 235, 0.45);
    cursor: pointer;
    z-index: 95;
    transition: transform 0.15s ease, background 0.15s ease;
    user-select: none;
  }

  .mobile-toggle-fab:active {
    transform: scale(0.94);
  }
}

/* ==============================================================
   RESPONSIVE DESIGN SMARTPHONE & TABLETTE (<= 860px)
   ============================================================== */
@media (max-width: 860px) {
  html, body {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  :root {
    --ui-header-height: auto;
  }

  /* En-tête multi-niveaux et compact */
  .app-header {
    height: auto;
    padding: 8px 10px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .header-left {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
  }

  .app-logo {
    font-size: 0.95rem;
  }

  .app-logo span:not(.logo-badge) {
    display: inline-block;
  }

  /* La barre de bascule de vue prend toute la largeur sur mobile */
  .view-switcher {
    flex: 1;
    display: flex;
  }

  .view-switcher .btn-mode {
    flex: 1;
    justify-content: center;
    padding: 6px 4px;
    font-size: 0.8rem;
  }

  /* Barre des actions (thème, style, exports) */
  .header-center, .header-right {
    gap: 6px;
  }

  .header-center {
    flex: 1;
    justify-content: flex-start;
  }

  .theme-picker-wrapper label {
    display: none;
  }

  .theme-picker-wrapper select {
    padding: 5px 6px;
    font-size: 0.8rem;
    max-width: 130px;
  }

  /* Ajustement de la taille des boutons */
  .btn {
    padding: 5px 8px;
    font-size: 0.8rem;
  }

  /* Panneau d'agencement principal */
  .app-layout {
    width: 100%;
    max-width: 100vw;
    flex: 1;
    overflow: hidden;
  }

  /* Mode Éditeur Seul : 100% de la largeur de l'écran */
  body[data-view="editor"] .editor-pane {
    width: 100% !important;
    max-width: 100vw !important;
    min-width: 0 !important;
  }

  .markdown-input {
    padding: 12px 14px !important;
    font-size: 15px !important;
    line-height: 1.55;
  }

  .quick-toolbar {
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    padding: 4px 6px !important;
    gap: 2px !important;
  }

  .quick-toolbar::-webkit-scrollbar {
    display: none;
  }

  /* Mode Lecture Seule : 100% de la largeur de l'écran */
  body[data-view="reader"] .preview-pane {
    width: 100% !important;
    max-width: 100vw !important;
    padding: 0 !important;
    display: block !important;
  }

  body[data-view="reader"] .markdown-body {
    width: 100% !important;
    max-width: 100% !important;
    padding: 16px 12px !important;
    margin: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
  }

  /* Mode Scindé / Partagé sur mobile : disposition verticale (haut / bas) */
  body[data-view="split"] .app-layout {
    flex-direction: column !important;
  }

  body[data-view="split"] .editor-pane {
    width: 100% !important;
    max-width: 100vw !important;
    height: 50% !important;
    min-width: 0 !important;
    border-right: none !important;
    border-bottom: 2px solid var(--ui-border) !important;
  }

  body[data-view="split"] .resizer-divider {
    width: 100% !important;
    height: 10px !important;
    cursor: row-resize !important;
    border-top: 1px solid var(--ui-border);
    border-bottom: 1px solid var(--ui-border);
  }

  body[data-view="split"] .preview-pane {
    width: 100% !important;
    max-width: 100vw !important;
    height: 50% !important;
    padding: 0 !important;
    display: block !important;
  }

  body[data-view="split"] .markdown-body {
    width: 100% !important;
    max-width: 100% !important;
    padding: 14px 10px !important;
    margin: 0 !important;
    box-shadow: none !important;
  }

  /* Tiroir de personnalisation des styles : plein écran sur mobile */
  .style-drawer {
    width: 100% !important;
    max-width: 100vw !important;
  }

  /* Notifications Toast sur mobile */
  .toast-container {
    bottom: 74px;
    right: 12px;
    left: 12px;
  }

  .toast {
    width: 100%;
    box-sizing: border-box;
  }
}

/* ==============================================================
   AJUSTEMENTS POUR ÉCRANS TRÈS ÉTROITS (SMARTPHONE <= 540px)
   ============================================================== */
@media (max-width: 540px) {
  .app-header {
    padding: 6px 8px;
    gap: 6px;
  }

  .header-right {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
  }

  /* Cacher les boutons secondaires directs sur les très petits écrans pour éviter tout débordement */
  .btn-secondary-mobile {
    display: none !important;
  }

  /* Rendre le bouton copier plus compact */
  #btn-copy-formatted {
    padding: 5px 8px;
  }

  .theme-picker-wrapper select {
    max-width: 110px;
    font-size: 0.75rem;
  }
}

