/* ============================================================================
   CHRONICLE FIXES AND ENHANCEMENTS
   Исправления для хроники и улучшения загрузки медиа
   ============================================================================ */

/* Стили для хроники */
.chronicle-closed {
  position: relative;
  display: grid;
  min-height: 55vh;
  place-items: center;
  isolation: isolate;
  color: var(--text-secondary);
  text-align: center;
}

.chronicle-closed::before {
  position: absolute;
  z-index: -1;
  top: 50%;
  right: 0;
  left: 0;
  height: 1px;
  background: var(--line-strong);
  content: "";
}

.chronicle-closed span {
  max-width: min(100% - 32px, 620px);
  padding: 12px 24px;
  background: var(--bg);
  color: var(--text);
  font-size: 18px;
  line-height: 1.5;
}

.chronicle-chapter {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: var(--transition-slow);
  cursor: pointer;
}

.chronicle-chapter:hover {
  transform: translateY(-4px);
  border-color: var(--line-strong);
  box-shadow: var(--shadow-lg);
}

.chronicle-cover {
  width: 100%;
  height: 280px;
  background: var(--bg-elevated);
  position: relative;
  overflow: hidden;
}

.chronicle-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: var(--transition-slow);
}

.chronicle-chapter:hover .chronicle-cover img {
  transform: scale(1.08);
}

.chronicle-cover-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--bg-elevated) 0%, var(--panel) 100%);
  color: var(--muted);
  font-size: 48px;
}

.chronicle-body {
  padding: 24px;
}

.chronicle-status {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 12px;
}

.chronicle-status.published {
  background: rgba(54, 199, 107, 0.15);
  color: #36c76b;
}

.chronicle-status.draft {
  background: rgba(251, 191, 36, 0.15);
  color: #fbbf24;
}

.chronicle-status.hidden {
  background: rgba(148, 163, 184, 0.15);
  color: #94a3b8;
}

.chronicle-chapter h3 {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 12px;
  line-height: 1.3;
}

.chronicle-chapter p {
  color: var(--text-secondary);
  line-height: 1.7;
  margin: 0;
}

.chronicle-progress {
  margin: 32px 0;
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
}

.chronicle-progress h3 {
  margin: 0 0 16px;
  font-size: 18px;
}

.progress-bar {
  height: 12px;
  background: var(--bg-elevated);
  border-radius: 999px;
  overflow: hidden;
  position: relative;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--red) 0%, var(--red2) 100%);
  border-radius: 999px;
  transition: width 0.5s ease;
  position: relative;
  overflow: hidden;
}

.progress-bar-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(255, 255, 255, 0.3) 50%,
    transparent 100%
  );
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.progress-stats {
  display: flex;
  justify-content: space-between;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

/* Модальное окно главы */
.chapter-modal-content {
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 8px;
}

.chapter-modal-content::-webkit-scrollbar {
  width: 8px;
}

.chapter-modal-content::-webkit-scrollbar-track {
  background: var(--bg-elevated);
  border-radius: 4px;
}

.chapter-modal-content::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 4px;
}

.chapter-modal-content::-webkit-scrollbar-thumb:hover {
  background: var(--line-strong);
}

.chapter-modal-media {
  margin: 24px 0;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--bg-elevated);
}

.chapter-modal-media img,
.chapter-modal-media video {
  width: 100%;
  height: auto;
  display: block;
}

.chapter-tasks {
  margin-top: 32px;
}

.chapter-tasks h3 {
  font-size: 20px;
  margin: 0 0 16px;
}

.chronicle-task {
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  transition: var(--transition);
}

.chronicle-task:hover {
  border-color: var(--line-strong);
}

.chronicle-task h4 {
  margin: 0 0 12px;
  font-size: 16px;
  font-weight: 700;
}

.chronicle-task p {
  color: var(--text-secondary);
  margin: 0 0 16px;
  line-height: 1.6;
}

.chronicle-task.solved {
  border-color: rgba(54, 199, 107, 0.3);
  background: rgba(54, 199, 107, 0.05);
}

.chronicle-task.solved::before {
  content: '✓';
  display: inline-block;
  width: 24px;
  height: 24px;
  background: #36c76b;
  color: white;
  border-radius: 50%;
  text-align: center;
  line-height: 24px;
  margin-right: 8px;
  font-weight: 700;
}

.task-answer-form {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.task-answer-form input {
  flex: 1;
}

/* Рейтинг участников */
.chronicle-rating {
  margin-top: 48px;
}

.chronicle-rating h3 {
  font-size: 24px;
  margin: 0 0 24px;
}

.rating-table {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.rating-row {
  display: grid;
  grid-template-columns: 60px 1fr auto auto;
  gap: 16px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  align-items: center;
  transition: var(--transition);
}

.rating-row:last-child {
  border-bottom: none;
}

.rating-row:hover {
  background: var(--panel-hover);
}

.rating-row.header {
  background: var(--bg-elevated);
  font-weight: 700;
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.rating-rank {
  font-size: 18px;
  font-weight: 700;
  color: var(--muted);
  text-align: center;
}

.rating-rank.top-1 {
  color: #fbbf24;
  font-size: 24px;
}

.rating-rank.top-2 {
  color: #94a3b8;
  font-size: 22px;
}

.rating-rank.top-3 {
  color: #cd7f32;
  font-size: 20px;
}

.rating-name {
  font-weight: 600;
}

.rating-score {
  font-family: 'Unbounded', monospace;
  font-weight: 700;
  color: var(--red2);
  font-size: 18px;
}

/* Media fixes */
.post-media img,
.post-media video,
.chronicle-cover img,
.chapter-modal-media img,
.chapter-modal-media video {
  max-width: 100%;
  height: auto;
  display: block;
}

/* Lazy loading placeholder */
img[data-src] {
  background: var(--bg-elevated);
  min-height: 200px;
}

img.loading {
  opacity: 0.5;
}

img.loaded {
  opacity: 1;
  transition: opacity 0.3s ease;
}

/* Error handling for images */
img.error {
  position: relative;
  min-height: 200px;
  background: var(--bg-elevated);
}

img.error::after {
  content: '🖼️ Изображение недоступно';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

/* Upload progress specifically for chronicle */
.chronicle-upload-progress {
  margin: 20px 0;
  padding: 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.chronicle-upload-progress-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.chronicle-upload-progress-bar {
  height: 8px;
  background: var(--line);
  border-radius: 999px;
  overflow: hidden;
}

.chronicle-upload-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--red) 0%, var(--red2) 100%);
  border-radius: 999px;
  transition: width 0.3s ease;
}

/* Responsive fixes */
@media (max-width: 780px) {
  .chronicle-cover {
    height: 200px;
  }

  .rating-row {
    grid-template-columns: 50px 1fr auto;
    gap: 12px;
    padding: 12px 16px;
  }

  .rating-row .rating-score {
    grid-column: 3;
  }

  .rating-row.header {
    display: none;
  }

  .chronicle-task {
    padding: 16px;
  }

  .task-answer-form {
    flex-direction: column;
  }

  .task-answer-form .btn {
    width: 100%;
  }
}

/* Admin chronicle management */
.chronicle-admin-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 24px;
}

.chronicle-admin-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 20px;
  transition: var(--transition);
}

.chronicle-admin-card:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow);
}

.chronicle-admin-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.chronicle-admin-actions .btn {
  flex: 1;
  min-width: 100px;
}

/* Voting system */
.chronicle-vote {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin: 24px 0;
}

.chronicle-vote h3 {
  margin: 0 0 20px;
  font-size: 20px;
}

.vote-options {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.vote-option {
  display: flex;
  align-items: center;
  padding: 16px;
  background: var(--bg-elevated);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  transition: var(--transition);
  position: relative;
  overflow: hidden;
}

.vote-option:hover {
  border-color: var(--line-strong);
  transform: translateX(4px);
}

.vote-option.selected {
  border-color: var(--accent);
  background: rgba(255, 106, 61, 0.05);
}

.vote-option input[type="radio"] {
  margin-right: 12px;
  width: 20px;
  height: 20px;
  cursor: pointer;
}

.vote-option label {
  cursor: pointer;
  margin: 0;
  flex: 1;
  font-weight: 600;
}

.vote-results {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.vote-result-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.vote-result-label {
  min-width: 120px;
  font-size: 14px;
  font-weight: 600;
}

.vote-result-progress {
  flex: 1;
  height: 24px;
  background: var(--bg-elevated);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
}

.vote-result-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--red) 0%, var(--red2) 100%);
  transition: width 0.5s ease;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
  color: white;
  font-weight: 700;
  font-size: 12px;
}

.vote-result-count {
  min-width: 60px;
  text-align: right;
  font-weight: 700;
  color: var(--muted);
}

/* Fragment system */
.chronicle-fragment {
  background: linear-gradient(135deg, rgba(228, 69, 49, 0.1) 0%, rgba(255, 106, 61, 0.05) 100%);
  border: 1px solid rgba(228, 69, 49, 0.3);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin: 24px 0;
  position: relative;
  overflow: hidden;
}

.chronicle-fragment::before {
  content: '📜';
  position: absolute;
  top: 16px;
  right: 16px;
  font-size: 48px;
  opacity: 0.1;
}

.chronicle-fragment h3 {
  margin: 0 0 16px;
  font-size: 18px;
  color: var(--red2);
}

.chronicle-fragment p {
  line-height: 1.8;
  color: var(--text-secondary);
  margin: 0;
  white-space: pre-wrap;
}

/* Community progress */
.chronicle-community {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin: 32px 0;
}

.chronicle-community h3 {
  margin: 0 0 20px;
  font-size: 20px;
}

.community-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
}

.community-stat {
  text-align: center;
  padding: 16px;
  background: var(--bg-elevated);
  border-radius: var(--radius);
}

.community-stat-value {
  display: block;
  font-size: 32px;
  font-weight: 700;
  color: var(--red2);
  font-family: 'Unbounded', monospace;
  margin-bottom: 8px;
}

.community-stat-label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Accessibility improvements */
.chronicle-chapter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
}

.vote-option:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255, 106, 61, 0.1);
}

/* Print styles */
@media print {
  .chronicle-chapter,
  .chronicle-task,
  .chronicle-vote {
    break-inside: avoid;
  }

  .chapter-tasks,
  .chronicle-rating {
    page-break-before: always;
  }
}
