:root {
  color-scheme: dark;
  --bg: #0f1117;
  --surface: #171a22;
  --border: #2a3040;
  --text: #e8ebf4;
  --muted: #8b93a7;
  --accent: #6c9ef8;
  --accent-dim: #4a6fa8;
  --radius: 10px;
  --font: system-ui, "Segoe UI", Roboto, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

.app-header {
  flex-shrink: 0;
  padding: 0.85rem max(1.25rem, env(safe-area-inset-right)) 0.85rem
    max(1.25rem, env(safe-area-inset-left));
  padding-top: max(0.85rem, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.app-header__title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.layout {
  flex: 1;
  min-height: 0;
  min-width: 0;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  gap: 1rem;
  padding: 1rem max(1rem, env(safe-area-inset-right)) max(1.25rem, env(safe-area-inset-bottom))
    max(1rem, env(safe-area-inset-left));
  /* Stacked: settings then preview; preview row grows with viewport */
  grid-template-rows: auto minmax(10rem, 1fr);
  align-content: start;
}

@media (min-width: 800px) {
  .layout {
    grid-template-columns: minmax(220px, min(30vw, 300px)) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    align-items: stretch;
    align-content: stretch;
    gap: 1rem 1.25rem;
  }
}

/* Wide but short (e.g. landscape phone): use columns so preview gets height */
@media (orientation: landscape) and (min-width: 640px) and (max-width: 799px) and (max-height: 520px) {
  .layout {
    grid-template-columns: minmax(200px, 42vw) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    align-items: stretch;
    align-content: stretch;
  }
}

.panel {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
}

.panel--preview {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  gap: 0.35rem;
}

.panel--controls {
  align-self: start;
}

@media (min-width: 800px) {
  .panel--controls {
    align-self: stretch;
  }
}

@media (orientation: landscape) and (min-width: 640px) and (max-width: 799px) and (max-height: 520px) {
  .panel--controls {
    align-self: stretch;
  }
}

.control-stack {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin: 0;
}

.field__label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.field__input {
  width: 100%;
  padding: 0.45rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.field__input--color {
  height: 2.25rem;
  padding: 0.2rem;
  cursor: pointer;
}

.field__input--range {
  width: 100%;
  height: 1.65rem;
  padding: 0;
  cursor: pointer;
  accent-color: var(--accent);
}

.field__value {
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
  margin-top: 0.35rem;
}

.field--inline {
  margin: 0;
}

.drawer {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  overflow: hidden;
}

.drawer__summary {
  padding: 0.45rem 0.55rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  cursor: pointer;
  list-style: none;
}

.drawer__summary::-webkit-details-marker {
  display: none;
}

.drawer__body {
  padding: 0 0.55rem 0.55rem;
  border-top: 1px solid var(--border);
}

.drawer__hint {
  margin: 0 0 0.5rem;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.4;
}

.drawer__hint strong {
  color: var(--text);
  font-weight: 600;
}

.drawer__hint--tight {
  margin-top: 0.45rem;
  margin-bottom: 0.35rem;
}

.split-band-ignores {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem 1rem;
  align-items: center;
}

.tileset-drawer__outline {
  margin-top: 0.45rem;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  cursor: pointer;
  user-select: none;
}

.check input {
  accent-color: var(--accent);
}

.guides-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  padding-top: 0.15rem;
}

.guides-toolbar {
  margin-top: 0.35rem;
  display: flex;
  justify-content: center;
}

.btn--small {
  padding: 0.3rem 0.45rem;
  font-size: 0.78rem;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.actions--row {
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.15rem;
}

.actions--row .btn {
  flex: 1 1 auto;
  min-width: 0;
}

.btn {
  padding: 0.45rem 0.55rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.btn:hover {
  border-color: var(--accent-dim);
}

.btn--icon-text {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}

.btn--icon-text .btn__icon {
  flex-shrink: 0;
  opacity: 0.92;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #0b0d12;
  font-weight: 600;
}

.btn--primary:hover {
  filter: brightness(1.06);
}

.preview-wrap {
  flex: 1 1 auto;
  min-height: 0;
  background: #000;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 0.5rem;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

/* Portrait: keep preview from claiming the whole screen when controls are long */
@media (orientation: portrait) and (max-width: 799px) {
  .preview-wrap {
    max-height: min(58dvh, 720px);
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  .preview-wrap {
    max-height: none;
  }
}

#previewCanvas {
  display: block;
  flex-shrink: 0;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  /* width/height set in JS to integer multiple of bitmap (sharp scaling) */
}

.preview-link-row {
  flex-shrink: 0;
  margin: 0;
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
}

.preview-open-link {
  color: var(--accent);
  text-decoration: none;
}

.preview-open-link:hover {
  text-decoration: underline;
}

.preview-open-link:not([href]) {
  pointer-events: none;
  opacity: 0.55;
}
