:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #f6f7f9);
  --text: var(--tg-theme-text-color, #17212b);
  --hint: var(--tg-theme-hint-color, #6b7886);
  --link: var(--tg-theme-link-color, #4f8cff);
  --button: var(--tg-theme-button-color, #4f8cff);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --border: rgba(112, 124, 138, 0.22);
  --danger: #d94c4c;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); }
button, input, select { font: inherit; }
button { border: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:focus { outline: none; }
button:focus-visible { outline: 2px solid color-mix(in srgb, var(--button) 60%, transparent); outline-offset: 2px; }
.app-shell { min-height: 100vh; padding: calc(14px + env(safe-area-inset-top)) 14px calc(88px + env(safe-area-inset-bottom)); }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
h1 { margin: 0; font-size: 28px; line-height: 1.1; letter-spacing: 0; }
.date-label { min-height: 18px; color: var(--hint); font-size: 13px; }
.icon-button { width: 40px; height: 40px; border-radius: 8px; background: var(--surface); color: var(--text); border: 1px solid var(--border); font-size: 22px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 4px; background: color-mix(in srgb, var(--surface) 82%, transparent); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 12px; }
.tab { height: 38px; border-radius: 6px; background: transparent; color: var(--hint); }
.tab.active { background: var(--button); color: var(--button-text); }
.view { display: none; }
.view.active { display: block; }
.status-bar { margin: 10px 0; padding: 10px 12px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border); color: var(--hint); }
.summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.summary-item { min-height: 62px; padding: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.summary-value { font-size: 21px; font-weight: 700; }
.summary-label { color: var(--hint); font-size: 12px; }
.habit-list { display: grid; gap: 10px; }
.habit-card { position: relative; display: grid; gap: 9px; padding: 13px; background: var(--surface); border: 1px solid var(--border); border-left: 5px solid var(--accent, var(--button)); border-radius: 8px; overflow: hidden; }
.habit-card.done { border-color: color-mix(in srgb, #36b26c 70%, var(--border)); }
.habit-head { display: flex; gap: 10px; align-items: flex-start; justify-content: space-between; }
.habit-main { min-width: 0; }
.habit-controls { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex: 0 0 auto; }
.habit-title { font-size: 17px; font-weight: 700; line-height: 1.25; overflow-wrap: anywhere; }
.habit-meta { display: flex; flex-wrap: wrap; gap: 6px; color: var(--hint); font-size: 12px; }
.badge { padding: 4px 7px; border-radius: 999px; background: color-mix(in srgb, var(--accent, var(--button)) 12%, transparent); color: var(--text); }
.progress { height: 8px; background: color-mix(in srgb, var(--hint) 18%, transparent); border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; width: var(--value, 0%); background: var(--accent, var(--button)); border-radius: inherit; transition: width 180ms ease; }
.card-actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
.card-actions button { min-height: 38px; border-radius: 8px; padding: 8px; background: color-mix(in srgb, var(--button) 10%, var(--surface)); color: var(--text); border: 1px solid var(--border); }
.card-actions .primary-action { background: var(--button); color: var(--button-text); border-color: transparent; }
.card-actions .skip-action { color: var(--danger); }
.card-actions button:disabled { cursor: default; color: var(--hint); background: color-mix(in srgb, var(--hint) 14%, var(--surface)); border-color: var(--border); opacity: 1; }
.not-due-note { min-height: 38px; display: flex; align-items: center; padding: 8px 10px; border-radius: 8px; background: color-mix(in srgb, var(--hint) 10%, transparent); color: var(--hint); border: 1px solid var(--border); font-size: 13px; }
.checkmark { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: #36b26c; color: #fff; transform: scale(0); opacity: 0; visibility: hidden; }
.habit-card.done .checkmark { visibility: visible; transform: scale(1); opacity: 1; }
.habit-card.animate-check .checkmark { animation: pop-check 420ms cubic-bezier(.2, 1.4, .4, 1) forwards; }
@keyframes pop-check { 0% { transform: scale(.2); opacity: 0; } 65% { transform: scale(1.16); opacity: 1; } 100% { transform: scale(1); opacity: 1; } }
.empty { padding: 30px 18px; text-align: center; color: var(--hint); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.fab { position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); width: 58px; height: 58px; padding: 0; border-radius: 50%; background: var(--button); color: var(--button-text); font-size: 0; line-height: 0; box-shadow: 0 10px 28px rgba(0,0,0,.22); -webkit-appearance: none; appearance: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
.fab::before, .fab::after { content: ""; position: absolute; left: 50%; top: 50%; width: 22px; height: 3px; border-radius: 999px; background: currentColor; transform: translate(-50%, -50%); }
.fab::after { transform: translate(-50%, -50%) rotate(90deg); }
.fab:focus, .fab:focus-visible { outline: none; }
.fab:active { background: color-mix(in srgb, var(--button) 82%, #000); }
.dialog { width: min(560px, calc(100vw - 24px)); border: 0; border-radius: 8px; padding: 0; background: var(--surface); color: var(--text); }
.dialog::backdrop { background: rgba(0,0,0,.36); }
.habit-form { display: grid; gap: 12px; padding: 14px; }
.dialog-header { display: flex; align-items: center; justify-content: space-between; }
.dialog-header h2 { margin: 0; font-size: 19px; }
.habit-form label { display: grid; gap: 6px; color: var(--hint); font-size: 13px; }
.habit-form input, .habit-form select { width: 100%; min-height: 42px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); padding: 8px 10px; font-size: 16px; line-height: 1.2; }
.reminder-color-row { display: grid; grid-template-columns: minmax(0, 1fr) 50px; gap: 8px; align-items: start; }
.time-field { min-width: 0; margin: 0; padding: 21px 0 0; border: 0; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 6px; align-items: end; color: var(--hint); font-size: 13px; }
.habit-form .reminder-toggle { min-height: 42px; display: flex; align-items: center; gap: 5px; color: var(--text); font-size: 15px; font-weight: 600; white-space: nowrap; }
.habit-form .reminder-toggle input { width: 16px; height: 16px; min-height: 0; flex: 0 0 auto; margin: 0; padding: 0; }
.time-picker { width: max-content; justify-self: start; display: grid; grid-template-columns: 42px max-content 42px; gap: 4px; align-items: center; }
.time-picker span { color: var(--hint); font-weight: 700; }
.time-picker input { text-align: center; font-variant-numeric: tabular-nums; padding-left: 4px; padding-right: 4px; }
.color-field { min-width: 0; }
.color-field input[type="color"] { width: 100%; min-width: 0; padding: 4px; }
.grid.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.weekday-grid { border: 1px solid var(--border); border-radius: 8px; padding: 10px; display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.weekday-grid legend { color: var(--hint); font-size: 13px; }
.weekday-grid label { display: grid; place-items: center; gap: 4px; padding: 6px 2px; border-radius: 8px; background: var(--bg); font-size: 12px; color: var(--text); }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 4px; }
.primary, .secondary { min-height: 44px; border-radius: 8px; padding: 8px 12px; }
.primary { background: var(--button); color: var(--button-text); }
.secondary { background: var(--bg); color: var(--danger); border: 1px solid var(--border); }
.reason-list { display: grid; gap: 7px; }
.reason-list button { min-height: 40px; border-radius: 8px; background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.toast { position: fixed; left: 12px; right: 12px; bottom: calc(84px + env(safe-area-inset-bottom)); display: none; align-items: center; justify-content: space-between; gap: 12px; padding: 12px; background: #17212b; color: #fff; border-radius: 8px; box-shadow: 0 10px 28px rgba(0,0,0,.25); }
.toast.visible { display: flex; }
.toast[hidden] { display: none !important; }
.toast button { background: transparent; color: #fff; text-decoration: underline; }
.toast button:disabled { opacity: .55; }
.segmented { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; padding: 4px; background: color-mix(in srgb, var(--surface) 82%, transparent); border: 1px solid var(--border); border-radius: 8px; }
.segmented button { min-height: 36px; border-radius: 6px; background: transparent; color: var(--hint); }
.segmented button.active { background: var(--button); color: var(--button-text); }
.list-filter { margin-bottom: 12px; }
.analytics-grid { display: grid; gap: 10px; }
.analytics-card { padding: 13px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.analytics-card h3 { margin: 0 0 8px; font-size: 16px; }
.analytics-toolbar { display: grid; gap: 10px; margin-bottom: 12px; }
.month-switcher { display: grid; grid-template-columns: 40px minmax(0, 1fr) auto 40px; gap: 8px; align-items: center; }
.month-title { min-width: 0; text-align: center; font-size: 18px; font-weight: 700; text-transform: capitalize; overflow-wrap: anywhere; }
.month-current, .habit-filter-row button { min-height: 40px; border-radius: 8px; padding: 0 10px; background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.month-switcher .icon-button:disabled { opacity: .4; cursor: default; }
.habit-filter-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; }
.habit-filter-row select { width: 100%; min-height: 42px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 10px; font-size: 16px; line-height: 1.2; }
.calendar-weekdays, .calendar-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 5px; }
.calendar-weekdays { margin-bottom: 6px; color: var(--hint); font-size: 11px; text-align: center; }
.calendar-day { position: relative; aspect-ratio: 1; min-width: 0; border-radius: 7px; border: 1px solid var(--border); background: color-mix(in srgb, var(--hint) 9%, transparent); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 13px; font-weight: 700; line-height: 1; }
.calendar-day small { min-height: 10px; color: var(--hint); font-size: 10px; font-weight: 600; }
.calendar-day.empty-cell { border-color: transparent; background: transparent; }
.calendar-day.status-success { background: color-mix(in srgb, #36b26c 24%, var(--surface)); border-color: color-mix(in srgb, #36b26c 55%, var(--border)); }
.calendar-day.status-partial { background: color-mix(in srgb, #f2b441 24%, var(--surface)); border-color: color-mix(in srgb, #f2b441 55%, var(--border)); }
.calendar-day.status-missed { background: color-mix(in srgb, #d94c4c 20%, var(--surface)); border-color: color-mix(in srgb, #d94c4c 55%, var(--border)); }
.calendar-day.status-today { background: color-mix(in srgb, var(--button) 15%, var(--surface)); border-color: color-mix(in srgb, var(--button) 50%, var(--border)); }
.calendar-day.status-future { opacity: .58; }
.calendar-day.is-today { box-shadow: inset 0 0 0 2px var(--button); }
.habit-stats-list { display: grid; gap: 8px; }
.habit-stat { width: 100%; display: grid; gap: 10px; padding: 12px; text-align: left; background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 8px; }
.habit-stat.selected { border-color: var(--button); box-shadow: inset 0 0 0 1px var(--button); }
.habit-stat.locked, .habit-stat:disabled { cursor: default; opacity: 1; }
.habit-stat > div:first-child { display: grid; gap: 3px; }
.habit-stat b { font-size: 15px; overflow-wrap: anywhere; }
.habit-stat span { color: var(--hint); font-size: 12px; }
.habit-stat-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.habit-stat-grid span { min-height: 44px; display: grid; align-content: center; gap: 2px; padding: 7px; border-radius: 7px; background: var(--surface); border: 1px solid var(--border); color: var(--text); text-align: center; font-weight: 700; }
.habit-stat-grid small { color: var(--hint); font-size: 10px; font-weight: 500; }
.compact-empty { padding: 16px 12px; }
@media (max-width: 420px) { .month-switcher { grid-template-columns: 40px minmax(0, 1fr) 40px; } .month-switcher .icon-button:first-child { grid-column: 1; grid-row: 1; } .month-title { grid-column: 2; grid-row: 1; } .month-switcher .icon-button:last-child { grid-column: 3; grid-row: 1; } .month-current { grid-column: 1 / -1; grid-row: 2; } .habit-filter-row { grid-template-columns: 1fr; } .habit-stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 380px) { .summary { grid-template-columns: repeat(2, 1fr); } .card-actions { grid-template-columns: 1fr; } .grid.two { grid-template-columns: 1fr; } .calendar-day { font-size: 12px; } }
