:root {
  --teal: #6f9ea5;
  --teal-d: #5b878d;
  --teal-l: #cfe2e3;
  --pink: #e79a98;
  --green: #86b6a6;
  --yellow: #d8c267;
  --purple: #b9a7c8;
  --ink: #2f3b3d;
  --muted: #8a9799;
  --bg: #eef4f7;
  --card: #ffffff;
  --line: #e6edf0;
  --radius: 16px;
  --shadow: 0 2px 10px rgba(60, 90, 100, .07);
  --ok: #6bbf8a;
  --bad: #e08585;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; margin: 0 0 .6rem; color: var(--teal-d); }
a { color: var(--teal-d); text-decoration: none; }
.muted { color: var(--muted); font-size: .9rem; }

/* ---------------- Topbar / nav ---------------- */
.topbar {
  display: flex; align-items: center; gap: 1.2rem;
  background: var(--card); padding: .7rem 1.2rem;
  box-shadow: var(--shadow); position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--ink); }
.brand span { color: var(--teal-d); }
.mainnav { display: flex; gap: .3rem; margin-left: auto; }
.mainnav a {
  padding: .45rem .9rem; border-radius: 10px; color: var(--muted); font-weight: 600;
}
.mainnav a:hover { background: var(--bg); }
.mainnav a.active { background: var(--teal); color: #fff; }
.logout { font-size: 1.3rem; color: var(--muted); padding: .2rem .5rem; }

.bottomnav { display: none; }

/* ---------------- Layout ---------------- */
.content { max-width: 1180px; margin: 0 auto; padding: 1.2rem; }
.page-head { display: flex; align-items: baseline; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head .spacer { margin-left: auto; }

.card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.1rem 1.2rem; margin-bottom: 1.1rem;
}
.card.center { text-align: center; }
.grid-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1.1rem; margin-bottom: 1.1rem; }

.big-num { font-size: 1.6rem; font-weight: 700; margin-top: .4rem; }
.btn-link { display: inline-block; margin-top: .6rem; font-weight: 600; }

/* ---------------- Buttons / forms ---------------- */
button, .btn { cursor: pointer; font: inherit; }
.btn-primary {
  background: var(--teal); color: #fff; border: none; padding: .6rem 1rem;
  border-radius: 10px; font-weight: 600; width: 100%;
}
.btn-primary:hover { background: var(--teal-d); }
.btn-sm { padding: .3rem .6rem; border-radius: 8px; border: 1px solid var(--line); background: #fff; }
.btn-sm:hover { background: var(--bg); }
.btn-ghost { background: transparent; border: 1px dashed var(--line); color: var(--muted); border-radius: 10px; padding: .5rem; width: 100%; }
.btn-ghost:hover { border-color: var(--teal); color: var(--teal-d); }
input, select, textarea {
  font: inherit; padding: .5rem .6rem; border: 1px solid var(--line);
  border-radius: 9px; background: #fff; color: var(--ink); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--teal-l); border-color: var(--teal); }
label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .7rem; }
label input, label select { margin-top: .2rem; }

/* ---------------- Login ---------------- */
.login-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; }
.login-card { width: 340px; text-align: center; }
.login-logo { font-size: 2.5rem; }
.login-card h1 { margin: .3rem 0; }
.login-card label { text-align: left; }
.alert { background: #fdeaea; color: #b5483f; padding: .55rem .7rem; border-radius: 9px; margin-bottom: .8rem; font-size: .9rem; }

/* ---------------- Charts ---------------- */
/* Контейнер фикс. высоты — обязателен для Chart.js c maintainAspectRatio:false,
   иначе холст бесконечно растёт вниз. */
.chart-box { position: relative; width: 100%; height: 220px; }
.chart-box.sm { height: 170px; }

/* ---------------- Donut ---------------- */
.donut { max-width: 160px; height: auto; margin: 0 auto; display: block; }

/* ---------------- Toolbar (period switch) ---------------- */
.toolbar { display: flex; align-items: center; gap: .6rem; }
.toolbar .title { font-weight: 700; font-size: 1.15rem; min-width: 9rem; text-align: center; }
.nav-btn { width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: #fff; font-size: 1.1rem; }
.nav-btn:hover { background: var(--bg); }

/* ---------------- Tables ---------------- */
table { border-collapse: collapse; width: 100%; }
th, td { padding: .45rem .55rem; text-align: left; }
thead th { color: var(--muted); font-size: .8rem; font-weight: 600; border-bottom: 1px solid var(--line); }
tbody tr { border-bottom: 1px solid var(--line); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* progress bar inline */
.bar { background: var(--bg); border-radius: 6px; height: 10px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--teal); }

/* checkbox cell */
.chk {
  width: 26px; height: 26px; border-radius: 7px; border: 2px solid var(--teal-l);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
  background: #fff; color: transparent; transition: .12s; user-select: none;
}
.chk.on { background: var(--teal); border-color: var(--teal); color: #fff; }
.chk:hover { border-color: var(--teal); }

/* ---------------- Habit grid ---------------- */
[x-cloak] { display: none !important; }
.grid-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -.3rem; }
.habit-grid { border-collapse: separate; border-spacing: 0; }
.habit-grid th, .habit-grid td { text-align: center; padding: .25rem; }
.sticky-col {
  position: sticky; left: 0; background: var(--card); z-index: 2;
  text-align: left !important; min-width: 168px; max-width: 168px;
  box-shadow: 1px 0 0 var(--line);
}
.habit-grid thead .sticky-col { z-index: 3; }
.habit-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hicon { margin-right: .4rem; }
.day-col { min-width: 34px; }
.day-col .dnum { font-weight: 700; font-size: .82rem; }
.day-col .dwd { font-size: .62rem; color: var(--muted); }
.day-col.weekend .dwd, .cell.weekend { color: var(--pink); }
.cell.weekend { background: #fbf3f3; }
.day-col.today { background: var(--teal); color: #fff; border-radius: 8px; }
.day-col.today .dwd { color: #e8f2f2; }
.cell.today { background: var(--teal-l); }
.habit-grid tbody tr { border: none; }
.cell .chk { width: 24px; height: 24px; }

/* progress per habit */
.progress-table td { vertical-align: middle; }
.hp { display: flex; align-items: center; gap: .6rem; }
.hp .bar { flex: 1; max-width: 240px; }
.hp-num { min-width: 70px; color: var(--muted); font-variant-numeric: tabular-nums; }
.hp-pct { min-width: 44px; text-align: right; font-weight: 700; }
.total-row td { border-top: 2px solid var(--line); padding-top: .6rem; }

.goal-card textarea { resize: vertical; }
.saved-hint { color: var(--ok); font-size: .85rem; }

/* habit management */
.habit-edit { display: flex; gap: .5rem; align-items: center; margin-bottom: .55rem; flex-wrap: wrap; }
.habit-edit .ic { width: 56px; text-align: center; }
.habit-edit .nm { flex: 1; min-width: 140px; }
.habit-edit .tg { width: 80px; }
.habit-edit.add { border-top: 1px dashed var(--line); padding-top: .7rem; margin-top: .3rem; }
.btn-sm.primary { background: var(--teal); color: #fff; border-color: var(--teal); }
.btn-sm.danger { color: var(--bad); }

/* ---------------- Finance ---------------- */
.summary .stat { text-align: center; padding: .9rem; }
.stat .lbl { color: var(--muted); font-size: .8rem; }
.stat .val { font-size: 1.25rem; font-weight: 700; margin-top: .25rem; font-variant-numeric: tabular-nums; }
.stat .val.pos { color: var(--ok); }
.stat .val.neg { color: var(--bad); }
.stat .bar { margin-top: .5rem; }

.sections-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.1rem; margin-bottom: 1.1rem; }
.section-card h3 { margin-bottom: .5rem; }
.fin-table th, .fin-table td { padding: .35rem .4rem; font-size: .88rem; }
.fin-table .plan-inp { width: 96px; padding: .3rem .4rem; text-align: right; font-size: .85rem; }
.fin-table .ph { margin-left: .2rem; }
.fin-table .note { color: var(--muted); font-size: .82rem; }
.fin-table .num.pos { color: var(--ok); }
.fin-table .num.neg { color: var(--ink); }
.cat-add { margin-top: .5rem; }
.cat-add input { font-size: .85rem; }

.op-form { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.op-form input[type=date] { width: auto; }
.op-form select { flex: 1; min-width: 180px; }
.op-form input[name=amount] { width: 120px; }
.op-form input[name=note] { flex: 1; min-width: 140px; }

/* ---------------- Planner ---------------- */
.habit-week table { font-size: .85rem; }
.habit-week .hname { white-space: nowrap; }
.habit-week .num2 { text-align: center; width: 30px; }
.chk.sm { width: 20px; height: 20px; border-width: 2px; font-size: .7rem; }

.days-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: .7rem; }
.day-card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: .6rem; display: flex; flex-direction: column; }
.day-card.is-today { outline: 2px solid var(--teal); }
.day-head { border-radius: 10px; padding: .45rem .6rem; text-align: center; margin-bottom: .5rem; }
.day-head b { display: block; text-transform: capitalize; font-size: .92rem; }
.day-head span { font-size: .72rem; color: #6c6259; }
.day-donut { text-align: center; }
.day-donut .donut { max-width: 96px; }
.day-card h4 { margin: .5rem 0 .3rem; font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; }

.task-list, .note-list { list-style: none; margin: 0; padding: 0; }
.note-list { list-style: decimal; padding-left: 1.2rem; }
.task-item { display: flex; align-items: center; gap: .4rem; padding: .15rem 0; }
.task-item .chk { width: 20px; height: 20px; flex: none; }
.task-item .ttl { flex: 1; font-size: .88rem; word-break: break-word; }
.task-item.done .ttl { text-decoration: line-through; color: var(--muted); }
.del { border: none; background: none; color: #cdb8b8; cursor: pointer; font-size: .8rem; padding: 0 .2rem; }
.del:hover { color: var(--bad); }
.note-list li { font-size: .85rem; padding: .1rem 0; }
.note-list li span { word-break: break-word; }
.inline-add input { font-size: .85rem; padding: .35rem .5rem; margin-top: .2rem; }
.day-foot { display: flex; justify-content: space-between; font-size: .78rem; margin: .5rem 0; padding: .35rem .5rem; background: var(--bg); border-radius: 8px; }
.day-foot .ok { color: var(--ok); }
.day-foot .no { color: var(--bad); }

@media (max-width: 1000px) { .days-grid { grid-template-columns: repeat(2, 1fr); } }

@media (max-width: 720px) {
  .days-grid { grid-template-columns: 1fr; }
  .topbar .mainnav { display: none; }
  .topbar { gap: .6rem; }
  .content { padding: .9rem; padding-bottom: 5rem; }
  .bottomnav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
    background: var(--card); box-shadow: 0 -2px 10px rgba(60,90,100,.08);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .bottomnav a {
    flex: 1; text-align: center; padding: .5rem 0; font-size: .7rem; color: var(--muted);
    display: flex; flex-direction: column; align-items: center; gap: .1rem;
  }
  .bottomnav a span { font-size: 1.25rem; }
  .bottomnav a.active { color: var(--teal-d); }
}
