:root {
  --bg: #111827;
  --bg-elev: #18212f;
  --bg-soft: #223146;
  --panel: #0f172a;
  --panel-2: #162033;
  --text: #e5edf7;
  --muted: #9fb0c7;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #2ea8ff;
  --accent-hover: #1c90e4;
  --success: #1fb874;
  --warning: #f59e0b;
  --error: #ef4444;
  --shadow: 0 12px 30px rgba(0, 0, 0, 0.28);
  --radius: 14px;
  --radius-sm: 10px;
  --topbar-h: 72px;
  --status-h: 42px;
  --transition: 180ms ease;
}

body.light {
  --bg: #f3f6fb;
  --bg-elev: #ffffff;
  --bg-soft: #e7edf5;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --text: #102032;
  --muted: #5f7288;
  --border: rgba(16, 32, 50, 0.12);
  --accent: #0077cc;
  --accent-hover: #005fa3;
  --shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
}

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

html,
body {
  margin: 0;
  height: 100%;
  font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  overflow: hidden;
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.topbar {
  grid-row: 1;
  min-width: 0;
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 18px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-mark {
  font-size: 1.5rem;
}

.brand h1 {
  margin: 0;
  font-size: 1rem;
  line-height: 1.1;
}

.brand p {
  margin: 4px 0 0;
  font-size: 0.84rem;
  color: var(--muted);
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.controls button,
.switch-btn,
.layout-btn,
.workspace-tab,
.btn {
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
  border-radius: 10px;
  transition:
    transform var(--transition),
    background var(--transition),
    border-color var(--transition);
}

.controls button {
  width: 40px;
  height: 40px;
  font-size: 1rem;
}

.controls button:hover,
.btn:hover {
  transform: translateY(-1px);
  border-color: rgba(46, 168, 255, 0.45);
}

.switch-btn:hover,
.layout-btn:hover,
.workspace-tab:hover {
  border-color: rgba(46, 168, 255, 0.45);
}

.app-shell {
  grid-row: 3;
  min-width: 0;
  display: flex;
  min-height: 0;
}

.workspace-tabs {
  grid-row: 2;
  min-width: 0;
  display: none;
}

.pane {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

#editorPane {
  flex: 1 1 58%;
  background: var(--panel);
}

#previewPane {
  flex: 1 1 42%;
  background: var(--panel-2);
  border-left: 1px solid var(--border);
}

.pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.02);
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.editor-view-controls,
.layout-actions,
.header-actions {
  display: flex;
  align-items: center;
}

.editor-view-controls {
  gap: 14px;
}

.layout-actions {
  gap: 4px;
}

.layout-label {
  margin-right: 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
}

.preview-hint {
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: none;
  letter-spacing: normal;
}

.header-actions {
  gap: 8px;
}

.switch-btn,
.layout-btn {
  padding: 6px 10px;
  font-size: 0.78rem;
}

.switch-btn.active,
.layout-btn.active,
.workspace-tab.active {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
}

.layout-btn:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.multi-editors {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  flex: 1;
  min-height: 0;
  padding: 12px;
}

.multi-editors[data-visible-count="1"] {
  grid-template-columns: minmax(0, 1fr);
}

.multi-editors[data-visible-count="2"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.editor-block[hidden] {
  display: none;
}

.editor-block {
  display: flex;
  flex-direction: column;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.editor-block.is-focused {
  border-color: rgba(46, 168, 255, 0.55);
  box-shadow: 0 0 0 1px rgba(46, 168, 255, 0.18);
}

.editor-label {
  padding: 10px 12px;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--accent);
}

/* 
  Masque le textarea source SEULEMENT après l'initialisation JS,
  pour éviter les conflits avec le DOM interne de CodeMirror.
  La classe .editor-ready est appliquée par init() après createEditor().
*/
.editor-ready > textarea {
  display: none;
}

.editor-block.textarea-fallback > textarea {
  display: block;
}

.CodeMirror {
  width: 100%;
  height: 100% !important;
  min-width: 0;
  min-height: 0;
  font-size: 14px;
  line-height: 1.5;
}

.CodeMirror-gutters {
  border-right: 1px solid var(--border);
}

#resizer {
  width: 6px;
  background: var(--border);
  cursor: col-resize;
  transition: background var(--transition);
}

#resizer:hover {
  background: var(--accent);
}

#resizer:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

#viewer {
  width: 100%;
  height: 100%;
  flex: 1;
  border: 0;
  background: #fff;
}

#status {
  grid-row: 4;
  min-width: 0;
  height: var(--status-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--muted);
  font-size: 0.86rem;
}

.notification {
  position: fixed;
  top: 88px;
  right: 18px;
  z-index: 3000;
  min-width: 220px;
  max-width: 360px;
  padding: 12px 14px;
  border-radius: 12px;
  color: #fff;
  box-shadow: var(--shadow);
  animation: slideIn 0.22s ease;
}

.notification.success {
  background: var(--success);
}

.notification.warning {
  background: var(--warning);
}

.notification.error {
  background: var(--error);
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-8px) translateX(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0) translateX(0);
  }
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 2500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.55);
}

.modal-content {
  width: 100%;
  max-width: 440px;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 22px;
}

.modal-content h2 {
  margin: 0 0 16px;
  font-size: 1.1rem;
}

.modal-content label {
  display: block;
  margin: 12px 0 8px;
  color: var(--muted);
  font-size: 0.92rem;
}

.modal-content input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
}

.check-row {
  display: flex !important;
  align-items: center;
  gap: 10px;
}

.check-row input {
  width: auto;
}

.modal-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}

.btn {
  padding: 10px 14px;
}

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

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

.zip-preview {
  display: none;
  margin: 12px 0 0;
  padding: 12px;
  border-radius: 12px;
  overflow: auto;
  background: #0b1220;
  color: #dbeafe;
  border: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 0.9rem;
  white-space: pre-wrap;
}

.zip-preview.is-visible {
  display: block;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

@media (max-width: 980px) {
  .app-shell {
    flex-direction: column;
  }

  #resizer {
    width: 100%;
    height: 6px;
    cursor: row-resize;
  }

  #previewPane {
    border-left: 0;
    border-top: 1px solid var(--border);
  }
}

@media (max-width: 767px) {
  :root {
    --topbar-h: auto;
    --status-h: 36px;
  }

  .workspace-tabs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    padding: 6px 10px;
    background: var(--bg-elev);
    border-bottom: 1px solid var(--border);
  }

  .workspace-tab {
    min-height: 36px;
    padding: 6px 12px;
    font-size: 0.82rem;
    font-weight: 700;
  }

  body[data-workspace-view="code"] #previewPane,
  body[data-workspace-view="preview"] #editorPane,
  body[data-workspace-view="code"] #resizer,
  body[data-workspace-view="preview"] #resizer {
    display: none;
  }

  #editorPane,
  #previewPane {
    flex: 1 1 100%;
    width: 100%;
    height: 100%;
  }

  #previewPane {
    border-top: 0;
  }

  .topbar {
    gap: 10px;
    padding: 10px 12px;
  }

  .controls {
    min-width: 0;
    flex: 1;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
  }

  .controls button {
    flex: 0 0 auto;
    width: 36px;
    height: 36px;
  }

  .brand p {
    display: none;
  }

  .pane-header {
    flex-wrap: wrap;
    padding: 10px 12px;
  }

  .layout-actions {
    display: none;
  }

  .multi-editors {
    padding: 8px;
  }

  #status {
    padding: 0 12px;
    font-size: 0.78rem;
  }
}

@media (max-width: 479px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .controls {
    width: 100%;
    gap: 5px;
  }

  .controls button {
    width: 34px;
    height: 34px;
  }
}

@media (max-width: 767px) and (max-height: 500px) {
  .topbar {
    min-height: 52px;
    padding-block: 6px;
  }

  .brand-mark {
    font-size: 1.2rem;
  }

  .brand h1 {
    font-size: 0.9rem;
  }

  .controls button {
    width: 32px;
    height: 32px;
  }

  .workspace-tabs {
    padding-block: 4px;
  }

  .workspace-tab {
    min-height: 32px;
  }
}
