:root {
  --ink-dark: #2c1612;
  --ink-light: #5a382c;
  --blood-red: #8a1c1c;
  --gold: #d4af37;
  --parchment: #f4e8d1;
  --parchment-dark: #e6d2b5;
  --font-title: 'Cinzel', serif;
  --font-text: 'Merienda', cursive;
}

/* HIDDEN SCROLLBAR FOR ALL ELEMENTS */
::-webkit-scrollbar { display: none; }
* {
  box-sizing: border-box;
  margin: 0; padding: 0;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

body, html {
  width: 100%; height: 100dvh;
  background-color: #1a0f0d;
  background-image: radial-gradient(circle at center, #2c1612 0%, #0a0504 100%);
  color: var(--ink-dark);
  font-family: var(--font-text);
  overflow: hidden; /* Prevent body scrolling entirely */
  touch-action: pan-y;
}

.app-container {
  position: relative; width: 100%; height: 100%;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; z-index: 1;
}

/* --- Unfurling Scroll Animations --- */
.scroll-container {
  display: flex; flex-direction: column; align-items: center;
  width: min(95%, 450px);
  max-height: 100vh;
  perspective: 1000px;
}

.scroll-content {
  width: 100%;
  overflow-y: scroll; /* Scrollable but hidden scrollbar */
  max-height: 0;
  opacity: 0;
  transform-origin: top;
  transition: max-height 1.2s cubic-bezier(0.25, 1, 0.5, 1), opacity 0.8s ease;
}

/* Open state applies the max-height allowing the scroll to "unfurl" */
.scroll-container.open .scroll-content {
  max-height: 85vh; /* Fits within mobile screens easily */
  opacity: 1;
}

/* The wooden rollers at the top and bottom of the scroll */
.roller {
  width: 105%; height: 25px;
  background: linear-gradient(to bottom, #4a2e1b, #2c1612, #4a2e1b);
  border: 2px solid #1a0f0d;
  border-radius: 12px;
  position: relative; z-index: 2;
  box-shadow: 0 5px 15px rgba(0,0,0,0.8);
}
.top-roller { margin-bottom: -10px; }
.bottom-roller { margin-top: -10px; }

/* The paper texture inside the scroll */
.scroll-paper {
  background: var(--parchment);
  background-image: url('https://www.transparenttextures.com/patterns/old-wall.png');
  border-left: 3px solid var(--gold);
  border-right: 3px solid var(--gold);
  box-shadow: inset 0 0 30px rgba(90, 56, 44, 0.4);
}

.parchment-box {
  background: var(--parchment-dark);
  border: 2px solid #c2a682; border-radius: 4px;
  box-shadow: inset 0 0 10px rgba(0,0,0,0.1);
}

/* --- Feather Pen Animation --- */
.feather-pen {
  position: absolute;
  font-size: 2.2rem;
  pointer-events: none;
  z-index: 9999;
  transform-origin: bottom left;
  transition: top 0.15s ease-out, left 0.15s ease-out, opacity 0.3s;
  filter: drop-shadow(2px 4px 6px rgba(0,0,0,0.4));
}
.feather-pen.hidden { opacity: 0; transform: scale(0.5); }

@keyframes writeWiggle {
  0% { transform: rotate(0deg) translate(0, 0); }
  25% { transform: rotate(-15deg) translate(-4px, -2px); }
  50% { transform: rotate(10deg) translate(2px, 2px); }
  75% { transform: rotate(-5deg) translate(-2px, 4px); }
  100% { transform: rotate(0deg) translate(0, 0); }
}
.feather-pen.is-writing { animation: writeWiggle 0.2s infinite; }

/* --- Canvas Layer --- */
.canvas-container {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 10px; z-index: 5;
}

canvas {
  max-width: 100%; max-height: 100%;
  aspect-ratio: 960 / 600; background: #110907;
  border: 4px double var(--gold); border-radius: 8px;
  box-shadow: 0 0 40px rgba(0,0,0,0.9); object-fit: contain;
}

/* --- HUD Layer --- */
.hud {
  position: absolute; top: 0; left: 0; width: 100%;
  padding: 16px; display: flex; justify-content: space-between;
  align-items: flex-start; z-index: 10; pointer-events: none;
}
.hud-timer { padding: 8px 16px; font-family: var(--font-title); font-size: 2rem; font-weight: 900; color: var(--blood-red); }
.hud-scores { display: flex; flex-direction: column; gap: 8px; padding: 12px 20px; }
.score-row { display: flex; justify-content: space-between; gap: 20px; font-size: 1.1rem; font-weight: 700; color: var(--ink-dark); }
.score-row.is-you { color: var(--blood-red); }
.hud-status { display: flex; flex-direction: column; gap: 6px; align-items: flex-end; padding: 8px; }
.badge { font-family: var(--font-title); font-size: 0.8rem; font-weight: 700; }
.badge.online { color: #2e7d32; }
.badge.offline { color: var(--blood-red); }

/* --- UI Panels --- */
.modal-card, .lobby-panel, .lore-card {
  padding: 30px 25px;
  display: flex; flex-direction: column; gap: 15px;
  text-align: center;
}

.brand h1, .castle-title { font-family: var(--font-title); font-size: 2.4rem; color: var(--blood-red); margin-bottom: 5px; text-shadow: 1px 1px 0px rgba(0,0,0,0.2);}
.brand p, .lore-text p { color: var(--ink-dark); font-size: 1.05rem; font-weight: 700; line-height: 1.4; margin-bottom: 10px;}

.settings-group { display: flex; gap: 10px; }
.setting-item { flex: 1; display: flex; flex-direction: column; gap: 4px; text-align: left;}
.setting-item label { font-family: var(--font-title); font-size: 0.75rem; color: var(--ink-dark); font-weight: 900; }

input {
  width: 100%; background: rgba(255,255,255,0.6);
  border: 1px solid var(--ink-light); color: var(--ink-dark);
  padding: 12px 16px; border-radius: 4px;
  font-family: var(--font-text); font-size: 1rem; font-weight: 700;
  transition: all 0.3s;
}
input:focus { outline: none; border-color: var(--blood-red); background: rgba(255,255,255,0.9); box-shadow: 0 0 8px rgba(138, 28, 28, 0.4);}

select {
  width: 100%;
  background: rgba(255,255,255,0.6);
  border: 1px solid var(--ink-light);
  color: var(--ink-dark);
  padding: 12px 16px;
  border-radius: 4px;
  font-family: var(--font-text);
  font-size: 1rem;
  font-weight: 700;
}

select:focus {
  outline: none;
  border-color: var(--blood-red);
  background: rgba(255,255,255,0.9);
  box-shadow: 0 0 8px rgba(138, 28, 28, 0.4);
}

.btn {
  width: 100%; padding: 14px; border: none;
  font-family: var(--font-title); font-weight: 900; font-size: 1.05rem;
  cursor: pointer; transition: all 0.2s; border-radius: 4px;
}
.btn:active { transform: scale(0.96); }

.wax-seal {
  background: var(--blood-red); color: #fff;
  border: 2px solid #5a0d0d;
  box-shadow: inset 0 0 10px rgba(0,0,0,0.5), 0 4px 6px rgba(0,0,0,0.3);
  text-shadow: 1px 1px 2px #000;
}
.outline-gold { background: transparent; color: var(--ink-dark); border: 2px solid var(--gold); }
.text-only { background: transparent; color: var(--ink-light); border: none; }

.action-group, .join-group, .modal-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 10px;}

.sound-toggle {
  margin-top: 4px;
}

.divider { text-align: center; position: relative; margin: 5px 0;}
.divider span { background: var(--parchment); padding: 0 10px; color: var(--ink-light); font-size: 0.8rem; position: relative; z-index: 2; font-family: var(--font-title);}
.divider::before { content: ''; position: absolute; top: 50%; left: 0; right: 0; height: 1px; background: var(--gold); }

.error-text { color: var(--blood-red); font-size: 0.85rem; font-weight: 700; min-height: 16px; }

/* Modals */
.modal-overlay {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; opacity: 1; transition: opacity 0.4s ease;
}
.modal-overlay.hidden { opacity: 0; pointer-events: none; }
.modal-card h3, .modal-card h2 { font-family: var(--font-title); color: var(--blood-red); font-size: 1.8rem;}

.code-display { font-family: var(--font-title); font-size: 2.8rem; font-weight: 900; letter-spacing: 5px; color: var(--blood-red); text-shadow: 2px 2px 0px #fff;}

/* ToD Specific */
.tod-prompt-box { padding: 20px; margin: 10px 0; border: 2px dashed var(--blood-red); }
.tod-prompt-box p { color: var(--ink-dark); font-size: 1.2rem; font-weight: 700; }
.tod-result-box { padding: 15px; margin: 15px 0; text-align: left; }
.tod-result-box h4 { font-family: var(--font-title); color: var(--blood-red); margin-bottom: 5px; }
.highlight-text { color: var(--ink-dark); font-weight: 700; font-size: 1.1rem; margin-top: 5px;}
.muted-text { font-size: 0.85rem; color: var(--ink-light); }

.spinner { width: 40px; height: 40px; border: 4px solid rgba(138, 28, 28, 0.2); border-top-color: var(--blood-red); border-radius: 50%; animation: spin 1s linear infinite; margin: 20px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Mobile Controls --- */
.touch-controls { position: absolute; bottom: 20px; left: 0; width: 100%; display: none; justify-content: center; z-index: 15; }
@media (pointer: coarse) { .touch-controls { display: flex; } }
.joystick { width: 130px; height: 130px; position: relative; border-radius: 50%; touch-action: none; }
.joystick-knob { position: absolute; width: 50px; height: 50px; border-radius: 50%; top: 50%; left: 50%; transform: translate(-50%, -50%); }

.hidden { display: none !important; }
