/* Categories page: "Refined dark", responsive layout
 *
 * Layout rules that prevent overlap:
 *  1. Every grid/flex child has min-width: 0 so long content can't push it wider.
 *  2. A nested group (a subcategory that has its own children) always spans the
 *     FULL row of its parent grid (grid-column: 1 / -1). Before, it was squeezed
 *     into one 170px cell and its own grid spilled over the neighbours.
 *  3. Columns use minmax(min(100%, var(--card-min)), 1fr), so a column can never
 *     be wider than the container, even on very small screens.
 *  4. Cards have a fixed row structure (tile, 2-line title, optional rows), so
 *     long names or aliases never make a card taller than its neighbours.
 */

/* ─── Tokens ────────────────────────────────────────────────────── */
:root {
  color-scheme: dark;

  --bg:           #0d0f12;
  --surface:      #13161b;
  --surface-2:    #1a1e26;
  --border:       #252a34;
  --border-bright:#353d4d;
  --text:         #e2e8f0;
  --text-muted:   #6b7790;
  --accent:       #00e5a0;
  --accent-dim:   rgba(0, 229, 160, 0.08);
  --accent-glow:  rgba(0, 229, 160, 0.25);
  --danger:       #f97474;

  --c-azul:    #3dd6f5;
  --c-celeste: #7eb8f7;
  --c-lima:    #a3e635;
  --c-rojo:    #f97474;
  --c-marron:  #d4a96a;
  --c-amarillo:#fbbf24;

  --font-display: 'Rajdhani', sans-serif;
  --font-mono:    'Share Tech Mono', monospace;
  --radius:       8px;
  --icon:         64px;   /* every tag image is drawn at 64x64 */

  /* responsive knobs */
  --gutter:   clamp(12px, 3vw, 28px);   /* page side padding */
  --gap:      clamp(8px, 1.6vw, 12px);  /* gap between cards */
  --card-min: 150px;                    /* min card width in the "All Categories" grid */
  --card-w:   150px;                    /* fixed card width inside groups */
}

@media (max-width: 480px) {
  :root { --card-min: 132px; --card-w: 132px; }
}

.azul    { --card-accent: var(--c-azul); }
.celeste { --card-accent: var(--c-celeste); }
.lima    { --card-accent: var(--c-lima); }
.rojo    { --card-accent: var(--c-rojo); }
.marron  { --card-accent: var(--c-marron); }
.amarillo{ --card-accent: var(--c-amarillo); }

*, *::before, *::after { box-sizing: border-box; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Mulish', sans-serif;
  overflow-x: hidden;   /* safety net: a stray wide element never scrolls the page sideways */
}

/* ─── Shared pieces ─────────────────────────────────────────────── */
#searchbar, #sort, .modal input:not([type="checkbox"]), .modal select {
  width: 100%;
  min-width: 0;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: .9rem;
  outline: none;
  caret-color: var(--accent);
  transition: border-color .15s, box-shadow .15s;
}

#searchbar:focus, #sort:focus-visible,
.modal input:focus, .modal select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

#searchbar::placeholder { color: var(--text-muted); }

/* small outlined buttons */
.edit-btn, .subtags-close, .dialog-actions button[type="button"] {
  background: transparent;
  border: 1px solid var(--border-bright);
  border-radius: 999px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: .72rem;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}

.edit-btn:hover, .edit-btn:focus-visible,
.subtags-close:hover, .subtags-close:focus-visible {
  color: var(--card-accent, var(--accent));
  border-color: var(--card-accent, var(--accent));
  background: color-mix(in srgb, var(--card-accent, var(--accent)) 10%, transparent);
  outline: none;
}

.edit-btn { position: relative; z-index: 3; padding: 3px 12px; }

/* amount badge */
.cat-badge {
  padding: 2px 9px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--card-accent, var(--accent)) 16%, transparent);
  color: var(--card-accent, var(--accent));
  font-family: var(--font-display);
  font-size: .8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}

.cat-badge.empty { background: transparent; color: var(--text-muted); }

.alias {
  font-family: var(--font-mono);
  font-size: .72rem;
  color: var(--text-muted);
}

/* ─── Toolbar ───────────────────────────────────────────────────── */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
  max-width: 960px;
  padding: var(--gutter) var(--gutter) 0;
}

.search-wrapper { flex: 1 1 260px; min-width: 0; }

.sort-form { display: flex; align-items: center; gap: 10px; }

.sort-form label, .section_title, #edit-form label, #translation-fields legend {
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.sort-form label { font-size: .85rem; }
#sort { width: auto; cursor: pointer; }

.edit-hint {
  margin-left: auto;
  padding: 8px 14px;
  background: transparent;
  border: 1px dashed var(--border-bright);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: .75rem;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}

.edit-hint kbd {
  padding: 1px 6px;
  background: var(--surface-2);
  border: 1px solid var(--border-bright);
  border-bottom-width: 2px;
  border-radius: 3px;
  color: var(--text);
  font-family: inherit;
}

.edit-hint:hover, .edit-hint:focus-visible, .edit-hint.active {
  color: var(--accent);
  border-color: var(--accent);
  outline: none;
}

.edit-hint.active { border-style: solid; background: var(--accent-dim); }

/* ─── Section titles ────────────────────────────────────────────── */
.section_title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 32px var(--gutter) 16px;
  font-size: 1rem;
}

.section_title::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.section_title[hidden] { display: none; }

/* ─── Card grids (one definition for every grid) ────────────────── */
.categories-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--card-min)), 1fr));
  gap: var(--gap);
  align-items: stretch;
  margin: 0 var(--gutter);
  padding-bottom: 40px;
}

/* Inside a group, cards have a FIXED width and wrap (flex), so the group
   shrinks to fit its content instead of stretching across the page. */
.group-grid {
  display: flex;
  flex-wrap: wrap;
  gap: var(--gap);
  align-items: stretch;
}

.group-grid > .tageado { flex: 0 0 var(--card-w); width: var(--card-w); }

/* ─── Category card ─────────────────────────────────────────────── */
.tageado { display: flex; min-width: 0; }

.cat-container {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 26px 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: transform .15s, border-color .15s, box-shadow .15s;
}

.cat-container:hover {
  transform: translateY(-2px);
  border-color: var(--card-accent);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .4);
}

.cat-container .cat-badge { position: absolute; top: 8px; right: 8px; z-index: 2; }
.cat-container:hover .cat-badge:not(.empty) { background: var(--card-accent); color: var(--bg); }

/* tinted tile; the image is always exactly 64x64 and the tile never shrinks */
.img-slot {
  flex: none;
  display: grid;
  place-items: center;
  width: calc(var(--icon) + 24px);
  height: calc(var(--icon) + 24px);
  border-radius: 14px;
  background: color-mix(in srgb, var(--card-accent) 12%, transparent);
}

.img-slot img {
  width: var(--icon);
  height: var(--icon);
  object-fit: contain;
  image-rendering: pixelated;
}

/* title: up to 2 lines, always reserves 2 lines so all cards match */
.cat-container h2 {
  width: 100%;
  min-height: 2.5em;
  margin: 0;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  font-family: var(--font-display);
  font-size: .95rem;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: .04em;
  text-align: center;
  text-transform: uppercase;
  overflow-wrap: anywhere;
  transition: color .15s;
}

.cat-container:hover h2 { color: var(--card-accent); }

/* the title link stretches over the whole card */
.cat-container h2 a { color: inherit; text-decoration: none; }
.cat-container h2 a::after { content: ""; position: absolute; inset: 0; z-index: 1; }
.cat-container h2 a:focus-visible { outline: none; }
.cat-container:has(h2 a:focus-visible) {
  border-color: var(--card-accent);
  box-shadow: 0 0 0 2px var(--card-accent);
}

/* fixed-height row for the edit button */
.card-row {
  position: relative;
  z-index: 3;            /* above the stretched title link */
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 28px;
}

.cat-container .alias {
  position: relative;
  z-index: 3;
  flex: none;
  max-width: 100%;
  height: 1em;
  line-height: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* aliases listed on the original's card while searching */
.alias-list {
  position: relative;
  z-index: 3;
  flex: none;
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  gap: 4px;
  width: 100%;
  min-height: 22px;
  overflow: hidden;
}

.alias-chip {
  min-width: 0;
  max-width: 100%;
  padding: 1px 8px;
  overflow: hidden;
  border: 1px solid var(--border-bright);
  border-radius: 999px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: .68rem;
  line-height: 1.6;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.alias-chip.more { flex: none; color: var(--card-accent, var(--accent)); border-color: currentColor; }

/* full list (dialog): wraps instead of clipping */
.alias-list.all { flex-wrap: wrap; justify-content: flex-start; overflow: visible; }

/* ─── Groups ────────────────────────────────────────────────────── */
/* Groups sit side by side and wrap, each one only as wide as its cards
   (small groups share a row; big ones take as much as they need). */
.groups {
  display: flex;
  flex-flow: row wrap;
  align-items: flex-start;   /* a short group doesn't stretch to the tallest one */
  gap: calc(var(--gap) * 1.5);
  margin: 0 var(--gutter) 28px;
}

.group {
  flex: 0 1 auto;
  max-width: 100%;
  min-width: 0;
  padding: clamp(8px, 1.6vw, 14px);
  background: color-mix(in srgb, var(--card-accent) 4%, var(--surface-2));
  border: 1px solid color-mix(in srgb, var(--card-accent) 30%, var(--border));
  border-left: 3px solid var(--card-accent);
  border-radius: var(--radius);
}

/* A nested group is a flex item like any card: it is only as wide as its own
   cards, sits next to its sibling cards when there is room, and wraps to a new
   line (never overlapping) when there isn't. */
.group.subgroup {
  padding: clamp(6px, 1.2vw, 10px);
  background: color-mix(in srgb, var(--card-accent) 7%, var(--surface));
  border-color: color-mix(in srgb, var(--card-accent) 22%, var(--border));
  border-left-width: 2px;
}

.group.subgroup .group.subgroup {
  background: color-mix(in srgb, var(--card-accent) 10%, var(--surface));
}

/* the parent stands out from its children */
.cat-container.is-parent {
  border-color: var(--card-accent);
  background: color-mix(in srgb, var(--card-accent) 8%, var(--surface));
}

.cat-container.is-parent h2 { color: var(--card-accent); }

/* ─── Dialogs ───────────────────────────────────────────────────── */
.modal {
  width: min(var(--w, 480px), calc(100vw - 24px));
  max-height: calc(100vh - 24px);   /* fallback */
  max-height: calc(100dvh - 24px);
  padding: 0;
  background: var(--surface);
  border: 1px solid var(--border-bright);
  border-top: 2px solid var(--card-accent, var(--accent));
  border-radius: var(--radius);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .7);
  overflow: hidden;
}

.modal[open] { display: flex; flex-direction: column; }
.modal::backdrop { background: rgba(5, 6, 8, .75); backdrop-filter: blur(3px); }

#subtags-dialog { --w: 460px; }
#edit-dialog    { --w: 540px; }

.subtags-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.subtags-head h3, #edit-form h3 {
  min-width: 0;
  margin: 0;
  overflow: hidden;
  font-family: var(--font-display);
  font-size: 1.15rem;
  letter-spacing: .1em;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--card-accent, var(--accent));
}

.subtags-close { flex: none; width: 30px; height: 30px; padding: 0; border-radius: 50%; font-size: .8rem; }
.subtags-body  { padding: 8px 20px 18px; overflow-y: auto; }
.subtags-empty { margin: 12px 0 0; color: var(--text-muted); font-family: var(--font-mono); font-size: .8rem; }

/* ─── Edit form ─────────────────────────────────────────────────── */
#edit-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 20px 22px 18px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

#edit-form label { display: flex; flex-direction: column; gap: 6px; min-width: 0; font-size: .8rem; }
#edit-form label.check {
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
  letter-spacing: .04em;
  text-transform: none;
  cursor: pointer;
}
#edit-form label.check input { width: auto; accent-color: var(--accent); }

#edit-form input[type="file"] { padding: 8px; font-size: .8rem; }
#edit-form input[type="file"]::file-selector-button {
  margin-right: 10px;
  padding: 4px 12px;
  background: transparent;
  border: 1px solid var(--border-bright);
  border-radius: 999px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  cursor: pointer;
}

#edit-form input[type="file"]::file-selector-button:hover { color: var(--accent); border-color: var(--accent); }

.image-field, .picker, #translation-fields {
  padding: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.image-field { display: flex; align-items: flex-start; flex-wrap: wrap; gap: 16px; }

#image-preview {
  flex: none;
  width: var(--icon);
  height: var(--icon);
  object-fit: contain;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  image-rendering: pixelated;
}

.image-controls { flex: 1 1 200px; min-width: 0; display: flex; flex-direction: column; gap: 10px; }
.image-controls .edit-btn { align-self: flex-start; }

/* pickers: images from other tags, Minecraft items */
.picker { display: flex; flex-direction: column; gap: 10px; }
.picker[hidden] { display: none; }

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, calc(var(--icon) + 8px));
  justify-content: center;
  gap: 6px;
  max-height: 190px;
  overflow-y: auto;
}

.gallery-item {
  position: relative;
  width: calc(var(--icon) + 8px);
  height: calc(var(--icon) + 8px);
  padding: 3px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color .15s;
}

.gallery-item:hover, .gallery-item:focus-visible, .gallery-item.selected { border-color: var(--accent); outline: none; }
.gallery-item.selected { box-shadow: 0 0 0 1px var(--accent); }

/* order number in "combine into a grid" mode */
.gallery-item[data-order]::after {
  content: attr(data-order);
  position: absolute;
  top: 2px;
  right: 2px;
  min-width: 18px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--bg);
  font: 700 .7rem/18px var(--font-display);
  text-align: center;
}

.gallery-item img, .gallery-item .mc-icon {
  display: block;
  width: var(--icon);
  height: var(--icon);
  object-fit: contain;
  image-rendering: pixelated;
}

#translation-fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
  gap: 12px;
  min-width: 0;
  margin: 0;
}

#translation-fields legend { padding: 0 8px; font-size: .8rem; }

#edit-error {
  margin: 0;
  padding: 10px 12px;
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 50%, transparent);
  border-radius: var(--radius);
  color: var(--danger);
  font-family: var(--font-mono);
  font-size: .8rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

#edit-error[hidden] { display: none; }

/* actions stay visible while the long form scrolls */
.dialog-actions {
  position: sticky;
  bottom: -18px;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin: 4px -22px -18px;
  padding: 12px 22px 18px;
  background: linear-gradient(to bottom, transparent, var(--surface) 30%);
}

.dialog-actions button {
  padding: 8px 22px;
  border-radius: 999px;
  font-family: var(--font-display);
  font-size: .85rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.dialog-actions button[type="button"]:hover { color: var(--text); border-color: var(--text-muted); }

.dialog-actions button[type="submit"] {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: var(--bg);
  cursor: pointer;
}

.dialog-actions button[type="submit"]:hover,
.dialog-actions button[type="submit"]:focus-visible { box-shadow: 0 0 20px var(--accent-glow); outline: none; }

/* ─── Small screens ─────────────────────────────────────────────── */
@media (max-width: 600px) {
  .sort-form { width: 100%; }
  #sort { flex: 1; }
  .edit-hint { margin-left: 0; width: 100%; }

  .cat-container { padding: 24px 8px 12px; }
  .group { border-left-width: 2px; }

  /* on phones every group is full width and its cards share the row evenly */
  .groups > .group { flex: 1 1 100%; }
  .group-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--card-min)), 1fr));
  }
  .group-grid > .tageado { width: auto; }
  .group.subgroup { grid-column: 1 / -1; }

  .modal { max-height: calc(100dvh - 16px); }
  #edit-form { padding: 16px 14px 14px; }
  .dialog-actions { margin: 4px -14px -14px; padding: 12px 14px 14px; bottom: -14px; }
  .dialog-actions button { flex: 1; }
}

/* very small phones: smaller tile so two cards still fit per row */
@media (max-width: 360px) {
  :root { --card-min: 120px; }
  .img-slot { width: calc(var(--icon) + 12px); height: calc(var(--icon) + 12px); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .cat-container:hover { transform: none; }
}

/* ─── View switch (cards / tree) ────────────────────────────────── */
.view-switch {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
  background: none;
  border: 0;
  color: var(--text-muted);
  font-family: var(--font-display);
  font-size: .85rem;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  cursor: pointer;
  transition: color .15s;
}

.switch-track {
  position: relative;
  flex: none;
  width: 34px;
  height: 18px;
  background: var(--surface-2);
  border: 1px solid var(--border-bright);
  border-radius: 999px;
  transition: background .15s, border-color .15s;
}

.switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform .15s, background .15s;
}

.view-switch:hover, .view-switch:focus-visible, .view-switch[aria-checked="true"] { color: var(--accent); outline: none; }
.view-switch:focus-visible .switch-track { box-shadow: 0 0 0 3px var(--accent-glow); }
.view-switch[aria-checked="true"] .switch-track { background: var(--accent-dim); border-color: var(--accent); }
.view-switch[aria-checked="true"] .switch-thumb { background: var(--accent); transform: translateX(16px); }

/* [hidden] must win over the display set on these containers */
.groups[hidden], .categories-container[hidden], .tree[hidden] { display: none; }

/* ─── Simplified tree view ──────────────────────────────────────── */
.tree { margin: var(--gutter) var(--gutter) 0; padding-bottom: 40px; }

.tree-list { margin: 0; padding: 0; list-style: none; }

/* roots flow into as many columns as fit, so there is no wide empty area */
.tree > .tree-list { columns: 260px; column-gap: 28px; }
.tree > .tree-list > .tree-item { break-inside: avoid; margin-bottom: 14px; }

/* children: vertical guide line + a short horizontal connector per item */
.tree-list .tree-list {
  margin-left: 14px;
  padding-left: 14px;
  border-left: 1px solid var(--border-bright);
}

.tree-list .tree-list > .tree-item { position: relative; }
.tree-list .tree-list > .tree-item::before {
  content: "";
  position: absolute;
  left: -14px;
  top: 13px;
  width: 11px;
  border-top: 1px solid var(--border-bright);
}

.tree-item details > summary {
  display: flex;
  align-items: center;
  gap: 2px;
  list-style: none;
  cursor: pointer;
}
.tree-item details > summary::-webkit-details-marker { display: none; }
.tree-item details > summary::before {
  content: "▸";
  flex: none;
  width: 14px;
  color: var(--text-muted);
  font-size: .7rem;
  text-align: center;
  transition: transform .15s;
}
.tree-item details[open] > summary::before { transform: rotate(90deg); }
.tree-item > .tree-row { margin-left: 16px; }   /* leaves line up with rows that have a caret */

.tree-row {
  display: flex;
  align-items: center;
  flex: 1;
  gap: 8px;
  min-width: 0;
  min-height: 26px;
  padding: 2px 8px 2px 4px;
  border-radius: 6px;
  transition: background .15s;
}

.tree-row:hover { background: color-mix(in srgb, var(--card-accent) 10%, transparent); }

.tree-img {
  flex: none;
  width: 16px;
  height: 16px;
  object-fit: contain;
  image-rendering: pixelated;
}

.tree-title {
  min-width: 0;
  overflow: hidden;
  color: var(--text);
  font-family: var(--font-display);
  font-size: .95rem;
  font-weight: 600;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-title:hover, .tree-title:focus-visible { color: var(--card-accent); outline: none; }

/* root level stands out */
.tree > .tree-list > .tree-item > .tree-row .tree-title,
.tree > .tree-list > .tree-item > details > summary .tree-title { color: var(--card-accent); font-weight: 700; }

.tree-row .cat-badge { flex: none; padding: 0 6px; font-size: .7rem; }
.tree-row .alias-list { flex: 0 1 auto; width: auto; min-height: 0; }
.tree-row .alias { flex: none; }
.tree-row .edit-btn { flex: none; margin-left: auto; padding: 0 8px; font-size: .68rem; }

@media (max-width: 600px) {
  .tree > .tree-list { columns: 1; }
  .view-switch { order: 3; }
}

#import-result { white-space: pre-wrap; margin: .5rem 0; padding: .5rem .75rem; border-radius: 6px; }
#import-result.error { color: #c0392b; }