/* Twofold Mini App — styles
   Driven entirely by Telegram theme CSS vars. No hardcoded palette.
   Falls back to sane light-mode values when vars are absent (e.g. plain
   browser preview without Telegram.WebApp). */

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --header-bg: var(--tg-theme-header-bg-color, var(--bg));

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 16px;
  --space-4: 24px;
  --space-5: 32px;
  --space-6: 48px;

  --radius: 16px;
  --radius-sm: 10px;

  color-scheme: light dark;
}

/* Fallbacks only apply when Telegram hasn't set the real --tg-theme-* vars
   (e.g. plain-browser ?mock=1 preview). Inside Telegram those vars are
   always present, so the app is driven entirely by them either way. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #1c1c1e);
    --text: var(--tg-theme-text-color, #ffffff);
    --hint: var(--tg-theme-hint-color, #98989e);
    --link: var(--tg-theme-link-color, #6ab7ff);
    --button: var(--tg-theme-button-color, #2481cc);
    --button-text: var(--tg-theme-button-text-color, #ffffff);
    --secondary-bg: var(--tg-theme-secondary-bg-color, #2c2c2e);
    --header-bg: var(--tg-theme-header-bg-color, #1c1c1e);
  }
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  overflow-x: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.serif {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", "Book Antiqua", serif;
}

button, input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

a {
  color: var(--link);
}

/* ---------- App shell ---------- */

#app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-3) var(--space-2);
  gap: var(--space-2);
}

.topbar h1 {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--hint);
  margin: 0;
}

.icon-btn {
  appearance: none;
  background: transparent;
  border: none;
  color: var(--hint);
  padding: var(--space-2);
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  cursor: pointer;
}

.icon-btn:active {
  background: var(--secondary-bg);
}

main {
  flex: 1;
  padding: var(--space-2) var(--space-3) var(--space-6);
}

/* ---------- Bottom nav ---------- */

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--header-bg);
  border-top: 1px solid color-mix(in srgb, var(--hint) 25%, transparent);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  max-width: 640px;
  margin: 0 auto;
}

.tab {
  flex: 1;
  appearance: none;
  background: transparent;
  border: none;
  padding: var(--space-2) 0 calc(var(--space-2) + 2px);
  min-height: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--hint);
  cursor: pointer;
}

.tab .tab-icon {
  font-size: 20px;
  line-height: 1;
}

.tab .tab-label {
  font-size: 11px;
  font-weight: 500;
}

.tab[aria-selected="true"] {
  color: var(--link);
}

/* ---------- Today screen ---------- */

.day-label {
  color: var(--hint);
  font-size: 13px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: var(--space-3) 0 var(--space-2);
}

.question-text {
  font-size: 24px;
  line-height: 1.35;
  margin: 0 0 var(--space-4);
}

.answer-form textarea {
  width: 100%;
  min-height: 120px;
  resize: vertical;
  background: var(--secondary-bg);
  border: none;
  border-radius: var(--radius);
  padding: var(--space-3);
  color: var(--text);
  outline: none;
}

.answer-form textarea::placeholder {
  color: var(--hint);
}

.char-count {
  text-align: right;
  color: var(--hint);
  font-size: 12px;
  margin-top: var(--space-1);
}

.primary-btn {
  appearance: none;
  border: none;
  width: 100%;
  min-height: 48px;
  background: var(--button);
  color: var(--button-text);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 600;
  margin-top: var(--space-3);
  cursor: pointer;
}

.primary-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.primary-btn:active:not(:disabled) {
  filter: brightness(0.92);
}

/* Quote cards */

.cards {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin-top: var(--space-2);
}

@media (min-width: 560px) {
  .cards.unlocked {
    flex-direction: row;
    align-items: stretch;
  }
  .cards.unlocked .quote-card {
    flex: 1;
  }
}

.quote-card {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: var(--space-3);
  position: relative;
  min-height: 96px;
}

.quote-card .who {
  font-size: 12px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 0 0 var(--space-2);
}

.quote-card .body {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.quote-card.mine {
  border: 1px solid color-mix(in srgb, var(--link) 30%, transparent);
}

/* Your own answer in the archive. Matters most on a missed day, where yours
   is the only one shown — without a marker it reads as your partner's. */
.archive-answer.mine {
  border-left: 2px solid color-mix(in srgb, var(--link) 35%, transparent);
  padding-left: var(--space-2);
}

/* Frosted / locked card */

.quote-card.locked {
  overflow: hidden;
}

.quote-card.locked .body {
  filter: blur(6px);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  user-select: none;
  color: transparent;
  background: color-mix(in srgb, var(--hint) 35%, transparent);
  border-radius: 6px;
}

.quote-card.locked .waiting-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-3);
  color: var(--hint);
  font-size: 14px;
  font-weight: 500;
  background: color-mix(in srgb, var(--secondary-bg) 55%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}

.pulse-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--hint);
  margin-left: 6px;
  animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

/* Unlock transition: locked card resolving into legible text */

.quote-card.unlocking .waiting-label {
  animation: fade-out 0.7s ease forwards;
}

.quote-card.unlocking .body {
  animation: unblur 0.7s ease forwards;
}

@keyframes fade-out {
  0% { opacity: 1; }
  40% { opacity: 1; }
  100% { opacity: 0; pointer-events: none; }
}

@keyframes unblur {
  0% {
    filter: blur(6px);
    color: transparent;
    background: color-mix(in srgb, var(--hint) 35%, transparent);
  }
  100% {
    filter: blur(0);
    color: var(--text);
    background: transparent;
  }
}

@media (prefers-reduced-motion: reduce) {
  .quote-card.unlocking .waiting-label,
  .quote-card.unlocking .body {
    animation: none !important;
  }
  .pulse-dot {
    animation: none !important;
  }
}

/* ---------- Status / empty / error states ---------- */

.status-block {
  text-align: center;
  color: var(--hint);
  padding: var(--space-6) var(--space-3);
}

.status-block .status-emoji {
  font-size: 32px;
  display: block;
  margin-bottom: var(--space-2);
}

.status-block .retry-btn {
  appearance: none;
  border: 1px solid color-mix(in srgb, var(--hint) 40%, transparent);
  background: transparent;
  color: var(--link);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  min-height: 44px;
  margin-top: var(--space-3);
  cursor: pointer;
}

/* ---------- Archive ---------- */

.archive-day {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: var(--space-3);
  margin-bottom: var(--space-3);
}

.archive-day .archive-date {
  font-size: 12px;
  color: var(--hint);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 0 0 var(--space-1);
}

.archive-day .archive-question {
  margin: 0 0 var(--space-2);
  font-size: 17px;
}

.archive-day .archive-answers {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.archive-answer .who {
  font-size: 12px;
  color: var(--hint);
  font-weight: 600;
  margin: 0 0 2px;
}

.archive-answer .body {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.archive-day.missed {
  opacity: 0.55;
}

.archive-day.missed .archive-question::after {
  content: " — missed";
  color: var(--hint);
  font-style: italic;
  font-size: 13px;
}

.load-more-btn {
  display: block;
  width: 100%;
  min-height: 44px;
  background: transparent;
  border: none;
  color: var(--link);
  cursor: pointer;
  padding: var(--space-3);
}

/* ---------- Settings ---------- */

.settings-group {
  margin-bottom: var(--space-4);
}

.settings-group label {
  display: block;
  font-size: 13px;
  color: var(--hint);
  font-weight: 500;
  margin-bottom: var(--space-2);
}

.settings-group select,
.settings-group input[type="time"] {
  width: 100%;
  min-height: 48px;
  background: var(--secondary-bg);
  border: none;
  border-radius: var(--radius-sm);
  padding: 0 var(--space-3);
  color: var(--text);
}

.pair-block {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: var(--space-3);
  margin-bottom: var(--space-4);
  font-size: 14px;
  color: var(--hint);
}

.pair-block a.invite-link {
  display: inline-block;
  margin-top: var(--space-2);
  word-break: break-all;
}

.save-toast {
  position: fixed;
  bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--button);
  color: var(--button-text);
  padding: var(--space-2) var(--space-4);
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  transition: opacity 0.25s ease;
  pointer-events: none;
  max-width: 90%;
  text-align: center;
}

.save-toast.show {
  opacity: 1;
}

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

.hidden {
  display: none !important;
}
