:root {
  --bg: #eef0ec;
  --surface: #ffffff;
  --border: #dbdfd7;
  --text: #1b211d;
  --text-dim: #667066;
  --accent: #c1440e;
  --accent-bg: #fbe6d9;
  --chip-bg: #e8ebe4;
  --shadow: 0 1px 2px rgba(20,30,20,0.05), 0 1px 10px rgba(20,30,20,0.05);
  --done-bg: #e5f1e7;
  --done-text: #2f7a4f;
  --danger: #b3261e;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14180f;
    --surface: #1d2317;
    --border: #313a29;
    --text: #eef1e8;
    --text-dim: #9aa593;
    --accent: #f0955c;
    --accent-bg: #3a2416;
    --chip-bg: #262d20;
    --shadow: 0 1px 2px rgba(0,0,0,0.35), 0 1px 10px rgba(0,0,0,0.35);
    --done-bg: #1c2b1f;
    --done-text: #82cf9b;
    --danger: #ff8a80;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { background: var(--bg); }
body {
  margin: 0;
  font-family: "PT Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
}
h1, h2, h3, .brand, .roomcard-name, .typehead span:first-child {
  font-family: "Manrope", "PT Sans", sans-serif;
}
.mono, .meta, .logtime, .roomcard-progress {
  font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, monospace;
  font-variant-numeric: tabular-nums;
}
button { font-family: inherit; }
a { color: inherit; }

#app { max-width: 720px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }

/* На iPhone приложение, добавленное на экран «Домой», рисуется под строкой
   состояния (часы, батарея) и под «чёлкой» — без этих отступов кнопка
   «Назад» и имя пользователя уезжают под неё. env(safe-area-inset-*) на
   всех остальных устройствах равен нулю, так что вёрстка не меняется. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px; /* запасной вариант, если env() не поддерживается */
  padding: calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right))
           12px calc(16px + env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand {
  font-weight: 800;
  font-size: 17px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.topbar .back {
  background: none;
  border: none;
  color: var(--text);
  font-size: 15px;
  font-weight: 700;
  padding: 6px 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}
.topbar .whoami {
  font-size: 12.5px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 8px;
}
/* Колокольчик появляется, только пока уведомления не разрешены. */
.topbar .whoami button.bell {
  background: var(--accent-bg);
  border-color: var(--accent);
  font-size: 14px;
  padding: 3px 7px;
  line-height: 1.2;
}
.topbar .whoami button {
  background: var(--chip-bg);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
}

main {
  flex: 1;
  padding: 14px 16px 40px; /* запасной вариант, если env() не поддерживается */
  padding: 14px calc(16px + env(safe-area-inset-right))
           calc(40px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}

/* ---------- Login ---------- */
/* На экране входа верхней панели нет, поэтому отступ от «чёлки» — здесь. */
.login-wrap {
  max-width: 380px;
  margin: 10vh auto 0;      /* запасные варианты, если env() не поддерживается */
  padding: 0 16px;
  margin: calc(10vh + env(safe-area-inset-top)) auto 0;
  padding: 0 calc(16px + env(safe-area-inset-right)) 0 calc(16px + env(safe-area-inset-left));
}
.login-wrap h1 { font-size: 22px; margin: 0 0 4px; }
.login-wrap p.sub { color: var(--text-dim); font-size: 14px; margin: 0 0 22px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 700; margin-bottom: 6px; }
.field select, .field input, .field textarea {
  width: 100%;
  padding: 12px 12px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  box-sizing: border-box;
}
.field textarea { resize: vertical; }
.field select:focus, .field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field .hint { font-size: 12px; color: var(--text-dim); margin-top: 6px; }
.btn-primary {
  width: 100%;
  padding: 13px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 15.5px;
  cursor: pointer;
}
.btn-primary:disabled { opacity: 0.55; }
.error-box {
  background: var(--accent-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13.5px;
  margin-bottom: 14px;
}

/* ---------- Room list ---------- */
.overall { margin-bottom: 16px; }
.overall .label { font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }
.overallbar { height: 7px; border-radius: 5px; background: var(--chip-bg); overflow: hidden; }
.overallbar > div { height: 100%; background: var(--done-text); transition: width .2s ease; }

/* ---------- Recent activity feed ---------- */
/* Лента целиком кликабельна — открывает полный журнал. */
.recentfeed { margin-bottom: 18px; cursor: pointer; border-radius: 12px; }
.recentfeed:active { opacity: 0.75; }
.recentfeed-title {
  font-size: 12.5px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.4px; color: var(--text-dim); margin: 0 0 8px;
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
}
.recentfeed-more { text-transform: none; letter-spacing: 0; color: var(--accent); font-weight: 700; white-space: nowrap; }
.recentrows { display: flex; flex-direction: column; gap: 7px; }
.recentrow {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border);
  border-radius: 10px;
  padding: 9px 12px;
  box-shadow: var(--shadow);
}
.recentrow.is-check { border-left-color: var(--done-text); }
.recentrow.is-uncheck { border-left-color: var(--accent); }
.recenticon {
  flex: 0 0 auto;
  width: 22px; height: 22px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
  margin-top: 1px;
}
.is-check .recenticon { background: var(--done-bg); color: var(--done-text); }
.is-uncheck .recenticon { background: var(--accent-bg); color: var(--accent); }
.is-edit .recenticon { background: var(--chip-bg); color: var(--text-dim); }
.is-edit { border-left-color: var(--text-dim); }
.is-create .recenticon { background: var(--accent-bg); color: var(--accent); font-weight: 800; }
.is-create { border-left-color: var(--accent); }
.is-delete .recenticon { background: var(--accent-bg); color: var(--danger); font-weight: 800; }
.is-delete { border-left-color: var(--danger); }
.recentbody { flex: 1; min-width: 0; }
.recenttext { font-size: 13.5px; line-height: 1.35; }
.recenttime { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

/* Помещения — по два в строке. minmax(0, 1fr) вместо 1fr: иначе длинное
   название («Малая гардеробная») растягивает свою колонку и строка едет. */
.roomlist {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.roomcard {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 10px;
  min-height: 92px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.roomcard.done { border-color: var(--done-text); }
.roomcard-name {
  font-weight: 700;
  font-size: 15.5px;
  line-height: 1.25;
  overflow-wrap: anywhere;
}
.roomcard-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.roomcard-progress { font-size: 13px; color: var(--text-dim); white-space: nowrap; }
.roomcard.done .roomcard-progress { color: var(--done-text); }
.roomcard-chevron { color: var(--text-dim); font-size: 18px; line-height: 1; }

.navtabs { display: flex; gap: 6px; margin-bottom: 16px; }
.navtabs button {
  flex: 1;
  padding: 9px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  font-weight: 700;
  font-size: 13.5px;
  cursor: pointer;
}
.navtabs button.active { background: var(--accent-bg); color: var(--accent); border-color: var(--accent); }

/* Плашка типа работ — тот же формат, что у помещений, но с цветной полосой
   слева, теми же цветами, что и заголовки типов внутри помещения. */
.typecard { border-left: 4px solid var(--tc, var(--accent)); }
.typecard .roomcard-name { color: var(--tc, var(--text)); }

/* В разрезе по типам заголовок группы — это помещение, по нему можно
   провалиться в само помещение. */
.typehead.clickable { cursor: pointer; }
.typehead.clickable:active { opacity: 0.6; }
.typehead.clickable span:first-child::after { content: ' ›'; font-weight: 800; }

.recent-empty {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  font-size: 13.5px;
  color: var(--text-dim);
  box-shadow: var(--shadow);
}

/* ---------- Room detail ---------- */
.roomhead { margin-bottom: 14px; }
.roomhead-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.roomhead h2 { margin: 0 0 4px; font-size: 21px; }
.roomhead .meta { font-size: 12.5px; color: var(--text-dim); }
.btn-add {
  flex: 0 0 auto;
  background: var(--accent-bg);
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 7px 12px;
  font-weight: 700;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}

.typegrid { display: flex; flex-direction: column; gap: 14px; }
.typecol {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px 14px;
  box-shadow: var(--shadow);
}
.typehead {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-weight: 700;
  font-size: 14px;
  color: var(--tc, var(--accent));
  border-bottom: 2px solid var(--tc, var(--accent));
  padding-bottom: 6px;
  margin-bottom: 8px;
}
.typehead .count { font-weight: 400; font-size: 11px; color: var(--text-dim); }

ul.items { list-style: none; margin: 0; padding: 0; }
li.item { border-radius: 8px; }
li.item + li.item { margin-top: 2px; }
.itemrow { display: flex; align-items: flex-start; gap: 10px; padding: 9px 4px; cursor: pointer; }
.itemrow:active { background: var(--chip-bg); }
.itembody { flex: 1; min-width: 0; }

/* ---------- Перетаскивание пунктов ---------- */
li.item {
  /* Разрешаем обычную вертикальную прокрутку списка пальцем, но убираем
     всплывающее меню iOS при долгом нажатии — оно мешает «взять» пункт. */
  touch-action: pan-y;
  -webkit-touch-callout: none;
  transition: transform 140ms ease, background 140ms ease, box-shadow 140ms ease;
}
/* Палец лёг на пункт — он слегка «вдавливается», видно, что удержание идёт. */
li.item.press-hold { background: var(--chip-bg); transform: scale(0.985); }
/* Пункт взят: приподнят, с тенью, следует за пальцем. */
li.item.dragging {
  background: var(--surface);
  box-shadow: 0 8px 22px rgba(20, 30, 20, 0.22);
  /* outline, а не border: рамка не занимает места и пункт не дёргается */
  outline: 1px solid var(--tc, var(--accent));
  position: relative;
  z-index: 5;
  transition: none;
  cursor: grabbing;
}
li.item.dragging .itemrow { cursor: grabbing; }
/* Список, внутри которого идёт перестановка, подсвечен — сразу понятно,
   что пункт двигается только в пределах своего типа работ. */
ul.items.reordering {
  background: var(--chip-bg);
  border-radius: 10px;
  outline: 1px dashed var(--tc, var(--border));
  outline-offset: 2px;
}
/* Отпустили — короткая вспышка подтверждает, что место принято. */
@keyframes just-moved-flash {
  from { background: var(--accent-bg); }
  to { background: transparent; }
}
li.item.just-moved { animation: just-moved-flash 900ms ease-out; }
body.is-dragging { -webkit-user-select: none; user-select: none; }

/* Квадратик отметки — самостоятельная кнопка: тап по нему сразу ставит или
   снимает галочку, не открывая карточку пункта. */
.box[role="checkbox"] { cursor: pointer; }
.box[role="checkbox"]:active { transform: scale(0.9); }
.box {
  flex: 0 0 auto;
  width: 22px; height: 22px;
  border: 2px solid var(--text-dim);
  border-radius: 6px;
  margin-top: 1px;
  position: relative;
  background: transparent;
}
.box.checked { background: var(--done-text); border-color: var(--done-text); }
.box.checked::after {
  content: ""; position: absolute; left: 6px; top: 2px;
  width: 6px; height: 11px;
  border: solid #fff; border-width: 0 2.5px 2.5px 0;
  transform: rotate(40deg);
}
.box.locked { opacity: 0.45; }
.box.checked.locked { opacity: 0.65; }
.itext { font-size: 15px; line-height: 1.35; padding-top: 1px; }
.itext.done { color: var(--text-dim); text-decoration: line-through; }
.who { display: inline-block; margin-left: 7px; font-size: 11.5px; color: var(--text-dim); background: var(--chip-bg); border-radius: 5px; padding: 2px 6px; white-space: nowrap; }
.donemeta { font-size: 11.5px; color: var(--done-text); margin-top: 2px; }
.notecount { font-size: 11.5px; color: var(--accent); margin-top: 2px; font-weight: 600; }

/* ---------- Item detail / notes ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 40;
  display: flex; align-items: flex-end;
}
.sheet {
  background: var(--surface);
  width: 100%;
  max-height: 86vh;
  overflow-y: auto;
  border-radius: 16px 16px 0 0;
  /* Снизу — запас на полоску «Домой» у iPhone без кнопки. */
  padding: 16px 16px 24px; /* запасной вариант, если env() не поддерживается */
  padding: 16px calc(16px + env(safe-area-inset-right))
           calc(24px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  box-shadow: 0 -4px 24px rgba(0,0,0,0.2);
}
.sheet .sheet-handle { width: 40px; height: 4px; background: var(--border); border-radius: 3px; margin: 0 auto 14px; }
.sheet h3 { margin: 0 0 4px; font-size: 17px; line-height: 1.3; }
.sheet-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.sheet-title-row h3 { flex: 1; }
.sheet-title-row.editing { flex-direction: column; align-items: stretch; }
.edit-btn {
  flex: 0 0 auto;
  background: var(--chip-bg);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  width: 30px; height: 30px;
  font-size: 14px;
  cursor: pointer;
  margin-top: -2px;
}
.edit-text-area {
  width: 100%;
  min-height: 70px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  resize: vertical;
  box-sizing: border-box;
}
.edit-btn-row { display: flex; gap: 8px; margin-top: 8px; }
.edit-btn-row .btn-primary { width: auto; flex: 1; padding: 10px; font-size: 14px; }
.edit-btn-row .sheet-close { margin: 0; flex: 0 0 auto; width: auto; padding: 10px 14px; border: 1px solid var(--border); border-radius: 10px; }
.sheet .sheet-who { font-size: 12.5px; color: var(--text-dim); margin-bottom: 14px; }
.sheet .toggle-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); margin-bottom: 6px; cursor: pointer; }
.sheet .toggle-row .box { width: 24px; height: 24px; }
.sheet .toggle-row .lbl { font-weight: 700; font-size: 14.5px; }
.sheet .toggle-row.disabled { cursor: not-allowed; opacity: 0.8; }
.lock-hint { font-size: 12.5px; color: var(--text-dim); margin-bottom: 14px; }

.notes-title { font-size: 12.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px; color: var(--text-dim); margin: 0 0 8px; }
.note { background: var(--chip-bg); border-radius: 10px; padding: 9px 11px; margin-bottom: 8px; }
.note-head { display: flex; justify-content: space-between; gap: 8px; font-size: 11.5px; color: var(--text-dim); margin-bottom: 3px; }
.note-text { font-size: 14px; white-space: pre-wrap; }
.note-del { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 11.5px; text-decoration: underline; padding: 0; }
.note-empty { font-size: 13.5px; color: var(--text-dim); margin-bottom: 10px; }

.noteform { display: flex; gap: 8px; margin-top: 10px; }
.noteform textarea {
  flex: 1; resize: none; min-height: 44px; padding: 10px 12px;
  border-radius: 10px; border: 1.5px solid var(--border);
  background: var(--surface); color: var(--text); font-family: inherit; font-size: 14.5px;
}
.noteform button {
  border: none; background: var(--accent); color: #fff; border-radius: 10px;
  padding: 0 16px; font-weight: 700; cursor: pointer; font-size: 14px;
}
.noteform button:disabled { opacity: 0.5; }

.sheet-close {
  display: block; width: 100%; text-align: center; margin-top: 16px;
  background: none; border: none; color: var(--text-dim); font-size: 14px; padding: 8px; cursor: pointer;
}

/* Удаление пункта — в два касания, чтобы случайный тап ничего не снёс. */
.btn-danger {
  width: 100%;
  margin-top: 10px;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--danger);
  background: none;
  color: var(--danger);
  font-weight: 700;
  font-size: 14.5px;
  cursor: pointer;
}
.btn-danger:disabled { opacity: 0.55; }
.danger-confirm {
  margin-top: 10px;
  border: 1px solid var(--danger);
  border-radius: 10px;
  padding: 12px;
  background: var(--accent-bg);
}
.danger-text { font-size: 13.5px; color: var(--danger); margin-bottom: 10px; line-height: 1.35; }
.danger-row { display: flex; gap: 8px; align-items: center; }
.danger-row .btn-danger { margin-top: 0; flex: 1; background: var(--danger); color: #fff; border-color: var(--danger); }
.danger-row .sheet-close { margin-top: 0; width: auto; flex: 0 0 auto; padding: 12px 14px; }

/* ---------- Log ---------- */
.logfeed { display: flex; flex-direction: column; gap: 8px; }
.logrow { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.logrow .logtime { font-size: 11px; color: var(--text-dim); float: right; }
.logrow .logtext { font-size: 14px; line-height: 1.4; }
.logrow .logtext b { font-weight: 700; }
.logrow .logroom { font-size: 11.5px; color: var(--accent); font-weight: 700; }
.empty-state { text-align: center; color: var(--text-dim); font-size: 14px; padding: 40px 20px; }

.spinner-line { text-align: center; padding: 30px; color: var(--text-dim); font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 20px;
  font-size: 13.5px; z-index: 60; box-shadow: 0 4px 16px rgba(0,0,0,0.25);
}
