@font-face {
  font-family: "Bravura";
  src: url("Bravura.woff2") format("woff2");
  font-display: swap;
}

:root {
  --cell-size: 48px;        /* glyph render size, driven by the +/- buttons */
  --toolbar-h: 52px;
  --sidebar-w: 260px;

  --bg: #f5f6f8;
  --bg-elev: #ffffff;
  --bg-sunken: #eceef1;
  --fg: #1c1e21;
  --fg-muted: #6b7280;
  --border: #d6d9de;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --hover: #eef2ff;
  --active: #dbe4ff;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.18);
}

[data-theme="dark"] {
  --bg: #15171c;
  --bg-elev: #1e2127;
  --bg-sunken: #0f1115;
  --fg: #e6e8eb;
  --fg-muted: #9aa1ab;
  --border: #333842;
  --accent: #5b8cff;
  --accent-fg: #0f1115;
  --hover: #232a3a;
  --active: #2c3650;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}
body {
  display: flex;
  flex-direction: column;
  height: 100vh;   /* fallback */
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Toolbar ---------- */
.toolbar {
  flex: 0 0 auto;
  z-index: 30;
  min-height: var(--toolbar-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.toolbar__title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  white-space: nowrap;
}
.toolbar__title .brand-glyph {
  font-family: "Bravura";
  font-size: 22px;
  line-height: 1;
}
.toolbar__group { display: flex; align-items: center; gap: 6px; }
.toolbar__search { flex: 1 1 auto; min-width: 160px; }
.toolbar__spacer { margin-left: auto; }

.size-label {
  min-width: 46px;
  text-align: center;
  color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
}

.search {
  width: 100%;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-sunken);
  color: var(--fg);
  font-size: 14px;
}
.search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.result-count { color: var(--fg-muted); white-space: nowrap; }

.btn {
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-sunken);
  color: var(--fg);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}
.btn:hover { background: var(--hover); }
.btn:active { transform: translateY(1px); }
.btn--icon { width: 34px; padding: 0; font-size: 18px; display: inline-flex; align-items: center; justify-content: center; }

/* ---------- Layout ---------- */
.layout {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
}

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  overflow-y: auto;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  padding: 8px 0;
}
.side-item {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  padding: 7px 14px;
  cursor: pointer;
  border-left: 3px solid transparent;
  color: var(--fg);
}
.side-item:hover { background: var(--hover); }
.side-item.active {
  background: var(--active);
  border-left-color: var(--accent);
  font-weight: 600;
}
.side-item__count { color: var(--fg-muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.side-item.hidden { display: none; }

.grid-root {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 16px 120px;
  scroll-behavior: smooth;
}
.grid-root:focus { outline: none; }

/* ---------- Sections & grid ---------- */
.section { margin-bottom: 14px; }
.section.hidden { display: none; }

.section__header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 8px 4px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.section__title { font-size: 15px; font-weight: 700; }
.section__range { color: var(--fg-muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(calc(var(--cell-size) + 22px), 1fr));
  gap: 6px;
  padding: 10px 0;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elev);
  cursor: pointer;
  overflow: hidden;
  font-family: "Bravura";
  font-size: var(--cell-size);
  line-height: 1;
  user-select: none;
}
.cell:hover { background: var(--hover); border-color: var(--accent); }
.cell.selected { background: var(--active); border-color: var(--accent); }
.cell.hidden { display: none; }
.cell__glyph { pointer-events: none; }

/* Glyphs upgraded to inline SVG: viewBox = the glyph's ink box, so the cell's
   flex centering centers the *ink* (not the baseline-anchored line box). The svg
   box is sized in em units (--gw/--gh) off --g-base, so +/- resizing just works. */
.glyph {
  display: block;
  width:  calc(var(--g-base, var(--cell-size)) * var(--gw));
  height: calc(var(--g-base, var(--cell-size)) * var(--gh));
  pointer-events: none;
}
.glyph text { font-family: "Bravura"; font-size: 1000px; fill: currentColor; }

/* ---------- Popover ---------- */
.popover {
  position: fixed;
  z-index: 50;
  width: 320px;
  max-width: calc(100vw - 24px);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 14px;
}
.popover[hidden] { display: none; }
.popover__close {
  position: absolute;
  top: 6px;
  right: 8px;
  border: none;
  background: transparent;
  color: var(--fg-muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.popover__close:hover { color: var(--fg); }
.popover__preview {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 96px;
  margin-bottom: 8px;
  background: var(--bg-sunken);
  border-radius: 8px;
  overflow: hidden;
}
.po-glyph { font-family: "Bravura"; font-size: 72px; line-height: 1; }
.popover__name { font-weight: 700; word-break: break-word; }
.popover__desc { color: var(--fg-muted); margin: 4px 0 6px; word-break: break-word; }
.popover__cp {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 13px;
  margin-bottom: 10px;
  color: var(--fg);
}
.popover__actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.copy-btn { padding: 0 6px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.copy-btn.copied { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

/* ---------- Mobile ---------- */
@media (max-width: 640px) {
  .toolbar {
    flex-wrap: wrap;
    row-gap: 6px;
    padding: 6px 10px;
    gap: 10px;
  }
  /* Hide the app name (keep the brand glyph). */
  .toolbar__title span:last-child { display: none; }
  /* Auto-margin misbehaves once items wrap. */
  .toolbar__spacer { margin-left: 0; }
  /* Search takes its own full-width row below title / size / theme. */
  .toolbar__search { order: 3; flex-basis: 100%; }

  .sidebar {
    width: clamp(120px, 42vw, 50%);
    flex-basis: clamp(120px, 42vw, 50%);
  }
  /* Let long category names wrap; keep the count pinned top-right. */
  .side-item { align-items: flex-start; }
  .side-item > span:first-child { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
  .side-item__count { flex: 0 0 auto; }

  /* Popover becomes a bottom sheet so it never covers the tapped glyph. */
  .popover {
    left: 8px;
    right: 8px;
    bottom: 8px;
    top: auto;
    width: auto;
    max-width: none;
  }
}
