* { box-sizing: border-box; }

:root {
  --ink: #f8ead3;
  --muted: #d9c8aa;
  --gold: #c8a760;
  --red: #8e1621;
  --panel: rgba(14, 16, 19, 0.88);
  --cell: clamp(54px, 7vw, 86px);
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(35, 91, 56, 0.45), transparent 34rem),
    radial-gradient(circle at bottom right, rgba(127, 18, 34, 0.35), transparent 30rem),
    linear-gradient(135deg, #07110d 0%, #141216 45%, #06110c 100%);
  font-family: Georgia, "Times New Roman", serif;
}

button, input, select { font: inherit; }

.hitl-header {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  align-items: center;
  padding: 1.25rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid rgba(200, 167, 96, 0.35);
  background: rgba(0, 0, 0, 0.42);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(8px);
}

.eyebrow {
  margin: 0 0 .3rem;
  text-transform: uppercase;
  letter-spacing: .16em;
  color: var(--gold);
  font-size: .78rem;
}

h1, h2 { margin: 0; line-height: 1.08; }
h1 { font-size: clamp(1.7rem, 4vw, 3rem); }
h2 { font-size: 1.1rem; color: #fff3d8; }

.intro, .hint, #statusLine, .rules-list, .pdf-title p { color: var(--muted); }
.intro { max-width: 54rem; margin: .45rem 0 0; }

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .6rem;
  justify-content: flex-end;
}

.btn {
  border: 1px solid rgba(200, 167, 96, 0.65);
  color: #fff8e8;
  background: linear-gradient(#9b1e2c, #64131e);
  padding: .7rem .95rem;
  border-radius: .65rem;
  cursor: pointer;
  box-shadow: 0 5px 18px rgba(0,0,0,.24);
}
.btn:hover { filter: brightness(1.08); }
.btn-ghost { background: rgba(255,255,255,.06); }
.btn-gold {
  background: linear-gradient(#d0b06b, #816225);
  color: #15110a;
  font-weight: 700;
}
.btn-wide { width: 100%; margin-top: .65rem; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
  gap: 1rem;
  padding: 1rem;
  align-items: start;
}

.control-panel { display: grid; gap: 1rem; }

.panel-card, .board-panel {
  border: 1px solid rgba(200, 167, 96, 0.32);
  background: var(--panel);
  border-radius: 1rem;
  box-shadow: 0 12px 45px rgba(0,0,0,.25);
}
.panel-card { padding: 1rem; }

label {
  display: grid;
  gap: .35rem;
  margin-top: .85rem;
  color: #fff1d5;
}

input, select {
  width: 100%;
  border: 1px solid rgba(200, 167, 96, .4);
  border-radius: .55rem;
  padding: .65rem;
  background: rgba(0,0,0,.35);
  color: var(--ink);
}

.stats {
  display: grid;
  gap: .35rem;
  margin-top: .9rem;
  color: var(--muted);
  font-size: .95rem;
}

.tile-stack {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: .65rem;
  margin-top: .75rem;
}

.room-tile {
  position: relative;
  display: grid;
  gap: .35rem;
  padding: .45rem;
  border: 1px solid rgba(200, 167, 96, .35);
  border-radius: .7rem;
  background: rgba(255,255,255,.05);
  color: var(--ink);
  text-align: left;
  cursor: grab;
  user-select: none;
}
.room-tile img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: .45rem;
  border: 1px solid rgba(255,255,255,.12);
}
.tile-name { font-size: .86rem; min-height: 2.1em; }
.tile-badges {
  position: absolute;
  right: .55rem;
  top: .55rem;
  display: flex;
  gap: .25rem;
}

.badge {
  display: inline-grid;
  place-items: center;
  width: 1.45rem;
  height: 1.45rem;
  border-radius: 50%;
  color: #160f08;
  background: var(--gold);
  font-size: .75rem;
  font-weight: 700;
  border: 1px solid rgba(0,0,0,.35);
}
.badge.portal { background: #7fff7f; }

.rules-list { padding-left: 1.2rem; margin-bottom: 0; }

.validation-box {
  margin-top: .75rem;
  padding: .75rem;
  border-radius: .6rem;
  background: rgba(0,0,0,.28);
  min-height: 3.2rem;
  color: var(--muted);
}
.validation-box.ok { border: 1px solid rgba(100, 220, 130, .6); }
.validation-box.warn { border: 1px solid rgba(255, 190, 80, .75); }

.board-panel { padding: 1rem; overflow: auto; }

.board-toolbar, .pdf-title {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: end;
  margin-bottom: .8rem;
}

.legend {
  display: flex;
  gap: .85rem;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: .9rem;
}
.dot {
  display: inline-block;
  width: .8rem;
  height: .8rem;
  border-radius: 50%;
  margin-right: .25rem;
  vertical-align: -1px;
}
.portal-dot { background: #7fff7f; }
.mirror-dot { background: var(--gold); }

.pdf-area {
  display: inline-block;
  min-width: max-content;
  padding: 1rem;
  border-radius: .9rem;
  background:
    linear-gradient(rgba(8, 9, 11, .72), rgba(8, 9, 11, .9)),
    repeating-linear-gradient(45deg, rgba(200,167,96,.07) 0 2px, transparent 2px 18px);
  border: 1px solid rgba(200, 167, 96, .4);
}

.pdf-title { align-items: center; }
.pdf-title h2 { font-size: 1.45rem; }

.grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-size, 7), var(--cell));
  grid-template-rows: repeat(var(--grid-size, 7), var(--cell));
  gap: 4px;
  padding: 4px;
  background: rgba(200,167,96,.24);
  border: 2px solid rgba(200,167,96,.65);
}

.cell {
  width: var(--cell);
  height: var(--cell);
  background:
    linear-gradient(rgba(255,255,255,.05), rgba(255,255,255,.02)),
    rgba(0,0,0,.36);
  border: 1px dashed rgba(200,167,96,.24);
  position: relative;
}
.cell.drag-over {
  outline: 3px solid var(--gold);
  outline-offset: -3px;
}

.placed-tile {
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: none;
  cursor: grab;
  position: relative;
}
.placed-tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: rotate(var(--rotation, 0deg));
  transition: transform .16s ease;
  display: block;
}
.placed-tile .marker {
  position: absolute;
  right: 4px;
  top: 4px;
  z-index: 3;
  display: flex;
  gap: 3px;
}
.placed-tile .coords {
  position: absolute;
  left: 4px;
  bottom: 3px;
  background: rgba(0,0,0,.64);
  color: #fff6df;
  font-size: .62rem;
  padding: 1px 4px;
  border-radius: 4px;
}

.saved-maps-list {
  display: grid;
  gap: .55rem;
  min-width: min(90vw, 36rem);
}
.saved-map-row {
  display: flex;
  justify-content: space-between;
  gap: .75rem;
  align-items: center;
  border: 1px solid rgba(200, 167, 96, .22);
  padding: .7rem;
  border-radius: .55rem;
  background: rgba(0,0,0,.22);
}

dialog {
  border: 1px solid rgba(200, 167, 96, .55);
  border-radius: .9rem;
  background: #151419;
  color: var(--ink);
  box-shadow: 0 20px 80px rgba(0,0,0,.45);
}
menu {
  padding: 0;
  display: flex;
  justify-content: flex-end;
}

@media (max-width: 980px) {
  .hitl-header, .board-toolbar, .pdf-title {
    align-items: start;
    flex-direction: column;
  }
  .app-shell { grid-template-columns: 1fr; }
  .tile-stack { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 620px) {
  :root { --cell: clamp(40px, 12vw, 58px); }
  .tile-stack { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* v1.3 PDF export: same-page print mode. No popups, no iframe, no CDN. */
@media print {
  @page {
    size: A4 landscape;
    margin: 8mm;
  }

  body.hitl-print-mode {
    background: #111318 !important;
    color: #f8ead3 !important;
  }

  body.hitl-print-mode .hitl-header,
  body.hitl-print-mode .control-panel,
  body.hitl-print-mode .board-toolbar,
  body.hitl-print-mode dialog {
    display: none !important;
  }

  body.hitl-print-mode .app-shell,
  body.hitl-print-mode .board-panel {
    display: block !important;
    padding: 0 !important;
    margin: 0 !important;
    background: transparent !important;
    border: 0 !important;
    box-shadow: none !important;
    overflow: visible !important;
  }

  body.hitl-print-mode .pdf-area {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    margin: 0 auto !important;
    padding: 8mm !important;
    border: 2px solid #c8a760 !important;
    border-radius: 0 !important;
    break-inside: avoid !important;
    page-break-inside: avoid !important;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  body.hitl-print-mode .grid {
    --cell: min(11.5vw, 22mm);
    margin: 0 auto !important;
    width: max-content !important;
    max-width: 100% !important;
    break-inside: avoid !important;
    page-break-inside: avoid !important;
  }

  body.hitl-print-mode .cell,
  body.hitl-print-mode .placed-tile img {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  body.hitl-print-mode .pdf-title {
    display: flex !important;
    flex-direction: row !important;
    align-items: end !important;
    justify-content: space-between !important;
    margin-bottom: 6mm !important;
  }
}


/* v1.4 zoom and pan grid viewport */
.board-tools {
  display: grid;
  gap: .6rem;
  justify-items: end;
}

.zoom-controls {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  justify-content: flex-end;
}

.btn-small {
  padding: .42rem .65rem;
  min-width: 2.6rem;
  border-radius: .5rem;
  font-size: .92rem;
}

.grid-viewport {
  position: relative;
  width: 100%;
  height: min(72vh, 820px);
  min-height: 520px;
  overflow: hidden;
  border: 1px solid rgba(200, 167, 96, .38);
  border-radius: .9rem;
  background:
    radial-gradient(circle at 20% 20%, rgba(200,167,96,.08), transparent 24rem),
    rgba(0,0,0,.28);
  cursor: grab;
  touch-action: none;
}

.grid-viewport.is-panning {
  cursor: grabbing;
}

.grid-viewport::before {
  content: "Mouse wheel / trackpad: zoom · drag empty map space to pan · click a room to rotate";
  position: absolute;
  left: .75rem;
  bottom: .75rem;
  z-index: 5;
  padding: .35rem .55rem;
  border-radius: .45rem;
  background: rgba(0,0,0,.62);
  color: rgba(248,234,211,.82);
  font-size: .78rem;
  pointer-events: none;
  border: 1px solid rgba(200,167,96,.28);
}

.grid-canvas {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.grid-canvas .pdf-area {
  margin: 0;
}

/* While panning, avoid accidentally dragging/selecting tiles */
body.is-map-panning .placed-tile,
body.is-map-panning .room-tile {
  pointer-events: none;
}

@media (max-width: 980px) {
  .board-tools {
    justify-items: start;
  }

  .grid-viewport {
    min-height: 440px;
    height: 64vh;
  }

  .grid-viewport::before {
    content: "Pinch/wheel: zoom · drag map space to pan";
  }
}

/* Print still outputs only the map, not the viewport controls. */
@media print {
  body.hitl-print-mode .grid-viewport {
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
    border: 0 !important;
    background: transparent !important;
  }

  body.hitl-print-mode .grid-viewport::before {
    display: none !important;
  }

  body.hitl-print-mode .grid-canvas {
    position: static !important;
    transform: none !important;
  }
}


/* v1.5: normal left-drag panning works from empty map/grid space. */
.grid-viewport {
  cursor: grab;
}

.grid-viewport.is-panning {
  cursor: grabbing;
}

.grid-viewport .cell {
  user-select: none;
}


/* v1.6: full-height workspace layout */
html,
body {
  height: 100%;
}

body {
  overflow: hidden;
}

.hitl-header {
  min-height: var(--header-height, 112px);
}

.app-shell {
  height: calc(100vh - var(--header-height, 112px));
  overflow: hidden;
  align-items: stretch;
}

.control-panel {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: .35rem;
  align-content: start;
  scrollbar-color: rgba(200, 167, 96, .7) rgba(0,0,0,.2);
}

.control-panel::-webkit-scrollbar {
  width: 10px;
}

.control-panel::-webkit-scrollbar-track {
  background: rgba(0,0,0,.22);
  border-radius: 999px;
}

.control-panel::-webkit-scrollbar-thumb {
  background: rgba(200, 167, 96, .62);
  border-radius: 999px;
  border: 2px solid rgba(0,0,0,.25);
}

.board-panel {
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.board-toolbar {
  flex: 0 0 auto;
}

.grid-viewport {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
}

@media (max-width: 980px) {
  body {
    overflow: auto;
  }

  .hitl-header {
    position: relative;
    min-height: 0;
  }

  .app-shell {
    height: auto;
    overflow: visible;
  }

  .control-panel {
    height: auto;
    max-height: none;
    overflow: visible;
    padding-right: 0;
  }

  .board-panel {
    height: auto;
    min-height: 70vh;
  }

  .grid-viewport {
    height: 68vh;
    min-height: 460px;
    flex: 0 0 auto;
  }
}

@media print {
  body {
    overflow: visible !important;
  }

  body.hitl-print-mode .app-shell {
    height: auto !important;
    overflow: visible !important;
  }

  body.hitl-print-mode .board-panel {
    height: auto !important;
    overflow: visible !important;
  }
}


/* v1.7 starter templates */
.template-buttons {
  display: grid;
  gap: .55rem;
  margin-top: .75rem;
}

.template-note {
  margin: .5rem 0 0;
  color: var(--muted);
  font-size: .9rem;
}


/* v1.8 Collector's Expansion double-length rooms */
.room-tile.wide-tile {
  grid-column: span 2;
}

.room-tile .wide-img {
  aspect-ratio: 2 / 1;
}

.badge.long {
  background: #d6d7de;
  color: #1c1e23;
}

.cell.covered-cell {
  background:
    linear-gradient(rgba(255,255,255,.03), rgba(255,255,255,.01)),
    rgba(0,0,0,.32);
}

.placed-tile {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 4;
  width: calc((var(--span-x, 1) * var(--cell)) + ((var(--span-x, 1) - 1) * 4px));
  height: calc((var(--span-y, 1) * var(--cell)) + ((var(--span-y, 1) - 1) * 4px));
  max-width: none;
  max-height: none;
}

.placed-tile img {
  border-radius: .2rem;
}

.cell.anchor-cell {
  z-index: 3;
}

body.is-map-panning .placed-tile,
body.is-map-panning .room-tile {
  pointer-events: none;
}

@media (max-width: 980px) {
  .room-tile.wide-tile {
    grid-column: span 3;
  }
}

@media (max-width: 620px) {
  .room-tile.wide-tile {
    grid-column: span 2;
  }
}


/* v1.9 fix: double-length rotated rooms need the image to be sized before rotation,
   otherwise a horizontal 2:1 image is clipped inside a vertical 1x2 footprint. */
.placed-tile {
  overflow: hidden;
}

.placed-tile.double-room img {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: center center;
}

.placed-tile.double-room-rotated img {
  left: 50%;
  top: 50%;
  width: calc((var(--span-y, 1) * var(--cell)) + ((var(--span-y, 1) - 1) * 4px));
  height: calc((var(--span-x, 1) * var(--cell)) + ((var(--span-x, 1) - 1) * 4px));
  transform: translate(-50%, -50%) rotate(var(--rotation, 0deg));
}

.placed-tile.double-room:not(.double-room-rotated) img {
  width: 100%;
  height: 100%;
  transform: rotate(var(--rotation, 0deg));
}


/* v2.1 hardened local file save/load */
.visually-hidden-file {
  position: fixed;
  left: -9999px;
  top: -9999px;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}


/* v2.3 JPG export buttons */
.header-actions {
  align-items: center;
}

@media (max-width: 980px) {
  .header-actions {
    justify-content: flex-start;
  }
}
