:root {
  --bg: #f4f4f6;
  --surface: #ffffff;
  --surface-2: #ebebef;
  --line: #d9d9e0;
  --text: #17181c;
  --muted: #6b6e7a;
  --accent: #5b5bf0;
  --danger: #d63a3a;
  --tile: 84px;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171d;
    --surface: #1f2028;
    --surface-2: #2a2b35;
    --line: #34363f;
    --text: #ececf1;
    --muted: #9a9cab;
    --accent: #8b8bff;
    --danger: #ff6b6b;
    color-scheme: dark;
  }
}
@media (max-width: 600px) { :root { --tile: 64px; } }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select, textarea { font: inherit; color: inherit; }
button {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { filter: brightness(1.06); }
button:disabled { opacity: .35; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger, .menu-list .danger { color: var(--danger); }
button.link { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
/* Save status: a small pill that drops in just below the top bar. */
.save-status {
  position: absolute; left: 50%; top: 100%; transform: translate(-50%, -6px);
  padding: 4px 12px; border-radius: 999px; font-size: 13px; font-weight: 600;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted);
  box-shadow: 0 4px 14px rgba(0,0,0,.2); white-space: nowrap;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s;
}
.save-status:not([data-state="idle"]) { opacity: 1; transform: translate(-50%, 8px); }
.save-status[data-state="saved"] { color: #2fa36b; border-color: color-mix(in srgb, #2fa36b 40%, var(--line)); }
.save-status[data-state="error"] {
  color: #fff; background: var(--danger); border-color: var(--danger); pointer-events: auto; cursor: pointer;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
.logo {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 7px;
  background: linear-gradient(135deg, #ff7f7f, #ffdf7f 50%, #7fbfff); color: #111; font-weight: 800;
}
#boardSelect {
  min-width: 0; flex: 0 1 240px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px;
}
.actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: var(--surface); }
.seg button[aria-selected="true"] { background: var(--accent); color: #fff; }
@media (max-width: 600px) { .brand-name { display: none; } }

.menu { position: relative; }
.menu summary {
  list-style: none; cursor: pointer; padding: 4px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 700;
}
.menu summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 210px;
  display: flex; flex-direction: column; padding: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.menu-list button { background: none; border: 0; text-align: left; padding: 8px 10px; }
.menu-list button:hover { background: var(--surface-2); }
.menu-list.ctx { position: fixed; top: auto; right: auto; z-index: 150; min-width: 190px; }
.menu-list.ctx[hidden] { display: none; }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }

/* ---------- main ---------- */
main { max-width: 1100px; margin: 0 auto; padding: 12px 16px 24px; }
.title-row { margin: 4px 0 12px; }
.board-name {
  width: 100%; font-size: 24px; font-weight: 700; background: none; border: 1px solid transparent;
  border-radius: 8px; padding: 4px 6px; margin-left: -6px;
}
.board-name:hover, .board-name:focus { border-color: var(--line); outline: none; background: var(--surface); }

.tiers { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.tier { display: flex; min-height: calc(var(--tile) + 4px); border-top: 1px solid var(--line); }
.tier:first-child { border-top: 0; }
.tier-label {
  flex: 0 0 calc(var(--tile) + 16px); border: 0; border-radius: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 22px; font-weight: 700; padding: 4px; word-break: break-word; text-align: center;
}
.tier-label small { font-size: 10px; font-weight: 500; line-height: 1.2; opacity: .85; }
.tier-ctrl { display: flex; flex-direction: column; justify-content: center; gap: 2px; padding: 4px; background: var(--surface-2); }
.icon { padding: 2px 6px; font-size: 10px; line-height: 1; background: none; border: 0; }

.zone {
  flex: 1; display: flex; flex-wrap: wrap; align-content: flex-start; gap: 2px; padding: 2px;
  min-height: calc(var(--tile) + 4px); transition: background .12s;
}
.zone.over { background: color-mix(in srgb, var(--accent) 12%, transparent); }

/* Every tile in a row has the same height; its width comes from the image's aspect ratio
   (set inline as style="aspect-ratio: …"), so images are scaled, never cropped. */
.item {
  position: relative; height: var(--tile); width: auto; aspect-ratio: 1; max-width: 100%; flex: none;
  border-radius: 4px; overflow: hidden; cursor: grab; background: var(--surface-2);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  outline-offset: 2px;
}
.item img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.item .cap {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 10px 4px 2px;
  font-size: 10px; line-height: 1.2; color: #fff; text-shadow: 0 1px 2px #000;
  background: linear-gradient(transparent, rgba(0,0,0,.7));
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.item.text { display: grid; place-items: center; padding: 4px; background: var(--surface-2); border: 1px solid var(--line); }
.text-label { font-size: 12px; font-weight: 600; text-align: center; line-height: 1.2; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; word-break: break-word; }
.item.has-reason::after {
  content: ""; position: absolute; top: 4px; right: 4px; width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent); box-shadow: 0 0 0 2px #fff;
}
.item.placeholder { opacity: .3; }
.ghost {
  position: fixed; left: 0; top: 0; z-index: 100; pointer-events: none;
  box-shadow: 0 12px 28px rgba(0,0,0,.4); border-radius: 6px;
}
body.dragging, body.dragging * { cursor: grabbing !important; }

.add-tier { margin: 8px 0 20px; width: 100%; background: none; border-style: dashed; color: var(--muted); }

.pool { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; }
.pool-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.pool-head h2 { font-size: 16px; margin: 0; }
.pool .zone { min-height: calc(var(--tile) * 1.5); border: 1px dashed var(--line); border-radius: 6px; }
.hint { font-size: 13px; color: var(--muted); margin: 8px 2px 0; }
body.filedrop .zone { outline: 2px dashed var(--accent); outline-offset: -2px; }

/* ---------- reasons view ---------- */
.reasons { display: flex; flex-direction: column; gap: 14px; }
.r-tier { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.r-head { display: flex; align-items: baseline; gap: 10px; padding: 8px 14px; }
.r-head strong { font-size: 20px; }
.r-tier ul { list-style: none; margin: 0; padding: 0; }
.r-tier li { display: flex; gap: 12px; padding: 10px 14px; border-top: 1px solid var(--line); cursor: pointer; }
.r-tier li:hover { background: var(--surface-2); }
.r-tier li img { height: 56px; width: auto; max-width: 160px; border-radius: 6px; object-fit: contain; flex: none; align-self: flex-start; }
.r-txt { width: 56px; height: 56px; border-radius: 6px; flex: none; background: var(--surface-2); }
.r-txt { display: grid; place-items: center; font-weight: 700; color: var(--muted); }
.r-body { min-width: 0; }
.r-body p { margin: 2px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.empty { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- dialogs ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; width: min(460px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
.dlg { display: flex; flex-direction: column; gap: 12px; padding: 16px; }
.dlg-media { display: flex; gap: 12px; align-items: center; }
.dlg-media { flex-wrap: wrap; }
.dlg-media img { height: 112px; width: auto; max-width: 100%; object-fit: contain; border-radius: 8px; }
.dlg-media.no-img img { display: none; }
.media-btns { display: flex; flex-direction: column; gap: 6px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.field input, .field select, .field textarea {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
  font-size: 16px; font-weight: 400; color: var(--text); width: 100%;
}
.field textarea { resize: vertical; min-height: 110px; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.dlg-foot { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch { width: 32px; height: 32px; border-radius: 50%; padding: 0; border: 2px solid transparent; }
.swatch.on { border-color: var(--text); box-shadow: inset 0 0 0 2px var(--surface); }
.swatch.custom { position: relative; display: grid; place-items: center; background: var(--surface-2); border: 1px dashed var(--muted); color: var(--muted); cursor: pointer; font-weight: 700; }
.swatch.custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ---------- footer / toast ---------- */
footer { max-width: 1100px; margin: 0 auto; padding: 8px 16px 24px; display: flex; gap: 12px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 999px; font-size: 14px;
  opacity: 0; transition: .2s; pointer-events: none; z-index: 200;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.fatal { padding: 20px; color: var(--danger); }
