:root {
  --bg: #080c14;
  --bg-deep: #050810;
  --panel: #0d1420;
  --panel-2: #121c2b;
  --panel-3: #182436;
  --surface: rgba(20, 31, 47, .82);
  --border: rgba(148, 163, 184, .17);
  --border-strong: rgba(148, 163, 184, .28);
  --text: #f4f7fb;
  --muted: #8e9bad;
  --muted-strong: #b4bfce;
  --accent: #f1b84b;
  --accent-hover: #ffd071;
  --accent-ink: #211707;
  --violet: #7a6cf1;
  --cyan: #51c7d7;
  --danger: #ff7878;
  --success: #65d6a6;
  --warning: #f1c56c;
  --x: #f56f72;
  --y: #67d5a0;
  --z: #62a7f8;

  --header-h: 68px;
  --sidebar-w: clamp(330px, 28vw, 390px);
  --editor-w: clamp(420px, 36vw, 560px);
  --drawer-w: min(92vw, 470px);
  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --shadow: 0 20px 55px rgba(0, 0, 0, .34);
  --anim: .22s cubic-bezier(.2, .8, .2, 1);
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, "Segoe UI", Arial, sans-serif;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, select, textarea { font: inherit; }
button, a, select { -webkit-tap-highlight-color: transparent; }

/* Accessibilité */
.skip-link {
  position: absolute;
  left: 12px;
  top: -60px;
  z-index: 100;
  padding: 10px 14px;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  transition: top var(--anim);
}

.skip-link:focus { top: 0; }

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

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* En-tête de l’application */
.app-header {
  position: relative;
  z-index: 40;
  display: grid;
  grid-template-columns: minmax(210px, auto) minmax(0, 1fr) auto;
  align-items: center;
  gap: 28px;
  height: var(--header-h);
  padding: 0 22px;
  background: rgba(8, 12, 20, .92);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 8px 30px rgba(0, 0, 0, .18);
  backdrop-filter: blur(18px);
}

.app-header::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -1px;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(122, 108, 241, .5), rgba(241, 184, 75, .4), transparent);
  pointer-events: none;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
  color: var(--text);
  text-decoration: none;
}

.brand-mark {
  position: relative;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border: 1px solid rgba(241, 184, 75, .48);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffd984 0 8%, #f1b84b 9% 24%, #a763e8 56%, #43367c 100%);
  box-shadow: 0 0 28px rgba(122, 108, 241, .25), inset -7px -8px 12px rgba(25, 15, 49, .42);
}

.brand-mark::before {
  content: "";
  position: absolute;
  width: 45px;
  height: 12px;
  border: 1.5px solid rgba(241, 184, 75, .72);
  border-radius: 50%;
  transform: rotate(-17deg);
}

.brand-mark span {
  position: absolute;
  right: -5px;
  top: 4px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #fff0bf;
  box-shadow: 0 0 10px var(--accent);
}

.brand-copy { display: grid; line-height: 1.05; }
.brand-copy strong { font-size: 15px; letter-spacing: .02em; }
.brand-copy small {
  margin-top: 4px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: .14em;
  text-transform: uppercase;
}

.page-title {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
  padding-left: 26px;
  border-left: 1px solid var(--border);
}

.page-title-dot {
  width: 7px;
  height: 7px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 12px rgba(101, 214, 166, .68);
}

.page-title h1 {
  margin: 0;
  overflow: hidden;
  color: var(--text);
  font-size: 14px;
  font-weight: 650;
  letter-spacing: .01em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.page-title p {
  margin: 2px 0 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.nav-link, .badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted-strong);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .01em;
  white-space: nowrap;
}

.nav-link {
  background: rgba(255, 255, 255, .025);
  text-decoration: none;
  transition: color .15s ease, border-color .15s ease, background .15s ease, transform .15s ease;
}

.nav-link:hover {
  color: var(--text);
  border-color: var(--border-strong);
  background: rgba(255, 255, 255, .055);
  transform: translateY(-1px);
}

.nav-link-accent { color: #ffe3a6; border-color: rgba(241, 184, 75, .28); }

.badge { color: var(--muted); }
.badge i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 9px rgba(241, 184, 75, .6);
}

/* Bouton hamburger */
.drawer-toggle {
  display: none;
  width: 40px;
  height: 40px;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  margin: 0;
  padding: 0;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
}

.drawer-toggle:hover { background: var(--panel-3); }
.drawer-toggle .bars,
.drawer-toggle .bars::before,
.drawer-toggle .bars::after {
  display: block;
  width: 18px;
  height: 1.5px;
  background: currentColor;
  border-radius: 2px;
  transition: transform var(--anim), opacity var(--anim), background var(--anim);
}

.drawer-toggle .bars { position: relative; }
.drawer-toggle .bars::before,
.drawer-toggle .bars::after { content: ""; position: absolute; left: 0; }
.drawer-toggle .bars::before { top: -6px; }
.drawer-toggle .bars::after { top: 6px; }
body.drawer-open .drawer-toggle .bars { background: transparent; }
body.drawer-open .drawer-toggle .bars::before { transform: translateY(6px) rotate(45deg); }
body.drawer-open .drawer-toggle .bars::after { transform: translateY(-6px) rotate(-45deg); }

/* Structures principales */
.layout, .editor-grid {
  display: flex;
  height: calc(100dvh - var(--header-h));
  min-height: 0;
}

.sidebar, .editor-pane {
  position: relative;
  z-index: 2;
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  min-height: 0;
  padding: 22px;
  overflow: auto;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .018), transparent 22%),
    var(--panel);
  border-right: 1px solid var(--border);
  scrollbar-color: var(--panel-3) transparent;
}

.sidebar { width: var(--sidebar-w); }
.editor-pane { width: var(--editor-w); }

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.eyebrow {
  display: block;
  margin-bottom: 7px;
  color: var(--accent);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: .16em;
  text-transform: uppercase;
}

.panel-heading h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 650;
  letter-spacing: -.02em;
}

.panel-index {
  color: rgba(142, 155, 173, .3);
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 25px;
  font-weight: 700;
}

.shortcut {
  flex: 0 0 auto;
  padding: 6px 8px;
  color: var(--muted);
  background: rgba(255, 255, 255, .03);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 10px;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, .18);
}

.form-stack { display: flex; flex-direction: column; gap: 17px; }
.field-group, .editor-field { min-width: 0; }
.field-group-grow { display: flex; flex-direction: column; }
.editor-field { display: flex; flex: 1 1 260px; flex-direction: column; min-height: 210px; }

.field-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 8px;
  color: var(--muted-strong);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .025em;
}

.field-meta {
  color: #6f7c8e;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 9px;
  font-weight: 500;
}

select, textarea {
  width: 100%;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}

select:hover, textarea:hover { border-color: var(--border-strong); }
select:focus, textarea:focus {
  border-color: rgba(241, 184, 75, .52);
  background: #111b2a;
  box-shadow: 0 0 0 3px rgba(241, 184, 75, .08);
  outline: none;
}

select {
  height: 43px;
  padding: 0 38px 0 12px;
  color-scheme: dark;
  cursor: pointer;
}

textarea {
  min-height: 130px;
  padding: 12px;
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 12px;
  line-height: 1.55;
  resize: vertical;
}

.code-shell {
  position: relative;
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .02), 0 10px 30px rgba(0, 0, 0, .16);
}

.code-shell::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 3px;
  background: linear-gradient(180deg, var(--violet), var(--accent));
  opacity: .82;
  pointer-events: none;
}

#code {
  flex: 1;
  min-height: 100%;
  padding: 15px 16px 15px 18px;
  resize: none;
  color: #dfe8f4;
  background: transparent;
  border: 0;
  border-radius: 0;
  font-size: 12px;
  line-height: 1.62;
  tab-size: 2;
}

#code:focus { background: rgba(255, 255, 255, .012); box-shadow: none; }
#code::placeholder, #content::placeholder { color: #596678; }

/* Boutons */
button {
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: transform .15s ease, background .15s ease, border-color .15s ease, color .15s ease, opacity .15s ease;
}

button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { cursor: wait; opacity: .58; }

.primary-action, button.secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 44px;
  padding: 0 14px;
  font-size: 12px;
  font-weight: 750;
}

.primary-action {
  color: var(--accent-ink);
  background: linear-gradient(135deg, var(--accent), #e9a83b);
  box-shadow: 0 8px 22px rgba(205, 140, 40, .16), inset 0 1px 0 rgba(255, 255, 255, .32);
}

.primary-action:hover { background: linear-gradient(135deg, var(--accent-hover), var(--accent)); }

button.secondary {
  color: var(--muted-strong);
  background: var(--panel-2);
  border: 1px solid var(--border);
}

button.secondary:hover { color: var(--text); background: var(--panel-3); border-color: var(--border-strong); }

.btn-row { display: grid; grid-template-columns: .9fr 1.1fr; gap: 10px; margin-top: 12px; }
.btn-row button { width: 100%; }

.tool-button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 34px;
  padding: 0 11px;
  color: var(--muted-strong);
  background: rgba(11, 17, 27, .72);
  border: 1px solid var(--border);
  font-size: 10px;
  font-weight: 700;
  backdrop-filter: blur(12px);
  pointer-events: auto;
}

.tool-button:hover { color: var(--text); background: rgba(25, 36, 53, .92); border-color: var(--border-strong); }
.tool-button span { color: var(--accent); font-size: 15px; }

/* Aide, console et résultats */
.hint {
  margin: 7px 0 0;
  color: #778597;
  font-size: 10px;
  line-height: 1.45;
}

.hint code { color: #c9a7fb; font-family: Consolas, "SFMono-Regular", monospace; }

.helper-card {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  margin: 11px 0 0;
  padding: 9px 10px;
  background: rgba(122, 108, 241, .055);
  border: 1px solid rgba(122, 108, 241, .14);
  border-radius: var(--radius-sm);
}

.helper-icon {
  display: grid;
  place-items: center;
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  color: #c8c0ff;
  border: 1px solid rgba(200, 192, 255, .34);
  border-radius: 50%;
  font-family: Georgia, serif;
  font-size: 10px;
  font-weight: 700;
}

.console-panel { margin-top: 13px; }
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 9px;
}

.section-heading h2, .card h2 {
  margin: 0;
  color: var(--muted-strong);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: .14em;
  text-transform: uppercase;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
}

.status-pill i, .viewport-status i {
  width: 6px;
  height: 6px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 9px rgba(101, 214, 166, .52);
}

.status-pill[data-state="busy"] i { background: var(--cyan); box-shadow: 0 0 9px rgba(81, 199, 215, .55); }
.status-pill[data-state="error"] i { background: var(--danger); box-shadow: 0 0 9px rgba(255, 120, 120, .55); }
.status-pill[data-state="limit"] i { background: var(--warning); box-shadow: 0 0 9px rgba(241, 197, 108, .55); }

.console {
  display: block;
  width: 100%;
  height: clamp(130px, 21vh, 190px);
  padding: 12px 13px;
  overflow: auto;
  color: #9fe0bd;
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: Consolas, "SFMono-Regular", monospace;
  font-size: 11px;
  line-height: 1.55;
  white-space: pre-wrap;
  scrollbar-color: var(--panel-3) transparent;
}

.console.err { color: #ffaaa9; border-color: rgba(255, 120, 120, .34); }
.console.limit { color: #f3d28d; border-color: rgba(241, 197, 108, .34); }
.console.busy { color: #9ddde5; border-color: rgba(81, 199, 215, .34); }

.card {
  margin-top: 18px;
  padding: 14px;
  background: linear-gradient(145deg, rgba(255, 255, 255, .026), rgba(255, 255, 255, .008)), var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.result-card { min-height: 102px; }
.result-card.err { color: var(--danger); border-color: rgba(255, 120, 120, .3); }
.result-error { margin: 0; color: #ffaaa9; font-size: 10px; line-height: 1.5; }

.kv {
  display: grid;
  grid-template-columns: minmax(68px, auto) minmax(0, 1fr);
  gap: 14px;
  padding: 4px 0;
  font-size: 10px;
}

.kv span:first-child { color: var(--muted); }
.kv span:last-child {
  overflow-wrap: anywhere;
  color: var(--text);
  font-family: Consolas, "SFMono-Regular", monospace;
  text-align: right;
}

.legend { display: flex; align-items: center; gap: 15px; }
.legend .item { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 9px; }
.legend .item b { color: var(--muted-strong); }
.legend .dot { width: 7px; height: 7px; border-radius: 2px; }
.legend .dot-x { background: var(--x); box-shadow: 0 0 8px rgba(245, 111, 114, .4); }
.legend .dot-y { background: var(--y); box-shadow: 0 0 8px rgba(103, 213, 160, .4); }
.legend .dot-z { background: var(--z); box-shadow: 0 0 8px rgba(98, 167, 248, .4); }

/* Scène 3D */
.stage {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 64% 46%, rgba(38, 58, 88, .28), transparent 34%),
    radial-gradient(circle at 88% 8%, rgba(122, 108, 241, .12), transparent 28%),
    var(--bg-deep);
}

.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  background-image:
    linear-gradient(rgba(148, 163, 184, .025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(148, 163, 184, .025) 1px, transparent 1px);
  background-size: 36px 36px;
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, .6), transparent 72%);
  pointer-events: none;
}

#viewport {
  position: relative;
  z-index: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#viewport:active { cursor: grabbing; }

.stage-toolbar {
  position: absolute;
  top: 18px;
  right: 18px;
  left: 18px;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  pointer-events: none;
}

.stage-identity {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-height: 34px;
  padding: 0 12px;
  color: var(--muted-strong);
  background: rgba(8, 12, 20, .62);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .16);
  backdrop-filter: blur(12px);
}

.viewport-title { font-size: 10px; font-weight: 750; letter-spacing: .04em; text-transform: uppercase; }
.viewport-status { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 9px; }

.stage-corner {
  position: absolute;
  right: 22px;
  bottom: 22px;
  z-index: 2;
  width: 38px;
  height: 38px;
  border-right: 1px solid rgba(241, 184, 75, .34);
  border-bottom: 1px solid rgba(241, 184, 75, .34);
  pointer-events: none;
}

.overlay {
  position: absolute;
  left: 18px;
  bottom: 18px;
  z-index: 3;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin: 0;
  pointer-events: none;
}

.overlay span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 29px;
  padding: 0 9px;
  color: var(--muted);
  background: rgba(8, 12, 20, .66);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 9px;
  backdrop-filter: blur(10px);
}

.overlay b { color: var(--accent); font-size: 11px; font-weight: 500; }

/* Voile du tiroir mobile */
.scrim {
  position: fixed;
  inset: var(--header-h) 0 0;
  z-index: 20;
  margin: 0;
  padding: 0;
  background: rgba(3, 6, 12, .68);
  border: 0;
  opacity: 0;
  pointer-events: none;
  backdrop-filter: blur(4px);
  transition: opacity var(--anim);
}

/* Tablette et mobile */
@media (max-width: 900px) {
  .app-header {
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 12px;
    padding: 0 12px;
  }

  .drawer-toggle { display: inline-flex; }
  .brand-copy small, .page-title, .badge { display: none; }
  .brand { min-width: 0; }
  .brand-mark { width: 31px; height: 31px; }
  .brand-mark::before { width: 40px; }
  .header-links { justify-self: end; }
  .nav-link { padding: 0 10px; }

  .layout, .editor-grid { display: block; }
  .stage { width: 100%; height: 100%; }

  .sidebar, .editor-pane {
    position: fixed;
    top: var(--header-h);
    bottom: 0;
    left: 0;
    z-index: 30;
    width: var(--drawer-w);
    max-width: 100%;
    transform: translateX(-100%);
    border-right-color: var(--border-strong);
    box-shadow: var(--shadow);
    transition: transform var(--anim);
  }

  body.drawer-open .sidebar,
  body.drawer-open .editor-pane { transform: translateX(0); }
  body.drawer-open .scrim { opacity: 1; pointer-events: auto; }

  .editor-field { min-height: 190px; }
  .console { height: 145px; }
  .stage-toolbar { top: 12px; right: 12px; left: 12px; }
  .overlay { left: 12px; bottom: 12px; }
  .overlay span:nth-child(3) { display: none; }
  .stage-corner { right: 15px; bottom: 15px; }
}

@media (max-width: 560px) {
  :root { --header-h: 62px; --drawer-w: 94vw; }
  .app-header { gap: 10px; }
  .brand-copy strong { font-size: 14px; }
  .brand-mark { display: none; }
  .nav-link { min-height: 32px; font-size: 10px; }
  .sidebar, .editor-pane { padding: 18px; }
  .panel-heading { margin-bottom: 17px; }
  .btn-row { grid-template-columns: 1fr; gap: 8px; }
  .helper-card { display: none; }
  .console-panel { margin-top: 11px; }
  .stage-identity { max-width: calc(100% - 105px); }
  .viewport-status { display: none; }
  .overlay span { min-height: 27px; }
  .overlay span:nth-child(2) { display: none; }
}

@media (max-height: 690px) and (min-width: 901px) {
  .sidebar, .editor-pane { padding-top: 17px; padding-bottom: 17px; }
  .panel-heading { margin-bottom: 14px; }
  .editor-field { min-height: 170px; }
  .helper-card { display: none; }
  .console { height: 125px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
  }
}

@media (prefers-contrast: more) {
  :root {
    --text: #fff;
    --muted: #c1cad6;
    --muted-strong: #e2e7ed;
    --border: rgba(255, 255, 255, .36);
    --border-strong: rgba(255, 255, 255, .58);
  }

  .stage::before { opacity: .3; }
}
