* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: #f5e6c8;
  font-family: 'Noto Serif SC', 'STSong', serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* Paper texture */
body::before {
  content: '';
  position: fixed; inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.08'/%3E%3C/svg%3E");
  pointer-events: none; opacity: 0.5; z-index: 0;
}

.bg-video-bg {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
  background: transparent;
}
.bg-video-el {
  position: absolute;
  inset: -2%;
  width: 104%;
  height: 104%;
  object-fit: cover;
  opacity: 0.36;
  mix-blend-mode: multiply;
  filter: saturate(0.9) contrast(1.04);
  transform: scale(1.02);
}
.bg-video-bg::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 50% 45%, rgba(245,230,200,0.18), rgba(245,230,200,0.62) 70%),
    rgba(245,230,200,0.24);
}

/* HEADER */
.header {
  background: linear-gradient(135deg, #1a0800 0%, #4a1e00 50%, #5a2d0c 100%);
  color: #f5e6c8;
  padding: 10px 40px;
  display: flex; align-items: center; justify-content: space-between;
  position: relative;
  box-shadow: 0 3px 12px rgba(0,0,0,0.5);
  flex-shrink: 0; z-index: 5;
}
.header-title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(23px, 1.45vw, 26px);
  font-weight: 700;
  letter-spacing: 4px;
  white-space: nowrap;
  pointer-events: none;
}
.header-sub {
  flex: 0 0 auto;
  font-size: clamp(15px, 0.95vw, 16px);
  font-weight: 700;
  opacity: 0.86;
  letter-spacing: 2px;
  position: relative;
  z-index: 1;
}

.scene-nav { display: flex; gap: 8px; margin-left: auto; position: relative; z-index: 1; }
.scene-btn {
  width: 60px; height: 44px;
  border-radius: 6px;
  border: 1.5px solid rgba(200,160,90,0.6);
  background: transparent;
  color: rgba(245,230,200,0.7);
  text-decoration: none;
  font-family: 'Noto Serif SC', serif;
  font-size: 10px; cursor: pointer;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  transition: all 0.25s; line-height: 1.4; text-align: center;
  padding: 2px 4px;
}
.scene-btn.active {
  background: #c8a05a; color: #1a0800;
  border-color: #c8a05a; font-weight: 700;
}
.scene-btn:hover:not(.active) { background: rgba(200,160,90,0.2); color: #f5e6c8; }
.scene-btn .num { font-size: 14px; font-weight: 700; }
.scene-btn.challenge-btn {
  width: 76px;
  color: #fff4c9;
  border-color: rgba(232,192,96,0.72);
}
.scene-btn.challenge-btn:hover {
  background: rgba(232,192,96,0.22);
  color: #fffbe8;
}

/* MAIN LAYOUT */
.main {
  --stage-baseline: clamp(150px, 16vh, 210px);
  --board-height: min(54vh, 540px);
  flex: 1;
  width: 100%;
  margin: 0;
  display: block;
  padding: 12px clamp(14px, 1.6vw, 28px) 8px;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

/* LEFT: triangle + board */
.left-panel {
  display: contents;
}
.tower-panel {
  position: absolute;
  left: 30.8%;
  top: auto;
  bottom: var(--stage-baseline);
  transform: translateY(10%);
  width: 31.5%;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 3;
}
#triSvg { width: min(100%, 480px); height: 430px; }

.blackboard {
  position: absolute;
  left: clamp(72px, 5vw, 96px);
  top: auto;
  bottom: var(--stage-baseline);
  width: min(30vw, 455px);
  height: var(--board-height);
  overflow-y: auto;
  padding: 18px 20px 20px;
  border: 10px solid #7a4a22;
  border-radius: 5px;
  z-index: 2;
  background:
    linear-gradient(145deg, rgba(255,255,255,0.05), transparent 34%),
    radial-gradient(circle at 20% 22%, rgba(255,255,255,0.05), transparent 34%),
    #18352d;
  box-shadow:
    inset 0 0 18px rgba(0,0,0,0.42),
    inset 0 0 0 2px rgba(245,230,200,0.12),
    0 12px 24px rgba(58,26,0,0.18);
  scrollbar-width: thin;
  scrollbar-color: rgba(245,230,200,0.46) rgba(0,0,0,0.16);
}
.blackboard::before {
  content: '';
  position: absolute;
  left: 18px; right: 18px; top: 10px;
  height: 1px;
  background: rgba(245,230,200,0.18);
}
.blackboard::-webkit-scrollbar {
  width: 8px;
}
.blackboard::-webkit-scrollbar-track {
  background: rgba(0,0,0,0.16);
}
.blackboard::-webkit-scrollbar-thumb {
  background: rgba(245,230,200,0.46);
  border-radius: 999px;
}
.board-heading {
  color: rgba(247,240,207,0.82);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 3px;
  margin-bottom: 8px;
}

/* RIGHT PANEL */
.right-panel {
  position: absolute;
  left: 48%;
  right: 28px;
  top: 0;
  bottom: 0;
  height: auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
  z-index: 4;
}

.scene-header {
  position: absolute;
  left: clamp(72px, 5vw, 96px);
  top: auto;
  bottom: calc(var(--stage-baseline) + var(--board-height) + 18px);
  width: min(30vw, 455px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  z-index: 5;
}
.scene-tag {
  background: #5a2d0c; color: #f5e6c8;
  padding: 3px 12px; font-size: 12px; letter-spacing: 1px;
  border-radius: 2px;
}
.scene-name { font-size: 20px; font-weight: 700; color: #3a1a00; letter-spacing: 1px; }

.character-stage {
  flex: 1 1 0;
  min-height: 0;
  height: 100%;
  width: 100%;
  position: relative;
  overflow: hidden;
}
.stage-figure {
  position: absolute;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  pointer-events: none;
  z-index: 1;
}
.elder-figure {
  left: 0;
  top: 0;
  width: 31%;
  height: 47%;
  justify-content: flex-start;
  overflow: visible;
}
.challenger-figure {
  right: 2.5%;
  bottom: var(--stage-baseline);
  width: 31%;
  height: 47%;
  justify-content: center;
  z-index: 1;
  overflow: visible;
}
.stage-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  object-position: center bottom;
  pointer-events: none;
  user-select: none;
  filter: drop-shadow(0 14px 14px rgba(58,26,0,0.22));
}
.elder-img {
  width: auto;
  height: 118%;
  max-width: none;
  max-height: none;
  object-position: center bottom;
  mix-blend-mode: multiply;
  transform: translateY(5%);
  transform-origin: center bottom;
}
.challenger-img {
  width: auto;
  height: 100%;
  max-width: none;
  max-height: none;
  object-position: center bottom;
  mix-blend-mode: multiply;
  transform: translateY(12%);
  transform-origin: center bottom;
}

.elder-talk {
  position: absolute;
  top: 8%;
  left: 35%;
  right: 3%;
  z-index: 2;
  display: flex;
  justify-content: flex-start;
  pointer-events: none;
}
.elder-talk-fading {
  z-index: 1;
  animation: elderBubbleFade 720ms ease-out both;
}
.challenger-log {
  position: absolute;
  left: 31%;
  right: 31%;
  top: 18%;
  bottom: auto;
  height: 52%;
  z-index: 2;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 9px;
  overflow-y: auto;
  padding: 32px 8px 2px;
  pointer-events: none;
  scrollbar-width: none;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, rgba(0,0,0,0.34) 4%, #000 15%, #000 100%);
  mask-image: linear-gradient(to bottom, transparent 0, rgba(0,0,0,0.34) 4%, #000 15%, #000 100%);
}
.challenger-log::-webkit-scrollbar {
  width: 0;
  height: 0;
}
.chat-row {
  display: flex;
  width: 100%;
  min-width: 0;
  flex-shrink: 0;
}
.chat-row.elder-row {
  justify-content: flex-start;
  padding: 0;
}
.chat-row.challenger-row {
  justify-content: flex-end;
  padding: 0;
}
.intro-stage .elder-figure {
  left: 0;
  top: 0;
  width: 31%;
  height: 47%;
}
.intro-stage .elder-talk {
  top: 8%;
  left: 35%;
  right: 3%;
}
.chat-bubble {
  position: relative;
  min-width: 0;
  width: fit-content;
  max-width: min(420px, 100%);
  background: rgba(255, 252, 230, 0.96);
  border: 2px solid #c8a05a;
  border-radius: 10px;
  padding: 11px 14px;
  color: #3a1a00;
  font-size: 14px;
  line-height: 1.85;
  letter-spacing: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
  box-shadow: 0 8px 18px rgba(58,26,0,0.12);
  transform-origin: left top;
  transition: width 120ms ease, height 120ms ease;
}
.challenger-row .chat-bubble {
  width: fit-content;
  max-width: 100%;
  transform-origin: right bottom;
}
.bubble-content {
  display: inline;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.chat-bubble.is-new {
  animation: bubbleGrow 520ms cubic-bezier(.2,.9,.2,1) both;
}
.chat-bubble.is-new .bubble-content {
  display: inline;
  overflow: visible;
}
.chat-bubble::before,
.chat-bubble::after {
  content: '';
  position: absolute;
  top: 22px;
  border: 6px solid transparent;
}
.elder-row .chat-bubble::before {
  left: -12px;
  border-right-color: #c8a05a;
}
.elder-row .chat-bubble::after {
  left: -9px;
  top: 23px;
  border-width: 5px;
  border-right-color: rgba(255,252,230,0.96);
}
.challenger-row .chat-bubble::before {
  right: -12px;
  border-left-color: #c8a05a;
}
.challenger-row .chat-bubble::after {
  right: -9px;
  top: 23px;
  border-width: 5px;
  border-left-color: rgba(255,252,230,0.96);
}
.speaker-name {
  display: block;
  margin-bottom: 3px;
  color: #8b5a16;
  font-size: 12px;
  font-weight: 700;
}
.chat-bubble b,
.blackboard .hi {
  color: #f4d56b;
  font-weight: 700;
}
.chat-bubble b { color: #e05c20; }

@keyframes bubbleGrow {
  0% {
    opacity: 0.18;
    transform: scale(0.78);
  }
  58% {
    opacity: 1;
    transform: scale(1.025);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes elderBubbleFade {
  0% {
    opacity: 0.86;
    transform: translateY(0) scale(1);
    filter: blur(0);
  }
  100% {
    opacity: 0;
    transform: translateY(-14px) scale(0.96);
    filter: blur(1px);
  }
}

@keyframes textType {
  0% {
    clip-path: inset(0 100% 0 0);
  }
  100% {
    clip-path: inset(0 0 0 0);
  }
}

/* Info area */
.info-area {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.ibox {
  background: rgba(255,252,230,0.9);
  border: 1px solid rgba(200,160,90,0.5);
  border-radius: 6px; padding: 10px 14px;
  font-size: 13px; color: #3a1a00; line-height: 1.9;
}
.ibox .hi { color: #e05c20; font-weight: 700; }

.fbox {
  background: #1a0800; color: #f5e6c8;
  border-radius: 6px; padding: 12px 16px;
  font-size: 13px; line-height: 2.2;
  font-family: 'Courier New', monospace; letter-spacing: 0.3px;
}
.fbox .hi { color: #e8c060; font-weight: bold; }
.fbox .dim { color: rgba(255,255,255,0.88); }

/* Memo cache table */
.cache-wrap { background: rgba(255,252,230,0.9); border: 1px solid rgba(200,160,90,0.5); border-radius: 6px; padding: 10px 12px; }
.cache-title { font-size: 12px; color: #5a2d0c; font-weight: 600; margin-bottom: 6px; }
table.cache { border-collapse: collapse; width: 100%; font-size: 13px; color: #3a1a00; }
table.cache th { background: #5a2d0c; color: #f5e6c8; padding: 4px 10px; font-weight: 600; font-size: 12px; }
table.cache td { border: 1px solid rgba(200,160,90,0.4); padding: 3px 10px; text-align: center; transition: background 0.4s; }
table.cache tr:nth-child(even) td { background: rgba(200,160,90,0.06); }
table.cache td.hit { background: #e8c060 !important; font-weight: 700; color: #3a1a00; }
table.cache td.new { background: #e05c20 !important; font-weight: 700; color: #fff; }

/* Complexity table */
table.complexity { border-collapse: collapse; width: 100%; font-size: 13px; color: #3a1a00; }
table.complexity th { background: #5a2d0c; color: #f5e6c8; padding: 4px 10px; font-weight: 600; }
table.complexity td { border: 1px solid rgba(200,160,90,0.4); padding: 4px 10px; text-align: center; }
table.complexity tr:nth-child(even) td { background: rgba(200,160,90,0.06); }
.ok { color: #5a9050; font-weight: 600; }
.warn { color: #c8a020; font-weight: 600; }
.bad { color: #e05c20; font-weight: 600; }
.xbad { color: #c03020; font-weight: 700; }

.blackboard .ibox,
.blackboard .cache-wrap {
  background: rgba(255,255,255,0.06) !important;
  border-color: rgba(245,230,200,0.3) !important;
  color: #f7f0cf;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.16);
}
.blackboard .ibox {
  line-height: 1.9;
  padding: 10px 12px;
}
.blackboard .ibox + .ibox,
.blackboard .ibox + .cache-wrap,
.blackboard .cache-wrap + .ibox {
  margin-top: 2px;
}
.blackboard .cache-title { color: rgba(247,240,207,0.86); }
.blackboard table.cache,
.blackboard table.complexity { color: #f7f0cf; }
.blackboard table.cache th,
.blackboard table.complexity th {
  background: rgba(245,230,200,0.14);
  color: #fff4c9;
}
.blackboard table.cache td,
.blackboard table.complexity td {
  border-color: rgba(245,230,200,0.24);
}
.blackboard table.cache tr:nth-child(even) td,
.blackboard table.complexity tr:nth-child(even) td {
  background: rgba(255,255,255,0.04);
}
.blackboard table.cache td.hit {
  background: rgba(244,213,107,0.72) !important;
  color: #18352d;
}
.blackboard table.cache td.new {
  background: rgba(224,92,32,0.82) !important;
  color: #fff;
}
.blackboard .cache-empty {
  color: rgba(255,255,255,0.86);
  font-size: 12px;
}
.blackboard .fbox {
  background: rgba(0,0,0,0.26);
  border: 1px solid rgba(245,230,200,0.18);
  line-height: 2;
}
.blackboard .ok { color: #9ee08d; }
.blackboard .warn { color: #f4d56b; }
.blackboard .bad,
.blackboard .xbad { color: #ff936c; }
.summary-card {
  display: grid;
  gap: 6px;
}
.summary-title {
  color: #fff4c9;
  font-size: 14px;
  font-weight: 700;
  text-align: center;
}
.summary-list {
  display: grid;
  gap: 4px;
}
.summary-list div {
  padding: 4px 7px;
  border: 1px solid rgba(245,230,200,0.2);
  border-radius: 5px;
  background: rgba(0,0,0,0.14);
}
.summary-formula {
  font-family: 'Courier New', monospace;
  color: #fff4c9;
  background: rgba(0,0,0,0.22);
  border: 1px solid rgba(245,230,200,0.22);
  border-radius: 5px;
  padding: 5px 8px;
  line-height: 1.65;
}
table.summary-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 11px;
}
table.summary-table th,
table.summary-table td {
  border: 1px solid rgba(245,230,200,0.24);
  padding: 3px 4px;
  text-align: center;
}
table.summary-table th {
  background: rgba(245,230,200,0.14);
  color: #fff4c9;
}
table.summary-table td {
  color: #f7f0cf;
}

/* CONTROLS */
.controls {
  padding: 8px 40px; display: flex; align-items: center; justify-content: center; gap: 20px;
  border-top: 1px solid rgba(200,160,90,0.3);
  background: rgba(90,45,12,0.05); flex-shrink: 0; z-index: 1;
}
.btn {
  padding: 7px 22px;
  border: 2px solid #c8a05a; background: transparent;
  color: #5a2d0c; font-family: 'Noto Serif SC', serif;
  font-size: 14px; cursor: pointer; border-radius: 4px; transition: all 0.2s;
}
.btn:hover:not(:disabled) { background: rgba(200,160,90,0.25); }
.btn:disabled { opacity: 0.35; cursor: default; }
.btn.primary { background: #c8a05a; color: #1a0800; font-weight: 700; }
.btn.primary:hover:not(:disabled) { background: #e8c060; }
.auto-toggle {
  min-width: 148px;
  text-align: center;
  font-weight: 700;
  background: rgba(255,252,230,0.62);
}
.auto-toggle.paused {
  background: #c8a05a;
  color: #1a0800;
}

/* ── BACKGROUND INK DECORATIONS ─────────────────────────────────── */
.bg-ink {
  position: fixed; inset: 0; pointer-events: none; z-index: 0; overflow: hidden;
}
.clouds-svg {
  position: absolute; inset: 0; width: 100%; height: 100%;
}
.mountain-svg {
  position: absolute; bottom: 0; right: 0; width: 680px; height: 520px;
}

/* Cloud drift animations */
@keyframes cloudDrift1 {
  0%, 100% { transform: translate(0px, 0px); }
  30%       { transform: translate(18px, -6px); }
  65%       { transform: translate(-10px, 4px); }
}
@keyframes cloudDrift2 {
  0%, 100% { transform: translate(0px, 0px); }
  40%       { transform: translate(-15px, -5px); }
  75%       { transform: translate(12px, 5px); }
}
@keyframes cloudDrift3 {
  0%, 100% { transform: translate(0px, 0px); }
  50%       { transform: translate(22px, -4px); }
}
/* Mist breathing */
@keyframes mistBreathe {
  0%, 100% { opacity: 0.16; }
  50%       { opacity: 0.26; }
}
@keyframes mistBreathe2 {
  0%, 100% { opacity: 0.12; }
  50%       { opacity: 0.20; }
}

.c1 { animation: cloudDrift1 14s ease-in-out infinite; }
.c2 { animation: cloudDrift2 19s ease-in-out infinite; }
.c3 { animation: cloudDrift3 22s ease-in-out infinite; }
.c4 { animation: cloudDrift1 17s ease-in-out infinite 4s; }
.c5 { animation: cloudDrift2 25s ease-in-out infinite 7s; }
.mist1 { animation: mistBreathe 8s ease-in-out infinite; }
.mist2 { animation: mistBreathe2 11s ease-in-out infinite 2s; }
.mist3 { animation: mistBreathe 14s ease-in-out infinite 5s; }

/* CHALLENGE PLACEHOLDER PAGE */
.game-main {
  flex: 1;
  width: 100%;
  display: grid;
  place-items: center;
  padding: 48px 24px;
  position: relative;
  z-index: 1;
}
.game-panel {
  width: min(760px, 92vw);
  border: 10px solid #7a4a22;
  border-radius: 6px;
  background:
    linear-gradient(145deg, rgba(255,255,255,0.06), transparent 36%),
    #18352d;
  color: #f7f0cf;
  padding: 42px 46px;
  box-shadow:
    inset 0 0 18px rgba(0,0,0,0.42),
    0 18px 36px rgba(58,26,0,0.18);
}
.game-title {
  color: #fff4c9;
  font-size: clamp(28px, 3vw, 42px);
  letter-spacing: 4px;
  text-align: center;
  margin-bottom: 18px;
}
.game-copy {
  font-size: 17px;
  line-height: 2;
  text-align: center;
  margin-bottom: 28px;
}
.game-actions {
  display: flex;
  justify-content: center;
}
.game-actions .btn {
  color: #f5e6c8;
  text-decoration: none;
}
.game-actions .btn.primary {
  color: #1a0800;
}

@media (max-width: 1320px) {
  .main {
    --stage-baseline: clamp(140px, 15vh, 190px);
    padding-left: 16px;
    padding-right: 16px;
  }
  .header { padding-left: 24px; padding-right: 24px; }
  .header-title { font-size: 22px; letter-spacing: 3px; }
  .header-sub { font-size: 14px; }
  .tower-panel {
    left: 30%;
    width: 32%;
  }
  .blackboard {
    left: clamp(52px, 4vw, 72px);
    width: min(30vw, 440px);
    padding: 16px 18px 18px;
  }
  .scene-header {
    left: clamp(52px, 4vw, 72px);
    width: min(30vw, 400px);
  }
  .right-panel {
    left: 47%;
    right: 24px;
  }
  .elder-talk { left: 35%; right: 4%; }
  .challenger-log { left: 31%; right: 31%; }
  .chat-bubble { max-width: min(390px, 100%); }
}

@media (max-width: 1220px) {
  .main { --stage-baseline: clamp(128px, 14vh, 172px); }
  .header-title { font-size: 20px; }
  .scene-nav { gap: 5px; }
  .scene-btn { width: 54px; }
  .scene-btn.challenge-btn { width: 68px; }
  .tower-panel { left: 29%; width: 33%; }
  #triSvg { width: min(100%, 440px); height: 410px; }
  .blackboard {
    left: 42px;
    width: min(30vw, 396px);
  }
  .scene-header { left: 42px; width: min(30vw, 360px); }
  .right-panel {
    left: 46%;
    right: 22px;
  }
  .elder-figure { width: 31%; height: 46%; }
  .elder-img { height: 116%; transform: translateY(6%); }
  .challenger-figure { right: 2.5%; width: 31%; height: 46%; }
  .challenger-img { height: 122%; transform: translateY(10%); }
  .elder-talk { left: 35%; right: 3%; }
  .challenger-log { left: 30%; right: 31%; top: 19%; }
  .chat-bubble { font-size: 13px; max-width: min(350px, 100%); }
  .controls { gap: 10px; padding-left: 20px; padding-right: 20px; }
  .btn { padding-left: 14px; padding-right: 14px; }
}
