/* ── Reset & base ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       var(--tg-theme-bg-color, #1a1a2e);
  --bg2:      var(--tg-theme-secondary-bg-color, #16213e);
  --text:     var(--tg-theme-text-color, #e0e0e0);
  --hint:     var(--tg-theme-hint-color, #8899aa);
  --accent:   var(--tg-theme-button-color, #7b68ee);
  --accent-t: var(--tg-theme-button-text-color, #fff);
  --link:     var(--tg-theme-link-color, #a78bfa);
  --border:   rgba(255,255,255,0.08);

  --header-h: 52px;
  --tabs-h:   44px;
  --chat-h:   360px;
  --input-h:  52px;
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

/* ── Layout ────────────────────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
}

/* ── Header ────────────────────────────────────────────────────────────────── */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  height: var(--header-h);
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

#headerTitle {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: .3px;
}

#chatToggleBtn {
  background: var(--accent);
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 17px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity .15s;
}
#chatToggleBtn:active { opacity: .7; }

/* ── Breadcrumbs ────────────────────────────────────────────────────────────── */
#breadcrumbs {
  display: flex;
  align-items: center;
  padding: 0 10px;
  height: var(--tabs-h);
  flex-shrink: 0;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  white-space: nowrap;
}
#breadcrumbs::-webkit-scrollbar { display: none; }

.bc-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--link);
  font-size: 13px;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
}
.bc-btn:hover { background: var(--border); }
.bc-btn.bc-current { color: var(--text); cursor: default; font-weight: 600; }
.bc-sep { color: var(--hint); font-size: 14px; flex-shrink: 0; padding: 0 1px; }

/* ── Gallery ───────────────────────────────────────────────────────────────── */
#gallery {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#photoGrid { /* контейнер — без своей сетки */ }

/* Сетка папок */
.folders-section {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  padding: 10px;
  padding-bottom: 4px;
}

/* Карточка папки */
.folder-card {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  background: var(--bg2);
  border: 1px solid var(--border);
  aspect-ratio: 3/4;
  transition: transform .15s, box-shadow .15s;
}
.folder-card:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(0,0,0,.4); }
.folder-card:active { transform: scale(.97); }

.fc-preview {
  width: 100%; height: 100%;
  object-fit: cover; display: block;
}
.fc-placeholder {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  font-size: 52px; opacity: .3;
}

.fc-footer {
  position: absolute; bottom: 0; left: 0; right: 0;
  padding: 28px 10px 8px;
  background: linear-gradient(transparent, rgba(0,0,0,.85));
  display: flex; flex-direction: column; gap: 2px;
}
.fc-name {
  font-size: 13px; font-weight: 600; color: #fff;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  text-shadow: 0 1px 3px rgba(0,0,0,.6);
}
.fc-count { font-size: 11px; color: rgba(255,255,255,.65); }

.fc-menu {
  position: absolute; top: 6px; right: 6px;
  background: rgba(0,0,0,.5); backdrop-filter: blur(4px);
  border: none; color: #fff;
  width: 28px; height: 28px; border-radius: 50%;
  font-size: 15px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .15s; z-index: 2;
}
.folder-card:hover .fc-menu,
.folder-card:focus-within .fc-menu { opacity: 1; }
@media (hover: none) { .fc-menu { opacity: 1; } }

/* Разделитель и сетка фото внутри папки */
.section-label {
  padding: 6px 12px 4px;
  font-size: 11px; color: var(--hint);
  text-transform: uppercase; letter-spacing: .6px;
}
.photos-section {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 4px;
  padding: 0 10px 10px;
}

/* Скелетон папки */
.folder-skeleton {
  aspect-ratio: 3/4;
  border-radius: 12px;
}

.photo-item {
  position: relative;
  aspect-ratio: 3/4;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  background: var(--bg2);
  border: 1px solid var(--border);
  transition: transform .15s;
}
.photo-item:active { transform: scale(.97); }

.photo-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Кнопка ⋮ на фото */
.photo-actions {
  position: absolute;
  top: 4px;
  right: 4px;
  opacity: 0;
  transition: opacity .2s;
}
.photo-item:hover .photo-actions { opacity: 1; }
.photo-item:focus-within .photo-actions { opacity: 1; }

.photo-actions button {
  background: rgba(0,0,0,.55);
  border: none;
  color: #fff;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}
.photo-actions button:active { opacity: .7; }


/* Loading skeleton */
.skeleton {
  aspect-ratio: 3/4;
  border-radius: 10px;
  background: linear-gradient(90deg, var(--bg2) 25%, var(--border) 50%, var(--bg2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

#emptyState {
  text-align: center;
  color: var(--hint);
  padding: 40px 20px;
  line-height: 1.6;
}

/* ── Chat Panel ────────────────────────────────────────────────────────────── */
#chatPanel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--chat-h);
  background: var(--bg2);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,.4);
  transition: transform .25s cubic-bezier(.4,0,.2,1);
  z-index: 20;
}

#chatPanel.chat-hidden {
  transform: translateY(100%);
}

#chatHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 10px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#chatHeader button {
  background: transparent;
  border: none;
  color: var(--hint);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
  padding: 2px 4px;
}

#chatMessages {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.msg {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 13.5px;
  line-height: 1.45;
  word-break: break-word;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-t);
  border-radius: 14px 14px 4px 14px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--bg);
  color: var(--text);
  border-radius: 14px 14px 14px 4px;
  border: 1px solid var(--border);
}
.msg.status {
  align-self: center;
  background: transparent;
  color: var(--hint);
  font-size: 12px;
  font-style: italic;
  padding: 4px 8px;
}

/* Photo row in chat */
.chat-photos {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 4px 0;
  scrollbar-width: none;
}
.chat-photos::-webkit-scrollbar { display: none; }
.chat-photo {
  flex-shrink: 0;
  width: 100px;
  height: 133px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
  border: 1px solid var(--border);
}

/* Typing dots */
.typing-dots {
  display: flex;
  gap: 4px;
  padding: 6px 0;
}
.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--hint);
  animation: bounce .9s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: .15s; }
.typing-dots span:nth-child(3) { animation-delay: .30s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); }
  40%           { transform: translateY(-6px); }
}

#chatInputRow {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#msgInput {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 9px 14px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color .15s;
}
#msgInput:focus { border-color: var(--accent); }
#msgInput::placeholder { color: var(--hint); }

#sendBtn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: var(--accent-t);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity .15s;
}
#sendBtn:disabled { opacity: .4; }
#sendBtn:active:not(:disabled) { opacity: .7; }

/* ── Lightbox ───────────────────────────────────────────────────────────────── */
#lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.9);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
#lightbox.open { display: flex; }

#lightboxInner {
  position: relative;
  max-width: 95vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#lightboxImg {
  max-width: 95vw;
  max-height: 82vh;
  object-fit: contain;
  border-radius: 10px;
}

#lightboxMeta {
  color: rgba(255,255,255,.6);
  font-size: 12px;
  text-align: center;
}

#lightboxClose {
  position: absolute;
  top: -14px;
  right: -14px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255,255,255,.15);
  border: none;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#lightboxActions {
  display: flex;
  gap: 8px;
}
#lightboxActions button {
  background: rgba(255,60,60,.7);
  border: none;
  color: #fff;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  cursor: pointer;
}
#lightboxActions button:active { opacity: .7; }

/* ── Header refresh btn ─────────────────────────────────────────────────────── */
#refreshBtn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 34px;
  height: 34px;
  color: var(--hint);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
#refreshBtn:active { opacity: .6; }

/* ── Context menu ────────────────────────────────────────────────────────────── */
#ctxMenu {
  display: none;
  position: fixed;
  z-index: 200;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 0;
  min-width: 180px;
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
}
.ctx-title {
  padding: 6px 16px 8px;
  font-size: 12px;
  color: var(--hint);
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#ctxMenu button {
  display: block;
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 9px 16px;
  font-size: 14px;
  cursor: pointer;
}
#ctxMenu button:hover { background: var(--border); }
#ctxMenu button.danger { color: #ff6b6b; }

/* ── Toast ──────────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(30,30,50,.95);
  color: #fff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 13px;
  opacity: 0;
  transition: opacity .25s, transform .25s;
  pointer-events: none;
  z-index: 300;
  white-space: nowrap;
  border: 1px solid var(--border);
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast.toast-error { background: rgba(180,30,30,.95); }

/* ── Backend switcher button ─────────────────────────────────────────────────── */
#backendBtn {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0 10px;
  height: 34px;
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
#backendBtn:active { opacity: .7; }

.bdot {
  width: 7px; height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}
.bdot-on  { background: #4caf50; box-shadow: 0 0 5px #4caf5088; }
.bdot-off { background: var(--hint); }

/* ── Backend panel ────────────────────────────────────────────────────────────── */
#backendPanel {
  position: absolute;
  top: calc(var(--header-h) + 6px);
  right: 10px;
  z-index: 250;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px 0;
  min-width: 200px;
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
  transition: opacity .15s, transform .15s;
}
#backendPanel.bp-hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(-6px);
}

.bp-title {
  padding: 6px 14px 8px;
  font-size: 11px;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: .6px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.bp-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  border-radius: 8px;
  margin: 0 4px;
  transition: background .1s;
}
.bp-option:hover { background: var(--border); }
.bp-option.bp-active { background: rgba(123,104,238,.15); }

.bp-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--hint);
}
.bp-dot.online  { background: #4caf50; box-shadow: 0 0 5px #4caf5088; }
.bp-dot.offline { background: #f44336; }

.bp-name { flex: 1; font-size: 14px; }

.bp-check { font-size: 14px; color: var(--accent); width: 16px; text-align: center; }
