:root {
  --bg: #0f172a;
  --bg2: #1e293b;
  --card: #1e293b;
  --line: #334155;
  --txt: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22c55e;
  --accent2: #38bdf8;
  --err: #f87171;
  --warn: #fb923c;
  --surplus: #f87171;
  --deficit: #22c55e;
  --radius: 16px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--txt);
}
.muted { color: var(--muted); font-size: .85rem; }
.err { color: var(--err); font-size: .85rem; min-height: 1.2em; }

.screen { display: none; height: 100%; flex-direction: column; }
.screen.active { display: flex; }

/* LOGIN */
#login { align-items: center; justify-content: center; padding-top: env(safe-area-inset-top); }
.login-box { width: min(360px, 88vw); text-align: center; }
.login-box .logo { font-size: 3rem; }
.login-box h1 { margin: .2em 0 0; }
.login-box input {
  width: 100%; margin-top: 1.2rem; padding: 14px 16px; font-size: 1rem;
  background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); color: var(--txt);
}
.login-box button {
  margin-top: 1rem; width: 100%; padding: 14px; font-size: 1rem; font-weight: 600;
  background: var(--accent); color: #06281a; border: none; border-radius: var(--radius);
}

/* APP layout */
#app { display: none; }
#app.active { display: flex; }
.tab { display: none; flex: 1; flex-direction: column; overflow: hidden; }
.tab.active { display: flex; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; padding-top: calc(14px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}
.topbar h2 { margin: 0; font-size: 1.15rem; }
.ghost { background: none; border: 1px solid var(--line); color: var(--muted); padding: 6px 12px; border-radius: 10px; font-size: .8rem; cursor: pointer; }

/* DASHBOARD */
.dash { flex: 1; overflow-y: auto; padding: 16px; }
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; margin-bottom: 10px;
}
.card.section-label {
  padding: 8px 16px; font-size: .7rem; text-transform: uppercase;
  letter-spacing: .08em; color: var(--muted); background: transparent;
  border-color: transparent; margin-bottom: 2px;
}
.big { font-size: 2.4rem; font-weight: 700; line-height: 1; }
.kpi-val { font-size: 1.5rem; font-weight: 600; margin-top: 4px; }
.kpi-val.warn { color: var(--warn); }
.row { display: flex; gap: 8px; }
.row .card { flex: 1; margin-bottom: 10px; }
.kpi-label { color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; }
.delta-up { color: var(--err); } .delta-down { color: var(--accent); }
.trend-ok { color: var(--accent); } .trend-bad { color: var(--err); }
.pill {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: .72rem; background: var(--bg2); border: 1px solid var(--line); margin-right: 4px;
}
.pill.trend-ok { border-color: var(--accent); color: var(--accent); }
.pill.trend-bad { border-color: var(--err); color: var(--err); }

/* Bilan énergétique net */
.balance { text-align: center; }
.balance-val { font-size: 1.4rem; font-weight: 700; margin-top: 4px; }
.balance.surplus .balance-val { color: var(--surplus); }
.balance.deficit .balance-val { color: var(--deficit); }

/* Bilan texte */
.bilan { white-space: pre-wrap; font-size: .88rem; line-height: 1.55; }
.bilan h1, .bilan h2, .bilan h3 { font-size: .95rem; margin: .7em 0 .2em; color: var(--accent2); }
.bilan strong { color: var(--txt); }
.bilan ul { margin: .3em 0; padding-left: 1.2em; }
canvas { width: 100%; height: 140px; margin-top: 12px; }

/* CHAT */
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; padding: 10px 14px; border-radius: 16px; font-size: .95rem; line-height: 1.48; }
.msg h1, .msg h2, .msg h3 { font-size: .95rem; margin: .5em 0 .2em; color: var(--accent2); }
.msg strong { font-weight: 700; }
.msg ul { margin: .3em 0; padding-left: 1.2em; }
.msg.user { align-self: flex-end; background: var(--accent2); color: #04222e; border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; background: var(--bg2); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.dev-log {
  align-self: flex-start; background: #081208; border: 1px solid var(--accent);
  font-family: "SF Mono", ui-monospace, monospace; font-size: .82rem;
  color: var(--accent); border-bottom-left-radius: 4px; max-width: 96%; white-space: pre-wrap;
}
.msg.typing { opacity: .7; font-style: italic; }
.chatbar { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); align-items: flex-end; }
.chatbar textarea {
  flex: 1; resize: none; max-height: 120px; padding: 12px 14px; font-size: 1rem;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 18px; color: var(--txt); font-family: inherit;
}
.chatbar button[type="submit"] {
  width: 48px; min-width: 48px; height: 48px; margin: 0; border-radius: 50%;
  padding: 0; font-size: 1.1rem; background: var(--accent); color: #06281a; border: none; cursor: pointer;
}
.dev-hint {
  text-align: center; font-size: .68rem; color: var(--muted); padding: 2px 0 6px;
  border-top: 1px solid var(--line);
}

/* NAV */
.bottomnav { display: flex; border-top: 1px solid var(--line); background: var(--bg); padding-bottom: env(safe-area-inset-bottom); }
.navbtn {
  flex: 1; background: none; border: none; color: var(--muted); padding: 10px 0 14px;
  font-size: 1.4rem; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
.navbtn span { font-size: .68rem; }
.navbtn.active { color: var(--accent); }
