:root {
  color-scheme: light dark;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --faint: #767674;
  --line: #e4e4e2;
  --line-strong: #cfcfcc;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --hover: #f0f0ee;
  --accent: #1a1a1a;
  --on-accent: #ffffff;
  --danger: #b3261e;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

/* Dark tokens: follow the system unless the header toggle picked light; the toggle can also force dark. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #ececea;
    --muted: #a3a3a0;
    --faint: #8f8f8c;
    --line: #2c2c2b;
    --line-strong: #3d3d3b;
    --bg: #141414;
    --surface: #1c1c1c;
    --hover: #262626;
    --accent: #ececea;
    --on-accent: #141414;
    --danger: #f2b8b5;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #ececea;
  --muted: #a3a3a0;
  --faint: #8f8f8c;
  --line: #2c2c2b;
  --line-strong: #3d3d3b;
  --bg: #141414;
  --surface: #1c1c1c;
  --hover: #262626;
  --accent: #ececea;
  --on-accent: #141414;
  --danger: #f2b8b5;
}

:root[data-theme="light"] {
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.visuallyHidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Header: one sticky bar holding filters, copy format, and tabs */

.topbar {
  position: sticky;
  top: 0;
  z-index: 3;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}

.topbarInner,
.shell {
  width: min(1240px, calc(100% - 32px));
  margin: 0 auto;
}

.topbarInner {
  display: grid;
  grid-template-columns: auto minmax(180px, 300px) auto auto 1fr auto auto;
  grid-template-areas: "brand search size format . tabs theme";
  align-items: center;
  gap: 10px 12px;
  min-height: 60px;
  padding: 10px 0;
}

.topbar.isGroups [data-explore-only] {
  display: none;
}

.brand {
  grid-area: brand;
  margin: 0 12px 0 0;
  font-size: 1rem;
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.search {
  grid-area: search;
  position: relative;
  min-width: 0;
}

.sizeControl {
  grid-area: size;
}

.formatControl {
  grid-area: format;
}

.tabs {
  grid-area: tabs;
  justify-self: end;
  display: flex;
  justify-content: flex-end;
  gap: 4px;
}

.themeToggle {
  grid-area: theme;
  justify-self: end;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
}

.themeToggle:hover {
  background: var(--hover);
  color: var(--ink);
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  border: 0;
  border-radius: 8px;
  padding: 0 12px;
  background: transparent;
  color: var(--muted);
  font-weight: 550;
}

.tab:hover {
  color: var(--ink);
  background: var(--hover);
}

.tab.isActive {
  color: var(--ink);
  background: var(--hover);
}

.badge {
  min-width: 18px;
  border-radius: 999px;
  padding: 1px 6px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 0.72rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.shell {
  padding: 20px 0 64px;
}

.search svg {
  position: absolute;
  left: 11px;
  top: 50%;
  translate: 0 -50%;
  color: var(--faint);
}

.search input {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 0 48px 0 34px;
  background: var(--surface);
}

.search input::placeholder {
  color: var(--faint);
}

.searchCount {
  position: absolute;
  right: 11px;
  top: 50%;
  translate: 0 -50%;
  color: var(--faint);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.segmented {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  margin: 0;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 2px;
  background: var(--surface);
}

.segmented label {
  position: relative;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented span {
  display: grid;
  place-items: center;
  min-width: 30px;
  min-height: 30px;
  border-radius: 6px;
  padding: 0 8px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 550;
  white-space: nowrap;
  cursor: pointer;
}

.segmented span:hover {
  color: var(--ink);
}

.segmented input:checked + span {
  background: var(--accent);
  color: var(--on-accent);
}

.segmented input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.toolbarLabel {
  float: left;
  padding: 0 6px 0 6px;
  color: var(--faint);
  font-size: 0.76rem;
  white-space: nowrap;
}

/* Two rows: title + tabs, then filters */
@media (max-width: 1179px) {
  .topbarInner {
    grid-template-columns: minmax(0, 1fr) auto auto auto;
    grid-template-areas:
      "brand brand tabs theme"
      "search size format format";
  }

  .topbar.isGroups .topbarInner {
    grid-template-areas: "brand brand tabs theme";
  }
}

/* Gallery */

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
}

.card {
  display: grid;
  align-content: start;
  gap: 4px;
  border-radius: var(--radius);
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
}

.strip {
  display: flex;
  overflow: hidden;
}

.strip > span {
  flex: 1;
}

.cardStrip {
  height: 132px;
  border-radius: 6px;
}

.cardHead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 4px 0 6px;
}

.cardHead h2 {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.addButton {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
}

.addButton:hover {
  border-color: var(--accent);
  color: var(--ink);
}

.addButton.isSaved {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
}

.colorList {
  display: grid;
  margin: 0;
  padding: 0;
  list-style: none;
}

.colorRow {
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr) 16px;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: 0;
  border-radius: 6px;
  padding: 6px;
  background: transparent;
  text-align: left;
}

.colorRow:hover {
  background: var(--hover);
}

.chip {
  width: 14px;
  height: 14px;
  border-radius: 999px;
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.12);
}

.colorText {
  display: grid;
  min-width: 0;
}

.colorName {
  overflow: hidden;
  font-size: 0.86rem;
  font-weight: 500;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.colorValue {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.74rem;
}

.copyIcon {
  color: var(--faint);
  opacity: 0;
}

.colorRow:hover .copyIcon,
.colorRow:focus-visible .copyIcon {
  opacity: 1;
}

.emptyState,
.error {
  margin: 48px 0;
  color: var(--muted);
  text-align: center;
}

/* Groups */

.groupsPanel {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}

.tray {
  position: sticky;
  top: 80px;
  max-height: calc(100vh - 100px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  background: var(--surface);
}

.sectionHead {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.sectionHead h2 {
  margin: 0;
  outline: none;
  font-size: 0.95rem;
  font-weight: 600;
}

.count {
  color: var(--faint);
  white-space: nowrap;
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
}

.hint {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.4;
}

.trayList {
  display: grid;
  gap: 6px;
  margin-top: 14px;
}

.trayEmpty {
  display: grid;
  justify-items: start;
  gap: 12px;
}

.trayEmpty p {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.5;
}

.inlineIcon {
  display: inline-grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  vertical-align: -5px;
}

.inlineIcon svg {
  width: 12px;
  height: 12px;
}

.trayItem {
  display: flex;
  align-items: center;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px;
  background: var(--surface);
  cursor: grab;
}

.trayItem:active {
  cursor: grabbing;
}

.trayItem.isSelected {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.trayPick {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 10px;
  min-width: 0;
  border: 0;
  border-radius: 5px;
  padding: 2px;
  background: transparent;
  cursor: inherit;
}

.trayStrip {
  width: 96px;
  height: 28px;
  border-radius: 4px;
}

.comboId {
  font-size: 0.82rem;
  font-weight: 550;
  font-variant-numeric: tabular-nums;
}

.iconButton {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--faint);
}

.iconButton:hover {
  background: var(--hover);
  color: var(--ink);
}

.iconButton.small {
  width: 22px;
  height: 22px;
}

.iconButton.small svg {
  width: 13px;
  height: 13px;
}

.groupsColumn {
  display: grid;
  gap: 12px;
}

.groups {
  display: grid;
  gap: 12px;
}

.group {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 12px 14px 16px;
  background: var(--surface);
  transition: border-color 120ms ease-out, box-shadow 120ms ease-out;
}

.groups.isDragging .group {
  border-style: dashed;
  border-color: var(--line-strong);
}

.group.isDropping,
.groups.isDragging .group.isDropping {
  border-style: solid;
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.groupHead {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.groupNameField {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 360px;
}

.groupName {
  width: 100%;
  min-height: 32px;
  border: 1px solid transparent;
  border-radius: 6px;
  margin-left: -7px;
  padding: 0 6px;
  background: transparent;
  font-size: 0.98rem;
  font-weight: 600;
}

.groupName:hover {
  border-color: var(--line);
}

.groupName:focus {
  border-color: var(--line-strong);
  outline: none;
  background: var(--bg);
}

.groupActions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: auto;
}

.groupCombos {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.groupCombo {
  display: grid;
  gap: 4px;
  width: 120px;
}

.groupStrip {
  height: 48px;
  border-radius: 6px;
}

.groupComboFoot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 2px;
}

.groupComboFoot .iconButton {
  opacity: 0;
}

.groupCombo:hover .iconButton,
.groupCombo:focus-within .iconButton {
  opacity: 1;
}

@media (hover: none) {
  .groupComboFoot .iconButton,
  .copyIcon {
    opacity: 1;
  }
}

.dropTarget {
  display: grid;
  place-items: center;
  flex: 1 1 120px;
  min-height: 48px;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  padding: 0 12px;
  background: transparent;
  color: var(--faint);
  font-size: 0.8rem;
}

.dropTarget.isAction {
  flex: 0 1 160px;
  align-self: start;
  border-color: var(--accent);
  color: var(--ink);
  font-weight: 550;
}

.dropTarget.isAction:hover {
  background: var(--hover);
}

.newGroup {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  font-weight: 550;
}

.newGroup:hover {
  border-color: var(--accent);
  color: var(--ink);
}

.importBox {
  margin-top: 8px;
}

.importBox summary {
  width: fit-content;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
}

.importBox summary:hover {
  color: var(--ink);
}

.importForm {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.importForm textarea {
  width: 100%;
  min-height: 96px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--surface);
  font-family: var(--mono);
  font-size: 0.8rem;
  resize: vertical;
}

.importForm textarea[aria-invalid="true"] {
  border-color: var(--danger);
}

.importFoot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.importFoot .hint {
  margin: 0;
}

/* Buttons */

.button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 0 12px;
  background: var(--surface);
  font-size: 0.85rem;
  font-weight: 550;
}

.button:hover {
  border-color: var(--accent);
}

.button.quiet {
  border-color: transparent;
  padding: 0 8px;
  background: transparent;
  color: var(--muted);
}

.button.quiet:hover {
  background: var(--hover);
  color: var(--ink);
}

.button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
}

.button.danger {
  border-color: var(--danger);
  background: var(--danger);
  color: var(--surface);
}

/* Modal + toast */

.confirmModal {
  width: min(400px, calc(100% - 32px));
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.2);
}

.confirmModal::backdrop {
  background: rgb(0 0 0 / 0.4);
}

.confirmModal form {
  padding: 20px;
}

.confirmModal h2 {
  margin: 0;
  font-size: 1rem;
}

.confirmModal p {
  margin: 8px 0 20px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.modalActions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.toast:not(.isVisible) {
  opacity: 0;
  pointer-events: none;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  z-index: 4;
  translate: -50% 0;
  margin: 0;
  border-radius: 8px;
  padding: 9px 14px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 0.85rem;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
}

/* Phones: tabs + size, then search + copy format; title hidden to save height */
@media (max-width: 699px) {
  .topbarInner,
  .shell {
    width: calc(100% - 24px);
  }

  .topbarInner {
    grid-template-columns: minmax(0, 1fr) auto auto;
    grid-template-areas:
      "size tabs theme"
      "search format format";
    gap: 8px;
    min-height: 0;
    padding: 8px 0;
  }

  .topbar.isGroups .topbarInner {
    grid-template-areas: "size tabs theme";
  }

  .brand {
    grid-area: auto;
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }

  .tabs {
    gap: 2px;
  }

  .sizeControl {
    justify-self: start;
  }

  .formatControl {
    justify-self: end;
  }

  .tab {
    padding: 0 8px;
  }

  .themeToggle {
    width: 30px;
  }

  .toolbarLabel {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }

  .segmented span {
    min-width: 24px;
    padding: 0 5px;
    font-size: 0.76rem;
  }

  .search input {
    padding: 0 10px 0 30px;
  }

  .searchCount {
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
  }

  .search svg {
    left: 9px;
  }

  .gallery {
    gap: 12px;
  }

  .groupsPanel {
    grid-template-columns: 1fr;
  }

  .tray {
    position: static;
    max-height: none;
  }
}
