:root {
  --bg: #f7f3f1;
  --surface: #ffffff;
  --surface-2: #f1ebe8;
  --text: #22191b;
  --muted: #7d6f72;
  --line: #e7dfdc;
  --accent: #c2335a;
  --accent-ink: #ffffff;
  --accent-soft: #f8e3e8;
  --danger: #b42318;
  --shadow: 0 1px 2px rgba(34, 25, 27, .06), 0 4px 16px rgba(34, 25, 27, .05);
  --radius: 16px;
  --tabbar-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181c;
    --surface: #212227;
    --surface-2: #2a2b31;
    --text: #f1ecec;
    --muted: #a19a9c;
    --line: #34353c;
    --accent: #f06b8f;
    --accent-ink: #1a0f12;
    --accent-soft: #3a2228;
    --danger: #ff8a80;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 52px; padding: 0 20px;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--surface); color: var(--text);
  font-weight: 600; cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .08s ease, background .15s ease, opacity .15s ease;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-sm { min-height: 36px; padding: 0 12px; border-radius: 10px; font-size: 14px; box-shadow: none; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border: 0; border-radius: 12px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }

input[type="date"], input:not([type]), input[type="password"] {
  width: 100%; min-height: 48px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); outline: none;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Вход ---------- */
.login {
  min-height: 100%; display: grid; place-items: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
}
.login-card {
  width: 100%; max-width: 360px;
  display: grid; gap: 14px;
  padding: 28px 22px; border-radius: 22px;
  background: var(--surface); box-shadow: var(--shadow);
}
.login-logo { justify-self: center; border-radius: 18px; }
.login-card h1 { margin: 0 0 6px; text-align: center; font-size: 26px; }
.login-card label { display: grid; gap: 6px; }
.login-card label span { font-size: 14px; color: var(--muted); }
.error { margin: 0; min-height: 1.2em; color: var(--danger); font-size: 14px; }

/* ---------- Каркас ---------- */
#app { min-height: 100%; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.topbar h1 { margin: 0; font-size: 22px; }

main {
  max-width: 640px; margin: 0 auto;
  padding: 8px 16px calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom));
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar button {
  display: grid; place-items: center; align-content: center; gap: 2px;
  border: 0; background: none; color: var(--muted);
  font-size: 12px; font-weight: 600; cursor: pointer;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--accent); }

/* ---------- День ---------- */
.counter {
  margin: 12px 0 24px; padding: 32px 16px 28px;
  text-align: center; border-radius: 24px;
  background: var(--surface); box-shadow: var(--shadow);
}
.counter-label { color: var(--muted); font-weight: 600; letter-spacing: .02em; }
.counter-value {
  margin: 4px 0 2px;
  font-size: clamp(88px, 30vw, 132px); line-height: 1; font-weight: 800;
  color: var(--accent); font-variant-numeric: tabular-nums;
}
.counter-sub { color: var(--muted); font-size: 15px; min-height: 1.4em; }

.actions { display: grid; gap: 12px; }
.earlier { display: grid; grid-template-columns: 1fr auto; gap: 10px; }

/* ---------- Список ---------- */
.table {
  width: 100%; border-collapse: separate; border-spacing: 0;
  background: var(--surface); border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow); margin-top: 8px;
}
.table th, .table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--line); }
.table tr:last-child td { border-bottom: 0; }
.table th { font-size: 13px; font-weight: 600; color: var(--muted); background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table td.num { font-weight: 700; }
.table .act { width: 1%; white-space: nowrap; text-align: right; padding: 4px 8px; }
.table td.date { font-variant-numeric: tabular-nums; }
.table tr.editing td { background: var(--accent-soft); }
.table tr.editing input { min-height: 40px; }
.edit-row { display: flex; gap: 6px; align-items: center; }
.edit-row input { flex: 1; min-width: 0; }
.muted { color: var(--muted); font-weight: 400; }

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

/* ---------- График ---------- */
.chart {
  margin-top: 8px; padding: 16px 0 12px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
}
.chart-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 0 16px 12px; }
.chart-stats div { display: grid; }
.chart-stats b { font-size: 22px; font-variant-numeric: tabular-nums; }
.chart-stats span { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chart-wrap { display: flex; }
.chart-axis { flex: none; width: 40px; height: 260px; }
.chart-scroll { flex: 1; overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch; scrollbar-width: thin; }
.chart-plot { display: block; height: 260px; }
.chart-caption { padding: 8px 16px 0; color: var(--muted); font-size: 14px; min-height: 1.4em; }

.chart text { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .avg { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 4 4; opacity: .8; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--accent); opacity: .08; }
.chart .pt { fill: var(--surface); stroke: var(--accent); stroke-width: 2.5; }
.chart .pt.sel { fill: var(--accent); }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .val { fill: var(--text); font-weight: 700; font-size: 12px; }
.chart .xl { fill: var(--muted); }
.chart .yr { fill: var(--muted); font-size: 10px; opacity: .8; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  width: max-content; max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: 15px; font-weight: 500;
  transition: opacity .2s ease, transform .2s ease; z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); color: #fff; }
