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

:root {
  --bg: #0a0a0f;
  --surface: #13131a;
  --accent: #7c3aed;
  --accent-glow: rgba(124,58,237,0.4);
  --text: #e2e8f0;
  --text-dim: #64748b;
  --border: #1e1e2e;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Space Mono', 'Courier New', monospace;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  transition: background-color 1s ease;
}

/* ── Splash ──────────────────────────────────────────────────────────────── */

#splash {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  transition: opacity 0.6s ease;
  animation: fadeIn 0.5s ease;
}

#splash.hidden { opacity: 0; pointer-events: none; }

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

#splash-content {
  position: relative;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

#splash-content::before {
  content: '';
  position: absolute;
  width: 320px;
  height: 320px;
  background: radial-gradient(circle, var(--accent-glow) 0%, transparent 70%);
  border-radius: 50%;
  pointer-events: none;
  z-index: -1;
  animation: pulse-orb 3s ease-in-out infinite;
}

@keyframes pulse-orb {
  0%, 100% { opacity: 0.4; transform: scale(1); }
  50%       { opacity: 0.7; transform: scale(1.15); }
}

#splash h1 {
  font-size: clamp(2rem, 8vw, 3.5rem);
  letter-spacing: 0.6em;
  color: var(--accent);
  text-shadow: 0 0 40px var(--accent-glow);
}

#splash p {
  font-size: 0.78rem;
  color: var(--text-dim);
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

#start-btn {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 12px 36px;
  font-family: 'Space Mono', 'Courier New', monospace;
  font-size: 0.85rem;
  letter-spacing: 0.35em;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  margin-top: 8px;
}

#start-btn:hover:not(:disabled) {
  background: rgba(124,58,237,0.12);
  box-shadow: 0 0 24px var(--accent-glow);
}

#start-btn:disabled { opacity: 0.5; cursor: default; }

/* ── App layout ──────────────────────────────────────────────────────────── */

#app {
  width: 100%;
  max-width: 640px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#right-panel { display: flex; flex-direction: column; gap: 12px; }

/* ── Header ──────────────────────────────────────────────────────────────── */

#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
}

#header h1 {
  flex: 1;
  text-align: center;
  font-size: 1rem;
  letter-spacing: 0.45em;
  color: var(--accent);
  text-shadow: 0 0 20px var(--accent-glow);
}

#pause-btn {
  width: 36px;
  height: 36px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

#pause-btn:hover  { border-color: var(--accent); background: #1e1e2e; }
#pause-btn.paused { border-color: var(--accent); color: var(--accent); }

#volume-control { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
#vol-icon { font-size: 0.9rem; opacity: 0.7; }

#volume-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 90px;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

#volume-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  box-shadow: 0 0 6px var(--accent-glow);
}

#volume-slider::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
}

/* ── Camera ──────────────────────────────────────────────────────────────── */

#camera-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 4/3;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  transition: box-shadow 0.6s ease, border-color 0.6s ease;
}

#video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: scaleX(-1);
}

/* ── Emotion display ─────────────────────────────────────────────────────── */

#current-emotion-display {
  background: var(--surface);
  border-left: 3px solid var(--border);
  border-radius: 10px;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: border-color 0.5s ease, background 0.5s ease;
}

#current-emotion-display.triggered { animation: flash 0.4s ease-out; }

@keyframes flash {
  0%   { box-shadow: 0 0 0 0 var(--accent-glow); }
  50%  { box-shadow: 0 0 28px 10px var(--accent-glow); }
  100% { box-shadow: 0 0 0 0 transparent; }
}

#emotion-icon  { font-size: 3rem; line-height: 1.2; }

#emotion-score {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--text-dim);
  line-height: 1.1;
  transition: color 0.4s;
  min-height: 2.2rem;
}

#emotion-label {
  font-size: 0.68rem;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--text-dim);
  transition: color 0.4s;
}

/* ── Bars ────────────────────────────────────────────────────────────────── */

#bars-container {
  background: var(--surface);
  border-radius: 10px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bar-row {
  display: grid;
  grid-template-columns: 110px 1fr 38px;
  align-items: center;
  gap: 10px;
  opacity: 0.45;
  transition: opacity 0.3s;
}

.bar-row.dominant { opacity: 1; }

.bar-label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-dim);
  transition: color 0.3s;
}

.bar-row.dominant .bar-label { color: var(--text); }
.bar-emoji { font-size: 0.9rem; line-height: 1; }
.bar-name  { font-size: 0.62rem; letter-spacing: 0.08em; text-transform: uppercase; }

.bar-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: visible;
  transition: height 0.3s;
}

.bar-row.dominant .bar-track { height: 6px; }

.bar-fill {
  height: 100%;
  border-radius: 2px;
  width: 0%;
  transition: width 0.12s ease;
}

.bar-pct {
  font-size: 0.6rem;
  color: var(--text-dim);
  text-align: right;
  transition: color 0.3s;
}

.bar-row.dominant .bar-pct { color: var(--text); }

/* ── Status ──────────────────────────────────────────────────────────────── */

#status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 16px;
  border-radius: 20px;
  background: var(--surface);
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  width: fit-content;
  align-self: center;
  color: var(--text-dim);
}

#status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-dim);
  flex-shrink: 0;
  transition: background 0.3s;
}

#status.detecting #status-dot { background: #34d399; animation: blink 2s ease infinite; }
#status.no-face   #status-dot { background: #f97316; }
#status.paused    #status-dot { background: var(--accent); }
#status.error     #status-dot { background: #ef4444; }

@keyframes blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.2; }
}

/* ── I — Desktop deux colonnes (> 900px) ─────────────────────────────────── */

@media (min-width: 900px) {
  #app {
    max-width: 1100px;
    min-height: calc(100vh - 32px);
    display: grid;
    grid-template-areas: "header header" "camera right";
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto 1fr;
    align-items: stretch;
    gap: 20px;
  }

  #header      { grid-area: header; }
  #camera-wrap {
    grid-area: camera;
    aspect-ratio: unset;
    height: 100%;
    min-height: 300px;
  }
  #right-panel { grid-area: right; }

  #current-emotion-display {
    flex: 1;
    justify-content: center;
  }

  #emotion-score { font-size: 2.2rem; }
}

/* ── J — Mobile portrait (< 480px) ──────────────────────────────────────── */

@media (max-width: 480px) {
  #app { padding: 10px; gap: 10px; }
  #header h1    { font-size: 0.8rem; }
  #pause-btn    { width: 30px; height: 30px; font-size: 0.85rem; }
  #volume-slider { width: 70px; }
  #emotion-icon  { font-size: 2.4rem; }
  #emotion-score { font-size: 1.5rem; }
  #bars-container { padding: 10px 12px; gap: 8px; }
  .bar-row  { grid-template-columns: 90px 1fr 30px; gap: 8px; }
  .bar-track { height: 5px; }
  .bar-row.dominant .bar-track { height: 8px; }
}

/* ── K — Mobile landscape (hauteur < 500px) ──────────────────────────────── */

@media (orientation: landscape) and (max-height: 500px) {
  body { min-height: unset; }

  #app {
    max-width: 100%;
    padding: 8px;
    display: grid;
    grid-template-areas: "header header" "camera right";
    grid-template-columns: 55% 45%;
    grid-template-rows: auto 1fr;
    gap: 8px;
  }

  #header      { grid-area: header; }
  #camera-wrap { grid-area: camera; }
  #right-panel { grid-area: right; gap: 6px; overflow-y: auto; max-height: calc(100vh - 52px); }

  #header h1     { font-size: 0.78rem; }
  #pause-btn     { width: 28px; height: 28px; font-size: 0.8rem; }
  #volume-slider { width: 65px; }

  #current-emotion-display { padding: 10px; flex-direction: row; justify-content: center; gap: 14px; }
  #emotion-icon  { font-size: 1.6rem; }
  #emotion-score { font-size: 1.2rem; min-height: unset; }
  #emotion-label { font-size: 0.58rem; }

  #bars-container { padding: 8px 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; }
  .bar-row   { grid-template-columns: 80px 1fr 26px; gap: 5px; }
  .bar-name  { font-size: 0.55rem; }
  .bar-track { height: 4px; }
  .bar-row.dominant .bar-track { height: 6px; }
  .bar-pct   { font-size: 0.55rem; }
  #status { font-size: 0.58rem; padding: 4px 12px; }
}

/* ── E — Timeline d'historique ───────────────────────────────────────────── */

#history-timeline {
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 46px;
  flex-wrap: wrap;
}

#history-timeline:empty::before {
  content: '— aucun déclenchement —';
  font-size: 0.6rem;
  color: var(--text-dim);
  letter-spacing: 0.1em;
  font-family: 'Space Mono', monospace;
}

.timeline-item {
  font-size: 1.4rem;
  line-height: 1;
  cursor: default;
  transition: opacity 0.4s, filter 0.4s;
  filter: drop-shadow(0 0 3px var(--item-color, transparent));
}

.timeline-item.latest {
  animation: pop-in 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes pop-in {
  from { transform: scale(0.4) translateY(6px); }
  to   { transform: scale(1)   translateY(0); }
}

/* ── F — Indicateur de cooldown ──────────────────────────────────────────── */

.cooldown-bar {
  position: absolute;
  bottom: -3px;
  left: 0;
  height: 2px;
  width: 0%;
  border-radius: 1px;
  opacity: 0;
  pointer-events: none;
}

.cooldown-bar.active {
  animation: cooldown-drain 1.5s linear forwards;
}

@keyframes cooldown-drain {
  0%   { width: 100%; opacity: 0.85; }
  85%  { opacity: 0.5; }
  100% { width: 0%;   opacity: 0; }
}

/* ── G — Compteur de session ─────────────────────────────────────────────── */

.bar-count {
  margin-left: auto;
  font-size: 0.52rem;
  color: var(--text-dim);
  background: #111118;
  border-radius: 8px;
  padding: 1px 5px;
  min-width: 18px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  opacity: 0;
  transition: opacity 0.3s, color 0.3s;
}

.bar-count.has-count { opacity: 1; }
.bar-row.dominant .bar-count { color: var(--text); }

.bar-count.bump { animation: count-bump 0.3s ease-out; }

@keyframes count-bump {
  0%   { transform: scale(1.6); }
  100% { transform: scale(1); }
}

/* ── H — Slider de sensibilité ───────────────────────────────────────────── */

#sensitivity-row {
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.ctrl-hint {
  font-size: 0.52rem;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  white-space: nowrap;
  text-transform: uppercase;
}

#sensitivity-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

#sensitivity-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  box-shadow: 0 0 6px var(--accent-glow);
}

#sensitivity-slider::-moz-range-thumb {
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
}

/* ── I — Bouton reset ────────────────────────────────────────────────────── */

#left-controls { display: flex; align-items: center; gap: 6px; }

#reset-btn {
  width: 36px; height: 36px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 1rem;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

#reset-btn:hover { border-color: var(--accent); background: #1e1e2e; }
