/* ==========================================================================
   Power Grid Custom Map Rulebook — Centralized Stylesheet
   Used by both the public scrollable/printable rulebook and the WYSIWYG editor.
   Book format: Half-Letter (5.5in x 8.5in — 8.5x11 folded in half)
   ========================================================================== */

:root {
  --pg-ink: #1e201e;
  --pg-muted: #5a635a;
  --pg-green-dark: #1f4e2a;
  --pg-green-body: #4a6b46;
  --pg-green-tint: #cfe0c3;
  --pg-green-soft: #eef4eb;
  --pg-border: #1e201e;
  --pg-desk-bg: #e7ebe4;
  --pg-page-bg: #ffffff;
  --pg-accent: #2d5a36;
  --pg-danger: #b91c1c;
  --pg-font: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --pg-page-w: 5.5in;
  --pg-page-h: 8.5in;
  --pg-pad-x: 0.42in;
  --pg-pad-y: 0.44in;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--pg-font);
  color: var(--pg-ink);
  background: var(--pg-desk-bg);
  -webkit-font-smoothing: antialiased;
}

/* Dark Theme Variables (Matches powergridledger.calebely.com; @media print stays light) */
[data-theme="dark"] {
  color-scheme: dark;
  --pg-ink: #ffffff;
  --pg-muted: #a0a0a5;
  --pg-green-dark: #22c55e;
  --pg-green-body: #d4d4d8;
  --pg-green-tint: #3b3b3b;
  --pg-green-soft: #2c2c2e;
  --pg-border: #2a2a2a;
  --pg-desk-bg: #0d0d0d;
  --pg-page-bg: #1a1a1a;
}

/* Custom Theme-Aware Scrollbars */
* {
  scrollbar-width: thin;
  scrollbar-color: #b7c4b3 transparent;
}

[data-theme="dark"] * {
  scrollbar-color: #3b3b3b #141414;
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #b7c4b3;
  border-radius: 999px;
}

::-webkit-scrollbar-thumb:hover {
  background: #8ea389;
}

[data-theme="dark"] ::-webkit-scrollbar-track {
  background: #141414;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: #3b3b3b;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
  background: #52525b;
}

/* --------------------------------------------------------------------------
   Top Toolbar (Screen Only)
   -------------------------------------------------------------------------- */
.top-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid #d4dcd0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

[data-theme="dark"] .top-bar {
  background: #1c1c1c;
  border-bottom-color: #2a2a2a;
}

.top-bar__brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 800;
  font-size: 14px;
  letter-spacing: 0.02em;
  color: var(--pg-green-dark);
  text-decoration: none;
}

.top-bar__zap {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  color: var(--pg-green-dark);
}

.top-bar__actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 6px;
  font-family: var(--pg-font);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
  text-decoration: none;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn svg {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

.btn:active {
  transform: translateY(1px);
}

/* Header TOC Dropdown */
.toc-dropdown {
  position: relative;
}

.toc-dropdown summary {
  list-style: none;
}

.toc-dropdown summary::-webkit-details-marker {
  display: none;
}

.toc-dropdown__menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 245px;
  max-height: 70vh;
  overflow-y: auto;
  background: var(--pg-page-bg);
  border: 1.5px solid var(--pg-border);
  border-radius: 8px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.16);
  padding: 6px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toc-dropdown__link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 5px;
  text-decoration: none;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--pg-green-body);
  letter-spacing: 0.02em;
}

.toc-dropdown__link svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.toc-dropdown__btn {
  width: 100%;
  background: transparent;
  border: 0;
  font-family: var(--pg-font);
  cursor: pointer;
  text-align: left;
}

.toc-dropdown__link:hover {
  background: var(--pg-green-soft);
  color: var(--pg-green-dark);
}

.toc-dropdown__link--core {
  color: var(--pg-green-dark);
  font-weight: 800;
}

.toc-dropdown__divider {
  margin: 4px 6px;
  border-top: 1px solid #d4dcd0;
  font-size: 9.5px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--pg-muted);
  padding-top: 5px;
  text-transform: uppercase;
}

[data-theme="dark"] .toc-dropdown__divider {
  border-top-color: #2f3d32;
}

.top-bar__user {
  font-size: 12px;
  color: var(--pg-green-dark);
  font-weight: 700;
}

.btn--primary {
  background: var(--pg-green-dark);
  color: #fff;
}

.btn--primary:hover {
  background: #173c20;
}

.btn--outline {
  background: #fff;
  color: var(--pg-ink);
  border-color: #c5cfc0;
}

.btn--outline:hover {
  background: var(--pg-green-soft);
  border-color: var(--pg-green-dark);
}

.btn--danger {
  background: #fef2f2;
  color: var(--pg-danger);
  border-color: #fecaca;
}

.btn--danger:hover {
  background: #fee2e2;
}

.btn--sm {
  padding: 5px 10px;
  font-size: 11px;
}

/* --------------------------------------------------------------------------
   Scrollable Document Container & Page-Break Indicators
   -------------------------------------------------------------------------- */
.rulebook-scroll {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 28px 16px 64px;
  gap: 28px;
}

.rulebook-section {
  width: var(--pg-page-w);
  background: var(--pg-page-bg);
  box-shadow: 0 8px 28px rgba(20, 32, 20, 0.12);
  border-radius: 6px;
  overflow: hidden;
}

/* On screen: hide TOC page, page numbers, and duplicate map-image title */
@media screen {
  body:not(.is-exporting-pdf) .rulebook-section--print-only {
    display: none;
  }

  body:not(.is-exporting-pdf) .book-page {
    min-height: auto;
    box-shadow: none;
    background: transparent;
    padding-bottom: 0.28in;
  }

  body:not(.is-exporting-pdf) .rulebook-section--map .book-page + .book-page {
    padding-top: 0;
  }

  body:not(.is-exporting-pdf) .rulebook-section--map .map-image-page .section-title {
    display: none;
  }

  body:not(.is-exporting-pdf) .page-footer-num {
    display: none;
  }
}

.book-page {
  position: relative;
  width: var(--pg-page-w);
  min-height: var(--pg-page-h);
  padding: var(--pg-pad-y) var(--pg-pad-x) 0.55in;
  background: var(--pg-page-bg);
  box-shadow: 0 8px 28px rgba(20, 32, 20, 0.12);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.page-footer-num {
  position: absolute;
  bottom: 0.24in;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 10.5pt;
  font-weight: 500;
  color: var(--pg-ink);
  letter-spacing: 0.04em;
}

/* --------------------------------------------------------------------------
   Cover & Back Cover Pages
   -------------------------------------------------------------------------- */
.cover-page {
  justify-content: space-between;
  align-items: center;
  text-align: center;
  padding-top: 1.7in;
  padding-bottom: 0.7in;
}

.cover-logo {
  width: 4.35in;
  max-width: 100%;
  height: auto;
}

.cover-logo--dark {
  display: none;
}

[data-theme="dark"] .cover-logo--light {
  display: none;
}

[data-theme="dark"] .cover-logo--dark {
  display: block;
}

.cover-title-block {
  margin-top: 1.9in;
}

.cover-title {
  margin: 0;
  font-size: 27pt;
  font-weight: 800;
  line-height: 1.06;
  letter-spacing: -0.02em;
  text-transform: uppercase;
  color: var(--pg-ink);
}

.cover-meta {
  margin-top: 0.38in;
  font-size: 9pt;
  line-height: 1.45;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--pg-green-dark);
}

.cover-meta strong {
  display: block;
  font-weight: 800;
}

.back-cover-page {
  justify-content: center;
  align-items: center;
  text-align: center;
}

.back-cover-qr {
  width: 2.3in;
  max-width: 75%;
  height: auto;
  border-radius: 10px;
}

/* --------------------------------------------------------------------------
   Section Titles & Table of Contents
   -------------------------------------------------------------------------- */
.section-title {
  margin: 0 0 10px;
  font-size: 24pt;
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: -0.02em;
  text-transform: uppercase;
  color: var(--pg-ink);
}

.section-rule {
  border: 0;
  border-top: 1.25px solid var(--pg-ink);
  margin: 0 0 18px;
}

.toc-list {
  margin: 0.45in auto 0;
  width: 76%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toc-group-label {
  margin-top: 16px;
  margin-bottom: 2px;
  padding-left: 44px;
  font-size: 10pt;
  font-weight: 800;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--pg-ink);
}

.toc-item {
  display: flex;
  align-items: baseline;
  gap: 18px;
  text-decoration: none;
  padding: 2px 4px;
  border-radius: 4px;
  transition: background 0.12s;
}

.toc-item:hover {
  background: var(--pg-green-soft);
}

.toc-item__page {
  width: 26px;
  flex-shrink: 0;
  font-size: 10pt;
  font-weight: 800;
  color: var(--pg-ink);
  text-align: left;
}

.toc-item__title {
  font-size: 10pt;
  font-weight: 600;
  color: var(--pg-green-body);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.toc-item--core .toc-item__title {
  font-weight: 800;
  color: var(--pg-green-dark);
}

/* --------------------------------------------------------------------------
   Core Setup & Resource Refill Tables
   -------------------------------------------------------------------------- */
.table-caption {
  margin: 14px 0 6px;
  font-size: 10.5pt;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.01em;
  color: var(--pg-ink);
}

.table-caption strong {
  font-weight: 800;
}

.table-subcaption {
  margin: -4px 0 6px;
  font-size: 9pt;
  font-weight: 500;
  text-transform: uppercase;
  color: var(--pg-ink);
}

.pg-table {
  width: 100%;
  border-collapse: collapse;
  border: 1.6px solid var(--pg-border);
  margin-bottom: 14px;
  font-size: 8.8pt;
}

.pg-table th,
.pg-table td {
  padding: 5.5px 6px;
  text-align: center;
  border: 1px dashed #4a554a;
}

.pg-table thead tr:first-child th {
  background: var(--pg-green-dark);
  color: #ffffff;
  font-weight: 700;
  border-bottom: 1.4px solid var(--pg-border);
}

.pg-table thead tr.pg-table__subhead th {
  background: var(--pg-green-tint);
  color: var(--pg-ink);
  font-weight: 800;
  border-bottom: 1.4px solid var(--pg-border);
}

.pg-table tbody td:first-child,
.pg-table thead th:first-child {
  font-weight: 700;
  border-right: 1.4px solid var(--pg-border);
}

.pg-table__group-sep {
  border-left: 1.4px solid var(--pg-border) !important;
}

/* --------------------------------------------------------------------------
   Centralized WYSIWYG Content Styles (Used in Rulebook AND TipTap Editor)
   -------------------------------------------------------------------------- */
.rule-content {
  font-size: 9.2pt;
  line-height: 1.4;
  color: var(--pg-green-body);
}

.rule-heading {
  margin: 14px 0 3px;
  font-size: 10.5pt;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.01em;
  color: var(--pg-ink);
}

.rule-heading:first-child {
  margin-top: 0;
}

.rule-subheading {
  margin: 9px 0 2px;
  font-size: 9.4pt;
  font-weight: 700;
  color: var(--pg-ink);
}

.rule-body {
  margin: 0 0 7px;
  font-size: 9pt;
  line-height: 1.42;
  font-weight: 500;
  color: var(--pg-green-body);
}

.rule-body strong {
  font-weight: 700;
  color: var(--pg-green-dark);
}

.rule-body em {
  font-style: italic;
}

.rule-list {
  margin: 4px 0 10px;
  padding-left: 18px;
  color: var(--pg-green-body);
  font-size: 9pt;
  line-height: 1.45;
}

.rule-list li {
  margin-bottom: 2px;
}

.rule-callout {
  margin: 10px 0;
  padding: 8px 11px;
  background: var(--pg-green-soft);
  border-left: 3.5px solid var(--pg-green-dark);
  border-radius: 0 4px 4px 0;
  color: var(--pg-green-dark);
  font-size: 8.8pt;
}

.rule-highlight {
  background: #e6f2df;
  color: var(--pg-green-dark);
  padding: 1px 4px;
  border-radius: 3px;
  font-weight: 700;
}

/* Visual marker inside WYSIWYG editor; split into separate .book-page on render */
.rule-page-break {
  margin: 14px 0;
  border-top: 2px dashed #8ca187;
  position: relative;
  text-align: center;
  height: 16px;
}

.rule-page-break span {
  position: relative;
  top: -9px;
  background: #fff;
  padding: 0 8px;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--pg-green-dark);
}

/* --------------------------------------------------------------------------
   Map Image Page
   -------------------------------------------------------------------------- */
.map-image-page {
  padding-top: 0.36in;
  padding-bottom: 0.52in;
}

.map-image-page .section-title {
  margin-bottom: 10px;
}

.map-frame {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  overflow: hidden;
}

.map-frame img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* Landscape maps: upright on screen, rotated 90deg for 5.5x8.5 print/PDF */
.map-frame--landscape {
  position: relative;
  width: 100%;
  height: auto;
}

.map-frame--landscape img {
  position: static;
  width: 100%;
  height: auto;
  max-width: 100%;
  max-height: none;
  object-fit: contain;
  transform: none;
}

body.is-exporting-pdf .map-frame--landscape {
  height: 6.75in;
}

body.is-exporting-pdf .map-frame--landscape img {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 6.75in;
  height: 4.65in;
  max-width: none;
  max-height: none;
  transform: translate(-50%, -50%) rotate(-90deg);
}

.map-frame--portrait img {
  max-height: 6.75in;
  width: auto;
}

@media screen {
  .map-frame img,
  .pg-table {
    cursor: zoom-in;
  }
}

/* Large Pinch-Zoomable Modal */
.pg-modal {
  position: fixed;
  inset: 0;
  z-index: 500;
  background: rgba(12, 18, 14, 0.88);
  backdrop-filter: blur(4px);
  display: none;
  flex-direction: column;
}

.pg-modal.is-open {
  display: flex;
}

.pg-modal__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 16px;
  background: #1c1c1c;
  color: #ffffff;
  border-bottom: 1px solid #2a2a2a;
}

.pg-modal__title {
  font-size: 14px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.pg-modal__controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pg-modal__controls .btn {
  padding: 8px 13px;
  font-size: 14px;
  min-width: 40px;
}

.pg-modal__viewport {
  flex: 1;
  overflow: hidden;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  user-select: none;
}

.pg-modal__content {
  transform-origin: center center;
  max-width: 100%;
}

.pg-modal__content img {
  display: block;
  max-width: 92vw;
  max-height: 82vh;
  width: auto;
  height: auto;
  border-radius: 6px;
  background: #fff;
  pointer-events: none;
}

.pg-modal__content .pg-table {
  font-size: 11.5pt;
  margin-bottom: 0;
  background: var(--pg-page-bg);
  cursor: default;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.45);
}

.pg-modal__content .pg-table th,
.pg-modal__content .pg-table td {
  padding: 8px 10px;
}

/* --------------------------------------------------------------------------
   Responsive Screen Adjustments
   -------------------------------------------------------------------------- */
@media screen and (max-width: 768px) {
  .top-bar {
    padding: 12px 16px;
    flex-wrap: nowrap;
    justify-content: space-between;
  }

  .top-bar__zap {
    width: 28px;
    height: 28px;
  }

  .top-bar__brand-text,
  .top-bar__user,
  .top-bar__signin,
  .btn__label {
    display: none;
  }

  .top-bar__actions {
    flex-wrap: nowrap;
    gap: 8px;
  }

  .btn {
    padding: 10px 11px;
  }

  .btn svg {
    width: 20px;
    height: 20px;
  }

  .toc-dropdown__menu {
    position: fixed;
    top: 62px;
    left: 12px;
    right: 12px;
    min-width: 0;
    width: auto;
    max-height: 75vh;
  }
}

@media screen and (max-width: 600px) {
  .rulebook-section {
    width: 100%;
  }

  .book-page {
    width: 100%;
    min-height: auto;
    padding: 20px 12px 42px;
  }

  .page-break-divider {
    width: 100%;
  }

  .cover-title {
    font-size: 22pt;
  }

  .section-title {
    font-size: 18pt;
  }

  .toc-list {
    width: 100%;
  }

  .pg-table {
    font-size: clamp(7.5px, 2.15vw, 10.5px);
  }

  .pg-table th,
  .pg-table td {
    padding: 3.5px 2px;
  }

  .table-subcaption {
    font-size: 7.5pt;
  }

  .map-frame--landscape {
    height: auto;
  }

  .map-frame--landscape img {
    position: static;
    width: 100%;
    height: auto;
    transform: none;
  }
}

[data-theme="dark"] .btn--primary {
  background: #16a34a;
  color: #ffffff;
}

[data-theme="dark"] .btn--primary:hover {
  background: #15803d;
}

[data-theme="dark"] .btn--outline {
  background: #2c2c2e;
  color: #ffffff;
  border-color: #3b3b3b;
}

[data-theme="dark"] .btn--outline:hover {
  background: #3b3b3b;
  border-color: #22c55e;
}

[data-theme="dark"] .pg-table thead tr:first-child th {
  background: #1c1c1c;
  color: #22c55e;
}

[data-theme="dark"] .rule-page-break span {
  background: var(--pg-page-bg);
}

/* --------------------------------------------------------------------------
   Print Stylesheet — 8.5 x 11 Folded in Half (5.5in x 8.5in Half-Letter)
   -------------------------------------------------------------------------- */
@media print {
  @page {
    size: 5.5in 8.5in;
    margin: 0;
  }

  :root,
  [data-theme="dark"] {
    --pg-ink: #1e201e;
    --pg-muted: #5a635a;
    --pg-green-dark: #1f4e2a;
    --pg-green-body: #4a6b46;
    --pg-green-tint: #cfe0c3;
    --pg-green-soft: #eef4eb;
    --pg-border: #1e201e;
    --pg-page-bg: #ffffff;
  }

  body {
    background: #ffffff !important;
    color: #1e201e !important;
  }

  .book-page {
    background: #ffffff !important;
    color: #1e201e !important;
  }

  .pg-table thead tr:first-child th {
    background: #1f4e2a !important;
    color: #ffffff !important;
  }

  .cover-logo--light,
  [data-theme="dark"] .cover-logo--light {
    display: block !important;
  }

  .cover-logo--dark,
  [data-theme="dark"] .cover-logo--dark {
    display: none !important;
  }

  .no-print,
  .top-bar,
  .page-break-divider {
    display: none !important;
  }

  .rulebook-scroll {
    padding: 0;
    margin: 0;
    display: block;
  }

  .rulebook-section,
  .rulebook-section--print-only {
    display: block !important;
    width: auto;
    background: transparent;
    box-shadow: none;
    border-radius: 0;
    overflow: visible;
  }

  .book-page {
    width: 5.5in;
    height: 8.5in;
    min-height: 8.5in;
    max-height: 8.5in;
    margin: 0;
    box-shadow: none;
    page-break-after: always;
    break-after: page;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .book-page:last-of-type {
    page-break-after: auto;
    break-after: auto;
  }

  .map-frame--landscape {
    height: 6.75in;
  }

  .map-frame--landscape img {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 6.75in;
    height: 4.65in;
    max-width: none;
    max-height: none;
    transform: translate(-50%, -50%) rotate(-90deg);
  }

  /* Collated 2-up Saddle-Stitch Booklet Mode (8.5x11 Landscape) */
  #booklet-print-root {
    display: none;
  }

  body.is-printing-booklet .rulebook-scroll {
    display: none !important;
  }

  body.is-printing-booklet #booklet-print-root {
    display: block !important;
  }

  .booklet-sheet {
    display: flex;
    flex-direction: row;
    width: 11in;
    height: 8.5in;
    margin: 0;
    padding: 0;
    page-break-after: always;
    break-after: page;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  .booklet-sheet:last-of-type {
    page-break-after: auto;
    break-after: auto;
  }

  .booklet-sheet .book-page {
    width: 5.5in;
    height: 8.5in;
    flex: 0 0 5.5in;
    page-break-after: auto;
    break-after: auto;
  }
}

@media screen {
  #booklet-print-root {
    display: none;
  }
}
