:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --primary: #236bff;
  --primary-dark: #1c54c4;
  --danger: #e5484d;
  --success: #2e9e5b;
  --warning: #d97706;
  --text: #1c2430;
  --muted: #6b7a90;
  --border: #e3e8f0;
  --radius: 12px;
  --shadow: 0 2px 12px rgba(20, 35, 60, .08);
  --header-h: 52px;
  --tabbar-h: 54px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  background: var(--bg);
}

/* ---------- header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-title { font-size: 17px; font-weight: 600; flex: 1; text-align: center; }

.icon-btn {
  width: 38px; height: 38px;
  border: none; background: transparent;
  font-size: 20px; color: var(--primary);
  border-radius: 10px; cursor: pointer;
}
.icon-btn:active { background: #eef3ff; }

/* ---------- боковое меню (drawer) ---------- */
.drawer-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(10, 18, 30, .45);
  opacity: 0; visibility: hidden;
  transition: opacity .2s ease, visibility .2s ease;
}
.drawer-overlay.open { opacity: 1; visibility: visible; }

.drawer {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: min(300px, 82vw);
  background: var(--surface);
  z-index: 51;
  display: flex; flex-direction: column;
  transform: translateX(-102%);
  transition: transform .25s ease;
  box-shadow: 4px 0 20px rgba(0,0,0,.15);
}
.drawer.open { transform: translateX(0); }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.drawer-head h2 { font-size: 16px; }

.drawer-user {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.drawer-user .name { font-weight: 600; }
.drawer-user .role { font-size: 12px; color: var(--muted); margin-top: 2px; }

.drawer-nav { flex: 1; overflow-y: auto; padding: 8px 0; }
.drawer-item {
  display: flex; align-items: center; gap: 12px;
  width: 100%;
  padding: 13px 16px;
  border: none; background: transparent;
  font-size: 15px; color: var(--text);
  cursor: pointer; text-align: left;
}
.drawer-item:active { background: #eef3ff; }
.drawer-item .d-emoji { font-size: 18px; }
.drawer-item.active { color: var(--primary); font-weight: 600; }

.drawer-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  color: var(--muted); font-size: 12px;
}

/* ---------- tabbar ---------- */
.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 20;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  max-width: 640px;
  margin: 0 auto;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  padding-top: 8px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}
.tab::before { content: attr(data-icon); font-size: 18px; }
.tab.active { color: var(--primary); }

/* ---------- content ---------- */
.content {
  flex: 1;
  padding: 12px 12px calc(var(--tabbar-h) + 24px + var(--safe-bottom));
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: transform .08s ease;
}
.card:active { transform: scale(.99); }

.card .card-title { font-weight: 600; margin-bottom: 6px; display: flex; align-items: center; gap: 8px; }
.card .card-meta { color: var(--muted); font-size: 13px; display: flex; flex-wrap: wrap; gap: 8px 14px; }
.card .card-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.badge {
  display: inline-flex; align-items: center;
  font-size: 12px; font-weight: 500;
  padding: 2px 8px; border-radius: 20px;
  background: #eef3ff; color: var(--primary);
}
.badge.red { background: #fdecec; color: var(--danger); }
.badge.green { background: #e7f7ee; color: var(--success); }
.badge.orange { background: #fdf1e3; color: var(--warning); }
.badge.gray { background: #eef1f5; color: var(--muted); }

.pill {
  display: inline-block;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 20px;
  color: #fff;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  border: none;
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  background: var(--primary); color: #fff;
  width: 100%;
  margin-bottom: 8px;
  transition: opacity .1s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.secondary { background: #eef1f5; color: var(--text); }
.btn.danger { background: var(--danger); }
.btn.success { background: var(--success); }
.btn.small { width: auto; padding: 7px 12px; font-size: 13px; border-radius: 8px; }

.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

/* ---------- forms ---------- */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; font-weight: 500; color: var(--muted); margin-bottom: 5px; }
.input, .select, .textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 15px;
  background: var(--surface);
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
}
.textarea { min-height: 84px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--primary); }

.checkline { display: flex; align-items: center; gap: 10px; padding: 8px 0; }
.checkline input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--primary); }

.switch { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; }
.switch label { font-size: 15px; color: var(--text); }
.switch input { width: 20px; height: 20px; accent-color: var(--primary); }

.photo-zone:active { background: #eef3ff; }
.file-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: #eef3ff; color: var(--primary);
  padding: 6px 12px; border-radius: 20px; font-size: 13px;
  text-decoration: none;
}
a.file-chip:active { background: #dbe8ff; }

/* ---------- вложения: миниатюры и просмотр фото ---------- */
.attach-grid { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-start; }

.attach-item { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 92px; }

.attach-thumb {
  width: 88px; height: 88px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #eef1f5;
  cursor: zoom-in;
}
.attach-thumb:active { opacity: .85; }

.attach-name {
  font-size: 11px; color: var(--muted);
  max-width: 92px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.lightbox {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(8, 14, 24, .92);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.lightbox.hidden { display: none; }
.lightbox img {
  max-width: 100%; max-height: 88dvh;
  border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.lightbox-close {
  position: absolute; top: 14px; right: 14px;
  width: 42px; height: 42px;
  border: none; border-radius: 50%;
  background: rgba(255,255,255,.15); color: #fff;
  font-size: 18px; cursor: pointer;
}
.lightbox-close:active { background: rgba(255,255,255,.3); }

.seg { display: flex; background: #eef1f5; border-radius: 10px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; border: none; background: transparent; border-radius: 8px;
  padding: 8px; font-size: 13px; font-weight: 500; color: var(--muted); cursor: pointer;
}
.seg button.active { background: var(--surface); color: var(--primary); box-shadow: 0 1px 4px rgba(0,0,0,.08); }

/* ---------- checklist ---------- */
.checklist-item {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 9px 0; border-bottom: 1px dashed var(--border);
}
.checklist-item:last-child { border-bottom: none; }
.checklist-item input[type="checkbox"] { width: 20px; height: 20px; margin-top: 2px; accent-color: var(--primary); }
.checklist-item .text { flex: 1; }
.checklist-item.done .text { text-decoration: line-through; color: var(--muted); }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.bubble {
  max-width: 85%;
  padding: 9px 12px;
  border-radius: 14px;
  font-size: 14px;
  background: #eef1f5;
  align-self: flex-start;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble.mine { background: var(--primary); color: #fff; align-self: flex-end; }
.bubble .bubble-meta { font-size: 11px; opacity: .65; margin-top: 3px; }

/* ---------- objects tree ---------- */
.tree { list-style: none; }
.tree ul { list-style: none; padding-left: 16px; }
.tree li { padding: 6px 0; position: relative; }
.tree li::before { content: "▸"; color: var(--muted); margin-right: 6px; }

/* ---------- rating table ---------- */
.lb-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.lb-pos { width: 26px; text-align: center; font-weight: 700; color: var(--muted); }
.lb-name { flex: 1; }
.lb-score { font-weight: 700; color: var(--primary); }

/* ---------- modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(10, 18, 30, .45);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  width: 100%;
  max-width: 640px;
  max-height: 88dvh;
  border-radius: 18px 18px 0 0;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 16px; }
.modal-body { padding: 16px; overflow-y: auto; }
.modal-wide .modal { border-radius: 12px; margin: 24px auto; max-height: 92dvh; max-width: 560px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: calc(var(--tabbar-h) + 18px + var(--safe-bottom));
  left: 50%; transform: translateX(-50%);
  background: #1c2430; color: #fff;
  padding: 10px 18px; border-radius: 24px;
  font-size: 14px; z-index: 60;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
  transition: opacity .2s;
}
.toast.hidden { opacity: 0; pointer-events: none; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* ---------- skeleton ---------- */
.skeleton { background: linear-gradient(90deg, #eef1f5 25%, #f7f9fc 50%, #eef1f5 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 10px; height: 84px; margin-bottom: 12px; }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

.empty { text-align: center; color: var(--muted); padding: 48px 20px; }
.empty .empty-emoji { font-size: 42px; display: block; margin-bottom: 10px; }

@media (min-width: 641px) {
  .modal-overlay { align-items: center; }
  .modal { border-radius: 18px; margin: 0 16px; }
}