:root {
  --bg: #f6f4f4;
  --surface: #ffffff;
  --text: #2b1a1a;
  --muted: #7d6b6b;
  --border: #e7dfdf;
  --primary: #b3261e;
  --primary-dark: #8c1d16;
  --danger: #b3261e;
  --danger-bg: #fdecea;
  --warn: #a15c00;
  --warn-bg: #fdf3e3;
  --chip-bg: #f3eeee;
  --chip-selected-bg: #b3261e;
  --chip-selected-text: #ffffff;

  --cat-normale: #2e7d32;
  --cat-elevee: #c9a300;
  --cat-stade1: #e06f00;
  --cat-stade2: #c62828;
  --cat-crise: #7b1fa2;

  --sys-color: #c62828;
  --dia-color: #1565c0;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171212;
    --surface: #221a1a;
    --text: #f0e7e7;
    --muted: #a99797;
    --border: #352828;
    --chip-bg: #2a2020;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

.hidden { display: none !important; }

.screen { width: 100%; min-height: 100vh; }

/* Login */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 32px 24px;
  width: 100%;
  max-width: 360px;
  text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}
.login-card h1 { margin: 0 0 4px; font-size: 1.4rem; }
.login-card input {
  width: 100%;
  padding: 12px 14px;
  margin: 16px 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}

.muted { color: var(--muted); font-size: 0.9rem; }
.error { color: var(--danger); font-size: 0.85rem; }

/* App shell */
.app-topbar {
  position: sticky;
  top: 0;
  z-index: 10;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px;
  background: var(--primary);
  color: #fff;
}
.app-header h1 {
  margin: 0;
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.link-btn {
  background: none;
  border: none;
  color: #fff;
  opacity: 0.85;
  cursor: pointer;
  font-size: 0.85rem;
  text-decoration: underline;
}

.link-btn-inline {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.85rem;
  text-decoration: underline;
  margin-top: 8px;
  display: block;
}

.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.tab-btn {
  flex: 1;
  padding: 12px 4px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.8rem;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
}
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: 48px;
  width: 100%;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card {
  background: var(--surface);
  border-radius: 14px;
  padding: 18px;
  margin-bottom: 18px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.card h2 { margin: 0 0 14px; font-size: 1.1rem; }

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}
.chip {
  border: 1px solid var(--border);
  background: var(--chip-bg);
  color: var(--text);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 0.85rem;
  cursor: pointer;
}
.chip.active {
  background: var(--chip-selected-bg);
  color: var(--chip-selected-text);
  border-color: var(--chip-selected-bg);
}

.row {
  display: flex;
  gap: 12px;
  margin-bottom: 14px;
}
.row label {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--muted);
}
.row input {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}

.block-label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 14px;
}
.block-label input {
  width: 100%;
  padding: 12px 14px;
  margin-top: 6px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1.2rem;
  font-weight: 600;
}

.bp-inputs { margin-bottom: 4px; }
.bp-inputs .block-label { flex: 1; margin-bottom: 0; }

.time-field { display: flex; gap: 6px; align-items: stretch; }
.time-field input { flex: 1; }
.now-btn {
  border: 1px solid var(--border);
  background: var(--chip-bg);
  border-radius: 10px;
  padding: 0 12px;
  cursor: pointer;
  font-size: 1rem;
}

button.primary {
  width: 100%;
  padding: 14px;
  margin-top: 16px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
button.primary:active { background: var(--primary-dark); }

.feedback {
  margin-top: 10px;
  font-size: 0.85rem;
  min-height: 1.2em;
}
.feedback.ok { color: var(--cat-normale); }
.feedback.error { color: var(--danger); }

/* Badge de catégorie de tension */
.cat-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}
.cat-normale { background: var(--cat-normale); }
.cat-elevee { background: var(--cat-elevee); color: #2b1a1a; }
.cat-stade1 { background: var(--cat-stade1); }
.cat-stade2 { background: var(--cat-stade2); }
.cat-crise { background: var(--cat-crise); }

#last-measure-card { text-align: center; }
#last-measure-card .big-reading {
  font-size: 2.2rem;
  font-weight: 700;
  margin: 4px 0;
}

/* Historique */
.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.history-item .reading {
  font-size: 1.1rem;
  font-weight: 700;
}
.history-item .meta {
  font-size: 0.78rem;
  color: var(--muted);
}
.history-actions { display: flex; gap: 6px; }
.icon-btn {
  border: none;
  background: var(--chip-bg);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  font-size: 0.95rem;
  cursor: pointer;
}
.icon-btn.danger { color: var(--danger); }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 32px 16px;
}

/* Graphique */
.chart-stats {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.chart-stats .stat {
  text-align: center;
}
.chart-stats .stat .value {
  font-size: 1.3rem;
  font-weight: 700;
}
.chart-stats .stat .label {
  font-size: 0.72rem;
  color: var(--muted);
}

#chart-container svg { width: 100%; height: auto; display: block; }

.legend {
  display: flex;
  gap: 18px;
  justify-content: center;
  margin-top: 10px;
  font-size: 0.8rem;
  color: var(--muted);
}
.legend .dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 5px;
}
.dot-sys { background: var(--sys-color); }
.dot-dia { background: var(--dia-color); }
