:root {
  --bg-parchment: #f5eedc;
  --bg-card: #fffaf0;
  --bg-elevated: #efe4c8;
  --bg-dark-ink: #1a1614;
  --bg-header: #1f1916;
  --ink-primary: #1c1613;
  --ink-secondary: #524338;
  --ink-muted: #7d6b5d;
  --accent-red: #b82925;
  --accent-red-dark: #8c1b18;
  --accent-gold: #d99b26;
  --accent-gold-light: #f7e1a0;
  --accent-green: #2c6e49;
  --accent-blue: #235789;
  --accent-purple: #5e3572;
  --border-ink: #2b221e;
  --border-soft: #d5c5a3;
  --shadow-cartoon: 4px 4px 0px #1c1613;
  --shadow-cartoon-sm: 2px 2px 0px #1c1613;
  --radius-md: 12px;
  --radius-lg: 16px;
}

body.dark-mode {
  --bg-parchment: #181412;
  --bg-card: #241e1b;
  --bg-elevated: #302824;
  --ink-primary: #f5eedc;
  --ink-secondary: #cbbba0;
  --ink-muted: #968571;
  --border-ink: #d99b26;
  --border-soft: #42362f;
  --shadow-cartoon: 4px 4px 0px #090706;
  --shadow-cartoon-sm: 2px 2px 0px #090706;
}

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

body {
  font-family: 'Sarabun', 'Chakra Petch', -apple-system, BlinkMacSystemFont, sans-serif;
  background-color: var(--bg-parchment);
  background-image: radial-gradient(rgba(43, 34, 30, 0.06) 1px, transparent 0);
  background-size: 20px 20px;
  color: var(--ink-primary);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

h1, h2, h3, h4, .font-heading {
  font-family: 'Chakra Petch', 'Sarabun', sans-serif;
  font-weight: 700;
  letter-spacing: 0.01em;
}

/* Top Newspaper / Cartoon Header */
.app-header {
  background: var(--bg-header);
  color: #f5eedc;
  border-bottom: 4px solid var(--accent-gold);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

.header-top {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-emblem {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent-red);
  border: 3px solid var(--accent-gold);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  box-shadow: 2px 2px 0 #000;
  flex-shrink: 0;
}

.brand-title {
  font-size: 1.35rem;
  font-weight: 800;
  color: #fff;
  line-height: 1.15;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.brand-badge {
  background: var(--accent-gold);
  color: #1c1613;
  font-size: 0.72rem;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-weight: 800;
  text-transform: uppercase;
}

.brand-subtitle {
  font-size: 0.82rem;
  color: #d5c5a3;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* Main Navigation Tabs */
.main-nav {
  background: #27201c;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.nav-container {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 1rem;
  display: flex;
  gap: 0.35rem;
  overflow-x: auto;
  scrollbar-width: thin;
}

.nav-tab {
  background: transparent;
  color: #d5c5a3;
  border: none;
  border-bottom: 3px solid transparent;
  padding: 0.7rem 1rem;
  font-family: 'Chakra Petch', sans-serif;
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  transition: all 0.15s ease;
}

.nav-tab:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.05);
}

.nav-tab.active {
  color: var(--accent-gold);
  border-bottom-color: var(--accent-gold);
  background: rgba(217, 155, 38, 0.12);
}

/* Main Content Layout */
.app-main {
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
  padding: 1.25rem;
  flex: 1;
}

.tab-panel {
  display: none;
  animation: fadeIn 0.2s ease;
}

.tab-panel.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Cartoon Cards & Containers */
.cartoon-card {
  background: var(--bg-card);
  border: 2.5px solid var(--border-ink);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-cartoon);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

.card-header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding-bottom: 0.75rem;
  margin-bottom: 1rem;
  border-bottom: 2px dashed var(--border-soft);
}

.card-title {
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* Buttons */
.btn {
  font-family: 'Chakra Petch', 'Sarabun', sans-serif;
  font-weight: 700;
  font-size: 0.88rem;
  padding: 0.5rem 0.95rem;
  border-radius: 8px;
  border: 2px solid var(--border-ink);
  background: var(--bg-elevated);
  color: var(--ink-primary);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  box-shadow: var(--shadow-cartoon-sm);
  transition: transform 0.1s ease, box-shadow 0.1s ease, background 0.15s ease;
  text-decoration: none;
}

.btn:hover:not(:disabled) {
  transform: translate(-1px, -1px);
  box-shadow: 3px 3px 0px var(--border-ink);
}

.btn:active:not(:disabled) {
  transform: translate(1px, 1px);
  box-shadow: 1px 1px 0px var(--border-ink);
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent-red);
  color: #fff;
  border-color: #1c1613;
}

.btn-gold {
  background: var(--accent-gold);
  color: #1c1613;
  border-color: #1c1613;
}

.btn-green {
  background: var(--accent-green);
  color: #fff;
  border-color: #1c1613;
}

.btn-purple {
  background: var(--accent-purple);
  color: #fff;
  border-color: #1c1613;
}

.btn-sm {
  padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
}

/* Inputs & Selects */
.input, .select, .textarea {
  font-family: 'Sarabun', sans-serif;
  font-size: 0.95rem;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 2px solid var(--border-ink);
  background: var(--bg-card);
  color: var(--ink-primary);
  width: 100%;
}

.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent-red);
  box-shadow: 0 0 0 3px rgba(184, 41, 37, 0.2);
}

/* Badges & Tags */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.18rem 0.55rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 700;
  border: 1.5px solid var(--border-ink);
}

.badge-base { background: #e6d7b8; color: #1c1613; }
.badge-foul { background: #d8e2dc; color: #1b4332; }
.badge-oddjobs { background: #e0c3fc; color: #3c096c; }
.badge-win { background: #d4edda; color: #155724; }
.badge-special { background: #fff3cd; color: #856404; }
.badge-lose { background: #f8d7da; color: #721c24; }

/* Rulebook 3D Digital Flipbook & Original Layout Viewer */
.rulebook-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.book-selector-group {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.flipbook-control-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  flex-wrap: wrap;
  background: #241e1b;
  color: #f5eedc;
  padding: 0.65rem 0.9rem;
  border-radius: 10px;
  border: 2.5px solid var(--border-ink);
  margin-bottom: 0.9rem;
  box-shadow: var(--shadow-cartoon-sm);
}

.flip-mode-group,
.flip-nav-group,
.flip-tools-group {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.flip-page-badge {
  background: #14100e;
  color: var(--accent-gold);
  border: 1.5px solid var(--accent-gold);
  border-radius: 6px;
  padding: 0.25rem 0.7rem;
  font-weight: 800;
  font-size: 0.86rem;
  min-width: 145px;
  text-align: center;
}

.flipbook-tabletop {
  position: relative;
  background:
    radial-gradient(circle at 50% 42%, #3a2d24 0%, #1c1511 72%, #110d0a 100%);
  border: 3px solid var(--border-ink);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-cartoon);
  overflow: hidden;
}

.flipbook-tabletop.fullscreen-table {
  position: fixed;
  inset: 0;
  z-index: 9990;
  border-radius: 0;
  display: flex;
  flex-direction: column;
}

.flipbook-tabletop.fullscreen-table .flipbook-viewport {
  flex: 1;
  height: auto;
  max-height: none;
  align-items: center;
}

.en-peek-floating-badge {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  background: rgba(184, 41, 37, 0.95);
  color: #fff9eb;
  border: 2px solid var(--accent-gold);
  border-radius: 999px;
  padding: 0.35rem 1.1rem;
  font-size: 0.85rem;
  font-weight: 700;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.55);
  pointer-events: none;
  white-space: nowrap;
}

.table-nav-arrow {
  position: absolute;
  top: 46%;
  transform: translateY(-50%);
  z-index: 40;
  width: 44px;
  height: 76px;
  border-radius: 10px;
  border: 2px solid rgba(255, 235, 190, 0.35);
  background: rgba(20, 16, 14, 0.78);
  color: #f5eedc;
  font-size: 2.2rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.18s ease;
  backdrop-filter: blur(4px);
}

.table-nav-arrow:hover:not(:disabled) {
  background: var(--accent-red);
  border-color: var(--accent-gold);
  transform: translateY(-50%) scale(1.06);
}

.table-nav-arrow:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.table-nav-arrow.left { left: 12px; }
.table-nav-arrow.right { right: 12px; }

.flipbook-viewport {
  position: relative;
  width: 100%;
  height: clamp(560px, 80vh, 880px);
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1rem 3.2rem;
  cursor: grab;
  user-select: none;
  touch-action: none;
}

.flipbook-viewport.is-dragging {
  cursor: grabbing;
}

@media (max-width: 768px) {
  .flipbook-viewport {
    height: clamp(420px, 70vh, 680px);
    padding: 0.5rem;
  }
  .table-nav-arrow {
    width: 34px;
    height: 56px;
    font-size: 1.6rem;
  }
}

.flipbook-3d-wrapper {
  width: 100%;
  max-width: 1180px;
  perspective: 2600px;
  display: flex;
  justify-content: center;
  align-items: center;
  transform: translate3d(var(--pan-x, 0px), var(--pan-y, 0px), 0px) scale(var(--zoom-scale, 1));
  transform-origin: center center;
  transition: transform 0.14s cubic-bezier(0.22, 1, 0.36, 1);
  will-change: transform;
}

.flipbook-viewport.is-dragging .flipbook-3d-wrapper {
  transition: none;
}

/* Realistic Open Book Paper Stack & Hardcover Rim */
.book-paper-stack {
  position: relative;
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: #dfd3b8;
  border-radius: 6px;
  border: 3px solid #1a1310;
  box-shadow:
    0 2px 0 #e9dfc8,
    0 5px 0 #cfc1a2,
    0 8px 0 #dfd3b8,
    0 11px 0 #bfb090,
    0 22px 45px rgba(0, 0, 0, 0.78);
  transform-style: preserve-3d;
  transition: transform 0.35s cubic-bezier(0.22, 1, 0.36, 1), max-width 0.3s ease;
}

/* Single-Page Mode (Front Cover / Back Cover / Single Sheet) */
.book-paper-stack.single-page-mode {
  grid-template-columns: 1fr;
  max-width: 54%;
}

.book-paper-stack.single-page-mode .book-page-left,
.book-paper-stack.single-page-mode .book-center-spine {
  display: none;
}

.book-page {
  position: relative;
  width: 100%;
  aspect-ratio: 612 / 792;
  background-color: #efe7d3;
  overflow: hidden;
  user-select: text;
  container-type: inline-size;
}

.book-paper-stack.oddjobs-ratio .book-page {
  aspect-ratio: 411 / 595;
}

.page-bg-layer {
  position: absolute;
  inset: 0;
  background-repeat: no-repeat;
  background-size: 200% 100%;
  z-index: 1;
  transition: filter 0.15s ease;
}

.book-page-left .page-bg-layer {
  background-position: 0% 0%;
}

.book-page-right .page-bg-layer {
  background-position: 100% 0%;
}

.book-paper-stack.single-page-mode .book-page-right .page-bg-layer {
  background-size: 100% 100%;
  background-position: 50% 50%;
}

/* Center Spine Crease & 3D Depth Shadows */
.book-center-spine {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 4px;
  transform: translateX(-50%);
  background: linear-gradient(
    to right,
    rgba(15, 10, 8, 0.55),
    rgba(255, 250, 235, 0.3) 50%,
    rgba(15, 10, 8, 0.6)
  );
  z-index: 25;
  pointer-events: none;
}

.page-spine-shadow-left {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    to right,
    rgba(0, 0, 0, 0.08) 0%,
    transparent 8%,
    transparent 88%,
    rgba(20, 14, 10, 0.14) 96%,
    rgba(10, 6, 4, 0.36) 100%
  );
  z-index: 15;
  pointer-events: none;
}

.page-spine-shadow-right {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    to left,
    rgba(0, 0, 0, 0.08) 0%,
    transparent 8%,
    transparent 88%,
    rgba(20, 14, 10, 0.14) 96%,
    rgba(10, 6, 4, 0.36) 100%
  );
  z-index: 15;
  pointer-events: none;
}

/* Interactive Corner Curls */
.page-corner-curl {
  position: absolute;
  bottom: 0;
  width: 52px;
  height: 52px;
  z-index: 30;
  cursor: pointer;
  transition: width 0.2s ease, height 0.2s ease, background 0.2s ease;
}

.page-corner-curl.left-curl {
  left: 0;
  background: linear-gradient(
    45deg,
    rgba(26, 19, 16, 0.32) 0%,
    rgba(235, 223, 195, 0.85) 46%,
    rgba(20, 14, 10, 0.18) 50%,
    transparent 54%
  );
  border-top-right-radius: 10px;
}

.page-corner-curl.right-curl {
  right: 0;
  background: linear-gradient(
    -45deg,
    rgba(26, 19, 16, 0.32) 0%,
    rgba(235, 223, 195, 0.85) 46%,
    rgba(20, 14, 10, 0.18) 50%,
    transparent 54%
  );
  border-top-left-radius: 10px;
}

.page-corner-curl:hover {
  width: 74px;
  height: 74px;
}

/* 3D Animated Turning Page Leaf */
.book-flip-leaf {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  z-index: 35;
  pointer-events: none;
  transform-style: preserve-3d;
  opacity: 0;
}

.book-paper-stack.single-page-mode .book-flip-leaf {
  width: 100%;
}

.flip-leaf-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  background-color: #efe7d3;
  background-repeat: no-repeat;
  overflow: hidden;
}

.flip-leaf-back {
  transform: rotateY(180deg);
}

.book-flip-leaf.turning-next {
  right: 0;
  left: auto;
  transform-origin: left center;
  opacity: 1;
  animation: flipPageNext 0.56s cubic-bezier(0.32, 0.08, 0.24, 1) forwards;
}

.book-flip-leaf.turning-prev {
  left: 0;
  right: auto;
  transform-origin: right center;
  opacity: 1;
  animation: flipPagePrev 0.56s cubic-bezier(0.32, 0.08, 0.24, 1) forwards;
}

@keyframes flipPageNext {
  0% {
    transform: rotateY(0deg);
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.25);
  }
  50% {
    box-shadow: -28px 0 45px rgba(0, 0, 0, 0.55);
  }
  100% {
    transform: rotateY(-180deg);
    box-shadow: -4px 0 12px rgba(0, 0, 0, 0.15);
  }
}

@keyframes flipPagePrev {
  0% {
    transform: rotateY(0deg);
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.25);
  }
  50% {
    box-shadow: 28px 0 45px rgba(0, 0, 0, 0.55);
  }
  100% {
    transform: rotateY(180deg);
    box-shadow: 4px 0 12px rgba(0, 0, 0, 0.15);
  }
}

/* Original-Layout Thai Overlay Layer & Text Blocks */
.page-overlay-layer {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: auto;
}

.layout-th-block {
  position: absolute;
  box-sizing: border-box;
  font-family: 'Sarabun', sans-serif;
  font-weight: 500;
  color: #1c1613;
  line-height: 1.48;
  letter-spacing: 0.008em;
  font-feature-settings: "kern" 1, "liga" 1, "mark" 1, "mkmk" 1;
  word-break: break-word;
  overflow-wrap: anywhere;
  padding: 0 2px;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.12s ease, box-shadow 0.12s ease;
}

.layout-th-block.is-bold,
.layout-th-block strong,
.layout-th-block b {
  font-weight: 800;
  -webkit-text-stroke: 0.15px currentColor;
}

.layout-th-block.role-heading {
  font-family: 'Sarabun', sans-serif;
  font-weight: 800;
  -webkit-text-stroke: 0.2px currentColor;
  line-height: 1.22;
  letter-spacing: 0.008em;
}

.layout-th-block.role-heading[data-lines]:not([data-lines="1"]) {
  line-height: 1.40;
  font-weight: 700;
  -webkit-text-stroke: 0.1px currentColor;
}

.layout-th-block.role-quote {
  font-style: italic;
  font-weight: 600;
  line-height: 1.48;
  letter-spacing: 0.01em;
}

.layout-th-block.align-center {
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.layout-th-block.preserve-newlines {
  white-space: pre-line;
  line-height: 1.46;
}

.layout-th-block.role-toc {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  white-space: nowrap;
  overflow: hidden;
  gap: 0.2rem;
}

.layout-th-block.role-toc .toc-title {
  flex: 0 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
}

.layout-th-block.role-toc .toc-dots {
  flex: 1 1 10px;
  border-bottom: 1.5px dotted rgba(28, 22, 19, 0.55);
  margin: 0 3px;
  transform: translateY(-2px);
}

.layout-th-block.role-toc .toc-page {
  flex: 0 0 auto;
  font-weight: 800;
}

.layout-th-block.is-light {
  color: #fff9eb;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.72);
}

/* Optional High-Contrast / Tinted Box Mode */
.page-overlay-layer.tinted-boxes .layout-th-block:not(.is-light) {
  background: rgba(242, 234, 214, 0.86);
  box-shadow: 0 0 0 1px rgba(45, 34, 26, 0.18);
}

.page-overlay-layer.tinted-boxes .layout-th-block.is-light {
  background: rgba(168, 42, 36, 0.82);
}

.layout-th-block:hover {
  background: rgba(255, 248, 220, 0.94) !important;
  color: #1c1613 !important;
  text-shadow: none !important;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.28), 0 0 0 1.5px var(--accent-gold);
  z-index: 28;
}

.flipbook-hint-bar {
  background: #1a1411;
  color: #d7c7a8;
  font-size: 0.82rem;
  padding: 0.45rem 0.9rem;
  text-align: center;
  border-top: 1.5px solid #3a2e27;
}

.page-thumbnails {
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  padding: 0.6rem;
  background: #1d1815;
  border-top: 2px solid var(--border-ink);
}

.page-thumb-btn {
  padding: 0.3rem 0.65rem;
  border-radius: 6px;
  border: 1.5px solid #524338;
  background: #2b231f;
  color: #d5c5a3;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}

.page-thumb-btn.active {
  background: var(--accent-gold);
  color: #1c1613;
  border-color: #fff;
}

.rule-section-box {
  background: var(--bg-elevated);
  border-left: 4px solid var(--accent-red);
  padding: 0.9rem 1rem;
  border-radius: 0 8px 8px 0;
  margin-bottom: 0.25rem;
}

.rule-section-box h4 {
  color: var(--accent-red);
  margin-bottom: 0.35rem;
  font-size: 1.02rem;
}

.rule-section-body {
  white-space: pre-line;
  font-size: 0.95rem;
}

/* Save Game Grid & Player Boards */
.save-top-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 1.25rem;
  margin-bottom: 1.25rem;
}

@media (max-width: 960px) {
  .save-top-grid {
    grid-template-columns: 1fr;
  }
}

.town-map-stages {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.65rem;
  margin-top: 0.75rem;
}

@media (max-width: 768px) {
  .town-map-stages {
    grid-template-columns: repeat(2, 1fr);
  }
}

.stage-box {
  background: var(--bg-elevated);
  border: 2px solid var(--border-ink);
  border-radius: 10px;
  padding: 0.75rem;
  position: relative;
}

.stage-box.current-stage {
  border-color: var(--accent-red);
  box-shadow: 0 0 0 3px rgba(184, 41, 37, 0.25);
}

.stage-box.defeated-stage {
  opacity: 0.85;
  background: rgba(44, 110, 73, 0.14);
}

.beatin-order-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.beatin-slot {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--bg-elevated);
  border: 2px solid var(--border-ink);
  border-radius: 8px;
  padding: 0.45rem 0.75rem;
}

.beatin-rank {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--ink-primary);
  color: var(--bg-card);
  font-weight: 800;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.party-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
  gap: 1.15rem;
}

.townsfolk-sheet-card {
  background: var(--bg-card);
  border: 2.5px solid var(--border-ink);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-cartoon);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.stat-counters-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.45rem;
}

.stat-pill {
  background: var(--bg-elevated);
  border: 2px solid var(--border-ink);
  border-radius: 8px;
  padding: 0.4rem 0.3rem;
  text-align: center;
}

.stat-pill-label {
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
  color: var(--ink-secondary);
}

.stat-pill-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  margin-top: 0.2rem;
}

.stat-pill-val {
  font-family: 'Chakra Petch', sans-serif;
  font-size: 1.15rem;
  font-weight: 800;
  min-width: 1.6rem;
}

.mini-step-btn {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 1.5px solid var(--border-ink);
  background: var(--bg-card);
  color: var(--ink-primary);
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.mini-step-btn:hover {
  background: var(--accent-gold);
  color: #1c1613;
}

.gear-slots-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.gear-slot-field label {
  display: block;
  font-size: 0.74rem;
  font-weight: 700;
  color: var(--ink-secondary);
  margin-bottom: 0.15rem;
}

/* Checklist & Trophy Wall */
.trophy-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 290px), 1fr));
  gap: 0.85rem;
}

.trophy-card {
  background: var(--bg-elevated);
  border: 2px solid var(--border-ink);
  border-radius: 10px;
  padding: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.trophy-card.mounted {
  border-color: var(--accent-gold);
  background: linear-gradient(135deg, var(--bg-elevated), rgba(217, 155, 38, 0.18));
}

.win-pip-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.pip-check {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  background: var(--bg-card);
  padding: 0.25rem 0.55rem;
  border-radius: 6px;
  border: 1.5px solid var(--border-ink);
}

.pip-check input {
  accent-color: var(--accent-red);
  width: 16px;
  height: 16px;
}

/* D10 Dice Roller */
.dice-roller-box {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  flex-wrap: wrap;
  background: linear-gradient(135deg, #27201c, #181412);
  color: #f5eedc;
  padding: 1.15rem 1.5rem;
  border-radius: var(--radius-md);
  border: 3px solid var(--accent-gold);
  box-shadow: var(--shadow-cartoon);
  margin-bottom: 1.25rem;
}

.d10-die-face {
  width: 78px;
  height: 78px;
  border-radius: 16px;
  background: var(--accent-red);
  border: 3px solid var(--accent-gold);
  color: #fff;
  font-family: 'Chakra Petch', sans-serif;
  font-size: 2.3rem;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 3px 3px 0 #000;
  flex-shrink: 0;
  transition: transform 0.2s ease;
}

.d10-die-face.rolling {
  transform: rotate(360deg) scale(1.1);
}

.d10-die-face.crit-star {
  background: var(--accent-gold);
  color: #1c1613;
  animation: pulseCrit 0.8s infinite alternate;
}

@keyframes pulseCrit {
  from { transform: scale(1); }
  to { transform: scale(1.08); }
}

/* Database Cards Grid */
.db-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
  gap: 1rem;
}

/* Results Grid */
.results-number-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 0.4rem;
  margin-bottom: 1.25rem;
}

.result-num-btn {
  padding: 0.5rem 0.25rem;
  font-family: 'Chakra Petch', sans-serif;
  font-weight: 800;
  font-size: 0.92rem;
  border-radius: 8px;
  border: 2px solid var(--border-ink);
  background: var(--bg-elevated);
  color: var(--ink-primary);
  cursor: pointer;
}

.result-num-btn.active {
  background: var(--accent-red);
  color: #fff;
}

/* AI Assistant Key Pills */
.api-key-bar {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.key-pill {
  font-size: 0.76rem;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  border: 1.5px solid var(--border-ink);
  background: var(--bg-elevated);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
}

.key-pill.active-key {
  background: var(--accent-green);
  color: #fff;
  font-weight: 700;
}

/* Modal Lightbox */
.lightbox-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  z-index: 999;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.lightbox-modal.open {
  display: flex;
}

.lightbox-modal img {
  max-width: 95vw;
  max-height: 92vh;
  object-fit: contain;
  border: 3px solid var(--accent-gold);
  border-radius: 8px;
}

/* Toast Notification */
.toast-container {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toast {
  background: #1c1613;
  color: #f5eedc;
  border: 2px solid var(--accent-gold);
  padding: 0.65rem 1rem;
  border-radius: 8px;
  box-shadow: var(--shadow-cartoon);
  font-weight: 600;
  font-size: 0.88rem;
}

#btnHoldCompareEn {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* ==========================================================================
   Multi-Device Responsive Layout (Tablet, iPad, Mobile Portrait & Landscape)
   ========================================================================== */

@media (max-width: 1024px) {
  .app-main {
    padding: 0.9rem;
  }

  .header-top {
    padding: 0.6rem 0.9rem;
    gap: 0.65rem;
  }

  .flipbook-viewport {
    height: clamp(440px, 70vh, 720px);
    padding: 0.75rem 2.4rem;
  }

  .book-paper-stack.single-page-mode {
    max-width: 68%;
  }
}

@media (max-width: 768px) {
  .app-header {
    position: sticky;
    top: 0;
  }

  .header-top {
    padding: 0.5rem 0.75rem;
    gap: 0.5rem;
  }

  .brand-group {
    gap: 0.6rem;
    width: 100%;
  }

  .brand-emblem {
    width: 38px;
    height: 38px;
    border-width: 2px;
  }

  .brand-title {
    font-size: 1.05rem;
    gap: 0.35rem;
  }

  .brand-badge {
    font-size: 0.64rem;
    padding: 0.1rem 0.45rem;
  }

  .brand-subtitle {
    font-size: 0.74rem;
    line-height: 1.3;
  }

  .header-actions {
    width: 100%;
    justify-content: space-between;
    gap: 0.35rem;
  }

  .header-actions #saveSlotSelect {
    flex: 1 1 140px;
    min-width: 120px !important;
    font-size: 0.85rem;
    padding: 0.35rem 0.5rem;
  }

  .nav-container {
    padding: 0 0.5rem;
    gap: 0.2rem;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .nav-container::-webkit-scrollbar {
    display: none;
  }

  .nav-tab {
    padding: 0.55rem 0.75rem;
    font-size: 0.82rem;
  }

  .app-main {
    padding: 0.65rem;
  }

  .cartoon-card {
    padding: 0.85rem;
    margin-bottom: 0.9rem;
    box-shadow: 2px 2px 0px var(--border-ink);
  }

  .card-title {
    font-size: 1.05rem;
  }

  /* Prevent iOS Safari auto-zoom on input focus */
  .input, .select, .textarea {
    font-size: 16px;
  }

  .rulebook-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .book-selector-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.35rem;
  }

  .book-selector-group .btn {
    width: 100%;
    font-size: 0.78rem;
    padding: 0.4rem 0.5rem;
  }

  .flipbook-control-bar {
    padding: 0.5rem;
    gap: 0.45rem;
    justify-content: center;
  }

  .flip-mode-group,
  .flip-nav-group,
  .flip-tools-group {
    width: 100%;
    justify-content: center;
    gap: 0.3rem;
  }

  .flip-page-badge {
    min-width: 115px;
    font-size: 0.78rem;
    padding: 0.2rem 0.5rem;
  }

  .flipbook-viewport {
    height: clamp(320px, 56vh, 560px);
    padding: 0.35rem 0.5rem;
  }

  .book-paper-stack.single-page-mode {
    max-width: 86%;
  }

  .table-nav-arrow {
    width: 32px;
    height: 52px;
    font-size: 1.5rem;
  }

  .table-nav-arrow.left { left: 4px; }
  .table-nav-arrow.right { right: 4px; }

  .flipbook-hint-bar {
    font-size: 0.74rem;
    padding: 0.4rem 0.55rem;
    line-height: 1.35;
  }

  .dice-roller-box {
    padding: 0.85rem 1rem;
    gap: 0.85rem;
  }

  .d10-die-face {
    width: 64px;
    height: 64px;
    font-size: 1.9rem;
  }

  .toast-container {
    left: 0.75rem;
    right: 0.75rem;
    bottom: 0.75rem;
  }

  .toast {
    width: 100%;
    text-align: center;
  }
}

@media (max-width: 480px) {
  .brand-subtitle {
    display: none;
  }

  .book-selector-group {
    grid-template-columns: 1fr;
  }

  .town-map-stages {
    grid-template-columns: 1fr;
  }

  .stat-counters-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .mini-step-btn {
    width: 28px;
    height: 28px;
  }

  .gear-slots-grid {
    grid-template-columns: 1fr;
  }

  .flipbook-viewport {
    height: clamp(270px, 48vh, 440px);
    padding: 0.25rem;
  }

  .book-paper-stack.single-page-mode {
    max-width: 94%;
  }
}

@media (max-height: 540px) and (orientation: landscape) {
  .app-header {
    position: static;
  }

  .flipbook-viewport {
    height: 82vh;
    padding: 0.35rem 2rem;
  }
}

/* =========================================================================
   GOOGLE ACCOUNT CLOUD SAVE LOGIN & BADGE
   ========================================================================= */
.btn-google-login {
  background: #ffffff;
  color: #1f1f1f;
  border: 2px solid var(--accent-gold);
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}

.btn-google-login:hover:not(:disabled) {
  background: #fff8e6;
}

.google-user-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  background: #2b231f;
  border: 2px solid var(--accent-gold);
  border-radius: 999px;
  padding: 0.18rem 0.45rem 0.18rem 0.25rem;
  color: #f5eedc;
}

.google-user-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  object-fit: cover;
  border: 1.5px solid var(--accent-gold);
}

.google-user-initial {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent-red);
  color: #fff;
  font-weight: 800;
  font-size: 0.78rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--accent-gold);
}

.google-user-info {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
  max-width: 155px;
}

.google-user-email {
  font-size: 0.74rem;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cloud-sync-status {
  font-size: 0.66rem;
  color: #8ce99a;
}

.google-auth-modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(15, 11, 9, 0.76);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.google-auth-card {
  width: 100%;
  max-width: 500px;
  max-height: 92vh;
  overflow-y: auto;
  margin: 0;
  animation: modalPopIn 0.18s ease-out;
}

@keyframes modalPopIn {
  from {
    opacity: 0;
    transform: scale(0.95) translateY(8px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

