:root {
  --bg: #f4f1ec;
  --surface: #ffffff;
  --surface-2: #ece7df;
  --text: #1d1f23;
  --muted: #6b6f76;
  --line: #e2ddd4;
  --accent: #e0633a;
  --accent-text: #ffffff;
  --good: #2e8b57;
  --bad: #c2453b;
  --character-bg: radial-gradient(ellipse at 50% 85%, #f9dcc7 0%, #f4f1ec 70%);
  --radius: 14px;
  --tabbar-h: 58px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181c;
    --surface: #202328;
    --surface-2: #2a2e34;
    --text: #f1f1f1;
    --muted: #9aa0a8;
    --line: #2f333a;
    --accent: #f07a50;
    --good: #5cc48a;
    --bad: #ef6b60;
    --character-bg: radial-gradient(ellipse at 50% 85%, #3a2a22 0%, #16181c 70%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; letter-spacing: -0.01em; }

/* ---------- Layout ---------- */

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  padding: calc(env(safe-area-inset-top) + 10px) 16px 8px;
  text-align: center;
  background: var(--bg);
}
.topbar h1 { font-size: 1.35rem; font-weight: 700; }

main {
  flex: 1;
  min-height: 0;
  position: relative;
}

.view { height: 100%; }

.page {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 16px 24px;
  max-width: 560px;
  margin: 0 auto;
}
.page > h2 { font-size: 1.15rem; margin: 6px 4px 12px; }

/* ---------- Home ---------- */

.home { display: flex; flex-direction: column; }

.character {
  position: relative;
  flex: 0 0 auto;
  height: 33dvh;
  background: var(--character-bg);
  display: flex;
  justify-content: center;
  overflow: hidden;
}
.character img {
  height: 100%;
  width: 100%;
  object-fit: contain;
  object-position: center bottom;
  transition: opacity 0.25s ease;
}
.character img.fading { opacity: 0; }

.character-badge {
  position: absolute;
  right: 12px;
  bottom: 10px;
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.8rem;
  font-weight: 600;
  box-shadow: 0 1px 4px rgb(0 0 0 / 0.15);
}
.character-badge:empty { display: none; }

.list-scroll {
  position: relative; /* rows' offsetTop is measured against this */
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  border-top: 1px solid var(--line);
}

.readings {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  max-width: 560px;
}

.reading {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background 0.15s;
}
.reading:active { background: var(--surface-2); }
.reading.current { background: color-mix(in srgb, var(--accent) 8%, transparent); }

.reading-date { font-size: 0.85rem; color: var(--muted); }
.reading-main { display: flex; align-items: baseline; gap: 10px; }
.reading-weight { font-size: 1.2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.reading-weight small { font-size: 0.75rem; font-weight: 500; color: var(--muted); margin-left: 2px; }
.reading-delta { font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.reading-delta.down { color: var(--good); }
.reading-delta.up { color: var(--bad); }
.reading-bf {
  grid-row: 1 / span 2;
  grid-column: 2;
  align-self: center;
  font-size: 1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.reading-bf.none { color: var(--muted); font-weight: 400; font-size: 0.85rem; }
.reading-comment {
  grid-column: 1 / -1;
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

/* ---------- Forms ---------- */

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.04);
}
.form { display: flex; flex-direction: column; gap: 12px; }
.form h3 { font-size: 1rem; }

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

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.field small { font-weight: 400; }

input[type="text"], input[type="password"], input[type="date"], select, textarea {
  width: 100%;
  min-width: 0;
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  -webkit-appearance: none;
  appearance: none;
}
input[type="date"] { min-height: 44px; }
select {
  min-height: 44px;
  padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a8f96' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 18px;
}
textarea { resize: vertical; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-weight: 500;
}
.toggle input {
  -webkit-appearance: none;
  appearance: none;
  width: 51px;
  height: 31px;
  border-radius: 999px;
  background: var(--surface-2);
  position: relative;
  flex: none;
  transition: background 0.2s;
  margin: 0;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.3);
  transition: transform 0.2s;
}
.toggle input:checked { background: var(--good); }
.toggle input:checked::after { transform: translateX(20px); }

.btn {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  font: inherit;
  font-weight: 600;
  padding: 12px 16px;
  border-radius: 12px;
  border: 0;
  cursor: pointer;
  text-decoration: none;
  min-height: 44px;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.danger { background: transparent; color: var(--bad); }
.btn:disabled { opacity: 0.5; }

.form-error { color: var(--bad); margin: 0; font-size: 0.9rem; }
.form-error:empty, .form-status:empty { display: none; }
.form-status { color: var(--good); margin: 0; font-size: 0.9rem; }

.hint { margin: 0; font-size: 0.9rem; color: var(--muted); }
.hint a { color: var(--accent); }
code { font-size: 0.85em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

.import-preview { font-size: 0.85rem; }
.import-preview table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.import-preview th, .import-preview td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.import-preview td:last-child { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.import-preview p { margin: 0 0 6px; }

/* ---------- Tab bar ---------- */

.tabbar {
  display: flex;
  justify-content: space-around;
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1;
  height: var(--tabbar-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.7rem;
  font-weight: 600;
}
.tabbar svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.tabbar a.active { color: var(--accent); }

/* ---------- Login ---------- */

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.login-logo { width: 96px; height: 96px; border-radius: 22px; margin: 0 auto; }
.login-card .field { text-align: left; }
