/* Planner — one stylesheet. Colours come from Telegram's theme variables (--tg-theme-*) with
   fallbacks; outside Telegram the fallbacks follow prefers-color-scheme, inside Telegram the
   scheme-dark / scheme-light class set by tg.js picks them. Mobile first (360–430 px). */

:root {
  --fb-bg: #ffffff;
  --fb-secondary-bg: #f1f2f6;
  --fb-text: #14171a;
  --fb-hint: #6f7a86;
  --fb-link: #2a7fd4;
  --fb-button: #2a7fd4;
  --fb-button-text: #ffffff;
  --fb-destructive: #d63b3b;
  --fb-separator: #e3e6ea;
  --fb-ok: #2e9e5b;
  --fb-warn: #c98400;
  --fb-soft: rgba(42, 127, 212, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --fb-bg: #17212b;
    --fb-secondary-bg: #0e1621;
    --fb-text: #f2f4f6;
    --fb-hint: #8796a6;
    --fb-link: #6ab3f3;
    --fb-button: #5288c1;
    --fb-button-text: #ffffff;
    --fb-destructive: #ef5b5b;
    --fb-separator: #26313d;
    --fb-ok: #4cc283;
    --fb-warn: #e6a73a;
    --fb-soft: rgba(106, 179, 243, 0.16);
    color-scheme: dark;
  }
}

html.scheme-light {
  --fb-bg: #ffffff;
  --fb-secondary-bg: #f1f2f6;
  --fb-text: #14171a;
  --fb-hint: #6f7a86;
  --fb-link: #2a7fd4;
  --fb-button: #2a7fd4;
  --fb-destructive: #d63b3b;
  --fb-separator: #e3e6ea;
  --fb-ok: #2e9e5b;
  --fb-warn: #c98400;
  --fb-soft: rgba(42, 127, 212, 0.12);
  color-scheme: light;
}

html.scheme-dark {
  --fb-bg: #17212b;
  --fb-secondary-bg: #0e1621;
  --fb-text: #f2f4f6;
  --fb-hint: #8796a6;
  --fb-link: #6ab3f3;
  --fb-button: #5288c1;
  --fb-destructive: #ef5b5b;
  --fb-separator: #26313d;
  --fb-ok: #4cc283;
  --fb-warn: #e6a73a;
  --fb-soft: rgba(106, 179, 243, 0.16);
  color-scheme: dark;
}

:root {
  --page: var(--tg-theme-secondary-bg-color, var(--fb-secondary-bg));
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-bg)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --link: var(--tg-theme-link-color, var(--fb-link));
  --accent: var(--tg-theme-button-color, var(--fb-button));
  --accent-text: var(--tg-theme-button-text-color, var(--fb-button-text));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-destructive));
  --line: var(--tg-theme-section-separator-color, var(--fb-separator));
  --ok: var(--fb-ok);
  --warn: var(--fb-warn);
  --soft: var(--fb-soft);
  --radius: 14px;
  --tap: 44px;
  --nav-h: 60px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-wrap: anywhere;
}

body.sheet-open { overflow: hidden; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--link); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: 8px 12px calc(var(--nav-h) + var(--safe-bottom) + 24px);
}

.loading, .render-error { padding: 32px 4px; text-align: center; color: var(--hint); }
.render-error { color: var(--danger); }
.muted { color: var(--hint); margin: 4px 0 0; }
.page-title { font-size: 22px; margin: 12px 4px 14px; }

/* --- status banner --- */
.banner { margin: 4px 0 8px; padding: 8px 12px; border-radius: 10px; font-size: 13px; }
.banner[hidden] { display: none; }
.banner-offline { background: var(--card); color: var(--hint); border: 1px solid var(--line); }
.banner-error { background: var(--danger); color: #fff; font-weight: 600; }

/* --- cards --- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px;
  margin: 0 0 12px;
}
.card-head { display: flex; align-items: center; gap: 8px; min-height: 32px; margin-bottom: 8px; }
.card-title { font-size: 16px; font-weight: 650; margin: 0; flex: 1; }
.count { color: var(--hint); font-size: 14px; font-variant-numeric: tabular-nums; }
.add-btn {
  width: var(--tap); height: var(--tap); margin: -6px -6px -6px 0;
  border: 0; border-radius: 50%; background: var(--soft); color: var(--accent);
  font-size: 24px; line-height: 1; font-weight: 500;
}
.empty-hint {
  display: block; width: 100%; min-height: var(--tap); padding: 10px 12px;
  border: 1px dashed var(--line); border-radius: 10px; background: transparent;
  color: var(--hint); text-align: left;
}
.link-btn {
  display: inline-flex; align-items: center; min-height: var(--tap); padding: 0 4px;
  color: var(--link); text-decoration: none; font-weight: 600;
}

/* --- day header --- */
.day-head { margin: 4px 0 12px; }
.day-nav { display: flex; align-items: center; gap: 4px; }
.icon-btn {
  flex: none; width: var(--tap); height: var(--tap);
  border: 0; border-radius: 50%; background: transparent; color: var(--accent);
  font-size: 18px; line-height: 1;
}
.nav-arrow { font-size: 30px; background: var(--card); }
.day-title-wrap { position: relative; flex: 1; min-width: 0; min-height: var(--tap); display: flex; align-items: center; justify-content: center; border-radius: 12px; }
.day-title { margin: 0; font-size: 19px; font-weight: 700; text-align: center; }
.date-input {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; border: 0; margin: 0; padding: 0; cursor: pointer;
}
.date-input::-webkit-calendar-picker-indicator { position: absolute; inset: 0; width: auto; height: auto; opacity: 0; cursor: pointer; }
.day-sub { display: flex; justify-content: center; align-items: center; gap: 10px; min-height: 28px; color: var(--hint); font-size: 13px; }
.today-label { color: var(--accent); font-weight: 600; }
.today-btn {
  position: relative; min-height: 34px; padding: 4px 18px; margin: 5px 0; border: 0; border-radius: 17px;
  background: var(--accent); color: var(--accent-text); font-weight: 600; font-size: 14px;
}
.today-btn::after { content: ""; position: absolute; inset: -5px 0; } /* 44px touch area */

.carry-banner {
  display: block; width: 100%; min-height: var(--tap); margin: 0 0 12px; padding: 10px 14px;
  border: 0; border-radius: var(--radius); background: var(--soft); color: var(--accent);
  font-weight: 600; text-align: left;
}
.carry-banner::after { content: " →"; }

/* --- prayers --- */
.prayer-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.prayer {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  min-height: 82px; padding: 8px 2px; border: 1px solid var(--line); border-radius: 12px;
  background: transparent; min-width: 0;
}
.prayer-name { font-weight: 600; font-size: 13px; }
.prayer-dot {
  display: grid; place-items: center; width: 26px; height: 26px; border-radius: 50%;
  border: 2px solid var(--line); font-size: 15px; font-weight: 700; color: #fff;
}
.prayer-state { font-size: 10.5px; color: var(--hint); white-space: nowrap; }
.prayer.on { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent); }
.prayer.on .prayer-dot { background: var(--ok); border-color: var(--ok); }
.prayer.on .prayer-state { color: var(--ok); font-weight: 600; }

/* --- habit chips --- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: var(--tap); max-width: 100%;
  padding: 6px 14px; border: 1px solid var(--line); border-radius: 22px; background: transparent;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip-check { font-weight: 700; }

/* --- rows (tasks, meetings, notes) --- */
.rows { list-style: none; margin: 0; padding: 0; }
.row { display: flex; align-items: flex-start; gap: 8px; border-top: 1px solid var(--line); }
.row:first-child { border-top: 0; }
.row-main {
  flex: 1; min-width: 0; min-height: var(--tap); padding: 9px 0;
  display: flex; flex-direction: column; gap: 3px;
  border: 0; background: transparent; text-align: left;
}
.row-line { display: flex; gap: 8px; align-items: baseline; }
.row-time { flex: none; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--accent); }
.row-title { font-weight: 500; }
.row-meta { display: flex; flex-wrap: wrap; gap: 4px 8px; font-size: 12.5px; color: var(--hint); }
.row-note, .row-text { white-space: pre-wrap; }
.row-note { font-size: 13px; color: var(--hint); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.row-text { display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }

.check {
  flex: none; width: var(--tap); height: var(--tap); margin-left: -8px; padding: 0;
  display: grid; place-items: center; border: 0; background: transparent;
}
.check .box {
  display: grid; place-items: center; width: 24px; height: 24px; border-radius: 7px;
  border: 2px solid var(--hint); color: #fff; font-size: 15px; font-weight: 700; line-height: 1;
}
.task.done .check .box { background: var(--ok); border-color: var(--ok); }
.task.done .row-title { text-decoration: line-through; color: var(--hint); }
.task.done .row-time { color: var(--hint); }

.prio { padding: 0 6px; border-radius: 6px; font-weight: 600; }
.prio-1 { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.prio-2 { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.prio-3 { color: var(--hint); background: color-mix(in srgb, var(--hint) 14%, transparent); }
.meta-goal { color: var(--accent); }

.status-chip {
  flex: none; align-self: center; min-height: 36px; padding: 4px 10px; margin: 4px 0;
  border: 1px solid var(--line); border-radius: 18px; background: transparent; font-size: 12.5px; font-weight: 600;
  position: relative;
}
.status-chip::after { content: ""; position: absolute; inset: -4px 0; } /* 44px touch area */
.status-chip.status-done { color: var(--ok); border-color: var(--ok); }
.status-chip.status-cancelled { color: var(--danger); border-color: var(--danger); }
.status-chip.status-planned { color: var(--accent); }
.meeting.status-cancelled .row-title { text-decoration: line-through; color: var(--hint); }

.tag { padding: 0 6px; border-radius: 6px; font-weight: 600; background: var(--soft); color: var(--accent); }
.tag-muhim { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.tag-goya { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.tag-saboq { color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }
.tag-boshqa { color: var(--hint); background: color-mix(in srgb, var(--hint) 14%, transparent); }
.note.pinned { background: linear-gradient(90deg, var(--soft), transparent 60%); border-radius: 8px; }
.note .row-main { padding-left: 4px; }

/* --- bottom navigation --- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: center;
  padding-bottom: var(--safe-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tab {
  flex: 1; max-width: 140px; min-height: var(--nav-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--hint); text-decoration: none; font-size: 11.5px; font-weight: 600;
}
.tab.active { color: var(--accent); }
.tab-icon { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* --- bottom sheet --- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0, 0, 0, 0); transition: background 0.18s;
}
.sheet-backdrop.open { background: rgba(0, 0, 0, 0.42); }
.sheet {
  width: 100%; max-width: 560px; max-height: 90vh; max-height: 90dvh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--card); border-radius: 18px 18px 0 0;
  padding: 6px 16px calc(16px + var(--safe-bottom));
  transform: translateY(100%); transition: transform 0.2s ease-out;
}
.sheet-backdrop.open .sheet { transform: none; }
.sheet-head { display: flex; align-items: center; gap: 8px; position: sticky; top: -6px; background: var(--card); padding: 6px 0; z-index: 1; }
.sheet-title { flex: 1; margin: 0; font-size: 18px; }
.sheet-close { color: var(--hint); }

/* --- forms --- */
.form { display: flex; flex-direction: column; gap: 12px; padding-top: 4px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field-label { font-size: 13px; font-weight: 600; color: var(--hint); }
.field-hint { font-size: 12px; color: var(--hint); }
.field-error { font-size: 13px; color: var(--danger); font-weight: 600; }
.field-error:empty { display: none; }
.input {
  width: 100%; min-height: var(--tap); padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--page);
  font-size: 16px; /* no zoom on iOS focus */
}
textarea.input { resize: vertical; min-height: 88px; }
.input-time { max-width: 140px; font-variant-numeric: tabular-nums; }
.field.invalid .input { border-color: var(--danger); }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg-btn {
  flex: 1 1 auto; min-height: var(--tap); padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: transparent; font-weight: 600;
}
.seg-btn[aria-checked="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.switch-row { display: flex; align-items: center; gap: 10px; min-height: var(--tap); font-weight: 500; }
.switch { width: 22px; height: 22px; accent-color: var(--accent); margin: 0; }
.form-actions { display: flex; gap: 10px; margin-top: 4px; }
.btn {
  flex: 1; min-height: 48px; padding: 10px 16px; border: 0; border-radius: 12px;
  font-weight: 650; font-size: 16px;
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); flex: 0 1 auto; }

/* --- toasts --- */
#toasts {
  position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px); z-index: 30;
  display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none;
}
.toast {
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px;
  background: rgba(20, 23, 26, 0.88); color: #fff; font-size: 14px;
}

/* --- sync line --- */
.sync-line { margin: 0; font-weight: 600; }
.sync-error { color: var(--danger); }
.sync-offline, .sync-local { color: var(--hint); }
.sync-saved { color: var(--ok); }
.soon .card-title { color: var(--hint); }

/* --- tabs, period header (goals and reports) --- */
.tabs { margin: 0 0 8px; flex-wrap: nowrap; }
.tabs .seg-btn { flex: 1 1 0; padding: 8px 2px; min-width: 0; }
.tabs .seg-btn[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.period-head { margin: 0 0 10px; }
.period-title-wrap { position: relative; flex: 1; min-width: 0; min-height: var(--tap); display: flex; align-items: center; justify-content: center; }
.period-title { margin: 0; font-size: 16px; font-weight: 700; text-align: center; }

/* --- goals --- */
.goal-list { display: flex; flex-direction: column; }
.goal { padding: 4px 12px; }
.goal-main {
  display: flex; flex-direction: column; gap: 6px; width: 100%; min-height: var(--tap); padding: 10px 0;
  border: 0; background: transparent; text-align: left;
}
.goal-head { display: flex; align-items: flex-start; gap: 8px; }
.goal-title { flex: 1; font-weight: 650; font-size: 16px; }
.goal-parent { font-size: 12.5px; color: var(--hint); }
.goal-nums { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; color: var(--hint); font-variant-numeric: tabular-nums; }
.goal-hint { font-size: 12.5px; color: var(--warn); }
.goal-line { padding: 8px 0; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 6px; list-style: none; }
.goal-line:first-child { border-top: 0; }
.status-pill { flex: none; padding: 1px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; background: var(--soft); color: var(--accent); }
.st-done { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.st-dropped { background: color-mix(in srgb, var(--hint) 15%, transparent); color: var(--hint); }
.goal-dropped .goal-title { text-decoration: line-through; color: var(--hint); }
.badge-behind { color: #fff; background: var(--danger); padding: 0 8px; border-radius: 8px; font-weight: 600; }
.static-value { padding: 10px 0; font-weight: 500; }
.btn-wide { width: 100%; margin-top: 4px; }

.pbar { position: relative; height: 10px; border-radius: 5px; background: color-mix(in srgb, var(--hint) 22%, transparent); overflow: visible; }
.pbar-fill { height: 100%; border-radius: 5px; background: var(--ok); min-width: 0; }
.pbar.behind .pbar-fill { background: var(--warn); }
.pbar-elapsed { position: absolute; top: -3px; bottom: -3px; width: 2px; margin-left: -1px; background: var(--text); opacity: 0.55; border-radius: 1px; }

/* --- reports --- */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile { padding: 10px; border: 1px solid var(--line); border-radius: 12px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.tile-big { grid-column: 1 / -1; background: var(--soft); border-color: transparent; }
.tile-label { font-size: 12.5px; color: var(--hint); font-weight: 600; }
.tile-value { font-size: 26px; font-weight: 750; font-variant-numeric: tabular-nums; line-height: 1.15; }
.tile-big .tile-value { font-size: 36px; }
.tile-sub { font-size: 12px; color: var(--hint); }
.delta { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--ok); }
.delta.down { color: var(--danger); }
.delta.flat { color: var(--hint); font-weight: 500; }
.small { font-size: 12.5px; }
.rates { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.rate-row { display: grid; grid-template-columns: 76px 1fr 64px; align-items: center; gap: 8px; min-height: 28px; }
.rate-name { font-weight: 500; min-width: 0; }
.rate-bar { display: block; height: 8px; border-radius: 4px; background: color-mix(in srgb, var(--hint) 22%, transparent); overflow: hidden; }
.rate-bar.wide { margin: 4px 0; }
.rate-fill { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.rate-val { text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; color: var(--hint); }
.habit-row { padding: 6px 0; border-top: 1px solid var(--line); }
.habit-row:first-child { border-top: 0; }
.habit-line { display: flex; justify-content: space-between; gap: 8px; }
.kv { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.chart-scroll { overflow-x: auto; margin: 0 -4px; }
.chart { display: block; width: 100%; height: auto; max-height: 190px; }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart .bar { fill: var(--accent); }
.chart .gap { stroke: var(--hint); stroke-width: 2; stroke-dasharray: 2 3; opacity: 0.7; }
.chart .val { font-size: 10px; fill: var(--hint); font-variant-numeric: tabular-nums; }
.chart .lab { font-size: 10.5px; fill: var(--hint); }
.chart .tappable .hit { cursor: pointer; }
.chart .tappable:hover .bar { opacity: 0.8; }
.chart .hit:focus-visible { outline: 2px solid var(--accent); }
.insight .card-head { align-items: flex-start; }
.md-h { margin: 10px 0 4px; font-size: 15px; }
.md-p { margin: 6px 0; }
.md-ul { margin: 6px 0; padding-left: 20px; }
.md-ul li { margin: 3px 0; }

/* --- settings --- */
.btn-secondary { background: var(--soft); color: var(--accent); }
.btn-small {
  flex: none; align-self: center; min-height: var(--tap); padding: 0 12px;
  border: 0; background: transparent; color: var(--accent); font-weight: 600;
}
.habit-manage.archived .row-title { color: var(--hint); }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.manual-copy { margin-top: 10px; }
.manual-copy[hidden] { display: none; }
.import-result { margin: 8px 0 0; color: var(--ok); font-weight: 600; }
.import-result:empty { display: none; }
.btn-link { display: flex; align-items: center; justify-content: center; text-decoration: none; }

/* --- welcome and help --- */
.welcome { background: var(--soft); }
.welcome-text { margin: 0 0 10px; }
.help-intro { margin: 0 4px 12px; }
.help-steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }

@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-backdrop { transition: none; }
}
