:root {
  --bg: #0e1013;
  --panel: rgba(22, 25, 30, 0.92);
  --panel-solid: #16191e;
  --border: rgba(255, 255, 255, 0.10);
  --text: #e8eaed;
  --dim: #9aa0a6;
  --accent: #ff7a00;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);

  /* Bevelled buttons: a solid face, a dark 1px outline, a light inner edge on
     the top and left and a dark one on the bottom and right. Pressed buttons
     swap the two inner edges so they look pushed in. */
  --btn-face: #ff7a00;
  --btn-face-hover: #ff8c22;
  --btn-hi: #ffb768;
  --btn-lo: #b85200;
  --btn-edge: #5c2600;
  --btn-text: #ffffff;

  --btn2-face: #dedede;
  --btn2-face-hover: #eaeaea;
  --btn2-hi: #ffffff;
  --btn2-lo: #a9a9a9;
  --btn2-edge: #666666;
  --btn2-text: #1d1d1d;
}

* { box-sizing: border-box; }

/* No rounded corners anywhere. */
*, *::before, *::after { border-radius: 0 !important; }

/* Several panels below set an explicit display, which would otherwise beat
   the user-agent rule for [hidden]. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

#board {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  touch-action: none;
}
#board.dragging { cursor: grabbing; }
#board.painting, #board.placing { cursor: crosshair; }
#board.brushing { cursor: cell; }

/* ---------- top bar ---------- */

#topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  padding: 10px 14px;
  background: linear-gradient(to bottom, rgba(14, 16, 19, 0.92), rgba(14, 16, 19, 0));
  pointer-events: none;
  z-index: 10;
}
#topbar > * { pointer-events: auto; }

.archive-banner {
  margin-right: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 5px 6px 5px 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(8px);
}
.archive-tag {
  padding: 1px 5px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.archive-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.archive-date { color: var(--dim); font-size: 12px; white-space: nowrap; }

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.allowance {
  min-width: 132px;
  padding: 6px 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(8px);
}
.allowance-count {
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.allowance-count .dim { color: var(--dim); font-weight: 400; }
.allowance-timer {
  font-size: 11px;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}
.allowance-bar {
  margin-top: 5px;
  height: 3px;
  background: rgba(255, 255, 255, 0.10);
  overflow: hidden;
}
#allowance-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.3s ease;
}

.account { display: flex; align-items: center; gap: 8px; }

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 4px 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(8px);
}
.user-chip img {
  width: 26px;
  height: 26px;
  object-fit: cover;
  background: #333;
}
.user-chip .avatar-fallback {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  background: var(--accent);
  border: 1px solid var(--btn-edge);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
}
.user-chip .uname {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- buttons ---------- */

/* Default buttons use the pale face; .primary uses the solid orange one. */
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--btn2-edge);
  background: var(--btn2-face);
  color: var(--btn2-text);
  box-shadow: inset 1px 1px 0 var(--btn2-hi), inset -1px -1px 0 var(--btn2-lo);
  padding: 6px 14px;
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}
.btn:hover { background: var(--btn2-face-hover); }
.btn:active,
.btn[aria-pressed="true"] {
  box-shadow: inset 1px 1px 0 var(--btn2-lo), inset -1px -1px 0 var(--btn2-hi);
  padding-top: 7px;
  padding-bottom: 5px;
}
.btn:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  box-shadow: inset 1px 1px 0 var(--btn2-hi), inset -1px -1px 0 var(--btn2-lo);
  padding-top: 6px;
  padding-bottom: 6px;
}

.btn.primary {
  background: var(--btn-face);
  border-color: var(--btn-edge);
  color: var(--btn-text);
  box-shadow: inset 1px 1px 0 var(--btn-hi), inset -1px -1px 0 var(--btn-lo);
  text-shadow: 0 1px 0 rgba(92, 38, 0, 0.45);
}
.btn.primary:hover { background: var(--btn-face-hover); }
.btn.primary:active,
.btn.primary[aria-pressed="true"] {
  box-shadow: inset 1px 1px 0 var(--btn-lo), inset -1px -1px 0 var(--btn-hi);
}
.btn.primary:disabled {
  box-shadow: inset 1px 1px 0 var(--btn-hi), inset -1px -1px 0 var(--btn-lo);
}

.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.danger { color: #b3261e; }
.btn.small:active { padding-top: 5px; padding-bottom: 3px; }
.btn.small:disabled { padding-top: 4px; padding-bottom: 4px; }

/* ---------- coords + zoom ---------- */

.coords {
  position: fixed;
  left: 14px;
  bottom: 14px;
  padding: 5px 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--dim);
  backdrop-filter: blur(8px);
  z-index: 9;
  pointer-events: none;
}

.zoom-controls {
  position: fixed;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  z-index: 9;
}
.zoom-controls .btn {
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 17px;
  line-height: 1;
}
.zoom-controls .btn:active { padding: 1px 0 0; }

/* ---------- palette ---------- */

.palette-bar {
  position: fixed;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  width: min(320px, calc(100vw - 24px));
  box-shadow: var(--shadow);
  z-index: 11;
}
/* Open, the bar becomes a panel around the palette. It needs a definite
   width: the swatch grid is all fr units and would otherwise collapse. */
.palette-bar.open {
  width: min(680px, calc(100vw - 24px));
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
}

/* Closed, the Paint button is the whole bar. */
.paint-toggle {
  display: flex;
  width: 100%;
  min-height: 46px;
  font-size: 16px;
  letter-spacing: 0.3px;
}
.palette-bar.open .paint-toggle { min-height: 34px; font-size: 14px; }

.palette {
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  gap: 4px;
  margin-top: 8px;
}
.swatch {
  aspect-ratio: 1;
  min-width: 0;
  border: 1px solid rgba(0, 0, 0, 0.6);
  background: var(--swatch);
  box-shadow: inset 1px 1px 0 rgba(255, 255, 255, 0.35), inset -1px -1px 0 rgba(0, 0, 0, 0.25);
  cursor: pointer;
  padding: 0;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}
.swatch:hover { transform: translateY(-2px); }

/* Admins' eraser: a white swatch struck through in red, in its own column. */
.swatch.eraser {
  background: linear-gradient(to top right, #fff 44%, #ff3b30 44%, #ff3b30 56%, #fff 56%);
  aspect-ratio: auto;
  grid-column: 17;
  grid-row: 1 / span 2;
}
.palette:has(.eraser:not([hidden])) { grid-template-columns: repeat(16, 1fr) 1.4fr; }
.staged-dot.eraser { background: linear-gradient(to top right, #fff 40%, #ff3b30 40%, #ff3b30 60%, #fff 60%) !important; }
.swatch[aria-selected="true"] {
  transform: translateY(-3px);
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
}

.confirm {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}
.confirm-target {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: auto;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--dim);
}
.staged-swatches { display: flex; gap: 3px; }
.staged-dot {
  width: 14px;
  height: 14px;
  border: 1px solid rgba(255, 255, 255, 0.28);
}

/* ---------- pixel inspector ---------- */

.pixel-info {
  position: fixed;
  z-index: 20;
  min-width: 168px;
  max-width: 240px;
  padding: 11px 13px;
  background: var(--panel-solid);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.pixel-info-close {
  position: absolute;
  top: 4px;
  right: 6px;
  border: 0;
  background: none;
  color: var(--dim);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
}
.pixel-info-close:hover { color: var(--text); }

.pi-head { display: flex; align-items: center; gap: 7px; margin-bottom: 7px; }
.pi-swatch {
  width: 17px;
  height: 17px;
  border: 1px solid rgba(255, 255, 255, 0.28);
}
.pi-coords { font-size: 12px; color: var(--dim); font-variant-numeric: tabular-nums; }
.pi-who { display: flex; align-items: center; gap: 6px; word-break: break-word; }
.pi-who.pi-dim { color: var(--dim); font-style: italic; }
.pi-badge {
  padding: 1px 5px;
  background: var(--accent);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.pi-when { margin-top: 3px; font-size: 11px; color: var(--dim); }

/* A user ID next to a name: names can repeat, IDs cannot. */
.uid {
  color: var(--dim);
  font-size: 0.85em;
  font-variant-numeric: tabular-nums;
}
.pi-who .uid { margin-left: 4px; }
.admin-table .pi-badge { margin-left: 6px; }
.user-chip .uid { margin-left: -2px; }

.owner-form .text-input { flex: 1; min-width: 0; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  top: 62px;
  left: 50%;
  transform: translateX(-50%);
  padding: 9px 16px;
  background: var(--panel-solid);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 13px;
  z-index: 50;
}
.toast.error { border-color: rgba(255, 90, 60, 0.55); color: #ffb4a2; }

/* ---------- modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(3px);
  z-index: 40;
}
.modal-card {
  width: min(400px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 24px;
  background: var(--panel-solid);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  text-align: center;
}
.modal-card h2 { margin: 0 0 8px; font-size: 19px; }
.modal-card p { margin: 0 0 18px; color: var(--dim); font-size: 13px; }

.text-input {
  width: 100%;
  min-width: 0;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit;
}
.text-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }


.turnstile-box { display: flex; justify-content: center; min-height: 65px; }

.link-button {
  margin-top: 14px;
  border: 0;
  background: none;
  color: var(--dim);
  font: inherit;
  font-size: 12px;
  text-decoration: underline;
  cursor: pointer;
}
.link-button:hover { color: var(--text); }

.auth-form { display: flex; flex-direction: column; gap: 8px; text-align: left; }

.auth-actions { display: flex; gap: 8px; margin-top: 4px; }
.auth-actions .btn { flex: 1; }

.auth-hint {
  margin: 4px 0 0 !important;
  font-size: 11px;
  color: var(--dim);
  text-align: center;
}

.modal-close { margin-top: 16px; width: 100%; }

/* ---------- archive and backup lists ---------- */

.list-card { width: min(620px, calc(100vw - 32px)); text-align: left; }
.list-summary { margin: 0 0 14px !important; }

.inline-form { display: flex; gap: 8px; margin-bottom: 12px; }
.inline-form .btn { flex: none; }

.snap-list {
  display: flex;
  flex-direction: column;
  max-height: min(440px, 55vh);
  overflow: auto;
  border: 1px solid var(--border);
}
.snap-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  color: inherit;
  text-decoration: none;
}
.snap-row:last-child { border-bottom: 0; }
a.snap-row:hover { background: rgba(255, 122, 0, 0.08); }
.snap-thumb {
  width: 64px;
  height: 64px;
  flex: none;
  image-rendering: pixelated;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.6);
}
.snap-text { display: flex; flex-direction: column; min-width: 0; margin-right: auto; }
.snap-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.snap-meta { color: var(--dim); font-size: 12px; }
.snap-actions { display: flex; gap: 5px; flex: none; }
.list-empty { padding: 14px; margin: 0 !important; text-align: center; }

/* ---------- admin panel ---------- */

.admin-card { width: min(880px, calc(100vw - 32px)); text-align: left; }
.admin-summary { margin: 0 0 12px !important; font-size: 12px; color: var(--dim); }

.admin-table-wrap {
  max-height: min(460px, 60vh);
  overflow: auto;
  border: 1px solid var(--border);
}
.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.admin-table th, .admin-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.admin-table th {
  position: sticky;
  top: 0;
  background: var(--panel-solid);
  color: var(--dim);
  font-weight: 500;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  text-align: left;
}
.admin-table tr:last-child td { border-bottom: 0; }
.admin-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.admin-table td:first-child { display: flex; align-items: center; gap: 6px; }

.admin-input {
  width: 74px;
  padding: 4px 7px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit;
  font-size: 12px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.admin-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.admin-actions { display: flex; gap: 5px; justify-content: flex-end; }

/* ---------- small screens ---------- */

@media (max-width: 760px) {
  .archive-date { display: none; }
}

@media (max-width: 620px) {
  .palette { grid-template-columns: repeat(8, 1fr); gap: 5px; }
  .palette:has(.eraser:not([hidden])) { grid-template-columns: repeat(8, 1fr); }
  .swatch.eraser { grid-column: auto; grid-row: auto; aspect-ratio: 1; }
  .palette-bar { bottom: 8px; }
  .palette-bar.open { width: calc(100vw - 16px); }

  /* The top bar wraps onto a second row rather than running off screen. */
  #topbar { padding: 8px; flex-wrap: wrap; gap: 6px; }
  .topbar-right { flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
  .archive-banner { flex: 1 1 100%; margin-right: 0; }
  .archive-banner .btn { margin-left: auto; }
  .allowance { min-width: 88px; padding: 4px 8px; }
  .allowance-count { font-size: 13px; }
  .user-chip .uname { display: none; }

  /* No hover on touch screens, so the coordinate readout has nothing to show;
     the zoom buttons move to the middle of the right edge, clear of both bars. */
  .coords { display: none; }
  .zoom-controls { bottom: auto; top: 50%; right: 8px; transform: translateY(-50%); }
  .snap-row { flex-wrap: wrap; }
  .snap-thumb { width: 48px; height: 48px; }
  .inline-form { flex-direction: column; }
}
