:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --ink: #1a2233;
  --muted: #6b7589;
  --line: #e4e8f0;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --success: #16a34a;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 6px 20px rgba(16, 24, 40, 0.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  line-height: 1.45;
}

/* ---- Top nav ---- */
#topnav {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.75rem 1.1rem;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}
#topnav .brand { font-weight: 700; font-size: 1.05rem; letter-spacing: -0.01em; }
#topnav nav { display: flex; gap: 0.35rem; flex: 1; flex-wrap: wrap; }
#topnav nav a {
  text-decoration: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0.4rem 0.7rem;
  border-radius: 9px;
}
#topnav nav a:hover { color: var(--ink); background: var(--bg); }
#topnav nav a.active { color: var(--accent); background: rgba(37, 99, 235, 0.08); }
#topnav #logout {
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  font-weight: 600;
  padding: 0.4rem 0.8rem;
  border-radius: 9px;
  min-height: auto;
  width: auto;
}

/* ---- Layout ---- */
#app {
  max-width: 760px;
  margin: 0 auto;
  padding: 1.1rem 1.1rem 4rem;
}

h1 { font-size: 1.5rem; letter-spacing: -0.02em; margin: 0.4rem 0 0.6rem; }
h2 { font-size: 1.1rem; margin: 0 0 0.7rem; letter-spacing: -0.01em; }
h3 { font-size: 0.98rem; margin: 1rem 0 0.5rem; }
small { color: var(--muted); font-weight: 500; }

/* ---- Cards ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem;
  margin-block: 1rem;
}

/* ---- Stat grid ---- */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 0.7rem;
  margin-bottom: 0.5rem;
}
.stat { padding: 0.85rem 0.9rem; margin: 0; text-align: left; }
.stat span { display: block; font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; color: var(--ink); }
.stat label { font-size: 0.74rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }

/* ---- Charts ---- */
.chartbox { position: relative; height: 260px; }

/* ---- Forms ---- */
form { display: flex; flex-direction: column; gap: 0.75rem; }
label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}
input, select {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.6rem 0.7rem;
  min-height: 44px;
  width: 100%;
}
input[type="range"] { padding: 0; min-height: auto; accent-color: var(--accent); }
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15); }
output { font-weight: 700; color: var(--ink); }

button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 10px;
  padding: 0.65rem 1rem;
  min-height: 44px;
  width: 100%;
  transition: filter 0.15s ease;
}
button:hover { filter: brightness(1.05); }
button:active { filter: brightness(0.95); }

details.card summary { font-weight: 700; cursor: pointer; }
details.card[open] summary { margin-bottom: 0.5rem; }

/* ---- Auth ---- */
.card.auth { max-width: 360px; margin: 4rem auto; text-align: center; }
.card.auth h1 { margin-top: 0; }

/* ---- Tabs ---- */
.tabs { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.5rem 0; }
.tabs button {
  width: auto;
  min-height: auto;
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--line);
  padding: 0.4rem 0.85rem;
  border-radius: 999px;
  font-size: 0.85rem;
}
.tabs button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ---- Table ---- */
table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
th { text-align: left; color: var(--muted); font-size: 0.74rem; text-transform: uppercase; letter-spacing: 0.04em; padding: 0.5rem 0.5rem; border-bottom: 1px solid var(--line); }
td { padding: 0.55rem 0.5rem; border-bottom: 1px solid var(--line); }
tbody tr:nth-child(even) { background: #fafbfd; }
.del { width: auto; min-height: auto; background: none; color: var(--danger); padding: 0.2rem 0.5rem; font-size: 0.9rem; }
.del:hover { background: #fdeaea; filter: none; }

/* ---- Status text ---- */
.error { color: var(--danger); font-weight: 600; font-size: 0.9rem; }
.ok { color: var(--success); font-weight: 600; font-size: 0.9rem; }

/* ---- Coaching ---- */
.coach {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  border-left: 4px solid var(--accent);
}
.coach strong { font-size: 1rem; letter-spacing: -0.01em; }
.coach span { color: var(--ink); font-size: 0.92rem; }
.coach-celebrate { border-left-color: var(--success); background: #f0fdf4; }
.coach-positive { border-left-color: var(--success); }
.coach-info { border-left-color: var(--accent); }
.coach-nudge { border-left-color: #f59e0b; background: #fffbeb; }
.coach-alert { border-left-color: var(--danger); background: #fef2f2; }

.nudge {
  margin: 0.7rem 0 0;
  padding: 0.5rem 0.7rem;
  border-radius: 8px;
  font-size: 0.84rem;
  background: var(--bg);
  color: var(--muted);
}
.nudge-celebrate { background: #f0fdf4; color: #166534; }
.nudge-info { background: #eff6ff; color: #1e40af; }
.nudge-nudge { background: #fffbeb; color: #92400e; }
.nudge-alert { background: #fef2f2; color: #991b1b; }

/* ---- Overwrite warning + edit/actions ---- */
.warn {
  margin: 0;
  padding: 0.5rem 0.7rem;
  border-radius: 8px;
  font-size: 0.85rem;
  background: #fffbeb;
  color: #92400e;
  border: 1px solid #fde68a;
}
.row-actions { display: flex; gap: 0.5rem; align-items: center; }
.edit-form .row-actions button { flex: 1; }
button.icon {
  width: auto;
  min-height: auto;
  background: none;
  color: var(--muted);
  padding: 0.25rem 0.45rem;
  font-size: 0.95rem;
}
button.icon:hover { background: var(--bg); color: var(--accent); filter: none; }
button.secondary { background: none; color: var(--ink); border: 1px solid var(--line); }
button.secondary:hover { background: var(--bg); filter: none; }

/* ---- Mobile: bottom tab bar nav ---- */
@media (max-width: 640px) {
  #topnav {
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.7rem 1rem;
    /* Remove backdrop-filter here: a filtered ancestor would become the
       containing block for the fixed bottom nav and break `bottom: 0`. */
    backdrop-filter: none;
    background: var(--surface);
  }
  /* Move the nav links to a fixed, thumb-reachable bottom bar */
  #topnav nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    flex: none;
    gap: 0;
    justify-content: space-around;
    padding: 0.35rem 0.25rem calc(0.35rem + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: saturate(180%) blur(12px);
    border-top: 1px solid var(--line);
    box-shadow: 0 -1px 12px rgba(16, 24, 40, 0.05);
    z-index: 20;
  }
  #topnav nav a {
    flex: 1;
    text-align: center;
    font-size: 0.72rem;
    padding: 0.45rem 0.2rem;
    border-radius: 8px;
  }
  #topnav nav a.active { background: rgba(37, 99, 235, 0.1); }
  /* Leave room so content isn't hidden behind the bottom bar */
  #app { padding-bottom: 5.5rem; }
  .card.auth { margin-top: 2rem; }
}
