:root {
  --tg-bg: #0e1621;
  --tg-surface: #17212b;
  --tg-surface-2: #212d3b;
  --tg-elevated: #2b5278;
  --tg-accent: #5ea5f7;
  --tg-accent-strong: #3390ec;
  --tg-text: #ffffff;
  --tg-text-dim: #8a99a8;
  --tg-divider: rgba(255, 255, 255, 0.08);
  --sheet-radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: 64px;
  --nav-h: 72px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
}

/* Lock background scroll while the sheet is open. */
body.picker-open {
  overflow: hidden;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

/* ---------------- Host demo screen ---------------- */
.host {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh;
}

.host__bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  background: var(--tg-surface);
  border-bottom: 1px solid var(--tg-divider);
}

.host__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #5ea5f7, #2b5278);
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 14px;
}

.host__title {
  display: flex;
  flex-direction: column;
  line-height: 1.3;
}

.host__title span {
  font-size: 12px;
  color: var(--tg-text-dim);
}

.host__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background:
    radial-gradient(circle at 20% 10%, rgba(94, 165, 247, 0.06), transparent 40%),
    var(--tg-bg);
}

.host__hint {
  max-width: 280px;
  margin: 24px auto;
  padding: 12px 16px;
  background: var(--tg-surface-2);
  border-radius: 12px;
  color: var(--tg-text-dim);
  font-size: 14px;
  text-align: center;
}

.host__compose {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px calc(8px + var(--safe-bottom));
  background: var(--tg-surface);
  border-top: 1px solid var(--tg-divider);
}

.host__attach,
.host__send {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--tg-text-dim);
  display: grid;
  place-items: center;
}

.host__attach:active {
  background: var(--tg-surface-2);
}

.host__send {
  background: var(--tg-accent-strong);
  color: #fff;
}

.host__send:disabled {
  opacity: 0.4;
}

.host__field {
  flex: 1;
  padding: 11px 14px;
  background: var(--tg-surface-2);
  border-radius: 18px;
  color: var(--tg-text-dim);
  font-size: 15px;
}

/* ---------------- Backdrop + sheet ---------------- */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  transition: opacity 200ms ease-out;
  z-index: 40;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.backdrop.is-open {
  opacity: 1;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  max-height: 92dvh;
  max-height: 92vh;
  background: var(--tg-surface);
  border-top-left-radius: var(--sheet-radius);
  border-top-right-radius: var(--sheet-radius);
  transform: translateY(100%);
  transition: transform 220ms ease-out;
  will-change: transform;
  overflow: hidden;
}

.sheet.is-open {
  transform: translateY(0);
}

.sheet__handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.25);
  margin: 8px auto 0;
  flex: none;
}

.sheet__header {
  position: relative;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 8px;
  flex: none;
}

.sheet__close {
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--tg-text);
  display: grid;
  place-items: center;
}

.sheet__close:active {
  background: var(--tg-surface-2);
}

.sheet__title {
  font-size: 17px;
  font-weight: 600;
  text-align: center;
}

.sheet__subtitle {
  display: block;
  font-size: 12px;
  font-weight: 400;
  color: var(--tg-text-dim);
}

/* ---------------- Scrollable grid ---------------- */
.sheet__scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 8px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1px;
}

.tile {
  position: relative;
  aspect-ratio: 1 / 1;
  background: var(--tg-surface-2);
  overflow: hidden;
  border: none;
  padding: 0;
  width: 100%;
  display: block;
}

.tile img,
.tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.tile__fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--tg-text-dim);
  gap: 4px;
  font-size: 11px;
  padding: 8px;
  text-align: center;
}

/* Action tiles (camera / choose) shown before selection */
.tile--action {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--tg-accent);
  background: var(--tg-surface-2);
}

.tile--action span {
  font-size: 13px;
  color: var(--tg-text-dim);
}

.tile--action:active {
  background: #283442;
}

/* Selection overlay + circle */
.tile.is-selected::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 3px solid var(--tg-accent-strong);
  pointer-events: none;
}

.tile.is-selected img,
.tile.is-selected video {
  transform: scale(0.86);
  transition: transform 120ms ease-out;
}

.tile__check {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.9);
  background: rgba(0, 0, 0, 0.25);
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
}

.tile.is-selected .tile__check {
  background: var(--tg-accent-strong);
  border-color: var(--tg-accent-strong);
}

.tile__badge {
  position: absolute;
  left: 6px;
  bottom: 6px;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.55);
  font-size: 11px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.tile__remove {
  position: absolute;
  left: 6px;
  top: 6px;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 16px;
  line-height: 1;
}

/* Empty-state message */
.empty {
  padding: 28px 16px 8px;
  text-align: center;
  color: var(--tg-text-dim);
  font-size: 14px;
}

/* ---------------- Bottom nav ---------------- */
.sheet__nav {
  flex: none;
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: var(--nav-h);
  padding-bottom: var(--safe-bottom);
  background: var(--tg-surface);
  border-top: 1px solid var(--tg-divider);
}

.nav-item {
  border: none;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--tg-text-dim);
  font-size: 11px;
  min-width: 44px;
  padding: 6px 4px;
}

.nav-item[data-active="true"] {
  color: var(--tg-accent);
}

.nav-item svg {
  width: 24px;
  height: 24px;
}

/* ---------------- Send bar ---------------- */
.sendbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px calc(10px + var(--safe-bottom));
  background: var(--tg-surface);
  border-top: 1px solid var(--tg-divider);
}

.sendbar__info {
  flex: 1;
  font-size: 14px;
  color: var(--tg-text-dim);
}

.sendbar__add {
  border: none;
  background: var(--tg-surface-2);
  color: var(--tg-accent);
  padding: 10px 16px;
  border-radius: 20px;
  font-size: 14px;
  min-height: 44px;
}

.sendbar__send {
  border: none;
  background: var(--tg-accent-strong);
  color: #fff;
  padding: 10px 22px;
  border-radius: 20px;
  font-size: 15px;
  font-weight: 600;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.sendbar__send:disabled {
  opacity: 0.45;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Toast for errors */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: #c0392b;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  max-width: 88%;
  text-align: center;
  opacity: 0;
  transition: opacity 200ms ease, transform 200ms ease;
  z-index: 60;
  pointer-events: none;
}

.toast.is-open {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .sheet,
  .backdrop,
  .toast {
    transition: none;
  }
}
