:root {
  --bg: #e6e5ef;
  --paper: #ffffff;
  --paper-2: #f6f5fb;
  --head: #eeedf6;
  --line: #e3e2ec;
  --line-strong: #cfcedb;
  --ink: #1d1c22;
  --ink-2: #55545f;
  --muted: #8b8a96;
  --accent: #d93a47;
  --accent-ink: #ffffff;
  --brand: #4b45c8;
  --brand-soft: #e6e4fb;
  --ok: #2f9e62;
  --warn: #d58a00;
  --ring: #2a2930;
  --shadow: 0 1px 2px rgba(30, 28, 50, 0.06), 0 4px 14px rgba(30, 28, 50, 0.06);
  --radius: 14px;
  --row-h: 25px;
  --col-t: 46px;
  --col-p: 52px;
  --font: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121216;
    --paper: #1f1f26;
    --paper-2: #26262e;
    --head: #2a2a33;
    --line: #2f2f39;
    --line-strong: #3d3d49;
    --ink: #ecebf3;
    --ink-2: #b9b8c5;
    --muted: #84838f;
    --accent: #ef5361;
    --brand: #8f8aff;
    --brand-soft: #2c2a52;
    --ring: #9b9aa8;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }

.btn {
  border: 1px solid var(--line-strong);
  background: var(--paper);
  border-radius: 12px;
  padding: 11px 16px;
  font-weight: 600;
  min-height: 44px;
}
.btn:hover { background: var(--paper-2); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--accent); }
.btn:disabled { opacity: 0.6; cursor: progress; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  position: relative;
  flex: none;
}
.icon-btn:hover { background: rgba(127, 127, 140, 0.14); }
.icon-btn svg { width: 24px; height: 24px; }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* ===================== Вход ===================== */
.auth {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px calc(24px + var(--safe-b));
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--paper);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 28px 22px 22px;
}
.auth-logo {
  width: 56px;
  height: 56px;
  margin: 0 auto 10px;
  background: url("icons/icon.svg") center / contain no-repeat;
}
.auth h1 { font-size: 21px; text-align: center; margin: 0 0 18px; }
.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--paper-2);
  border-radius: 12px;
  padding: 3px;
  margin-bottom: 16px;
}
.tab { border: 0; background: transparent; border-radius: 9px; padding: 9px; font-weight: 600; color: var(--ink-2); }
.tab[aria-selected="true"] { background: var(--paper); color: var(--ink); box-shadow: var(--shadow); }
.auth-form { display: grid; gap: 12px; }
.auth-form label { display: grid; gap: 5px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.auth-form input {
  font-size: 16px;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: var(--paper);
  color: var(--ink);
}
.auth-form input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.form-error { color: var(--accent); font-size: 13px; min-height: 1em; margin: 0; }
.auth-note { font-size: 13px; color: var(--ink-2); text-align: center; margin: 14px 0 0; }
.auth-note:empty { display: none; }

/* ===================== Планер ===================== */
.planner {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
  background: var(--bg);
  position: relative;
  z-index: 5;
}
.spacer { flex: 1; }
.today-badge {
  display: grid;
  justify-items: center;
  align-content: center;
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  line-height: 1;
  margin-right: 4px;
  flex: none;
}
.today-badge.is-current { opacity: 0.92; }
.today-dow { font-size: 11px; opacity: 0.9; }
.today-num { font-size: 19px; font-weight: 700; margin-top: 2px; }
.title-btn {
  border: 0;
  background: transparent;
  display: grid;
  text-align: left;
  padding: 4px 6px;
  border-radius: 10px;
  min-width: 0;
}
.title-btn:hover { background: rgba(127, 127, 140, 0.14); }
.title-month { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-week { font-size: 12px; color: var(--ink-2); }
.date-picker { position: absolute; left: 60px; top: 40px; width: 1px; height: 1px; opacity: 0; pointer-events: none; border: 0; padding: 0; }

.sync-btn .sync-count {
  position: absolute;
  top: 2px;
  right: 0;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--warn);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
}
.sync-btn .sync-count:empty { display: none; }
.sync-btn.state-ok:not(.has-pending) { color: var(--ok); }
.sync-btn.state-offline, .sync-btn.state-error { color: var(--accent); }
.sync-btn.state-syncing svg { animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.4; } }

.board {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 0 max(10px, env(safe-area-inset-right)) calc(14px + var(--safe-b)) max(10px, env(safe-area-inset-left));
  touch-action: pan-y;
}
.spread {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 18px minmax(0, 1fr);
  max-width: 1280px;
  margin: 0 auto;
}
.spread.anim { transition: transform 0.18s ease-out, opacity 0.18s ease-out; }
.page { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

/* Спираль блокнота */
.spiral {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='15' viewBox='0 0 18 15'%3E%3Cpath d='M3 10.5c0-4.5 12-4.5 12 0' fill='none' stroke='%23303038' stroke-width='2.4' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: repeat-y;
  background-position: center 4px;
  margin: 6px 0;
}
@media (prefers-color-scheme: dark) {
  .spiral {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='15' viewBox='0 0 18 15'%3E%3Cpath d='M3 10.5c0-4.5 12-4.5 12 0' fill='none' stroke='%239a99a8' stroke-width='2.4' stroke-linecap='round'/%3E%3C/svg%3E");
  }
}

/* Карточка дня */
.day {
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.day-head {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 4px 4px 9px;
  background: var(--head);
  border-bottom: 1.5px solid var(--line-strong);
}
.day-name { flex: 1; min-width: 0; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.day-name b { font-weight: 700; }
.day-mon { color: var(--ink-2); font-size: 12.5px; }
.day.is-today .day-head { background: var(--accent); color: #fff; border-bottom-color: var(--accent); }
.day.is-today .day-mon { color: rgba(255, 255, 255, 0.85); }
.expand-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 28px;
  height: 26px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  opacity: 0.75;
  padding: 0;
}
.expand-btn:hover { opacity: 1; background: rgba(127, 127, 140, 0.18); }
.expand-btn svg { width: 17px; height: 17px; }

.col-head, .row {
  display: grid;
  grid-template-columns: var(--col-t) minmax(0, 1fr) var(--col-p);
}
.col-head {
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 2px 0 1px;
  border-bottom: 1px solid var(--line);
}
.col-head span { padding: 0 6px; }
.col-head span:last-child { text-align: right; }
.row { border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row input {
  min-width: 0;
  width: 100%;
  height: var(--row-h);
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  font-size: var(--font);
  padding: 0 6px;
  outline: none;
}
.row input + input { border-left: 1px solid var(--line); }
.row .f-t { font-variant-numeric: tabular-nums; padding-right: 2px; }
.row .f-p { text-align: right; font-variant-numeric: tabular-nums; }
.row input:focus { background: var(--brand-soft); }
.row.filled .f-c { font-weight: 500; }
.row.flash { animation: flash 1.8s ease-out; }
@keyframes flash { 0%, 40% { background: #ffe98a; } 100% { background: transparent; } }

.day-sum {
  font-size: 11.5px;
  color: var(--ink-2);
  text-align: right;
  padding: 3px 8px;
  border-top: 1.5px solid var(--line-strong);
  background: var(--paper-2);
  min-height: 21px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Итоги недели */
.week-summary {
  background: var(--brand);
  color: #fff;
  border-radius: var(--radius);
  padding: 12px 12px 13px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.week-summary h3 { margin: 0 0 6px; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.85; }
.sum-big { display: grid; gap: 1px; }
.sum-count { font-size: 15px; font-weight: 600; }
.sum-money { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.sum-month { margin-top: 8px; font-size: 12.5px; opacity: 0.88; }

/* Развёрнутый день */
.backdrop { position: fixed; inset: 0; background: rgba(10, 10, 20, 0.45); z-index: 20; }
.day.expanded {
  position: fixed;
  z-index: 21;
  left: 50%;
  top: max(16px, env(safe-area-inset-top));
  transform: translateX(-50%);
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  --row-h: 46px;
  --font: 17px;
  --col-t: 74px;
  --col-p: 92px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.day.expanded .col-head { font-size: 11px; padding: 6px 0; }
.day.expanded .row input { padding: 0 10px; }
.day.expanded .day-head { padding: 10px 8px 10px 14px; }
.day.expanded .day-name { font-size: 18px; }
.day.expanded .day-mon { font-size: 15px; }
.day.expanded .expand-btn { width: 38px; height: 34px; }
.day.expanded .expand-btn svg { width: 22px; height: 22px; }
.day.expanded .day-sum { font-size: 14px; padding: 8px 14px; }
body.has-expanded .board { overflow: hidden; }

/* ===================== Диалоги ===================== */
dialog.sheet {
  border: 0;
  padding: 0;
  width: min(620px, 100vw);
  max-width: 100vw;
  max-height: min(88dvh, 860px);
  border-radius: 20px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
  overflow: hidden;
  display: none;
  flex-direction: column;
}
dialog.sheet[open] { display: flex; }
dialog.sheet.small { width: min(400px, 100vw); }
dialog::backdrop { background: rgba(10, 10, 20, 0.45); }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 14px 10px 6px 18px; }
.sheet-head h2 { flex: 1; font-size: 19px; margin: 0; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.sheet > p { margin: 0 18px 12px; }
.sheet-foot { padding: 10px 16px calc(14px + var(--safe-b)); border-top: 1px solid var(--line); display: flex; justify-content: flex-end; }

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin: 4px 16px 6px;
  background: var(--paper-2);
  border-radius: 12px;
  padding: 3px;
}
.segmented button { border: 0; background: transparent; padding: 8px; border-radius: 9px; font-weight: 600; color: var(--ink-2); }
.segmented button[aria-selected="true"] { background: var(--paper); color: var(--ink); box-shadow: var(--shadow); }
.period-nav { display: flex; align-items: center; gap: 6px; padding: 2px 10px; }
.period-nav span { flex: 1; text-align: center; font-weight: 600; font-size: 15px; }

.stats-body, .search-body { overflow-y: auto; padding: 6px 16px 18px; flex: 1; }
.stats-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 18px 0 8px; }
.tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.tile { background: var(--paper-2); border-radius: 14px; padding: 10px 12px; display: grid; gap: 2px; min-width: 0; }
.tile.accent { background: var(--brand); color: #fff; }
.tile-label { font-size: 12px; opacity: 0.75; }
.tile-value { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note { font-size: 13px; margin: 8px 0 0; }

.breakdown { display: grid; gap: 2px; }
.bd-row {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(40px, 1fr) 2.2em 6.5em;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.bd-row.empty { color: var(--muted); }
.bd-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { height: 8px; border-radius: 4px; background: var(--paper-2); position: relative; overflow: hidden; }
.bar::after { content: ""; position: absolute; inset: 0 auto 0 0; width: var(--w, 0%); background: var(--brand); border-radius: 4px; }
.bd-count { text-align: right; font-weight: 700; }
.bd-sum { text-align: right; }

table.clients { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.clients th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 0; border-bottom: 1px solid var(--line-strong); }
.clients td { padding: 7px 0; border-bottom: 1px solid var(--line); }
.clients th:nth-child(n+2), .clients td:nth-child(n+2) { text-align: right; padding-left: 10px; white-space: nowrap; }
.clients td:first-child { word-break: break-word; }

.search-input {
  margin: 6px 16px 4px;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: var(--paper-2);
  font-size: 16px;
  color: var(--ink);
}
.search-input:focus { outline: none; border-color: var(--brand); }
.search-total { font-weight: 600; margin: 6px 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 6px; }
.chip { border: 1px solid var(--line-strong); background: var(--paper); border-radius: 999px; padding: 6px 12px; display: inline-flex; gap: 6px; align-items: baseline; }
.chip small { color: var(--muted); }
.hits { display: grid; }
.hit {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas: "date price" "client time";
  gap: 1px 10px;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  padding: 9px 4px;
}
.hit:hover { background: var(--paper-2); }
.hit-date { grid-area: date; font-size: 12.5px; color: var(--ink-2); }
.hit-time { grid-area: time; text-align: right; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.hit-client { grid-area: client; font-weight: 600; }
.hit-price { grid-area: price; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }

.menu-sync { margin: 0 18px 14px; padding: 10px 12px; border-radius: 12px; background: var(--paper-2); font-size: 14px; }
.menu-sync.state-offline, .menu-sync.state-error { color: var(--accent); }
.menu-actions { display: grid; gap: 8px; padding: 0 18px calc(18px + var(--safe-b)); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-b));
  transform: translate(-50%, 20px);
  max-width: min(460px, calc(100vw - 32px));
  background: #26252d;
  color: #fff;
  padding: 11px 16px;
  border-radius: 12px;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ===================== Адаптив ===================== */
/* Телефоны: нижняя «шторка» для диалогов */
@media (max-width: 640px) {
  dialog.sheet, dialog.sheet.small {
    width: 100vw;
    margin: auto 0 0;
    border-radius: 20px 20px 0 0;
    max-height: 92dvh;
  }
  .tile-value { font-size: 17px; }
  .tile { padding: 9px 10px; }
}

/* Телефоны: вся неделя на одном экране — карточки делят высоту экрана поровну,
   строки дня растягиваются на высоту карточки. Если экран слишком низкий — прокрутка. */
@media (max-width: 759px) {
  :root { --radius: 11px; }
  .topbar { padding-top: max(6px, env(safe-area-inset-top)); padding-bottom: 6px; }
  .today-badge { width: 38px; height: 38px; }
  .today-num { font-size: 17px; }
  .title-month { font-size: 15px; }
  .board { padding-bottom: calc(6px + var(--safe-b)); }
  .spread { height: 100%; min-height: 500px; grid-template-rows: minmax(0, 1fr); }
  .row input { min-height: 0; }
  .page { display: grid; grid-template-rows: repeat(4, minmax(0, 1fr)); gap: 6px; }
  .spiral { margin: 2px 0; }
  .day { min-height: 0; }
  .day:not(.expanded) .col-head,
  .day:not(.expanded) .day-sum { display: none; }
  .day:not(.expanded) .rows { flex: 1; min-height: 0; display: grid; grid-template-rows: repeat(7, minmax(0, 1fr)); }
  .day:not(.expanded) .row { min-height: 0; }
  .day:not(.expanded) .row input { height: 100%; font-size: 13px; }
  .day-head { padding: 2px 2px 2px 7px; }
  .day-name { font-size: 13px; }
  .day-mon { font-size: 11px; }
  .expand-btn { width: 28px; height: 22px; }
  .expand-btn svg { width: 15px; height: 15px; }
  .week-summary { min-height: 0; overflow: hidden; padding: 8px 10px; display: flex; flex-direction: column; justify-content: center; }
  .week-summary h3 { font-size: 10.5px; margin-bottom: 3px; }
  .sum-count { font-size: 13px; }
  .sum-money { font-size: 19px; }
  .sum-month { font-size: 11px; margin-top: 4px; }
}

/* Узкие телефоны */
@media (max-width: 400px) {
  :root { --font: 13.5px; --row-h: 24px; --col-t: 40px; --col-p: 44px; }
  .topbar { gap: 0; padding-left: 8px; padding-right: 6px; }
  .nav-btn { display: none; }
  .icon-btn { width: 35px; }
  .board { padding-left: 6px; padding-right: 6px; }
  .spread { grid-template-columns: minmax(0, 1fr) 14px minmax(0, 1fr); }
  .row input { padding: 0 4px; }
  .col-head span { padding: 0 4px; }
  .bd-row { grid-template-columns: minmax(0, 1.6fr) minmax(24px, 0.7fr) 2em 5.8em; gap: 6px; font-size: 13px; }
}

/* Совсем узкие экраны — одна колонка */
@media (max-width: 330px) {
  .spread { grid-template-columns: minmax(0, 1fr); grid-template-rows: none; height: auto; }
  .page { grid-template-rows: none; grid-auto-rows: 170px; }
  .spiral { display: none; }
  .page-left { margin-bottom: 12px; }
}

/* Планшеты и ПК */
@media (min-width: 760px) {
  :root { --row-h: 30px; --font: 15px; --col-t: 60px; --col-p: 72px; }
  .topbar { padding: 14px 20px 12px; gap: 6px; }
  .board { padding: 0 20px 24px; }
  .spread { grid-template-columns: minmax(0, 1fr) 26px minmax(0, 1fr); }
  .spiral { background-size: 26px 20px; }
  .page { gap: 16px; }
  .col-head { font-size: 10.5px; }
  .day-head { padding: 7px 6px 6px 12px; }
  .day-name { font-size: 16px; }
  .day-mon { font-size: 13.5px; }
  .row input { padding: 0 9px; }
  .col-head span { padding: 0 9px; }
  .day-sum { font-size: 13px; padding: 5px 12px; }
  .title-month { font-size: 18px; }
  .week-summary { padding: 16px 18px; }
  .sum-money { font-size: 28px; }
}

@media (min-width: 1100px) {
  .spread { gap: 0; }
  .page-left, .page-right { display: grid; grid-template-columns: 1fr 1fr; align-content: start; gap: 16px; }
  .page-left .week-summary { align-self: stretch; display: flex; flex-direction: column; justify-content: center; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

@media print {
  .topbar, .spiral, .expand-btn { display: none; }
  body, .planner { height: auto; background: #fff; }
  .board { overflow: visible; }
}
