:root {
  --bg1: #11553f;
  --bg2: #062219;
  --gold: #e8c874;
  --gold-d: #b8862c;
  --green1: #5ee08c;
  --green2: #22b35e;
  --green3: #127a3c;
  --red: #e5484d;
  --teal1: #6fd3e0;
  --teal2: #1f8ea3;
  --text: #fffaf0;
  --muted: #cfe6da;
  --size: min(70vw, 50vh, 480px);
  --lvl: 0;
}
@media (orientation: landscape) {
  :root { --size: min(54vh, 460px); }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg2);
  color: var(--text);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
body {
  font-family: "Noto Kufi Arabic", "Noto Sans Arabic", "Noto Naskh Arabic", "Segoe UI", Tahoma, Arial, sans-serif;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: max(3vh, env(safe-area-inset-top)) 4vw max(3vh, env(safe-area-inset-bottom));
  background: radial-gradient(120% 90% at 50% 40%, var(--bg1) 0%, var(--bg2) 72%);
}
#app::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.07;
  background-size: 96px 96px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 80 80'%3E%3Cg fill='none' stroke='%23e8c874' stroke-width='1.3'%3E%3Cpath d='M40 6 49 31 74 40 49 49 40 74 31 49 6 40 31 31Z'/%3E%3Crect x='24' y='24' width='32' height='32' transform='rotate(45 40 40)'/%3E%3Ccircle cx='40' cy='40' r='6'/%3E%3Cpath d='M0 0 12 12M80 0 68 12M0 80 12 68M80 80 68 68'/%3E%3C/g%3E%3C/svg%3E");
}

/* ------------------------------------------------ en-tête : heure et date */
.top { position: relative; text-align: center; min-height: 1px; }
.clock {
  direction: ltr;
  font-size: clamp(34px, 8.5vmin, 96px);
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}
.date { margin-top: 0.6vh; font-size: clamp(16px, 3.2vmin, 34px); color: var(--muted); }
.date .hijri { color: var(--gold); }

/* ------------------------------------------------ centre */
.center {
  position: relative;
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3vh;
}
.title { margin: 0; font-size: clamp(28px, 6vmin, 64px); color: var(--gold); font-weight: 800; }

.talk {
  position: relative;
  width: var(--size);
  height: var(--size);
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  cursor: pointer;
  outline: none;
  flex: none;
}
.disc {
  position: absolute;
  inset: 9%;
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2%;
  background: radial-gradient(circle at 34% 28%, var(--green1), var(--green2) 46%, var(--green3));
  box-shadow:
    0 2.2vmin 6vmin rgba(0, 0, 0, 0.5),
    inset 0 -1.2vmin 3vmin rgba(0, 0, 0, 0.28),
    inset 0 1vmin 2.4vmin rgba(255, 255, 255, 0.35);
  transition: transform 0.15s ease, filter 0.3s ease;
}
.talk:active .disc { transform: scale(0.95); }
.ico { display: none; width: 36%; height: 36%; fill: #fff; filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3)); }
.talk-label {
  font-size: calc(var(--size) * 0.105);
  font-weight: 800;
  line-height: 1.1;
  color: #fff;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

.rings i {
  position: absolute;
  inset: 9%;
  border-radius: 50%;
  border: max(4px, 0.9vmin) solid rgba(94, 224, 140, 0.55);
  opacity: 0;
}
.spin {
  position: absolute;
  inset: 3%;
  border-radius: 50%;
  border: max(5px, 1.1vmin) solid transparent;
  border-top-color: var(--gold);
  border-left-color: rgba(232, 200, 116, 0.4);
  opacity: 0;
}

.status {
  margin: 0;
  max-width: 92vw;
  min-height: 2.7em;
  font-size: clamp(22px, 5vmin, 54px);
  font-weight: 700;
  line-height: 1.35;
  text-align: center;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}
.caption {
  margin: 0;
  max-width: 88vw;
  max-height: 3.9em;
  overflow: hidden;
  font-size: clamp(18px, 3.6vmin, 40px);
  line-height: 1.3;
  color: var(--muted);
  text-align: center;
}

.end {
  display: inline-flex;
  align-items: center;
  gap: 1.4vmin;
  padding: 1.6vmin 4.5vmin;
  border: 0;
  border-radius: 999px;
  background: var(--red);
  color: #fff;
  font: inherit;
  font-size: clamp(20px, 4.4vmin, 46px);
  font-weight: 800;
  box-shadow: 0 1.2vmin 3vmin rgba(0, 0, 0, 0.4);
  cursor: pointer;
}
.end svg { width: 1.1em; height: 1.1em; fill: #fff; }
.end:active { transform: scale(0.96); }
@media (orientation: landscape) {
  .end { position: absolute; left: 4vw; bottom: max(4vh, env(safe-area-inset-bottom)); }
}
@media (orientation: portrait) {
  .end { margin-top: 2vh; }
}

/* ------------------------------------------------ états du bouton */
[data-state="idle"] .ico-mic,
[data-state="connecting"] .ico-mic,
[data-state="reconnecting"] .ico-mic,
[data-state="listening"] .ico-mic,
[data-state="waiting"] .ico-mic { display: block; }
[data-state="speaking"] .ico-wave { display: block; }
[data-state="thinking"] .ico-book { display: block; }

/* au repos : le bouton « respire » pour inviter à appuyer */
[data-state="idle"] .rings i { animation: breathe 3.2s ease-out infinite; }
[data-state="idle"] .rings i:nth-child(2) { animation-delay: 1.05s; }
[data-state="idle"] .rings i:nth-child(3) { animation-delay: 2.1s; }
@keyframes breathe {
  0% { transform: scale(1); opacity: 0.55; }
  100% { transform: scale(1.22); opacity: 0; }
}

/* connexion */
[data-state="connecting"] .spin,
[data-state="reconnecting"] .spin { opacity: 1; animation: spin 1.1s linear infinite; }
[data-state="connecting"] .disc,
[data-state="reconnecting"] .disc { filter: saturate(0.7) brightness(0.92); }
@keyframes spin { to { transform: rotate(360deg); } }

/* écoute : les anneaux suivent sa voix */
[data-state="listening"] .rings i,
[data-state="speaking"] .rings i {
  opacity: calc(0.25 + var(--lvl) * 0.75);
  transition: transform 0.09s linear, opacity 0.09s linear;
}
[data-state="listening"] .rings i:nth-child(1),
[data-state="speaking"] .rings i:nth-child(1) { transform: scale(calc(1.02 + var(--lvl) * 0.1)); }
[data-state="listening"] .rings i:nth-child(2),
[data-state="speaking"] .rings i:nth-child(2) { transform: scale(calc(1.05 + var(--lvl) * 0.2)); }
[data-state="listening"] .rings i:nth-child(3),
[data-state="speaking"] .rings i:nth-child(3) { transform: scale(calc(1.08 + var(--lvl) * 0.32)); }

/* le robot parle : bouton doré */
[data-state="speaking"] .disc {
  background: radial-gradient(circle at 34% 28%, #ffe7a3, var(--gold) 46%, var(--gold-d));
}
[data-state="speaking"] .rings i { border-color: rgba(232, 200, 116, 0.6); }

/* le robot consulte l'expert */
[data-state="thinking"] .disc {
  background: radial-gradient(circle at 34% 28%, #a8edf5, var(--teal1) 40%, var(--teal2));
}
[data-state="thinking"] .spin { opacity: 1; border-top-color: var(--teal1); animation: spin 2.2s linear infinite; }

/* mode « un appui par question » : en attente d'un appui */
[data-state="waiting"] .rings i { animation: breathe 3.2s ease-out infinite; }
[data-state="waiting"] .rings i:nth-child(2) { animation-delay: 1.6s; }

.talk.pulse .disc { animation: pulse 0.35s ease; }
@keyframes pulse {
  50% { transform: scale(1.05); }
}

/* ------------------------------------------------ veille (écran assombri) */
.dim {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4vh;
  background: #000;
  color: rgba(255, 250, 240, 0.55);
}
.dim-clock {
  direction: ltr;
  font-size: clamp(60px, 18vmin, 220px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.dim-hint { color: var(--green2); font-size: 6vmin; animation: breathe2 3s ease-in-out infinite; }
@keyframes breathe2 { 50% { opacity: 0.25; } }

/* ------------------------------------------------ diagnostic et configuration (en français) */
.hotspot { position: fixed; top: 0; left: 0; width: 70px; height: 70px; z-index: 30; }
.diag, .setup {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.8);
  font-family: system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: text;
  user-select: text;
}
.diag-box, .setup-box {
  width: min(680px, 100%);
  max-height: 92vh;
  overflow: auto;
  padding: 20px;
  border-radius: 14px;
  background: #10261e;
  border: 1px solid #2f5a49;
}
.diag h2, .setup h2 { margin: 0 0 12px; font-size: 20px; }
.diag pre {
  margin: 0 0 12px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.45;
  color: #d9efe4;
}
.diag-meter { height: 14px; border-radius: 7px; background: #0a1914; overflow: hidden; margin-bottom: 14px; }
.diag-meter span { display: block; height: 100%; width: 0; background: var(--green2); transition: width 0.1s linear; }
.diag-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.diag button, .setup button {
  padding: 12px 18px;
  border: 0;
  border-radius: 10px;
  background: var(--green2);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
}
.diag button#diagClose { background: #44564f; }
.setup p { line-height: 1.5; }
.setup input {
  width: 100%;
  margin: 8px 0 14px;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid #2f5a49;
  background: #0a1914;
  color: #fff;
  font-size: 18px;
  letter-spacing: 0.08em;
}

.is-dimmed .center * { animation-play-state: paused !important; }

[hidden] { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  [data-state="idle"] .rings i, [data-state="waiting"] .rings i { animation-duration: 6s; }
}
