:root {
  --bg: #fff7ec;
  --card: #ffffff;
  --ink: #3a2e21;
  --muted: #8a7a67;
  --line: #efe3d2;
  --brand: #f4a63b;
  --green: #6bbf59;
  --green-d: #4e9c3f;
  --amber: #e8a13a;
  --red: #e0654a;
  --blue: #5b8def;
  --shadow: 0 10px 30px rgba(120, 90, 40, .14);
  --radius: 20px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* L'attribut hidden doit primer sur les display:flex des .modal/.game-hud */
[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  min-height: 100dvh;
  position: relative;
  overflow-x: hidden;
}

/* Ciel dégradé qui réagit à l'état de la porte */
.sky {
  position: fixed;
  inset: 0;
  z-index: -1;
  transition: background 1.2s ease;
  background: linear-gradient(180deg, #cfe8ff 0%, #ffe9c7 55%, var(--bg) 100%);
}
body[data-state="open"] .sky,
body[data-state="opening"] .sky {
  background: linear-gradient(180deg, #a9dbff 0%, #ffe6b0 55%, var(--bg) 100%);
}
body[data-state="closed"] .sky,
body[data-state="closing"] .sky {
  background: linear-gradient(180deg, #2b3a67 0%, #6b5a86 40%, #d9b892 80%, var(--bg) 100%);
}

/* ---------- topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(14px, env(safe-area-inset-top)) 18px 8px;
  max-width: 520px;
  margin: 0 auto;
}
.brand {
  display: flex; align-items: center; gap: 8px;
  background: none; border: 0; cursor: pointer;
  font-size: 1.4rem; font-weight: 800; color: var(--ink);
  padding: 6px; border-radius: 12px;
}
.brand-emoji { font-size: 1.7rem; display: inline-block; transition: transform .15s; }
.brand:active .brand-emoji { transform: rotate(-18deg) scale(1.15); }
.brand-txt { letter-spacing: .3px; }
.icon-btn {
  background: rgba(255,255,255,.6); border: 1px solid var(--line);
  width: 42px; height: 42px; border-radius: 50%; font-size: 1.1rem; cursor: pointer;
}

/* ---------- layout ---------- */
.wrap {
  max-width: 520px;
  margin: 0 auto;
  padding: 6px 16px calc(30px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 16px;
}
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

/* ---------- hero ---------- */
.hero { text-align: center; padding-top: 26px; }
.coop-scene { display: flex; justify-content: center; }
.coop { width: 210px; height: auto; }

.roof { fill: #c15b3c; }
.wall { fill: #f0d9b6; stroke: #d8bd93; stroke-width: 2; }
.frame { fill: #b98a55; }
.hole { fill: #241a12; }
.perch { fill: #a9793f; }
.celestial { fill: #ffd34e; transition: fill 1s, transform 1s; }

/* La porte coulisse vers le haut à l'ouverture */
.door {
  fill: #8a5a2b;
  transform-box: fill-box;
  transform-origin: top center;
  transition: transform .9s cubic-bezier(.5,.05,.2,1), opacity .3s;
}
body[data-state="open"] .celestial { fill: #ffd34e; }
body[data-state="closed"] .celestial { fill: #eef3ff; transform: translateX(-2px); }

body[data-state="open"] .door { transform: scaleY(0.04); }
body[data-state="opening"] .door { transform: scaleY(0.5); }
body[data-state="closing"] .door { transform: scaleY(0.75); }
body[data-state="closed"] .door,
body[data-state="unknown"] .door { transform: scaleY(1); }

/* ---------- status ---------- */
.status { display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 18px; }
.status-label { font-size: 1.5rem; font-weight: 800; }
.status-sub { color: var(--muted); margin: 4px 0 0; font-size: .92rem; }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); }
body[data-state="open"] .dot { background: var(--green); box-shadow: 0 0 0 4px rgba(107,191,89,.2); }
body[data-state="closed"] .dot { background: var(--blue); box-shadow: 0 0 0 4px rgba(91,141,239,.2); }
body[data-state="opening"] .dot,
body[data-state="closing"] .dot,
body[data-state="stopping"] .dot { background: var(--amber); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: #faf3e7; border: 1px solid var(--line);
  border-radius: 999px; padding: 6px 12px; font-size: .85rem; font-weight: 600; color: var(--ink);
}
.chip.demo { background: #fff2d6; border-color: #f0d79a; color: #9a6b12; }

/* ---------- actions ---------- */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.btn {
  border: 0; border-radius: 18px; padding: 18px 12px; font-size: 1.05rem; font-weight: 800;
  color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px;
  box-shadow: var(--shadow); transition: transform .08s, filter .15s, opacity .15s;
  min-height: 66px;
}
.btn .btn-ico { font-size: 1.25rem; }
.btn:active { transform: scale(.97); }
.btn.open { background: linear-gradient(160deg, #7cc96a, var(--green-d)); }
.btn.close { background: linear-gradient(160deg, #7a86b5, #47507e); }
.btn[disabled] { opacity: .5; cursor: default; }
.btn.busy { position: relative; color: transparent; }
.btn.busy::after {
  content: ""; position: absolute; width: 20px; height: 20px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.5); border-top-color: #fff; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.btn.small { padding: 12px 18px; font-size: .95rem; background: var(--brand); }
.btn.ghost { background: #eee6d8; color: var(--ink); box-shadow: none; padding: 12px 18px; }

/* ---------- stats ---------- */
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.card-head h2 { font-size: 1.05rem; margin: 0; }
.link { background: none; border: 0; font-size: 1.2rem; color: var(--muted); cursor: pointer; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.stat {
  background: #faf3e7; border-radius: 14px; padding: 12px 14px;
}
.stat .k { font-size: .78rem; color: var(--muted); font-weight: 600; }
.stat .v { font-size: 1.15rem; font-weight: 800; margin-top: 2px; }

/* timeline 48h : barres open (vert) / closed (bleu) */
.timeline-wrap { margin: 4px 0 12px; }
.timeline {
  display: flex; height: 26px; border-radius: 8px; overflow: hidden;
  background: #eee; border: 1px solid var(--line);
}
.timeline i { display: block; height: 100%; }
.timeline i.o { background: var(--green); }
.timeline i.c { background: #6f7cae; }
.timeline i.u { background: #dcd3c4; }
.timeline-axis { display: flex; justify-content: space-between; color: var(--muted); font-size: .72rem; margin-top: 4px; }

.events { list-style: none; margin: 6px 0 0; padding: 0; }
.events li { display: flex; justify-content: space-between; padding: 8px 2px; border-top: 1px solid var(--line); font-size: .9rem; }
.events li .e-state { font-weight: 700; }
.events li .o { color: var(--green-d); }
.events li .c { color: #47507e; }
.events .empty { color: var(--muted); justify-content: center; border: 0; }

.foot { text-align: center; color: var(--muted); font-size: .8rem; padding: 4px 0 10px; }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(40,28,12,.45); padding: 16px;
}
.modal-card {
  background: #fff; border-radius: 22px; padding: 22px; width: 100%; max-width: 460px;
  box-shadow: var(--shadow); animation: slideup .25s ease;
}
@keyframes slideup { from { transform: translateY(30px); opacity: 0; } }
.modal-card h2 { margin: 0 0 14px; }
.modal-card label { font-weight: 700; font-size: .9rem; }
.modal-card .hint { color: var(--muted); font-size: .82rem; margin: 4px 0 10px; }
.modal-card input {
  width: 100%; padding: 13px; border: 1px solid var(--line); border-radius: 12px; font-size: 1rem;
  background: #faf7f1;
}
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%); z-index: 60;
  background: #3a2e21; color: #fff; padding: 12px 18px; border-radius: 999px;
  font-weight: 600; font-size: .9rem; box-shadow: var(--shadow); max-width: 90vw;
}
.toast.err { background: var(--red); }
.toast.ok { background: var(--green-d); }

/* ---------- écran de connexion ---------- */
.login {
  position: fixed; inset: 0; z-index: 40;
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.login-card {
  background: var(--card); border-radius: 24px; box-shadow: var(--shadow);
  padding: 32px 26px; width: 100%; max-width: 380px; text-align: center;
  display: flex; flex-direction: column; gap: 10px;
}
.login-emoji { font-size: 3.2rem; }
.login-card h1 { margin: 0; font-size: 1.7rem; }
.login-card p { margin: 0 0 6px; color: var(--muted); font-size: .92rem; }
.login-card input {
  width: 100%; padding: 14px; border: 1px solid var(--line); border-radius: 14px;
  font-size: 1.05rem; background: #faf7f1; text-align: center;
}
.login-card .btn { width: 100%; }
.login-err { color: var(--red); font-weight: 600; margin-top: 4px !important; }

/* ---------- bandeau setup ---------- */
.setup-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: #fff2d6; border: 1px solid #f0d79a; box-shadow: none; padding: 14px 16px;
  font-weight: 600; color: #9a6b12;
}
.config-status { font-size: .88rem; font-weight: 700; margin: 8px 0 0; }
.config-status.ok { color: var(--green-d); }
.config-status.warn { color: var(--amber); }

@media (min-width: 480px) {
  .coop { width: 240px; }
}
