* { box-sizing: border-box; }

/* Тёмная тема — по умолчанию */
:root {
  --ink: #23242e;
  --bg: #0d0d10;
  --paper: #f4efe9;
  --surface: rgba(255, 255, 255, 0.10);   /* фон кнопок-таблеток */
  --surface-press: rgba(255, 255, 255, 0.18);
  --surface-soft: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.25);
  --border-strong: rgba(255, 255, 255, 0.35);
  --border-soft: rgba(255, 255, 255, 0.16);
  --overlay: rgba(10, 8, 9, 0.96);         /* фон окон избранного/фильтра/каталога */
  --muted: rgba(255, 255, 255, 0.55);      /* второстепенный текст */
  --disabled-icon: rgba(255, 255, 255, 0.35);
  color-scheme: dark;
}

/* Светлая тема — чистый, почти белый фон */
:root[data-theme="light"] {
  --bg: #fafafa;
  --paper: #1f2126;
  --surface: rgba(0, 0, 0, 0.05);
  --surface-press: rgba(0, 0, 0, 0.10);
  --surface-soft: rgba(0, 0, 0, 0.04);
  --border: rgba(0, 0, 0, 0.20);
  --border-strong: rgba(0, 0, 0, 0.28);
  --border-soft: rgba(0, 0, 0, 0.10);
  --overlay: rgba(250, 250, 250, 0.975);
  --muted: rgba(0, 0, 0, 0.5);
  --disabled-icon: rgba(0, 0, 0, 0.3);
  color-scheme: light;
}

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--paper);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  overflow: hidden;
}

.app {
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 2vh, 20px);
  padding: 6px 20px;
  overflow: hidden;
}

/* --- Угловые кнопки: карта дня (слева) и QR (справа) --- */
.qr-btn,
.day-btn {
  position: fixed;
  top: calc(6px + env(safe-area-inset-top));
  z-index: 30;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--paper);
  cursor: pointer;
  transition: transform 0.15s ease;
}

.qr-btn { right: calc(10px + env(safe-area-inset-right)); }
.day-btn { left: calc(10px + env(safe-area-inset-left)); }

.qr-btn:active,
.day-btn:active { transform: scale(0.92); }

/* Бейдж «Карта дня» — по центру над картой, читается на любом фото */
.day-badge {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  padding: 5px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  letter-spacing: 0.02em;
  pointer-events: none;
}

.qr-box {
  /* QR всегда на белом — иначе камера не считает в тёмной теме */
  background: #fff;
  padding: 22px;
  border-radius: 18px;
  width: min(78vw, 320px);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.35);
}

.qr-box img {
  display: block;
  width: 100%;
  height: auto;
}

.qr-hint {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
  text-align: center;
  margin: 18px 0 0;
}

/* --- Верхний ряд управления: избранное | стрелки | фильтр --- */
.controls-top {
  width: min(100%, 300px);
  display: flex;
  align-items: stretch;
  gap: 8px;
}

/* общий вид всех кнопок ряда */
.fav-btn,
.hist-nav,
.catalog-btn,
.filter-btn {
  min-width: 0;
  height: 32px;
  padding: 0;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--paper);
  font-weight: 600;
  cursor: pointer;
}

/* избранное и стрелки — компактные по содержимому;
   «Все карты» и «Все цвета» делят оставшуюся ширину поровну */
.fav-btn,
.hist-nav { flex: 0 0 auto; }
.catalog-btn,
.filter-btn { flex: 1 1 0; }

.fav-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0 11px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.fav-btn .fav-heart { color: #e63950; font-size: 14px; }

/* --- История просмотра: назад / вперёд --- */
.hist-nav {
  display: flex;
  align-items: stretch;
  overflow: hidden;        /* половинки не вылезают за скругление */
}

.hist-btn {
  flex: 1 1 0;
  min-width: 0;
  padding: 0 8px;          /* стрелки компактные, но с зоной под палец */
  border: none;
  background: transparent;
  color: var(--paper);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  transition: opacity 0.15s ease, background 0.15s ease;
}

.hist-btn:active:not(:disabled) { background: var(--surface-press); }

.hist-btn:disabled {
  opacity: 0.28;
  cursor: default;
}

.filter-btn,
.catalog-btn {
  font-size: 12px;
  overflow: hidden;
}

/* Отступ задан внутреннему тексту, а не самой кнопке: у <button> отступ
   прибавляется к доле flex поверх border-box и ломает равные ширины. */
.filter-btn span,
.catalog-btn span {
  display: block;
  padding: 0 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Очень узкие экраны (старые маленькие телефоны): ужимаем отступы,
   чтобы «Все карты» и «Все цвета» помещались без многоточия. */
@media (max-width: 359px) {
  .filter-btn span,
  .catalog-btn span { padding: 0 3px; }
  .hist-btn { padding: 0 6px; }
  .fav-btn { padding: 0 9px; }
}

.filter-btn.active {
  background: rgba(230, 57, 80, 0.85);
  border-color: transparent;
}

.filter-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 420px;
}

.filter-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-soft);
  background: var(--surface-soft);
  color: var(--paper);
  font-size: 15px;
  cursor: pointer;
  text-align: left;
  width: 100%;
}

.filter-row.chosen {
  border-color: #e63950;
  background: rgba(230, 57, 80, 0.18);
}

.filter-row .swatches {
  display: flex;
  gap: 3px;
  flex: 0 0 auto;
}

.filter-row .swatches i {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  display: block;
}

.filter-row .name { flex: 1; }

.filter-row .num {
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.like-btn {
  position: absolute;
  /* не top:10 — карта на некоторых экранах поднимается почти к краю,
     и сердечко сталкивалось бы с угловой кнопкой QR */
  top: 30px;
  right: 10px;
  z-index: 5;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.38);
  color: rgba(255, 255, 255, 0.65);
  font-size: 21px;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.18s ease, color 0.18s ease;
}

.like-btn.liked {
  color: #ff4d67;
  transform: scale(1.12);
}

.like-btn:active { transform: scale(0.92); }

/* «Поделиться» — зеркально сердечку, в левом верхнем углу карты */
.share-btn {
  position: absolute;
  top: 30px;   /* вровень с сердечком */
  left: 10px;
  z-index: 5;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.38);
  color: rgba(255, 255, 255, 0.75);
  cursor: pointer;
  transition: transform 0.18s ease, color 0.18s ease;
}

.share-btn:active { transform: scale(0.92); }

.fav-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--overlay);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: calc(54px + env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
  overflow-y: auto;
  animation: fadeIn 0.25s ease;
}

.fav-title {
  color: var(--paper);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 22px;
  font-weight: 600;
  margin: 0 0 16px;
}

.fav-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 12px;
  width: 100%;
  max-width: 520px;
}

.fav-item { position: relative; }

.fav-item img {
  width: 100%;
  aspect-ratio: 70 / 120;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  cursor: pointer;
}

.fav-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}

.fav-empty {
  color: var(--muted);
  font-size: 15px;
  text-align: center;
  line-height: 1.5;
  margin-top: 24px;
}

.card-wrap {
  position: relative;
  perspective: 1600px;
  /* Ширина карты = наименьшее из: ширины экрана, потолка и того,
     что помещается по высоте (оставляем место на подсказку и кнопки).
     Если места впритык — лишнее по краю карты обрежет .stage, кнопки не тронет. */
  width: min(86vw, 360px, calc((100vh - 225px) * 7 / 12));
  aspect-ratio: 70 / 120;
  touch-action: manipulation;
  flex: 0 0 auto;
}

/* 100vh на телефоне не учитывает адресную строку браузера — карта считалась
   выше, чем есть место, и подсказка уезжала под обрез. svh — это высота при
   ВИДИМОЙ адресной строке, то есть гарантированно помещается в любом случае. */
@supports (height: 100svh) {
  .card-wrap {
    width: min(86vw, 360px, calc((100svh - 218px) * 7 / 12));
  }
}

.card {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.7s cubic-bezier(.2,.8,.25,1);
  cursor: pointer;
  border-radius: 18px;
  box-shadow: 0 18px 40px rgba(0,0,0,0.55);
}

.card.flipped {
  transform: rotateY(180deg);
}

.card.drawing {
  animation: shakeCard 0.5s ease;
}

@keyframes shakeCard {
  0%, 100% { transform: rotate(0deg) translateX(0); }
  20% { transform: rotate(-6deg) translateX(-6px); }
  40% { transform: rotate(5deg) translateX(5px); }
  60% { transform: rotate(-4deg) translateX(-4px); }
  80% { transform: rotate(3deg) translateX(3px); }
}

.card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 18px;
  overflow: hidden;
  background: #111;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.card-front.empty {
  background: linear-gradient(160deg, #2a2a33, #16161b);
}

.card-front.empty img {
  display: none;
}

.card-front.empty::after {
  content: "Потрясите\A телефон";
  white-space: pre-line;
  text-align: center;
  color: rgba(255,255,255,0.35);
  font-size: 15px;
  letter-spacing: 0.02em;
}

.card-back {
  transform: rotateY(180deg);
  background-image: url("assets/back-bg.webp");
  background-size: cover;
  background-position: center;
  flex-direction: column;
  padding: 10% 8%;
  text-align: center;
}

.back-logo {
  width: 46%;
  max-width: 140px;
  height: auto;
  margin-bottom: auto;
  margin-top: 4%;
  filter: brightness(0) invert(1) drop-shadow(0 2px 6px rgba(0,0,0,0.45));
}

.phrase {
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
  font-weight: 600;
  font-size: clamp(19px, 6.4vw, 26px);
  line-height: 1.32;
  color: #fdf6f0;
  text-shadow: 0 2px 10px rgba(0,0,0,0.55);
  margin: 0 0 6% 0;
}

.hint {
  font-size: clamp(12px, 3.3vw, 15px);
  opacity: 0.7;
  text-align: center;
  margin: 0;
  min-height: 18px;
  transition: opacity 0.3s;
}

.controls {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 0 0 auto;
  gap: 8px;
  padding: 8px 20px calc(10px + env(safe-area-inset-bottom));
}

.btn {
  width: min(100%, 330px);
  padding: clamp(10px, 1.5vh, 14px) 18px;
  border-radius: 14px;
  border: none;
  font-size: clamp(15px, 4vw, 16px);
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.2s ease;
}

/* Ряд: слева кнопка звука, справа «Тряхнуть колоду» на весь остаток */
.draw-row {
  width: min(100%, 330px);
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.draw-row #shakeBtn {
  flex: 1 1 auto;
  width: auto;
}

.sound-btn,
.theme-btn {
  flex: 0 0 auto;
  width: 50px;
  border-radius: 14px;
  border: 1px solid var(--border-strong);
  background: transparent;
  color: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.15s ease, color 0.15s ease;
}

.sound-btn.muted { color: var(--disabled-icon); }
.sound-btn:active,
.theme-btn:active { transform: scale(0.95); }

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: linear-gradient(135deg, #e63950, #b5203a);
  color: #fff;
}

.btn-secondary {
  background: #fff;
  color: #111;
}

.btn-ghost {
  background: transparent;
  color: var(--paper);
  border: 1px solid var(--border-strong);
}

.btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.hidden {
  display: none !important;
}

/* Подсказка «коснитесь экрана» — появляется, только если браузер отказал
   в вибрации из-за того, что по странице ещё ни разу не касались. */
.touch-hint {
  position: fixed;
  left: 50%;
  bottom: calc(22px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 70;
  max-width: 86vw;
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(230, 57, 80, 0.94);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  animation: hintIn 0.3s ease;
  transition: opacity 0.4s ease;
  pointer-events: none;
}

.touch-hint.fading { opacity: 0; }

@keyframes hintIn {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* Короткое уведомление внизу (напр. «ссылка скопирована») */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(22px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 80;
  max-width: 86vw;
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(30, 30, 36, 0.96);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  animation: hintIn 0.28s ease;
  transition: opacity 0.4s ease;
  pointer-events: none;
}

.toast.fading { opacity: 0; }

/* Галерея фото готовых работ */
.work-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: calc(20px + env(safe-area-inset-top)) 12px calc(18px + env(safe-area-inset-bottom));
  animation: fadeIn 0.25s ease;
  touch-action: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.work-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  overflow: hidden;
  touch-action: none; /* жесты зума обрабатываем сами */
}

.work-img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  animation: fadeIn 0.2s ease;
  transform-origin: center center;
  transition: transform 0.18s ease;
  will-change: transform;
  /* touch-action не наследуется — задаём и самому фото, иначе жест перехватит браузер */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
}

/* во время щипка/перетаскивания — без сглаживания, чтобы шло за пальцем */
.work-img.gesture { transition: none; }

.work-meta {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  flex: 0 0 auto;
}

.work-caption {
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  margin: 0;
}

.work-caption .work-counter {
  color: rgba(255, 255, 255, 0.55);
  font-weight: 400;
  margin-left: 8px;
}

.work-zoomhint {
  color: rgba(255, 255, 255, 0.45);
  font-size: 12px;
  text-align: center;
  margin: -4px 0 0;
}

.work-dots {
  display: flex;
  gap: 9px;
  justify-content: center;
}

.work-dots button {
  width: 9px;
  height: 9px;
  padding: 0;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.3);
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
}

.work-dots button.active {
  background: #fff;
  transform: scale(1.25);
}

.work-close {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  right: 16px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
}

.work-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 30px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}

.work-prev { left: 10px; }
.work-next { right: 10px; }
.work-nav:disabled { opacity: 0.25; cursor: default; }
