:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --selected: #dbeafe;
  --selected-border: #2563eb;
  --danger: #b91c1c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 100vh;
}

.header {
  padding: 14px 14px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.header h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.sub {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.noun-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.noun-badge.ok {
  background: #ecfdf5;
  border-color: #a7f3d0;
  color: #047857;
}

.noun-badge.bad {
  background: #fef2f2;
  border-color: #fecaca;
  color: #b91c1c;
}

.noun-badge.warn {
  background: #fffbeb;
  border-color: #fde68a;
  color: #b45309;
}

.noun-session-group {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
}

.noun-help {
  margin: 0 0 6px;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.35;
}

.noun-input {
  width: 100%;
  resize: vertical;
  min-height: 56px;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 11px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}

.noun-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
}

.btn-secondary {
  height: 28px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.btn-secondary:hover {
  border-color: #93c5fd;
  background: #eff6ff;
}

.btn-link {
  border: none;
  background: none;
  color: var(--accent);
  font-size: 12px;
  cursor: pointer;
  padding: 0;
}

.chk-inline {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
}

.chk-inline input {
  margin: 0;
}

.grid.loading {
  opacity: 0.55;
  pointer-events: none;
}

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

.search-row {
  display: flex;
  gap: 8px;
  padding: 10px 14px 6px;
  background: var(--surface);
}

#searchInput {
  flex: 1;
  min-width: 0;
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  outline: none;
  background: #fff;
}

#searchInput:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

#searchBtn {
  height: 36px;
  padding: 0 14px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

#searchBtn:hover {
  background: var(--accent-hover);
}

#searchBtn:disabled {
  opacity: 0.6;
  cursor: wait;
}

.sources-panel {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.sources-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  border: none;
  background: transparent;
  cursor: pointer;
  font: inherit;
  color: var(--text);
  text-align: left;
}

.sources-toggle:hover {
  background: #f3f4f6;
}

.sources-toggle-left {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.sources-toggle .chevron {
  display: inline-block;
  width: 1em;
  color: var(--muted);
  transition: transform 0.15s ease;
  font-size: 11px;
}

.sources-toggle[aria-expanded="true"] .chevron {
  transform: rotate(90deg);
}

.sources-title {
  font-weight: 600;
  font-size: 12px;
}

.sources-summary {
  font-size: 11px;
  color: var(--muted);
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  white-space: nowrap;
}

.sources-body {
  padding: 0 14px 10px;
}

.sources-body.collapsed,
.sources-body[hidden] {
  display: none;
}

.source-group {
  margin-top: 6px;
}

.source-group-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-bottom: 6px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

.chip input {
  margin: 0;
}

.status {
  margin-left: auto;
  color: var(--muted);
  font-size: 11px;
  flex-shrink: 0;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.selected-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: #eff6ff;
  border-bottom: 1px solid #bfdbfe;
}

.selected-bar.hidden {
  display: none;
}

.selected-thumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1;
  min-width: 0;
  max-height: 64px;
  overflow: auto;
}

.selected-thumb {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: #fff;
  border: 1px solid var(--selected-border);
  display: grid;
  place-items: center;
  padding: 3px;
}

.selected-thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.link-btn {
  border: none;
  background: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 12px;
  white-space: nowrap;
}

.grid {
  flex: 1;
  overflow: auto;
  padding: 10px 12px 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
  align-content: start;
}

.card {
  position: relative;
  aspect-ratio: 1;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 6px 6px;
  transition: border-color 0.12s, background 0.12s, transform 0.08s;
  user-select: none;
}

.card:hover {
  border-color: #93c5fd;
}

.card:active {
  transform: scale(0.97);
}

.card.selected {
  border-color: var(--selected-border);
  background: var(--selected);
}

.card .preview {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
}

.card .preview img {
  width: 40px;
  height: 40px;
  object-fit: contain;
  pointer-events: none;
}

.card .name {
  margin-top: 6px;
  width: 100%;
  font-size: 10px;
  color: var(--muted);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card .badge {
  position: absolute;
  top: 4px;
  left: 4px;
  font-size: 9px;
  padding: 1px 4px;
  border-radius: 4px;
  background: #f3f4f6;
  color: #4b5563;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.card .check {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #d1d5db;
  background: #fff;
  display: grid;
  place-items: center;
  font-size: 11px;
  color: transparent;
  font-weight: 700;
}

.card.selected .check {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--muted);
}

.empty.hidden {
  display: none;
}

.footer {
  flex-shrink: 0;
  padding: 10px 14px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.04);
}

.footer-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 12px;
  color: var(--muted);
}

.size-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.size-label select {
  height: 26px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
}

.insert-btn {
  width: 100%;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.insert-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.insert-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.footnote-hint {
  margin: 8px 0 0;
  font-size: 10px;
  color: var(--muted);
  line-height: 1.35;
}

.toast {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: 100px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #111827;
  color: #fff;
  font-size: 12px;
  text-align: center;
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.toast.show {
  opacity: 1;
}

.toast.error {
  background: var(--danger);
}
