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

/* UI chrome — aligned with Hoshino Legotecture (white / grey / black). Canvas scene unchanged. */
:root {
  --ui-bg: #ffffff;
  --ui-surface: #f7f7f7;
  --ui-border: #d4d4d4;
  --ui-border-strong: #1a1a1a;
  --ui-text: #1a1a1a;
  --ui-muted: #6b6b6b;
  --ui-hover: #ebebeb;
  --ui-active-bg: #1a1a1a;
  --ui-active-text: #ffffff;
  --ui-danger: #8a3030;
  --stud: #f5f5f0;
  --glass: #9fd4ea;
  --font: "Helvetica Neue", Helvetica, Arial, system-ui, sans-serif;
  --palette-rail-width: 212px;
  --palette-toolbar-btn: 1.75rem;
  --palette-pad-top: 0.4rem;
  --palette-sticky-cover: 18px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html,
body {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: var(--font);
  font-weight: 300;
  background: var(--ui-bg);
  color: var(--ui-text);
}

#app {
  position: relative;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--ui-bg);
  padding-top: env(safe-area-inset-top, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
}

#app.view-only .palette-wrap {
  visibility: hidden;
  pointer-events: none;
}

.view-only-exit-anchor {
  position: fixed;
  bottom: calc(0.85rem + env(safe-area-inset-bottom, 0px));
  right: calc(0.85rem + env(safe-area-inset-right, 0px));
  z-index: 10;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.45rem;
}

.view-only-exit-anchor .action-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  width: 2.35rem;
  height: 2.35rem;
  min-width: 2.35rem;
  min-height: 2.35rem;
  padding: 0;
  border: 1px solid var(--ui-border-strong);
  background: var(--ui-bg);
}

.view-only-exit-anchor .action-btn-night.active {
  background: var(--ui-active-bg);
  color: var(--ui-active-text);
}

.view-only-exit-anchor .action-btn-night.active .toolbar-icon {
  stroke: currentColor;
}

.palette-wrap {
  position: fixed;
  top: env(safe-area-inset-top, 0px);
  right: env(safe-area-inset-right, 0px);
  bottom: env(safe-area-inset-bottom, 0px);
  z-index: 5;
  display: flex;
  flex-direction: column;
  width: var(--palette-rail-width);
  min-height: 0;
  background: var(--ui-bg);
  border-left: 1px solid var(--ui-border);
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.06);
}

.palette-footer {
  flex-shrink: 0;
  padding: 0.45rem 0.4rem 0.5rem;
  border-top: 1px solid var(--ui-border);
  background: var(--ui-bg);
}

.palette-toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: nowrap;
  gap: 0.2rem;
  width: 100%;
}

.palette-toolbar .action-btn-icon {
  flex: 0 0 var(--palette-toolbar-btn);
  width: var(--palette-toolbar-btn);
  height: var(--palette-toolbar-btn);
  min-width: var(--palette-toolbar-btn);
  border-width: 1px;
}

.palette-toolbar .toolbar-icon {
  width: 14px;
  height: 14px;
}

.palette {
  flex: 1;
  overflow-y: auto;
  padding: var(--palette-pad-top) 0.75rem 1rem;
}

.palette-list-title {
  margin-bottom: 0.9rem;
  padding-bottom: 0.55rem;
  border-bottom: 1px solid var(--ui-border);
  font-size: 32px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: 0.04em;
  color: #000000;
}

.auto-build-active .palette-list-title::after {
  content: ' · auto';
  font-size: 16px;
  font-weight: 500;
  color: var(--ui-muted);
  letter-spacing: 0.04em;
}

.view-night-active .palette-list-title::after {
  content: ' · night';
  font-size: 16px;
  font-weight: 500;
  color: var(--ui-muted);
  letter-spacing: 0.04em;
}

.palette-hints {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--ui-border);
}

.palette-help {
  margin: 0;
  padding: 0;
  font-size: 0.72rem;
  font-weight: 400;
  line-height: 1.55;
  color: var(--ui-text);
  white-space: pre-line;
}

.palette-help-perspective {
  margin-top: 0.85rem;
}

.palette-references {
  margin-top: 1.75rem;
  padding-top: 1rem;
  padding-bottom: 0.35rem;
  border-top: 1px solid var(--ui-border);
}

.palette-references-list {
  list-style: none;
}

.palette-references-list li + li {
  margin-top: 0.35rem;
}

.palette-reference-btn {
  display: block;
  width: 100%;
  padding: 0;
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 400;
  line-height: 1.45;
  color: var(--ui-text);
  text-align: left;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: var(--ui-border);
  text-underline-offset: 0.15em;
  transition: color 0.12s ease, text-decoration-color 0.12s ease;
}

.palette-reference-btn:hover {
  color: var(--ui-muted);
  text-decoration-color: var(--ui-muted);
}

.palette-section {
  margin-bottom: 1.15rem;
}

.palette-section + .palette-section {
  padding-top: 2px;
}

.palette-section h2 {
  position: sticky;
  top: calc(-1 * var(--palette-sticky-cover));
  z-index: 1;
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: var(--ui-muted);
  margin-top: 0;
  margin-bottom: 0.2rem;
  margin-left: -0.75rem;
  margin-right: -0.75rem;
  padding: calc(0.15rem + var(--palette-sticky-cover)) calc(0.75rem + 0.2rem) 0.2rem;
  background: var(--ui-bg);
}

.piece-btn {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  width: 100%;
  padding: 0.45rem 0.35rem 0.45rem 0.45rem;
  margin-bottom: 0.2rem;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: var(--ui-text);
  font-size: 0.82rem;
  font-weight: 400;
  cursor: pointer;
  text-align: left;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
  outline: none;
}

.piece-btn:focus,
.piece-btn:focus-visible {
  outline: none;
  box-shadow: none;
}

.piece-btn:hover {
  background: var(--ui-hover);
}

.piece-btn.active {
  background: var(--ui-active-bg);
  color: var(--ui-active-text);
  font-weight: 500;
}

.piece-swatch {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 3px;
  overflow: hidden;
  background-color: #ffffff;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  display: block;
}

.piece-swatch-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  border-radius: 3px;
}

/* Viewport / canvas — do not restyle #canvas; scene colors stay in app.js */
.viewport-wrap {
  position: absolute;
  inset: 0;
  min-height: 0;
  background: var(--ui-bg);
}

.nerd-stats {
  position: absolute;
  left: 12px;
  bottom: 12px;
  z-index: 2;
  padding: 8px 10px;
  border: none;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-family: var(--font);
  font-size: 0.72rem;
  font-weight: 400;
  line-height: 1.55;
  pointer-events: auto;
  cursor: pointer;
  text-align: left;
  white-space: pre-line;
  transition: background 0.12s ease;
}

.nerd-stats:hover {
  background: rgba(0, 0, 0, 0.58);
}

.nerd-stats:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.crazy-mode-label {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-family: var(--font);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  pointer-events: none;
  white-space: nowrap;
}

.viewport-wrap.view-only {
  background: linear-gradient(
    180deg,
    #4a4a4a 0%,
    #2a2a2a 32%,
    #141414 58%,
    #080808 100%
  );
}

.viewport-wrap.view-only.view-night {
  background: linear-gradient(
    180deg,
    #0a0e1a 0%,
    #060810 38%,
    #030408 68%,
    #000000 100%
  );
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
  background-size: contain;
}

#canvas.cursor-remove {
  cursor:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath d='M18 6 6 18M6 6l12 12' stroke='%231a1a1a' stroke-width='2' stroke-linecap='round' fill='none'/%3E%3C/svg%3E")
      12 12,
    crosshair;
}

#canvas.view-only {
  cursor: grab;
}

#canvas.view-only:active {
  cursor: grabbing;
}


.action-btn {
  padding: 0.4rem 0.75rem;
  font-size: 0.75rem;
  font-family: inherit;
  font-weight: 400;
  border: 1.5px solid var(--ui-border-strong);
  border-radius: 5px;
  background: var(--ui-bg);
  color: var(--ui-text);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
  outline: none;
}

.action-btn:focus,
.action-btn:focus-visible {
  outline: none;
}

.action-btn:focus:not(.active),
.action-btn:focus-visible:not(.active) {
  box-shadow: none;
}

.action-btn:hover {
  background: var(--ui-hover);
}

.action-btn:active {
  background: #e0e0e0;
}

.action-btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.15rem;
  height: 2.15rem;
  padding: 0;
}

#btn-dismiss-ghost[hidden],
#btn-undo[hidden],
#btn-clear[hidden],
#btn-save[hidden],
#btn-view-only[hidden],
#btn-night-mode[hidden] {
  display: none !important;
}

.toolbar-icon {
  display: block;
  width: 17px;
  height: 17px;
  flex-shrink: 0;
  fill: none;
  stroke: var(--ui-text);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.action-btn.active .toolbar-icon,
.action-btn:hover .toolbar-icon {
  stroke: currentColor;
}

#btn-clear:hover {
  border-color: var(--ui-danger);
  color: var(--ui-danger);
}

#btn-clear:hover .toolbar-icon {
  stroke: var(--ui-danger);
}

#btn-rotate {
  display: none !important;
}

@media (pointer: coarse) {
  #btn-rotate:not([hidden]) {
    display: inline-flex !important;
  }

  #canvas {
    touch-action: none;
  }
}

@media (max-width: 640px) {
  #app {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .viewport-wrap {
    position: relative;
    inset: auto;
    grid-row: 2;
    grid-column: 1;
  }

  .palette-wrap {
    position: relative;
    top: auto;
    right: auto;
    bottom: auto;
    width: auto;
    max-height: min(42vh, 300px);
    border-left: none;
    border-bottom: 1px solid var(--ui-border);
    box-shadow: none;
    grid-row: 1;
    grid-column: 1;
  }

  #app.view-only .palette-wrap {
    display: none;
  }

  .palette-footer {
    padding: 0.45rem 0.55rem 0.5rem;
  }

  .palette-toolbar {
    gap: 0.25rem;
  }

  .palette {
    display: block;
    flex: 1;
    min-height: 0;
    --palette-pad-top: 0.3rem;
    padding: var(--palette-pad-top) 0.75rem 0.65rem;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .palette-section {
    margin-bottom: 1rem;
    min-width: 0;
  }
}

html:has(body.stats-page) {
  height: auto;
  min-height: 100%;
  min-height: 100dvh;
  overflow-y: auto;
  overflow-x: clip;
}

body.stats-page {
  height: auto;
  min-height: 100%;
  min-height: 100dvh;
  overflow: visible;
  background: var(--ui-bg);
  color: var(--ui-text);
}

.stats-shell {
  max-width: 920px;
  margin: 0 auto;
  padding: calc(1.25rem + env(safe-area-inset-top, 0px))
    calc(1rem + env(safe-area-inset-right, 0px))
    calc(1.5rem + env(safe-area-inset-bottom, 0px))
    calc(1rem + env(safe-area-inset-left, 0px));
}

.stats-header {
  margin-bottom: 1.25rem;
}

.stats-kicker {
  margin-bottom: 0.35rem;
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ui-muted);
}

.stats-header h1 {
  margin-bottom: 0.45rem;
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: 0.02em;
}

.stats-lead {
  max-width: 38rem;
  margin-bottom: 0.85rem;
  font-size: 0.92rem;
  line-height: 1.55;
  color: var(--ui-muted);
}

.stats-close-btn {
  display: inline-block;
  padding: 0;
  border: none;
  background: transparent;
  font: inherit;
  font-size: 0.82rem;
  color: var(--ui-text);
  text-decoration: underline;
  text-decoration-color: var(--ui-border);
  text-underline-offset: 0.15em;
  cursor: pointer;
}

.stats-close-btn:hover {
  color: var(--ui-muted);
}

.stats-close-btn:focus-visible {
  outline: 2px solid var(--ui-border-strong);
  outline-offset: 2px;
}

.stats-status {
  font-size: 0.85rem;
  color: var(--ui-muted);
}

.stats-table-wrap {
  overflow: visible;
}

.stats-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.82rem;
}

.stats-table th,
.stats-table td {
  padding: 0.55rem 0.65rem;
  border-bottom: 1px solid var(--ui-border);
  text-align: left;
  vertical-align: top;
}

.stats-table th {
  background-color: var(--ui-bg);
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ui-muted);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.stats-table th[data-sort-key]::after {
  display: inline-block;
  width: 0.85em;
  margin-left: 0.3em;
  font-size: 0.95em;
  line-height: 1;
  text-align: center;
  opacity: 0;
  content: '';
}

.stats-table th[aria-sort='ascending']::after,
.stats-table th[aria-sort='descending']::after {
  opacity: 1;
  color: var(--ui-text);
}

.stats-table th[aria-sort='ascending']::after {
  content: '↑';
}

.stats-table th[aria-sort='descending']::after {
  content: '↓';
}

.stats-table th:hover {
  color: var(--ui-text);
}

.stats-table th[aria-sort='ascending'],
.stats-table th[aria-sort='descending'] {
  color: var(--ui-text);
  font-weight: 600;
}

.stats-table td.stats-num {
  font-variant-numeric: tabular-nums;
  min-width: 9rem;
}

.stats-cell-value {
  display: block;
  white-space: nowrap;
}

.stats-bar-track {
  display: block;
  width: 150px;
  max-width: 100%;
  height: 3px;
  margin-top: 0.35rem;
}

.stats-bar-fill {
  display: block;
  height: 100%;
  background: #b8b8b8;
  border-radius: 1px;
}

.stats-building-link {
  color: var(--ui-text);
  text-decoration: underline;
  text-decoration-color: var(--ui-border);
  text-underline-offset: 0.15em;
}

.stats-building-link:hover {
  color: var(--ui-muted);
}

.stats-row-current {
  background: var(--ui-surface);
}

.stats-row-current td:first-child {
  font-weight: 500;
}

.stats-head-sentinel {
  height: 0;
  pointer-events: none;
}

.stats-sticky-head {
  position: fixed;
  top: env(safe-area-inset-top, 0px);
  left: 0;
  z-index: 20;
  display: none;
  background: var(--ui-bg);
  box-shadow: 0 1px 0 var(--ui-border);
}

.stats-sticky-head.is-visible {
  display: block;
}

.stats-sticky-head-inner {
  display: flex;
  box-sizing: border-box;
}

.stats-sticky-head-cell {
  flex: 0 0 auto;
  box-sizing: border-box;
  padding: 0.55rem 0.65rem;
  border: none;
  background: transparent;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ui-muted);
  text-align: left;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.stats-sticky-head-cell:hover {
  color: var(--ui-text);
}

.stats-sticky-head-cell[aria-sort='ascending'],
.stats-sticky-head-cell[aria-sort='descending'] {
  color: var(--ui-text);
  font-weight: 600;
}

.stats-sticky-head-cell[data-sort-key]::after {
  display: inline-block;
  width: 0.85em;
  margin-left: 0.3em;
  font-size: 0.95em;
  line-height: 1;
  text-align: center;
  opacity: 0;
  content: '';
}

.stats-sticky-head-cell[aria-sort='ascending']::after,
.stats-sticky-head-cell[aria-sort='descending']::after {
  opacity: 1;
  color: var(--ui-text);
}

.stats-sticky-head-cell[aria-sort='ascending']::after {
  content: '↑';
}

.stats-sticky-head-cell[aria-sort='descending']::after {
  content: '↓';
}
