/* ══════════════════════════════════════
   TOKENS — DARK (default)
══════════════════════════════════════ */
:root {
  --bg:       #0f1117;
  --surface:  #1a1d27;
  --surface2: #222636;
  --border:   #2e3348;
  --accent:   #6c8eff;
  --accent2:  #a78bfa;
  --success:  #34d399;
  --danger:   #f87171;
  --text:     #e8eaf0;
  --muted:    #7b82a0;
  --radius:   16px;
  --topbar-bg: rgba(15,17,23,0.88);
  --cs:       dark;   /* color-scheme for inputs */
  --font: -apple-system,"SF Pro Display","Segoe UI",Roboto,sans-serif;
}

/* LIGHT THEME */
html.light {
  --bg:       #f0f2f7;
  --surface:  #ffffff;
  --surface2: #e8eaf0;
  --border:   #d0d4e0;
  --accent:   #4a6ef5;
  --accent2:  #7c5cd6;
  --success:  #16a34a;
  --danger:   #dc2626;
  --text:     #1a1d2e;
  --muted:    #6b7090;
  --topbar-bg: rgba(240,242,247,0.9);
  --cs:       light;
}

/* ══ RESET ══ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;   /* no horizontal scroll ever */
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  width: 100%;
  padding-bottom: env(safe-area-inset-bottom, 24px);
  transition: background 0.2s, color 0.2s;
}

/* ══ TOP BAR ══ */
#top-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--topbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  width: 100%;
}

#top-bar h1 {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
  white-space: nowrap;
}

.top-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  position: relative;
  flex-shrink: 0;
}

.icon-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 1.1rem;
  flex-shrink: 0;
  transition: color 0.15s, border-color 0.15s;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent); }

/* ══ DROPDOWN ══ */
.dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  width: 240px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
  z-index: 200;
}
.dropdown-menu.open { display: block; }

/* Theme toggle row */
.theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 8px 6px;
  gap: 8px;
}
.theme-row span {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.theme-toggle {
  display: flex;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.theme-toggle button {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 5px 10px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.theme-toggle button.active {
  background: var(--accent);
  color: #fff;
}

.menu-divider { border: none; border-top: 1px solid var(--border); margin: 6px 0; }

.dropdown-menu .section-label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 4px 8px 4px;
}

.dropdown-menu input[type=password] {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.95rem;
  margin-bottom: 8px;
  outline: none;
  font-family: var(--font);
  color-scheme: var(--cs);
}
.dropdown-menu input[type=password]:focus { border-color: var(--accent); }

.dropdown-menu .save-btn {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 4px;
}

.logout-menu-btn {
  width: 100%;
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 10px;
  padding: 10px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
}

#pw-error {
  color: var(--danger);
  font-size: 0.8rem;
  padding: 0 8px 6px;
}

/* ══ LAYOUT ══ */
main {
  padding: 16px;
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
}

/* ══ STATS GRID ══ */
.stats-strip {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px;
  min-width: 0;
}
.stat-card .label {
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}
.stat-card .value {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
  word-break: break-all;
}
.stat-card .value small {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 400;
}
.delta { font-size: 0.76rem; margin-top: 3px; display: block; }
.delta.down { color: var(--success); }
.delta.up   { color: var(--danger); }
.delta.flat { color: var(--muted); }

/* ══ CHART CARDS ══ */
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 16px;
  width: 100%;
  min-width: 0;
  overflow: hidden;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.chart-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.range-btns { display: flex; gap: 5px; flex-wrap: nowrap; }
.range-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 7px;
  padding: 4px 9px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.range-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

#main-chart-wrap { height: 170px; width: 100%; position: relative; }
#main-chart-wrap canvas { width: 100% !important; }

.minichart-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  margin-bottom: 10px;
  padding-bottom: 2px;
}
.minichart-tabs::-webkit-scrollbar { display: none; }

.tab-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 20px;
  padding: 5px 11px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
  flex-shrink: 0;
}
.tab-btn.active { background: var(--accent2); border-color: var(--accent2); color: #fff; }

#mini-chart-wrap { height: 120px; width: 100%; position: relative; }
#mini-chart-wrap canvas { width: 100% !important; }

/* ══ ENTRY FORM ══ */
.form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  width: 100%;
  min-width: 0;
}

.form-card h2 {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
}

.date-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.field-label {
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 5px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.field-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 0.95rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.15s;
  color-scheme: var(--cs);
}
.field-input:focus { border-color: var(--accent); }

.today-btn {
  font-size: 0.68rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--accent);
  border-radius: 5px;
  padding: 2px 7px;
  cursor: pointer;
  line-height: 1.4;
}

/* 2-col grid for metrics */
.metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.metric-field { min-width: 0; }

.stepper {
  display: flex;
  align-items: center;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  width: 100%;
}

.stepper-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.15rem;
  width: 36px;
  height: 42px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color 0.1s, background 0.1s;
}
.stepper-btn:active { background: var(--border); color: var(--text); }

.stepper input[type=number] {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: none;
  color: var(--text);
  text-align: center;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: var(--font);
  height: 42px;
  outline: none;
  -moz-appearance: textfield;
  width: 100%;
}
.stepper input[type=number]::-webkit-inner-spin-button,
.stepper input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; }

.save-entry-btn {
  width: 100%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
}
.save-entry-btn:active { opacity: 0.85; transform: scale(0.98); }

/* ══ HISTORY ══ */
.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.history-header h2 {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.copy-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 5px 11px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: color 0.15s;
}
.copy-btn:hover { color: var(--text); }

.entry-list { display: flex; flex-direction: column; gap: 8px; }

.entry-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  width: 100%;
}

.entry-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  cursor: pointer;
  gap: 8px;
}

.entry-date-weight { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.entry-date { font-size: 0.78rem; color: var(--muted); }
.entry-weight { font-size: 1.2rem; font-weight: 700; letter-spacing: -0.03em; }

.entry-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.entry-chips { display: flex; gap: 5px; }
.chip {
  background: var(--surface2);
  border-radius: 6px;
  padding: 3px 7px;
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
}

.chevron {
  color: var(--muted);
  font-size: 0.85rem;
  transition: transform 0.2s;
  flex-shrink: 0;
}
.entry-card.open .chevron { transform: rotate(180deg); }

.entry-expand { display: none; border-top: 1px solid var(--border); }
.entry-expand.open { display: block; }

/* Edit form — 2-col */
.edit-form {
  padding: 12px 14px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.edit-form .field-label { font-size: 0.7rem; }

.edit-form input[type=number],
.edit-form input[type=date],
.edit-form input[type=time] {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 0.9rem;
  font-family: var(--font);
  outline: none;
  color-scheme: var(--cs);
}
.edit-form input:focus { border-color: var(--accent); }

.edit-form-actions {
  grid-column: span 2;
  display: flex;
  gap: 8px;
  margin-top: 2px;
}
.edit-save-btn {
  flex: 1;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
}
.edit-delete-btn {
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 0.92rem;
  cursor: pointer;
}

/* ══ AUTH PAGES ══ */
.auth-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px;
  padding-top: max(10vh, 48px);
  background: var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 24px;
  text-align: center;
}
.auth-card .logo { font-size: 2.4rem; margin-bottom: 14px; line-height: 0; }
.auth-card .logo svg { display: inline-block; vertical-align: middle; filter: drop-shadow(0 4px 12px rgba(0,0,0,0.12)); }
.auth-card h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  margin-bottom: 24px;
}
.auth-card input[type=password] {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 1rem;
  font-family: var(--font);
  outline: none;
  margin-bottom: 14px;
  color-scheme: var(--cs);
}
.auth-card input[type=password]:focus { border-color: var(--accent); }
.auth-card button[type=submit] {
  width: 100%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}
.auth-hint { font-size: 0.82rem; color: var(--muted); margin-top: 14px; line-height: 1.5; }
