:root {
  --bg: #080c14;
  --bg-subtle: #0d1320;
  --surface: #111827;
  --surface-elevated: #172033;
  --surface-hover: #1e2a42;
  --border: rgba(148, 179, 224, 0.12);
  --border-strong: rgba(148, 179, 224, 0.24);
  --text: #f0f5ff;
  --text-secondary: #9fb0cc;
  --text-muted: #687a99;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --accent-soft: rgba(59, 130, 246, 0.16);
  --accent-text: #93c5fd;
  --ok: #2dd4bf;
  --ok-soft: rgba(45, 212, 191, 0.14);
  --warn: #fbbf24;
  --danger: #f43f5e;
  --tile-size: 96px;
  --radius: 12px;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Monaco, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

button, input, select, textarea { font: inherit; color: inherit; }
svg { display: block; flex-shrink: 0; }

/* Header — crisp, zero-blur sticky command bar */
.dashboard-controls {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--bg);
}
.header {
  background: #0a101b;
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: 1560px;
  margin: 0 auto;
  padding: 12px 20px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

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

.logo {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: -0.02em;
  margin-right: 6px;
}

.logo-mark {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent);
  display: grid;
  place-items: center;
  color: #ffffff;
}

.count-chip {
  font-family: var(--mono);
  font-size: 0.7rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  padding: 2px 7px;
  border-radius: 6px;
}

/* Search */
.search-box {
  position: relative;
  flex: 1 1 220px;
  max-width: 360px;
}
.search-box .search-ico {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}
.search-box input {
  width: 100%;
  height: 36px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0 34px 0 34px;
  font-size: 0.84rem;
  outline: none;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}
.search-box input::placeholder { color: var(--text-muted); }
.search-box input:focus { border-color: var(--accent); background: var(--surface-elevated); }

.kbd-hint {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--mono);
  font-size: 0.68rem;
  color: var(--text-muted);
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 1px 6px;
  border-radius: 5px;
  pointer-events: none;
}

.search-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  display: none;
  border-radius: 5px;
}
.search-clear:hover { color: var(--text); background: var(--surface-hover); }

.library-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}
.bar > .library-actions { margin-inline-start: auto; }

.library-tools { position: relative; }
.library-tools summary {
  display: flex;
  justify-content: center;
  gap: 8px;
  list-style: none;
}
.library-tools summary::-webkit-details-marker { display: none; }
.library-tools summary::after {
  content: '';
  flex: none;
  height: 6px;
  width: 6px;
  border: solid currentColor;
  border-width: 0 1.5px 1.5px 0;
  transform: translateY(-2px) rotate(45deg);
}
.library-tools[open] summary::after { transform: translateY(2px) rotate(225deg); }
.library-tools-panel {
  position: absolute;
  inset: calc(100% + 6px) 0 auto auto;
  width: min(288px, calc(100vw - 20px));
  display: grid;
  grid-auto-rows: 1fr;
  gap: 4px;
  padding: 6px;
  background: var(--surface-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 35%);
  z-index: 1;
}
.library-tools-panel .btn.ghost {
  height: auto;
  padding: 8px 10px;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 3px;
  white-space: normal;
  text-align: start;
  color: var(--text);
}
.library-tools-panel .btn.ghost:hover { background: var(--surface-hover); }
.library-tools-panel span { color: var(--text-secondary); font-size: 0.75rem; font-weight: 400; line-height: 1.3; }
.library-tools :is(summary, button):focus-visible { outline: 2px solid var(--accent-hover); outline-offset: -2px; }

.spacer { flex: 1; }

/* Buttons */
.btn {
  height: 36px;
  padding: 0 12px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.1s ease, border-color 0.1s ease, color 0.1s ease;
}
.btn:hover { background: var(--surface-hover); border-color: var(--border-strong); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  font-weight: 650;
}
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn.ghost:hover { background: var(--surface-elevated); color: var(--text); }
.btn.active {
  background: var(--accent-soft);
  border-color: rgba(59, 130, 246, 0.55);
  color: var(--accent-text);
}
.btn.danger {
  background: rgba(244, 63, 94, 0.12);
  border-color: rgba(244, 63, 94, 0.35);
  color: #fda4af;
}
.btn.danger:hover { background: rgba(244, 63, 94, 0.22); }
.btn.sm { height: 29px; padding: 0 10px; font-size: 0.76rem; border-radius: 7px; }
.btn.icon-only { width: 36px; padding: 0; justify-content: center; }
.btn > svg { width: 15px; height: 15px; }
.btn.modal-close { width: 30px; padding: 0; justify-content: center; }
.modal-close::before {
  content: "";
  width: 15px;
  height: 15px;
  background: currentColor;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='m6 6 12 12M18 6 6 18'/%3E%3C/svg%3E") center / contain no-repeat;
  forced-color-adjust: none;
}

/* Subbar */
.subbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding-top: 2px;
}

.tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1 1 auto;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  height: 29px;
  padding: 0 11px;
  border-radius: 7px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.78rem;
  font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: color 0.1s ease, background-color 0.1s ease, border-color 0.1s ease;
}
.tab:hover { color: var(--text); background: var(--surface); }
.tab.active {
  background: var(--surface-elevated);
  border-color: rgba(59, 130, 246, 0.5);
  color: var(--text);
}
.tab.warn-tab { color: #fda4af; }
.tab .tab-cnt {
  font-family: var(--mono);
  font-size: 0.68rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}
.tab.active .tab-cnt { color: var(--accent-text); }
.tab.drag-over {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #fff;
}

.tools-right { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }

.tools-right :is(#sortMode, .select-trigger[aria-controls="sortMode-options"]) {
  inline-size: 132px;
  height: 29px;
  align-items: center;
}

.size-ctrl {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 2px 8px 2px 2px;
  height: 29px;
}
.size-ctrl .seg { border: none; background: transparent; padding: 0; }
.size-slider {
  width: 72px;
  height: 4px;
  accent-color: var(--accent);
  cursor: pointer;
}
.size-val {
  font-family: var(--mono);
  font-size: 0.68rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  min-width: 34px;
  text-align: right;
}

.seg {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 2px;
  gap: 2px;
}
.seg button {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--mono);
  font-size: 0.71rem;
  font-weight: 600;
  padding: 3px 7px;
  border-radius: 5px;
  cursor: pointer;
}
.seg button:hover { color: var(--text); }
.seg button.active { background: var(--accent-soft); color: var(--accent-text); }

select.ctrl, .ctrl-input {
  height: 29px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 0 8px;
  font-size: 0.77rem;
  color: var(--text);
  outline: none;
}
select.ctrl:focus, .ctrl-input:focus { border-color: var(--accent); }

/* Batch Bar */
.batch-bar {
  display: none;
  max-width: 1560px;
  margin: 0 auto;
  padding: 10px 20px 0;
}
.batch-bar.open { display: block; }
.batch-inner {
  background: var(--surface-elevated);
  border: 1px solid rgba(59, 130, 246, 0.48);
  border-radius: 10px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

/* Main */
main {
  max-width: 1560px;
  margin: 0 auto;
  padding: 18px 20px 72px;
}

.broken-bar {
  background: rgba(244, 63, 94, 0.08);
  border: 1px solid rgba(244, 63, 94, 0.28);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

/* Mood Tag Chips Bar */
.tag-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.tag-chip {
  height: 26px;
  padding: 0 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.tag-chip:hover { color: var(--text); border-color: var(--border-strong); background: var(--surface-elevated); }
.tag-chip.active {
  background: var(--accent-soft);
  border-color: rgba(59, 130, 246, 0.55);
  color: var(--accent-text);
}

.pin-badge {
  position: absolute;
  top: 5px;
  right: 5px;
  font-family: var(--mono);
  font-size: 0.56rem;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--accent);
  color: #ffffff;
  pointer-events: none;
}

.replace-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.replace-title {
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--text-secondary);
}

/* Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(var(--tile-size), 100%), 1fr));
  gap: 10px;
  align-items: start;
}

.grid.virtual {
  display: block;
  position: relative;
  width: 100%;
  contain: layout style;
}

.grid.virtual > .tile {
  position: absolute;
  top: 0;
  left: 0;
  margin: 0;
  aspect-ratio: auto;
  content-visibility: visible;
  contain: layout paint style;
  backface-visibility: hidden;
  transition: none;
}

/* Studio Sticker Tile — CSS-contained & zero-jank */
.tile {
  position: relative;
  width: 100%;
  min-width: 0;
  aspect-ratio: 1 / 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  content-visibility: visible;
  contain: layout paint style;
  transition: border-color 0.1s ease, background-color 0.1s ease;
}
@media (hover: hover) and (pointer: fine) {
  .tile:hover {
    background: var(--surface-elevated);
    border-color: rgba(96, 165, 250, 0.55);
  }
  .tile:hover .pin-badge { opacity: 0; }
}
body.select-mode .pin-badge { opacity: 0; }
.tile:active { border-color: var(--accent); }
.tile.dragging { opacity: 0.4; }
.tile.drag-before { box-shadow: inset 3px 0 0 0 var(--accent); border-color: var(--accent); }
.tile.selected { border-color: var(--accent); box-shadow: inset 0 0 0 1.5px var(--accent); }
.tile.copied { border-color: var(--ok); box-shadow: inset 0 0 0 1.5px var(--ok); }
.tile.broken { border-color: rgba(244, 63, 94, 0.45); }

/* Lightweight loading placeholder — hides stale pooled image until new frame decodes */
.tile.loading {
  background: var(--surface);
}
.tile.loading .tile-media img {
  opacity: 0;
}
.tile.broken .tile-media img {
  display: none;
}
body:not(.select-mode) .tile-btn.select-only-btn {
  display: none;
}

/* Tile Action Overlay (Desktop fine-pointer hover + Select mode) */
.tile-overlay {
  position: absolute;
  top: 5px;
  left: 5px;
  right: 5px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s ease;
  z-index: 3;
}
@media (hover: hover) and (pointer: fine) {
  .tile:hover .tile-overlay { opacity: 1; }
}
body.select-mode .tile .tile-overlay { opacity: 1; }

.tile-btn {
  pointer-events: auto;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 1px solid rgba(148, 179, 224, 0.2);
  background: rgba(8, 12, 20, 0.82);
  color: var(--text-secondary);
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
  transition: background-color 0.1s ease, color 0.1s ease, border-color 0.1s ease;
}
.tile-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
  border-color: var(--border-strong);
}
.tile-btn.fav-on {
  color: var(--warn);
  border-color: rgba(251, 191, 36, 0.45);
}
.tile-btn.check-on {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.tile-media {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 5px;
}
.tile-media img {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
  border-radius: 6px;
  opacity: 1;
  transition: none;
  image-rendering: auto;
}

/* Text Note Tile */
.text-tile-body {
  padding: 10px 8px;
  font-size: 0.72rem;
  line-height: 1.38;
  color: var(--text-secondary);
  text-align: center;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  word-break: break-word;
}
.text-pill {
  position: absolute;
  bottom: 5px;
  left: 5px;
  font-family: var(--mono);
  font-size: 0.58rem;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent-text);
}

.fav-dot {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--warn);
  pointer-events: none;
  opacity: 0;
  z-index: 2;
}
.tile.is-fav .fav-dot { opacity: 1; }
@media (hover: hover) and (pointer: fine) {
  .tile:hover .fav-dot { opacity: 0; }
}
body.select-mode .fav-dot { opacity: 0; }

/* Caption on desktop hover */
.tile-caption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 14px 6px 4px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.85));
  font-size: 10.5px;
  color: #e4e4e7;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s;
  z-index: 3;
}
@media (hover: hover) and (pointer: fine) {
  .tile:hover .tile-caption { opacity: 1; }
}

.copied-badge {
  position: absolute;
  inset: 0;
  background: rgba(8, 12, 20, 0.84);
  display: grid;
  place-items: center;
  color: var(--ok);
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  opacity: 0;
  pointer-events: none;
}
.tile.copied .copied-badge { opacity: 1; }

.err-tag {
  position: absolute;
  bottom: 5px;
  left: 5px;
  font-family: var(--mono);
  font-size: 0.6rem;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(244, 63, 94, 0.92);
  color: #fff;
  pointer-events: none;
}

.warn-tag {
  position: absolute;
  bottom: 5px;
  right: 5px;
  font-family: var(--mono);
  font-size: 0.56rem;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--warn);
  color: #0c0c0e;
  pointer-events: none;
}

/* Right-Click Context Menu */
.ctx-menu {
  position: fixed;
  z-index: 120;
  min-width: 192px;
  max-width: 244px;
  background: var(--surface-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 5px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.6);
  display: none;
  flex-direction: column;
  gap: 2px;
}
.ctx-menu.open { display: flex; }
.ctx-item {
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 500;
  text-align: left;
  padding: 7px 10px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.ctx-item:hover { background: var(--surface-hover); }
.ctx-item.danger { color: #fda4af; }
.ctx-item.danger:hover { background: rgba(244, 63, 94, 0.16); }
.ctx-sep { height: 1px; background: var(--border); margin: 4px 6px; }
.ctx-label {
  font-family: var(--mono);
  font-size: 0.65rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 4px 10px 2px;
}
.ctx-packs {
  max-height: 150px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.ctx-pack-active { color: var(--accent-text); font-weight: 600; }

/* Packs View */
.pack-reorder { display: flex; align-items: center; gap: 4px; margin-top: 3px; }
.pack-reorder .btn { min-width: 28px; padding-inline: 4px; justify-content: center; }
.pack-drag-handle { width: 28px; height: 29px; display: grid; place-items: center; color: var(--text-secondary); cursor: grab; }
.pack-drag-handle::before { content: ''; width: 12px; height: 16px; background: radial-gradient(circle, currentColor 1.25px, transparent 1.5px) 0 0 / 6px 5px; }
.pack-drag-handle:active { cursor: grabbing; }
.pack-card.pack-drop-target { outline: 2px solid var(--accent-hover); outline-offset: -2px; }
.packs-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.packs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(236px, 1fr));
  gap: 12px;
}
.pack-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  cursor: pointer;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}
.pack-card:hover { background: var(--surface-elevated); border-color: rgba(96, 165, 250, 0.5); }
.pack-thumbs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  height: 50px;
}
.pack-thumb-slot {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  overflow: hidden;
  display: grid;
  place-items: center;
  padding: 2px;
}
.pack-thumb-slot img { width: 100%; height: 100%; object-fit: contain; }
.pack-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.pack-meta strong { font-size: 0.88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pack-actions { display: flex; flex-wrap: wrap; gap: 6px; }

.starter-packs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
}
.starter-pack-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.starter-pack-card:hover { border-color: var(--border-strong); }
.starter-pack-card span {
  font-size: 0.82rem;
  font-weight: 550;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty {
  margin-top: 18px;
  padding: 56px 20px;
  text-align: center;
  border: 1px dashed var(--border-strong);
  border-radius: 14px;
  background: var(--surface);
}
.empty h3 { margin: 0 0 6px; font-size: 1rem; letter-spacing: -0.01em; }
.empty p { margin: 0 auto 16px; max-width: 440px; font-size: 0.84rem; color: var(--text-secondary); line-height: 1.5; }

/* Toast */
#toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 90;
  background: var(--surface-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 9px;
  font-size: 0.81rem;
  font-weight: 550;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.14s ease, transform 0.14s ease;
}
#toast.show { opacity: 1; transform: translateY(0); }
#toast .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
#toast.err .dot { background: var(--danger); }
#toast.info .dot { background: var(--accent); }

/* Modals */
dialog {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  padding: 0;
  width: min(520px, 94vw);
  max-height: 88vh;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.72);
}
dialog::backdrop { background: rgba(6, 6, 8, 0.78); }

.modal-head {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.91rem;
  font-weight: 650;
}
.modal-body {
  padding: 16px 18px;
  overflow-y: auto;
  max-height: calc(88vh - 114px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
#exportModal .modal-head { padding: 8px 18px; gap: 8px; }
#exportModal .modal-close { flex: none; width: 32px; height: 32px; }
#exportModal .modal-body > .note-text { margin: 0; }
.export-actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.export-actions .btn { justify-content: center; padding-inline: 6px; text-align: center; white-space: normal; }

@media (max-width: 480px) {
  .export-actions { grid-template-columns: minmax(0, 1fr); }
}

.modal-foot {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 550;
  color: var(--text-secondary);
}
.field input, .field textarea, .field select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 0.85rem;
  color: var(--text);
  outline: none;
  font-weight: 400;
}
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }

select.ctrl, .field select, .field input[list], .field input[type="file"] { color-scheme: dark; }
.field { min-width: 0; }
.field select, .field input[type="file"] { width: 100%; min-width: 0; }
select.ctrl, .field select {
  appearance: none;
  padding-inline-end: 28px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%239fb0cc' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 14px;
  cursor: pointer;
  text-overflow: ellipsis;
}
select option { background: var(--surface-elevated); color: var(--text); }
select:disabled, .select-trigger:disabled { opacity: 0.5; cursor: not-allowed; }

.pack-combobox { position: relative; min-width: 0; }
.field .pack-combobox input { width: 100%; padding-inline-end: 34px; }
.select-trigger, .pack-combobox-toggle {
  color: var(--text);
  cursor: pointer;
}
.select-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}
.select-trigger.ctrl {
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--bg-subtle);
  padding: 0 8px;
  font-size: 0.78rem;
}
.field .select-trigger {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  padding: 9px 11px;
  font-size: 0.85rem;
  font-weight: 400;
}
.select-trigger span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.pack-combobox-toggle {
  position: absolute;
  inset: 1px 1px 1px auto;
  width: 32px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 0 7px 7px 0;
  background: transparent;
}
.pack-combobox-toggle:hover { background: var(--surface-hover); }
.select-trigger::after, .pack-combobox-toggle::after {
  content: '';
  width: 7px;
  height: 7px;
  flex: none;
  border: solid var(--text-secondary);
  border-width: 0 1.5px 1.5px 0;
  transform: translateY(-2px) rotate(45deg);
}
.dropdown-options {
  position: fixed;
  inset: auto;
  margin: 0;
  padding: 4px;
  overflow-y: auto;
  overscroll-behavior: contain;
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  background: var(--surface-elevated);
  color: var(--text);
  box-shadow: 0 8px 24px rgb(0 0 0 / 35%);
  font-size: 0.85rem;
  font-weight: 400;
}
.dropdown-options [role="option"] {
  display: block;
  padding: 8px 9px;
  border-radius: 5px;
  overflow-wrap: anywhere;
  cursor: pointer;
}
.dropdown-options [aria-selected="true"] { background: var(--accent-soft); color: var(--accent-text); }
.dropdown-options [role="option"]:is(:hover, .active) { background: var(--surface-hover); }
.dropdown-options .active { outline: 1px solid var(--accent-hover); outline-offset: -1px; }

@supports (appearance: base-select) {
  select.ctrl, .field select, select::picker(select) { appearance: base-select; }
  select.ctrl, .field select { background-image: none; padding-inline-end: 8px; }
  select::picker-icon { color: var(--text-secondary); }
  select::picker(select) {
    margin-block: 4px;
    padding: 4px;
    max-width: calc(100vw - 20px);
    border: 1px solid var(--border-strong);
    border-radius: 9px;
    background: var(--surface-elevated);
    color: var(--text);
    box-shadow: 0 8px 24px rgb(0 0 0 / 35%);
  }
  select option { padding: 7px 9px; border-radius: 5px; white-space: normal; }
  select option:is(:hover, :focus-visible) { background: var(--surface-hover); }
  select option:checked { background: var(--accent-soft); color: var(--accent-text); }
}

.field input[type="file"] {
  padding: 5px;
  color: var(--text-secondary);
  cursor: pointer;
}
.field input[type="file"]::file-selector-button {
  margin-inline-end: 9px;
  padding: 6px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface-elevated);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.field input[type="file"]::file-selector-button:hover { background: var(--surface-hover); }
:is(select.ctrl, .field select, .select-trigger, .pack-combobox input, .field input[type="file"], .modal-close):focus-visible {
  outline: 2px solid var(--accent-hover);
  outline-offset: 2px;
}

@media (forced-colors: active) {
  select.ctrl, .field select, select::picker(select) { appearance: auto; }
  select.ctrl, .field select { background-image: none; }
}

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.bar > .library-actions { flex-wrap: nowrap; gap: 5px; }

@media (min-width: 681px) {
  .header-inner { gap: 5px; padding-block: 8px; --toolbar-height: 32px; }
  .bar { flex-wrap: nowrap; }
  .bar .logo { flex: none; }
  .bar .search-box { min-width: 0; }
  .subbar { flex-wrap: nowrap; padding-top: 0; }
  .tabs { flex: 1 1 0; min-width: 0; scrollbar-width: thin; }
  .tabs::-webkit-scrollbar { display: block; height: 4px; }
  .tabs .tab { flex: none; padding-inline: 8px; }
  .tabs .tab:focus-visible { outline-offset: -2px; }
  .tools-right { flex: none; flex-wrap: nowrap; gap: 6px; }
  .size-slider { width: 56px; }
  .header :is(.search-box input, .library-tools summary, #openSettingsBtn, .tabs .tab, .tools-right > :is(.btn, .tab, .size-ctrl, #sortMode, .select-trigger)) {
    height: var(--toolbar-height);
  }
  #openSettingsBtn { width: var(--toolbar-height); }
}

/* Responsive Multi-Device Optimization (Large Desktop -> Tablet -> Mobile -> Small Phone) */
@media (max-width: 960px) {
  .header-inner { padding: 10px 14px 8px; gap: 8px; }
  main { padding: 14px 14px calc(68px + env(safe-area-inset-bottom, 0px)); }
  .size-slider { width: 54px; }
  .kbd-hint { display: none; }
}

@media (max-width: 680px), (max-height: 440px) {
  body.select-mode .dashboard-controls { display: contents; }
  .batch-bar {
    position: sticky;
    top: env(safe-area-inset-top, 0px);
    z-index: 40;
    background: var(--bg);
  }
  .batch-inner { max-height: 50dvh; overflow-y: auto; }
}

@media (max-width: 680px) {
  .dashboard-controls { display: contents; }
  .header-inner { padding: 6px 10px; gap: 5px; }
  .bar { gap: 5px; }
  .logo { flex: none; font-size: 0.88rem; gap: 7px; margin-right: 2px; }
  .logo-mark { width: 25px; height: 25px; border-radius: 7px; }
  .search-box { flex: 1 1 0; min-width: 0; max-width: none; }
  .search-box input { height: 34px; font-size: 16px; padding: 0 8px 0 31px; }
  .search-box input:not(:placeholder-shown) { padding-right: 37px; }
  .search-clear { width: 28px; height: 28px; padding: 7px; }
  .bar > .library-actions { flex: none; justify-content: flex-end; }
  .library-tools-panel { inset-inline-end: -37px; }

  @media (max-width: 479px) {
    .bar > .library-actions { flex-basis: 100%; }
  }
  .btn { height: 32px; padding: 0 10px; font-size: 0.76rem; border-radius: 8px; }
  .tools-right .btn { justify-content: center; }
  .btn.icon-only { width: 32px; padding: 0; }

  .subbar {
    flex-direction: column;
    align-items: stretch;
    gap: 5px;
    padding-top: 0;
  }
  .tabs { width: 100%; -webkit-overflow-scrolling: touch; }
  .tabs .tab { height: 32px; }
  .tools-right {
    display: grid;
    grid-template-columns: minmax(0, 1fr) repeat(3, auto);
    gap: 5px;
  }
  .tools-right :is(.ctrl, .btn, #packsTabBtn) { height: 32px; padding-inline: 7px; }
  .tools-right :is(#sortMode, .select-trigger[aria-controls="sortMode-options"]) { inline-size: 100%; height: 32px; }
  .tools-right select.ctrl { min-width: 0; padding-inline-end: 28px; }
  @supports (appearance: base-select) {
    .tools-right select.ctrl { padding-inline-end: 8px; }
  }
  @media (max-width: 479px) {
    .size-slider, .size-val { display: none; }
  }
  .size-ctrl { height: 32px; padding: 2px; }
  .size-ctrl .seg { height: 100%; }

  @media (max-width: 479px) {
    .batch-inner {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 6px;
    }
    .batch-inner .spacer { display: none; }
    .batch-inner .btn { justify-content: center; }
    .batch-inner :is(.btn, .ctrl) { min-width: 0; height: 36px; }
  }

  .packs-header { flex-direction: column; align-items: stretch; gap: 10px; }
  .packs-grid { grid-template-columns: repeat(auto-fill, minmax(156px, 1fr)); gap: 10px; }
  .starter-packs-grid { grid-template-columns: repeat(auto-fill, minmax(156px, 1fr)); }
}

@media (max-width: 560px) {
  .two-col { grid-template-columns: 1fr; }
  main { padding: 10px 10px calc(64px + env(safe-area-inset-bottom, 0px)); }
  .grid { gap: 8px; }
  .batch-bar { padding: 6px 10px 0; }
  .field input, .field textarea, .field select, .field .select-trigger, .ctrl-input { font-size: 16px; }
  #toast {
    left: 12px;
    right: 12px;
    bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    justify-content: center;
  }
  dialog {
    width: 100vw;
    max-width: 100vw;
    max-height: 90dvh;
    margin: auto 0 0 0;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }
}

.note-text { font-size: 0.76rem; color: var(--text-muted); line-height: 1.5; }
code {
  font-family: var(--mono);
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.74rem;
  color: var(--accent-text);
}

.edit-preview-box {
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  min-height: 128px;
}
.edit-preview-box img { max-width: 220px; max-height: 220px; object-fit: contain; border-radius: 6px; }
.edit-preview-text { font-size: 0.9rem; color: var(--text); white-space: pre-wrap; text-align: center; }

.edit-meta-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  font-family: var(--mono);
  font-size: 0.73rem;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}
.meta-pill {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 9px;
}
.meta-pill.warn {
  background: rgba(245, 158, 11, 0.14);
  border-color: rgba(245, 158, 11, 0.45);
  color: #fcd34d;
}
