:root {
  color-scheme: dark;
  --bg: #0d0d10;
  --panel: #17171c;
  --border: #2a2a32;
  --text: #e8e6e1;
  --text-dim: #9a978f;
  --accent: #d9a441;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

main {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 1.5rem;
  padding: 1.5rem 2rem;
  align-items: start;
  flex: 1;
  min-height: 0;
}

#legalFooter {
  flex-shrink: 0;
  padding: 0.4rem 2rem;
  border-top: 1px solid var(--border);
  font-size: 0.7rem;
  line-height: 1.3;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

#legalText {
  flex: 1;
}

#githubLink {
  flex-shrink: 0;
  display: inline-flex;
  color: var(--text-dim);
  transition: color 0.15s ease;
}

#githubLink:hover {
  color: var(--accent);
}

@media (max-width: 900px) {
  body {
    height: auto;
    overflow: visible;
  }

  main {
    grid-template-columns: 1fr;
    flex: none;
  }
}

#gallerySidebar {
  position: sticky;
  top: 1.5rem;
  max-height: 100%;
  overflow-y: auto;
  padding-right: 0.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

@media (max-width: 900px) {
  #gallerySidebar {
    position: static;
    max-height: none;
  }
}

.panel-heading {
  text-transform: capitalize;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin: 0 0 0.5rem;
  letter-spacing: 0.05em;
}

.era-heading {
  text-transform: capitalize;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin: 1.25rem 0 0.5rem;
  letter-spacing: 0.05em;
}

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

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 0.5rem;
}

.gallery-card {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  color: inherit;
  font: inherit;
  text-align: left;
}

.gallery-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  display: block;
}

.gallery-caption {
  padding: 0.4rem 0.5rem;
  font-size: 0.75rem;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.gallery-card:hover {
  border-color: var(--border);
}

/* --- Layers panel --- */

.layers-empty {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin: 0;
}

#layersList {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.layer-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--panel);
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 0.3rem;
  cursor: pointer;
}

.layer-row:hover {
  border-color: var(--border);
}

.layer-row.selected {
  border-color: var(--accent);
}

.layer-row-thumb {
  width: 32px;
  height: 32px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
}

.layer-row-title {
  flex: 1;
  min-width: 0;
  font-size: 0.78rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.layer-row-actions {
  display: flex;
  gap: 0.2rem;
  flex-shrink: 0;
}

.layer-row-actions button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 4px;
  width: 22px;
  height: 22px;
  line-height: 1;
  cursor: pointer;
  font-size: 0.75rem;
}

.layer-row-actions button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.layer-row-actions button:disabled {
  opacity: 0.35;
  cursor: default;
}

/* --- Workspace / canvas --- */

#workspace {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  height: 100%;
  min-height: 0;
}

.preview-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #000;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#previewPlaceholder {
  color: var(--text-dim);
  font-size: 0.85rem;
  padding: 1rem;
  text-align: center;
}

#canvasStage {
  position: relative;
  overflow: hidden;
}

#gridOverlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
  opacity: 0;
  transition: opacity 0.1s ease;
  background-image:
    linear-gradient(to right, rgba(217, 164, 65, 0.4) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(217, 164, 65, 0.4) 1px, transparent 1px);
}

#gridOverlay.visible {
  opacity: 1;
}

.center-line {
  position: absolute;
  background: rgba(217, 60, 60, 0.9);
}

.center-line-v {
  left: 50%;
  top: 0;
  bottom: 0;
  width: 1.5px;
  transform: translateX(-50%);
}

.center-line-h {
  top: 50%;
  left: 0;
  right: 0;
  height: 1.5px;
  transform: translateY(-50%);
}

.layer-frame {
  position: absolute;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
}

.layer-frame.grabbing {
  cursor: grabbing;
}

.layer-img {
  position: absolute;
  top: 0;
  left: 0;
  max-width: none;
  user-select: none;
  pointer-events: none;
}

/* Sibling of .layer-frame, sharing identical position/size/rotation but
   never clipped, so the outline stroke and rotate handle stay fully
   visible even though the image itself is clipped to the shape. */
.layer-gizmo {
  position: absolute;
  pointer-events: none;
}

/* Selection indicator: a dashed box around the gizmo's (unclipped) frame
   box, independent of the shape outline below - which is reserved for
   previewing the border color/width so the two don't fight over the
   same stroke. */
.layer-gizmo.selected {
  outline: 2px dashed var(--accent);
  outline-offset: 3px;
}

.layer-outline {
  position: absolute;
  inset: 0;
  overflow: visible;
}

/* stroke/stroke-width are set inline by main.js per layer (border color
   preview); this is just the shared geometry (fill/vector-effect). */
.layer-outline polygon,
.layer-outline circle {
  fill: none;
  vector-effect: non-scaling-stroke;
}

.rotate-handle {
  position: absolute;
  left: 50%;
  top: -22px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--text);
  transform: translate(-50%, -50%);
  cursor: grab;
  pointer-events: auto;
  padding: 0;
}

.rotate-handle:active {
  cursor: grabbing;
}

/* --- Toolbars / control panels --- */

.canvas-toolbar,
#layerControls,
#canvasControls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.8rem;
}

#layerControls {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.6rem 0.7rem;
  flex-direction: column;
  align-items: stretch;
  transition: opacity 0.1s ease;
}

/* Stays in the layout at all times (rather than being hidden/shown) so
   selecting or deselecting a layer never changes #workspace's flex
   layout height - that used to visibly resize the canvas below it. */
#layerControls.disabled {
  opacity: 0.45;
}

#layerControlsTitle {
  margin: 0;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#layerFieldsRow {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.8rem;
}

.checkbox-field {
  cursor: pointer;
}

.checkbox-field input {
  cursor: pointer;
}

.palette-row {
  display: flex;
  gap: 0.3rem;
}

.swatch {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  border: 1px solid var(--border);
  padding: 0;
  cursor: pointer;
}

.swatch:hover {
  transform: scale(1.1);
  border-color: var(--accent);
}

.crop-toolbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

/* Always reserves its line height (visibility, not display) so toggling
   crop-pan mode never changes #layerControls' own height either. */
.crop-label {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin: 0.3rem 0 0;
  visibility: hidden;
}

.crop-label.visible {
  visibility: visible;
}

#editCropToggle,
#resetLayerBtn,
.danger-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 0.35rem 0.7rem;
  font-size: 0.8rem;
  cursor: pointer;
}

#editCropToggle:hover,
#resetLayerBtn:hover {
  border-color: var(--accent);
}

#editCropToggle.active {
  border-color: var(--accent);
  color: var(--accent);
}

.danger-btn {
  margin-left: auto;
}

.danger-btn:hover {
  border-color: #d9534f;
  color: #d9534f;
}

fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
}

legend {
  font-size: 0.75rem;
  color: var(--text-dim);
  padding: 0 0.3rem;
}

.shape-options {
  display: flex;
  gap: 0.6rem;
  font-size: 0.85rem;
  white-space: nowrap;
}

.field {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}

#downloadLink {
  margin-left: auto;
  text-align: center;
  color: var(--accent);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.8rem;
  font-size: 0.85rem;
}

#downloadLink:hover {
  border-color: var(--accent);
}

/* Keep the button's slot in the layout even while hidden (visibility, not
   display:none) so toggling it off/on during a generate never changes
   #canvasControls' height. That height change used to shrink/grow the
   flex:1 preview box, and the ResizeObserver re-fitting the canvas made it
   visibly scale up then back down on every click or edit. */
#downloadLink[hidden] {
  display: inline-block;
  visibility: hidden;
}

#status {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-dim);
  min-height: 1em;
}
