:root {
  color-scheme: dark;
  --bg: radial-gradient(circle at top left, rgba(33, 128, 141, 0.42), rgba(11, 19, 25, 0.96));
  --surface: rgba(22, 32, 39, 0.86);
  --surface-alt: rgba(27, 40, 48, 0.88);
  --border: rgba(142, 230, 230, 0.2);
  --border-strong: rgba(142, 230, 230, 0.45);
  --accent: #4fd1c5;
  --accent-strong: #38b2ac;
  --text: #f1fbfb;
  --muted: rgba(238, 249, 249, 0.68);
  --danger: #f56565;
  --radius: 18px;
  --shadow: 0 18px 44px rgba(0, 0, 0, 0.34);
  --blur: blur(22px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --tg-viewport-h: 100vh;
  --content-gap: clamp(16px, 4vw, 28px);
  font-family: "Geist", "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@supports (height: 100dvh) {
  :root {
    --tg-viewport-h: 100dvh;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Скрытие скроллбара */
* {
  scrollbar-width: none;
  -ms-overflow-style: none;
}

*::-webkit-scrollbar {
  display: none;
}

html,
body {
  height: 100%;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  min-height: var(--tg-viewport-h);
  height: var(--tg-viewport-h);
  width: 100%;
  display: flex;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
}

.app-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
  min-height: var(--tg-viewport-h);
  height: var(--tg-viewport-h);
  width: 100%;
  padding: 0;
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: clamp(16px, 3vw, 32px);
  padding: clamp(55px, 7vw, 85px) clamp(18px, 4vw, 32px) 0;
  padding-top: calc(clamp(55px, 7vw, 85px) + var(--safe-top));
  margin: 0;
  border-radius: 0;
  background: rgba(12, 22, 28, 0.65);
  border: none;
  border-bottom: 1px solid rgba(79, 209, 197, 0.25);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(28px);
  -webkit-backdrop-filter: blur(28px);
  isolation: isolate;
  width: 100%;
}

.topbar__brand {
  display: flex;
  align-items: flex-end;
  gap: 16px;
  flex: 1;
  min-width: 0;
}

.topbar__badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(15, 23, 42, 0.95), rgba(15, 118, 110, 0.9));
  border: 1px solid rgba(148, 233, 222, 0.55);
  color: #e6fffa;
  font-weight: 600;
  font-size: 22px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  flex-shrink: 0;
  margin-bottom: -10px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
}

.topbar__text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  margin-bottom: -12px;
  position: relative;
}

.topbar__title-btn {
  display: none;
  flex-direction: column;
  gap: 2px;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-align: left;
  transition: opacity 0.2s ease;
}

.topbar__title-btn:hover {
  opacity: 0.8;
}

.topbar__text-static {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.topbar__title {
  display: block;
  font-size: clamp(18px, 2vw, 22px);
  letter-spacing: 0.02em;
  margin: 0;
  font-weight: 700;
  color: var(--accent);
}

.topbar__subtitle {
  display: block;
  font-size: 12px;
  color: var(--muted);
  font-weight: 400;
}

.topbar__user {
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: right;
  color: var(--muted);
  flex: 1;
  min-width: 120px;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__user strong {
  color: var(--text);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__user span {
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: clamp(12px, 3vw, 28px);
  min-width: 220px;
}

.topbar__profile-btn {
  min-height: 42px;
  padding: 10px 18px;
  font-size: 14px;
  border-radius: 14px;
  border: 1px solid rgba(79, 209, 197, 0.4);
  background: rgba(79, 209, 197, 0.12);
  color: var(--accent);
}

.profile-header .btn {
  min-height: 40px;
}

.profile-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 16px;
  border-radius: 14px;
  background: rgba(15, 23, 30, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.profile-summary__item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.profile-summary__value {
  margin: 0;
  font-weight: 600;
  font-size: 14px;
  word-break: break-word;
}

.profile-logout {
  align-self: flex-start;
}

.app-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  /* Паддинги только для расположения контента, фон берётся из body */
  padding: 0 clamp(12px, 4vw, 32px);
  padding-bottom: var(--safe-bottom);
  background: transparent;
  overflow: visible;
}

.view {
  display: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 4px;
  animation: fade 0.35s ease forwards;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.view::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

.view--active {
  display: flex;
  flex-direction: column;
  gap: var(--content-gap);
}

.card {
  background: transparent;
  border-radius: 0;
  box-shadow: none;
  padding: clamp(18px, 4vw, 26px);
  min-width: 0;
  overflow: visible;
}

.auth-card h2 {
  text-align: center;
  margin-bottom: 8px;
}

.auth-card > p {
  text-align: center;
}

.auth-card {
  position: relative;
}

/* Формы авторизации: статический поток, плавное появление */
.auth-card form {
  width: 100%;
  display: none;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 1s ease-out, transform 1s ease-out;
}

.auth-card form.auth-form--active {
  display: flex;
  flex-direction: column;
  gap: 14px;
  opacity: 1;
  transform: translateY(0);
}

/* Контейнер форм: фиксированная высота корректируется JS без анимации */
/* Обертка без принудительной высоты */
.auth-card .auth-forms-wrapper {
  position: relative;
}

.card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
  min-width: 0;
  overflow: hidden;
}

.card__header--collapsible h2 {
  font-size: 22px;
}

.card__body {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
  overflow: visible;
}

.grid {
  display: grid;
  gap: var(--content-gap);
  min-width: 0;
}

@media (min-width: 960px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.stack--tight {
  gap: 12px;
}

.split {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  min-width: 0;
}

.split > * {
  flex: 1;
  min-width: 140px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.field span {
  font-size: 13px;
  color: var(--muted);
}

input,
textarea,
select {
  font: inherit;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  padding: 12px 14px;
  color: var(--text);
  transition: border 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
  min-width: 0;
  max-width: 100%;
  width: 100%;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--border-strong);
  box-shadow: 0 0 0 2px rgba(79, 209, 197, 0.25);
  transform: translateY(-1px);
}

textarea {
  resize: vertical;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 18px;
  border-radius: 14px;
  border: 1px solid transparent;
  font-weight: 600;
  cursor: pointer;
  min-height: 48px;
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
}

.btn--xs {
  min-height: 32px;
  padding: 6px 10px;
  font-size: 11px;
  border-radius: 999px;
}

.btn:active {
  transform: translateY(1px);
}

.btn--primary {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-strong) 100%);
  color: #0b1419;
  box-shadow: 0 0 0 0 rgba(79, 209, 197, 0.4), 0 8px 20px rgba(79, 209, 197, 0.26);
  transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.btn--primary:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(79, 209, 197, 0.35), 0 8px 20px rgba(79, 209, 197, 0.26);
}

.btn--secondary {
  border-color: var(--border-strong);
  background: rgba(79, 209, 197, 0.12);
  color: var(--text);
}

.btn--ghost {
  border: none;
  background: transparent;
  color: var(--muted);
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(79, 209, 197, 0.18);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
}

.item {
  padding: 18px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(15, 23, 30, 0.78);
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  overflow: hidden;
}

.item header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  overflow: hidden;
}

.item h3 {
  margin: 0;
  font-size: 16px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item__footer {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  overflow: visible;
}

.meta {
  display: grid;
  gap: 8px;
  margin: 0;
  min-width: 0;
}

.meta div {
  display: grid;
  gap: 2px;
  min-width: 0;
  overflow: hidden;
}

.meta dt {
  color: var(--muted);
  font-size: 12px;
}

.meta dd {
  margin: 0;
}

.muted {
  color: var(--muted);
  font-size: 14px;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Smooth collapse/expand animation */
.collapsible {
  overflow: visible;
  max-height: 9999px;
  opacity: 1;
  transition: max-height 0.24s ease, opacity 0.24s ease;
}

.collapsible.collapsible--collapsed {
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
}

.toggle-arrow {
  border: none;
  background: none;
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 4px;
  font-size: 18px;
  line-height: 1;
  transform: rotate(90deg);
  transition: transform 0.2s ease;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.toggle-arrow::before {
  content: '\203A';
  display: block;
}

.toggle-arrow.toggle-arrow--collapsed {
  transform: rotate(0deg);
}

.toggle-arrow:focus {
  outline: none;
}

.toggle-arrow:focus-visible {
  outline: 2px solid rgba(79, 209, 197, 0.65);
  outline-offset: 2px;
}

.estimate-table {
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 18px;
  background: rgba(6, 12, 16, 0.55);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.estimate-row {
  display: grid;
  grid-template-columns: 2.2fr 0.8fr 0.8fr 0.8fr 1fr auto;
  gap: 0;
  align-items: stretch;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  min-height: 52px;
  min-width: min-content;
}

.estimate-table--compact {
  margin-top: 8px;
}

.estimate-row--readonly {
  grid-template-columns: 2.2fr 0.8fr 0.8fr 0.8fr 1fr;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.estimate-row--readonly .estimate-cell {
  padding: 8px 10px;
  font-size: 12px;
}

.estimate-row:last-child {
  border-bottom: 0;
}

.estimate-row__input {
  border-radius: 0;
  border: none;
  border-right: 1px solid rgba(255, 255, 255, 0.05);
}

.estimate-row__input:last-of-type {
  border-right: none;
}

.estimate-row__total {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  border-right: 1px solid rgba(255, 255, 255, 0.05);
}

.estimate-row__remove {
  border: none;
  background: transparent;
  color: var(--danger);
  font-size: 20px;
  cursor: pointer;
  padding: 0 12px;
}

.tag {
  padding: 4px 8px;
  border-radius: 8px;
  background: rgba(79, 209, 197, 0.14);
  color: var(--accent);
  font-size: 12px;
}

.status-chip {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.status-chip[data-status="submitted"] {
  background: rgba(245, 158, 11, 0.2);
  color: #f6ad55;
}

.status-chip[data-status="draft"] {
  background: rgba(66, 153, 225, 0.22);
  color: #90cdf4;
}

.status-chip[data-status="approved"] {
  background: rgba(72, 187, 120, 0.25);
  color: #9ae6b4;
}

.status-chip[data-status="rejected"] {
  background: rgba(245, 101, 101, 0.22);
  color: #feb2b2;
}

.toast {
  position: fixed;
  bottom: calc(16px + var(--safe-bottom));
  right: 16px;
  left: 16px;
  padding: 14px 18px;
  border-radius: 16px;
  background: rgba(12, 20, 26, 0.9);
  border: 1px solid rgba(79, 209, 197, 0.28);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.45);
  display: none;
}

.toast--visible {
  display: block;
}

@keyframes fade {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .topbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 18px;
    padding: clamp(45px, 9vw, 68px) clamp(16px, 6vw, 24px) clamp(6px, 1.5vw, 10px);
    padding-top: calc(clamp(45px, 9vw, 68px) + var(--safe-top));
    border-radius: 0;
  }

  .topbar__actions {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    min-width: auto;
  }

  .topbar__profile-btn {
    width: 100%;
  }

  .topbar__user {
    text-align: left;
    max-width: 100%;
  }

  .topbar__brand {
    width: 100%;
  }

  .profile-summary {
    padding: 16px;
  }

  .profile-logout {
    width: 100%;
  }

  .topbar__subtitle {
    font-size: 13px;
  }

  .app-main {
    padding-left: clamp(10px, 4vw, 18px);
    padding-right: clamp(10px, 4vw, 18px);
  }

  .view--active {
    gap: 18px;
  }

  .card {
    padding: 18px;
    border-radius: 16px;
  }

  .btn {
    width: 100%;
  }

  .item {
    padding: 14px;
  }

  .item h3 {
    font-size: 14px;
  }

  .item__footer {
    flex-direction: column;
    align-items: flex-start;
  }

  .estimate-table {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .estimate-row {
    grid-template-columns: 1.8fr 0.7fr 0.7fr 0.7fr 0.9fr 40px;
    font-size: 12px;
    min-width: min-content;
  }

  .estimate-row__input {
    padding: 8px 10px;
    font-size: 12px;
  }

  .toast {
    right: 12px;
    left: 12px;
  }

  .grid {
    grid-template-columns: 1fr;
  }

  .meta {
    font-size: 13px;
  }

  .meta dt {
    font-size: 11px;
  }

  .meta dd {
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .topbar__badge {
    width: 42px;
    height: 42px;
    font-size: 18px;
  }

  .topbar__title {
    font-size: 20px;
  }

  .stack {
    gap: 14px;
  }

  .field span {
    font-size: 12px;
  }

  .card {
    padding: 14px;
  }

  .card__header {
    margin-bottom: 12px;
  }

  .card h2 {
    font-size: 16px;
    margin: 0;
  }

  .card__header--collapsible h2 {
    font-size: 18px;
  }

  .estimate-row {
    min-height: 44px;
    grid-template-columns: 1.5fr 0.6fr 0.6fr 0.6fr 0.8fr 36px;
  }

  .estimate-row__input {
    padding: 6px 8px;
    font-size: 11px;
  }

  .estimate-row__total {
    font-size: 11px;
  }

  .estimate-row__remove {
    padding: 0 8px;
    font-size: 18px;
  }

  .item {
    padding: 12px;
    gap: 8px;
  }

  .item header {
    gap: 8px;
  }

  .item h3 {
    font-size: 13px;
  }

  .item p {
    font-size: 12px;
  }

  .badge {
    font-size: 11px;
    padding: 2px 8px;
  }

  .btn {
    padding: 12px 14px;
    font-size: 13px;
  }

  input,
  textarea,
  select {
    padding: 10px 12px;
    font-size: 14px;
  }

  .muted {
    font-size: 13px;
  }

  .meta {
    font-size: 12px;
  }
}
