@font-face {
  font-family: 'Enchanted Land';
  src: url('fonts/EnchantedLand.otf') format('opentype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

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

:root {
  --parchment: #d4b896;
  --parchment-dark: #b8956a;
  --parchment-light: #e8d5b7;
  --ink: #2a1a0a;
  --gold: #c9a84c;
  --gold-dark: #8b6914;
  --brown: #5c3a1e;
  --brown-dark: #3d2510;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Enchanted Land', cursive;
  color: var(--ink);
}

/* Fallback colour lives on <html>; <body> stays transparent so the
   body::before background image (painted at negative z-index) shows through. */
html {
  background: #1a1108;
}

/* Living background video — covers the whole screen, behind everything.
   Shows the painting (poster) until images/background-loop.mp4 exists. */
.bg-video {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;   /* fallback */
  height: 100lvh;  /* full physical screen */
  object-fit: cover;
  object-position: left top;
  z-index: -1;
  pointer-events: none;
}

/* Global full-screen background — covers the entire device screen,
   including behind Safari's top status bar and bottom search/toolbar.
   Uses the largest viewport height so it never leaves black bands. */
body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;   /* fallback for older browsers */
  height: 100lvh;  /* full physical screen even when browser UI is retracted */
  background: #1a1108 url('images/background.jpg') center top / cover no-repeat;
  z-index: -1;
}

/* SCREENS */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease;
  z-index: 1;
}

.screen.active {
  opacity: 1;
  pointer-events: all;
  z-index: 10;
}

/* BACKGROUND */
.screen-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
}

/* Per-screen image hidden — the global body::before background covers everything */
.bg-image {
  display: none;
}

.screen-bg::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    180deg,
    rgba(0,0,0,0.1) 0%,
    rgba(0,0,0,0) 30%,
    rgba(0,0,0,0) 60%,
    rgba(0,0,0,0.4) 100%
  );
}

.screen-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  padding: 3rem 1.5rem 2rem;
  overflow-y: auto;
}

/* HOME SCREEN */
.home-logo {
  width: min(66vw, 270px);
  height: auto;
  margin-top: 4vh;
  filter: drop-shadow(0 4px 12px rgba(0,0,0,0.5));
}

.dice-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  gap: 1.2rem;
}

.dice-image {
  width: min(36vw, 148px);
  height: auto;
  filter: drop-shadow(0 6px 20px rgba(0,0,0,0.5));
  transition: transform 0.15s ease;
}

.dice-image:active {
  transform: scale(0.93);
}

.dice-image.shaking {
  animation: diceShake 0.1s infinite;
}

@keyframes diceShake {
  0% { transform: rotate(0deg) scale(1.05); }
  25% { transform: rotate(12deg) scale(1.1); }
  50% { transform: rotate(-8deg) scale(1.05); }
  75% { transform: rotate(15deg) scale(1.08); }
  100% { transform: rotate(-12deg) scale(1.05); }
}

.dice-hint {
  font-family: 'Enchanted Land', cursive;
  color: var(--parchment-light);
  font-size: 0.9rem;
  text-shadow: 0 2px 4px rgba(0,0,0,0.8);
  text-align: center;
  animation: pulse 2s ease-in-out infinite;
}

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

/* HOW TO PLAY BUTTON (image-based) */
.btn-how-to-play {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  margin-bottom: 2rem;
  transition: transform 0.15s;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.5));
}

.btn-how-to-play img {
  width: min(46vw, 185px);
  height: auto;
}

.btn-how-to-play:active {
  transform: scale(0.95);
}

/* HOME BUTTONS STACK */
.home-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
  flex-shrink: 0;
}

.home-buttons .btn-how-to-play {
  margin-bottom: 0;
}

/* CUSTOM GAME BUTTON (image-based) */
.btn-custom-game {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.5));
}

.btn-custom-game img {
  width: min(39vw, 156px);
  height: auto;
  display: block;
}

.btn-custom-game:active {
  transform: scale(0.95);
}

/* CUSTOM GAME — PICK SCROLLS SCREEN */
#screen-custom-pick .screen-content {
  justify-content: flex-start;
  gap: 0.9rem;
  padding-top: 4rem;
  padding-bottom: 1.5rem;
}

.pick-hint {
  font-family: 'Enchanted Land', cursive;
  color: var(--parchment-light);
  font-size: 1rem;
  text-shadow: 0 2px 4px rgba(0,0,0,0.8);
  text-align: center;
  flex-shrink: 0;
}

.pick-hint #pick-count {
  color: var(--gold);
  font-weight: bold;
}

.pick-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 460px;
  flex: 1;
  overflow-y: auto;
  padding: 18px 4px;
  /* Soft fade at the top & bottom edges instead of a hard cut */
  -webkit-mask-image: linear-gradient(
    to bottom,
    transparent 0,
    #000 28px,
    #000 calc(100% - 28px),
    transparent 100%
  );
  mask-image: linear-gradient(
    to bottom,
    transparent 0,
    #000 28px,
    #000 calc(100% - 28px),
    transparent 100%
  );
}

.pick-grid::-webkit-scrollbar {
  display: none;
}

.pick-card {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px;
  cursor: pointer;
  background: rgba(20, 12, 5, 0.6);
  border: 2px solid rgba(201, 168, 76, 0.25);
  border-radius: 12px;
  transition: transform 0.1s ease, border-color 0.15s ease, background 0.15s ease, opacity 0.15s ease;
  backdrop-filter: blur(2px);
}

.pick-thumb {
  width: 90px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}

.pick-info {
  flex: 1;
  min-width: 0;
  padding-right: 26px;
}

.pick-name {
  font-family: 'Enchanted Land', cursive;
  font-size: 1.2rem;
  color: var(--gold);
  line-height: 1.15;
  margin-bottom: 6px;
}

.pick-effect {
  font-family: 'Enchanted Land', cursive;
  font-size: 1rem;
  color: var(--parchment-light);
  line-height: 1.35;
}

.pick-card:active {
  transform: scale(0.98);
}

.pick-card.selected {
  border-color: var(--gold);
  background: rgba(201, 168, 76, 0.18);
}

.pick-card.selected::after {
  content: '\2713';
  position: absolute;
  top: 8px;
  right: 8px;
  width: 22px;
  height: 22px;
  background: var(--gold);
  color: var(--brown-dark);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: bold;
  box-shadow: 0 2px 4px rgba(0,0,0,0.5);
  animation: checkPop 0.25s ease-out;
}

@keyframes checkPop {
  0% { transform: scale(0); }
  60% { transform: scale(1.3); }
  100% { transform: scale(1); }
}

.pick-card.dimmed {
  opacity: 0.4;
}

.btn-custom-action[disabled] {
  opacity: 0.4;
  pointer-events: none;
}

.btn-custom-action {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  margin-top: auto;
  transition: transform 0.15s;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.5));
}

.btn-custom-action img {
  width: min(42vw, 165px);
  height: auto;
  display: block;
}

.btn-custom-action:active {
  transform: scale(0.95);
}

/* CLOSE / BACK / CORNER ICON BUTTONS */
.btn-close,
.btn-back,
.btn-icon-corner {
  position: absolute;
  top: 1.5rem;
  background: rgba(0,0,0,0.5);
  color: var(--parchment-light);
  border: 2px solid var(--gold-dark);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  transition: background 0.2s, transform 0.15s;
  backdrop-filter: blur(4px);
  padding: 0;
}

.btn-close:active,
.btn-back:active,
.btn-icon-corner:active {
  background: rgba(0,0,0,0.7);
  transform: scale(0.92);
}

.btn-save-set {
  right: 1.5rem;
}

.btn-saved-sets {
  right: 1.5rem;
}

/* On the scrolls screen the close (X) sits on the left, save on the right */
#btn-close-scrolls {
  left: 1.5rem;
  right: auto;
}

.btn-close {
  right: 1.5rem;
}

.btn-back {
  left: 1.5rem;
  font-size: 2rem;
  padding-bottom: 2px;
}

/* SCROLLS SCREEN */
#screen-scrolls .screen-content {
  justify-content: center;
  align-items: center;
  gap: 1.2rem;
  padding-top: 4rem;
  padding-bottom: 2rem;
}

.scrolls-title-img {
  width: min(45vw, 200px);
  height: auto;
  filter: drop-shadow(0 4px 12px rgba(0,0,0,0.5));
  flex-shrink: 0;
}

.scrolls-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 70%;
  max-width: 240px;
  flex-shrink: 0;
}

.scroll-tile {
  aspect-ratio: 1;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  transition: transform 0.15s;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.4));
}

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

.scroll-tile:active {
  transform: scale(0.93);
}

.scroll-tile {
  animation: tileAppear 0.4s ease-out backwards;
}

.scroll-tile:nth-child(1) { animation-delay: 0.1s; }
.scroll-tile:nth-child(2) { animation-delay: 0.2s; }
.scroll-tile:nth-child(3) { animation-delay: 0.3s; }
.scroll-tile:nth-child(4) { animation-delay: 0.4s; }
.scroll-tile:nth-child(5) { animation-delay: 0.5s; }
.scroll-tile:nth-child(6) { animation-delay: 0.6s; }

@keyframes tileAppear {
  from {
    opacity: 0;
    transform: scale(0.5) rotateY(90deg);
  }
  to {
    opacity: 1;
    transform: scale(1) rotateY(0);
  }
}

/* SCROLL DETAIL — FULL CARD IMAGE */
#screen-detail .screen-content {
  justify-content: center;
  gap: 0.9rem;
  padding-top: calc(1.25rem + env(safe-area-inset-top, 0px));
  padding-bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px));
}

.detail-scroll-number {
  font-family: 'Enchanted Land', cursive;
  font-size: clamp(1.8rem, 6vw, 2.5rem);
  color: var(--parchment-light);
  text-shadow: 2px 2px 0 rgba(0,0,0,0.6);
  flex-shrink: 0;
}

.detail-title-img {
  position: absolute;
  top: calc(1.25rem + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  height: clamp(64px, 12vh, 110px);
  width: auto;
  filter: drop-shadow(0 4px 10px rgba(0,0,0,0.5));
  z-index: 2;
}

.scroll-card-wrapper {
  width: 100%;
  max-width: 320px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* CUSTOM SCROLL — TEXT ON PARCHMENT FRAME */
.scroll-text-card {
  position: relative;
  display: inline-block;
  max-width: 100%;
  max-height: 66vh;
}

/* Frame keeps its natural aspect ratio; sizes to fit the wrapper */
.scroll-text-card .scroll-frame-img {
  position: static;
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 66vh;
  object-fit: contain;
}

.scroll-text-content {
  position: absolute;
  inset: 13% 14%;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  text-align: center;
  font-family: 'Enchanted Land', cursive;
  font-size: clamp(1rem, 4.6vw, 1.4rem);
  line-height: 1.45;
  color: #1a1208;
  word-break: break-word;
}

.scroll-text-content::-webkit-scrollbar {
  display: none;
}

.scroll-card-image {
  width: 100%;
  max-width: 320px;
  height: auto;
  max-height: 66vh;
  object-fit: contain;
  border-radius: 12px;
  animation: cardReveal 0.35s ease-out;
}

@keyframes cardReveal {
  from { opacity: 0; transform: scale(0.92); }
  to { opacity: 1; transform: scale(1); }
}

/* Placeholder when no card image exists */
.scroll-card-placeholder {
  width: 100%;
  max-width: 280px;
  aspect-ratio: 2/3;
  background: linear-gradient(180deg, var(--parchment) 0%, var(--parchment-dark) 100%);
  border: 4px solid var(--gold-dark);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1.5rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}

.scroll-card-placeholder .placeholder-icon {
  font-size: 4rem;
}

.scroll-card-placeholder .placeholder-name {
  font-family: 'Enchanted Land', cursive;
  font-size: 1.4rem;
  color: var(--brown-dark);
  text-align: center;
}

.scroll-card-placeholder .placeholder-effect {
  font-size: 0.95rem;
  color: var(--brown);
  text-align: center;
  line-height: 1.4;
  border-top: 1px solid var(--gold-dark);
  padding-top: 0.8rem;
  width: 100%;
}

/* MUSIC BUTTON */
.btn-music {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(0,0,0,0.5);
  border: 2px solid var(--gold-dark);
  color: var(--parchment-light);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(4px);
  transition: background 0.2s;
}

.btn-music:active {
  background: rgba(0,0,0,0.7);
}

@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .btn-music {
    bottom: calc(1.5rem + env(safe-area-inset-bottom));
  }
}

/* On the home screen, dock the music button to the top-left corner */
body:has(#screen-home.active) .btn-music {
  top: calc(1.5rem + env(safe-area-inset-top, 0px));
  left: 1.5rem;
  right: auto;
  bottom: auto;
}

/* Propose-a-rule button — home top-right corner (aligned with the music button) */
.btn-propose {
  right: 1.5rem;
  top: calc(1.5rem + env(safe-area-inset-top, 0px));
}

/* HOW TO PLAY MODAL */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  padding: 1rem;
}

.modal.active {
  opacity: 1;
  pointer-events: all;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.8);
}

.modal-content {
  position: relative;
  background: linear-gradient(180deg, var(--parchment-light) 0%, var(--parchment) 100%);
  border: 3px solid var(--gold-dark);
  border-radius: 16px;
  padding: 2rem 1.5rem;
  max-width: 400px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,0.6);
}

.rules-title-img {
  width: min(50vw, 180px);
  height: auto;
  display: block;
  margin: 0 auto 1.5rem;
}

.rules {
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
}

.rule {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
}

.rule-number {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  background: var(--brown-dark);
  color: var(--parchment-light);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Enchanted Land', cursive;
  font-size: 1.1rem;
}

.rule p {
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--brown);
}

/* MODAL — HEADINGS / INPUT / ACTIONS */
.modal-heading {
  font-family: 'Enchanted Land', cursive;
  font-size: 1.5rem;
  color: var(--brown-dark);
  text-align: center;
  margin-bottom: 1.2rem;
}

.modal-narrow {
  max-width: 340px;
}

.modal-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 2px solid var(--gold-dark);
  background: rgba(255,255,255,0.5);
  color: var(--ink);
  font-family: 'Enchanted Land', cursive;
  font-size: 1.1rem;
  outline: none;
  -webkit-appearance: none;
}

.modal-input:focus {
  border-color: var(--gold);
  background: rgba(255,255,255,0.75);
}

.modal-textarea {
  display: block;
  resize: none;
  min-height: 120px;
  line-height: 1.4;
  margin-bottom: 0.8rem;
}

.modal-actions {
  display: flex;
  gap: 0.8rem;
  margin-top: 1.4rem;
}

.modal-btn {
  flex: 1;
  padding: 12px;
  border-radius: 10px;
  font-family: 'Enchanted Land', cursive;
  font-size: 1.05rem;
  cursor: pointer;
  border: 2px solid var(--gold-dark);
  transition: transform 0.15s, filter 0.15s;
}

.modal-btn:active {
  transform: scale(0.96);
}

.modal-btn-ghost {
  background: transparent;
  color: var(--brown-dark);
}

.modal-btn-gold {
  background: linear-gradient(180deg, var(--gold) 0%, var(--gold-dark) 100%);
  color: var(--brown-dark);
  font-weight: bold;
}

/* SAVED SETS LIST */
.saved-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 60vh;
  overflow-y: auto;
}

.saved-empty {
  font-size: 0.95rem;
  color: var(--brown);
  text-align: center;
  line-height: 1.5;
  padding: 1rem 0;
}

.saved-item {
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.saved-load {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: rgba(92, 58, 30, 0.12);
  border: 2px solid var(--gold-dark);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: background 0.15s, transform 0.12s;
}

.saved-load:active {
  transform: scale(0.98);
  background: rgba(92, 58, 30, 0.22);
}

.saved-name {
  font-family: 'Enchanted Land', cursive;
  font-size: 1.05rem;
  color: var(--brown-dark);
}

.saved-sub {
  font-size: 0.75rem;
  color: var(--brown);
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saved-del {
  flex-shrink: 0;
  width: 44px;
  border: 2px solid var(--gold-dark);
  border-radius: 10px;
  background: rgba(92, 58, 30, 0.12);
  color: var(--brown-dark);
  font-size: 1.4rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.12s;
}

.saved-del:active {
  transform: scale(0.94);
  background: rgba(120, 30, 20, 0.25);
}

/* TOAST */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(2.5rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%) translateY(20px);
  background: rgba(20, 12, 5, 0.92);
  color: var(--parchment-light);
  border: 1px solid var(--gold-dark);
  border-radius: 999px;
  padding: 10px 22px;
  font-family: 'Enchanted Land', cursive;
  font-size: 1rem;
  z-index: 300;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  box-shadow: 0 6px 20px rgba(0,0,0,0.5);
}

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

/* ROLLING OVERLAY */
.rolling-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.rolling-overlay.active {
  opacity: 1;
  pointer-events: all;
}

.rolling-dice-img {
  width: 140px;
  height: auto;
  animation: shakeRoll 0.12s infinite;
  filter: drop-shadow(0 0 30px rgba(201, 168, 76, 0.4));
}

@keyframes shakeRoll {
  0% { transform: rotate(0deg) scale(1); }
  25% { transform: rotate(18deg) scale(1.15); }
  50% { transform: rotate(-12deg) scale(0.95); }
  75% { transform: rotate(22deg) scale(1.1); }
  100% { transform: rotate(-15deg) scale(1.05); }
}

.rolling-text {
  font-family: 'Enchanted Land', cursive;
  font-size: 1.5rem;
  color: var(--gold);
  text-shadow: 0 0 20px rgba(201, 168, 76, 0.4);
  animation: pulse 1s ease-in-out infinite;
}

/* SAFE AREAS */
@supports (padding-top: env(safe-area-inset-top)) {
  .screen-content {
    padding-top: calc(3rem + env(safe-area-inset-top));
    padding-bottom: calc(1.5rem + env(safe-area-inset-bottom));
  }
}

.screen-content::-webkit-scrollbar {
  display: none;
}

/* TABLET */
@media (min-width: 600px) {
  .scrolls-grid {
    max-width: 360px;
    gap: 16px;
  }

  .scroll-card-wrapper {
    max-width: 380px;
  }

  .scroll-card-image {
    max-height: 70vh;
  }

  .home-logo {
    width: min(60vw, 320px);
  }
}

/* DESKTOP */
@media (min-width: 900px) {
  .screen-content {
    max-width: 500px;
    margin: 0 auto;
  }
}

/* SMALL PHONES */
@media (max-height: 700px) {
  .scrolls-grid {
    gap: 8px;
    max-width: 260px;
  }

  .home-logo {
    width: min(55vw, 200px);
    margin-top: 3vh;
  }

  .scrolls-title-img {
    width: min(45vw, 180px);
  }

  .scroll-card-image {
    max-height: 60vh;
  }
}
