:root {
  --gap: 8px;
  --ink: #1f2430;
  --accent: #2f6fe0;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
html { height: 100%; }
body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: #fff;
  background: #222;
}
#bg, #bgimg {
  position: fixed;
  inset: 0;
  z-index: -1;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

/* Bovenbalk */
#topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: rgba(20, 24, 32, 0.55);
  backdrop-filter: blur(8px);
}
#tabs { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; min-width: 0; }
.tab, .pill {
  border: 0;
  border-radius: 999px;
  padding: 7px 16px;
  font: inherit;
  cursor: pointer;
  color: #fff;
  background: rgba(255, 255, 255, 0.18);
  transition: background 0.12s;
}
.tab:hover, .pill:hover { background: rgba(255, 255, 255, 0.3); }
.tab.active { background: #fff; color: var(--ink); }
.tab.add { padding: 7px 12px; font-weight: 600; }
.tab.dragover { outline: 2px solid #fff; }
.actions { display: flex; gap: 6px; flex: none; }
body.editing #btnEdit { background: #fff; color: var(--ink); }

/* Raster */
[hidden] { display: none !important; }
#stage { position: relative; flex: 1; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; padding: 84px 16px; }
#grid {
  --s: var(--size, 92px); /* vaste grootte: op een smal scherm scrol je zijwaarts in plaats van dat de tegels krimpen */
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--s));
  grid-template-rows: repeat(var(--rows), var(--s));
  gap: var(--gap);
  padding: 12px;
  width: max-content;
  margin: 0 auto;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(4px);
}
.cell { position: relative; border-radius: 8px; }
/* Lege vakjes: de achtergrond staat in ::before (z-index -1) zodat een groepskleur erboven kan liggen */
.cell.empty::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -1;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
}
body.editing .cell.empty { cursor: pointer; }
body.editing .cell.empty::before {
  border: 2px dashed rgba(255, 255, 255, 0.45);
  background: rgba(255, 255, 255, 0.08);
}
body.editing .cell.empty:hover::before { background: rgba(255, 255, 255, 0.22); }
.cell.grouped.empty::before { background: transparent; }
.cell.over { z-index: 2; outline: 3px solid #fff; outline-offset: -1px; }

/* Groepen: elk geverfd vakje krijgt een laag die 4px verder reikt, zodat buren samensmelten */
#grid { position: relative; --paint-alpha: 0.5; }
.paintlayer {
  position: absolute;
  inset: 12px;
  z-index: 0;
  pointer-events: none;
  opacity: var(--paint-alpha);
}
.bgc {
  position: absolute;
  left: calc(var(--px) * (var(--s) + var(--gap)) - 4px);
  top: calc(var(--py) * (var(--s) + var(--gap)) - 4px);
  width: calc(var(--s) + 8px);
  height: calc(var(--s) + 8px);
}
.tile { z-index: 1; }

/* Verfmodus */
#paintbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(20, 24, 32, 0.55);
  backdrop-filter: blur(8px);
}
#paintTools { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
#paintbar .hint { color: rgba(255, 255, 255, 0.75); }
#paintbar input[type="color"] { width: 36px; height: 28px; padding: 0; border: 0; border-radius: 6px; background: none; cursor: pointer; }
#paintbar .presets button.sel { outline: 2px solid #fff; outline-offset: 2px; }
.pill.on { background: #fff; color: var(--ink); }
body.painting #grid { touch-action: none; cursor: crosshair; user-select: none; }
body.painting .tile:hover { transform: none; filter: none; outline: none; }
body.painting .tile { cursor: crosshair; }

.tile {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px;
  overflow: hidden;
  border-radius: 8px;
  text-decoration: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: transform 0.12s, filter 0.12s;
  user-select: none;
  -webkit-user-drag: element;
}
.tile:hover { transform: scale(1.04); filter: brightness(1.06); }
.tile.preview-tile:hover { transform: none; filter: none; }
.tile img { width: 52%; height: 52%; object-fit: contain; flex: none; pointer-events: none; }
.tile .letter { font-size: 28px; font-weight: 600; opacity: 0.9; }
.tile .label {
  max-width: 100%;
  font-size: 11px;
  line-height: 1.15;
  text-align: center;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-word;
}
.tile.fill { padding: 0; }
.tile.fill img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.tile.fill .label {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 14px 4px 4px;
  color: #fff;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
}
body.editing .tile { cursor: grab; }
body.editing .tile:hover { outline: 2px solid #fff; }
.tile.dragging { opacity: 0.35; }

/* Status */
#status {
  position: fixed;
  left: 16px;
  bottom: 12px;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 12px;
  background: rgba(20, 24, 32, 0.7);
  opacity: 0;
  transition: opacity 0.25s;
  pointer-events: none;
}
#status.show { opacity: 1; }

/* Dialogen */
dialog {
  width: min(460px, 92vw);
  max-height: 92vh;
  padding: 20px;
  border: 0;
  border-radius: 12px;
  color: var(--ink);
  background: #fff;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
dialog h3 { margin: 18px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: #667; }
dialog label, dialog .field { display: block; margin-bottom: 10px; font-size: 13px; }
dialog .lbl { display: block; margin-bottom: 4px; }
dialog input[type="text"], dialog input[type="number"], dialog input[type="password"] {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid #c5cad3;
  border-radius: 8px;
  font: inherit;
  color: var(--ink);
}
dialog input[type="range"] { display: block; width: 100%; margin-top: 6px; }
dialog input[type="color"] { width: 44px; height: 32px; padding: 0; border: 1px solid #c5cad3; border-radius: 6px; background: none; }
dialog label.check { display: flex; align-items: center; gap: 8px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.row.two > label { flex: 1; margin: 0; }
/* Knoppen en bestandsknoppen in een rij staan op een lijn: geen extra ondermarge */
.row > .filebtn, .row > button { margin: 0; }
.presets { display: flex; flex-wrap: wrap; gap: 4px; }
dialog .presets { max-width: 256px; } /* 20 kleuren in 2 rijen van 10 */
.presets button {
  width: 22px; height: 22px; padding: 0;
  border: 1px solid rgba(0, 0, 0, 0.25);
  border-radius: 50%;
  cursor: pointer;
}
dialog button, .filebtn {
  padding: 8px 14px;
  border: 1px solid #c5cad3;
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: #f4f5f8;
  cursor: pointer;
}
dialog button:hover, .filebtn:hover { background: #e9ebf1; }
dialog button.primary { color: #fff; border-color: var(--accent); background: var(--accent); }
dialog button.primary:hover { background: #2559b8; }
dialog button.danger { color: #b3261e; border-color: #e3b4b0; }
.buttons { display: flex; align-items: center; gap: 8px; margin-top: 16px; }
.spacer { flex: 1; }
.hint { margin: 0; font-size: 12px; color: #667; }
.previewWrap { margin-top: 6px; }
.preview { --s: 92px; position: relative; width: var(--s); height: var(--s); background: #ddd; border-radius: 8px; }
.preview .tile { cursor: default; }

@media (max-width: 600px) {
  #topbar { flex-direction: column; align-items: stretch; }
  .actions { justify-content: flex-end; }
}

/* Verfbalk zweeft in de bovenmarge, zodat het raster niet verspringt als de balk verschijnt */
#paintbar {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  margin: 0;
  z-index: 3;
}

/* Kleurkeuze in dialogen */
.colorfield { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.presets button.sel { outline: 2px solid var(--accent); outline-offset: 2px; }
#paintbar .presets button.sel { outline-color: #fff; }

@media (max-width: 760px) {
  #stage { padding: 16px; }
  #paintbar { position: static; transform: none; width: auto; max-width: none; margin-bottom: 12px; }
}

/* Icoonknoppen in de bovenbalk */
.pill.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
}

/* Loginpagina */
body.login {
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: linear-gradient(160deg, #232ba4, #1f2430);
}
.loginbox {
  width: min(360px, 100%);
  padding: 28px 24px;
  border-radius: 14px;
  color: var(--ink);
  background: #fff;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}
.loginbox h1 { margin: 0 0 18px; font-size: 22px; }
.loginbox label { display: block; margin-bottom: 14px; font-size: 13px; }
.loginbox input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border: 1px solid #c5cad3;
  border-radius: 8px;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
}
.loginbox input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.loginbox button {
  width: 100%;
  padding: 11px 14px;
  border: 0;
  border-radius: 8px;
  font: inherit;
  font-size: 15px;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
}
.loginbox button:hover { background: #2559b8; }
.loginbox button:disabled { opacity: 0.6; cursor: wait; }
.loginerror { margin: 0 0 14px; padding: 9px 12px; border-radius: 8px; font-size: 13px; color: #8a1c14; background: #fbe4e1; }

/* Waarschuwing als de server een oude versie draait */
.warnbar { padding: 10px 16px; font-size: 14px; line-height: 1.4; color: #fff; background: #b3261e; }
.warnbar code { padding: 1px 6px; border-radius: 4px; background: rgba(0, 0, 0, 0.25); white-space: nowrap; }
