:root {
  --theme-bg:     #1a1a2e;
  --bg:           #1a1a2e;
  --bg-elevated:  #16213e;
  --bg-active:    #1c2750;
  --border:       #1a1a3e;
  --border-hover: #7c6aef88;
  --accent:       #7c6aef;
  --accent-hover: #9080ff;
  --text:         #e0e0e0;
  --text-muted:   #9aa0ad;
  /* Status colors: readable on the dark chrome (≥ 7:1 on --bg-elevated). */
  --ok:           #87e3ad;
  --ok-bg:        rgba(128, 255, 176, 0.10);
  --ok-border:    rgba(128, 255, 176, 0.35);
  --err:          #ff9b9b;
  --err-bg:       rgba(255, 128, 128, 0.10);
  --err-border:   rgba(255, 128, 128, 0.35);
}

[data-theme="light"] {
  --bg:           #ffffff;
  --bg-elevated:  #f6f8fa;
  --bg-active:    #e8eaed;
  --border:       #d0d7de;
  --border-hover: #6e5ce688;
  --accent:       #6e5ce6;
  --accent-hover: #5a4bd1;
  --text:         #24292f;
  --text-muted:   #656d76;
  /* The dark-chrome pastels drop below 2:1 on a light surface; use saturated
     tones that clear 4.5:1 on --bg-elevated. */
  --ok:           #1a7f37;
  --ok-bg:        rgba(26, 127, 55, 0.08);
  --ok-border:    rgba(26, 127, 55, 0.35);
  --err:          #b42318;
  --err-bg:       rgba(180, 35, 24, 0.06);
  --err-border:   rgba(180, 35, 24, 0.35);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

/* App-like layout: the page is locked to the viewport and the sidebars and the
   preview scroll internally (which is what keeps the sticky "Add font/theme"
   buttons in reach). The narrow-viewport query below switches to a stacked,
   page-scrolling layout instead. */
body {
  display: grid;
  /* minmax(0, 1fr): the middle column must be able to shrink below its
     content's minimum width, or the controls bar pushes the themes sidebar off
     the right edge on ~900px viewports. */
  grid-template-columns: 240px minmax(0, 1fr) 240px;
  grid-template-rows: auto auto auto 1fr;
  grid-template-areas:
    "fonts header  themes"
    "fonts tabs    themes"
    "fonts ctrls   themes"
    "fonts preview themes";
  gap: 16px;
  padding: 16px;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--theme-bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

.sidebar-fonts  { grid-area: fonts;   overflow-y: auto; min-height: 0; }
.sidebar-themes { grid-area: themes;  overflow-y: auto; min-height: 0; }
.lang-tabs      { grid-area: tabs; }
.controls-bar   { grid-area: ctrls; }

.pill {
  display: flex; align-items: center; gap: 6px;
  background: var(--bg-elevated);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  transition: border-color 0.2s, background 0.2s;
}
.pill[hidden] { display: none; }
.pill:hover { border-color: var(--border-hover); }
.pill[aria-selected="true"] { border-color: var(--accent); background: var(--bg-active); }
.pill:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.pill-name { font-weight: 600; }

/* Font pill: name + badges on top, a specimen line in the font itself below. */
.pill-font {
  flex-direction: column;
  align-items: flex-start;
}
.pill-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  width: 100%;
}
.pill-badges {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.pill-font .pill-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pill-preview {
  font-size: 1.1em;
  opacity: 0.7;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.pill-theme {
  flex-direction: column;
  align-items: stretch;
  padding: 10px 14px;
}
.pill-theme > .pill-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
  font-weight: normal;
}
.pill-meta {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* "×" to delete a user-added font/theme pill: revealed on hover/focus for
   pointer devices, always shown on touch (see the media query below). Not a
   Tab stop — keyboard users press Delete/Backspace on the focused pill. */
.pill-remove {
  display: none;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}
.pill:hover .pill-remove,
.pill:focus-within .pill-remove { display: flex; }
.pill-remove:hover { color: var(--err); background: var(--bg-active); }
/* The hover-reveal above never fires on touch. Cover touch-only devices
   (hover:none) AND hybrids with a touch screen plus a mouse (any-pointer:coarse)
   so the × is reachable whenever a finger is a possible input. */
@media (hover: none), (any-pointer: coarse) {
  .pill-remove {
    display: flex;
    width: 28px;
    height: 28px;
    font-size: 1.15rem;
  }
}


.preview-pane { font-feature-settings: "liga", "calt"; }
.preview-pane.no-liga { font-feature-settings: "liga" 0, "calt" 0; }
.preview-error { color: var(--err); padding: 8px; }

.sidebar-fonts ul, .sidebar-themes ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-search-wrap {
  position: relative;
  margin-bottom: 12px;
}

.sidebar-search {
  width: 100%;
  padding: 8px 36px 8px 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  transition: border-color 0.2s;
  -webkit-appearance: none;
  appearance: none;
}
/* The field ships its own clear "×"; hide the UA search decorations. */
.sidebar-search::-webkit-search-decoration,
.sidebar-search::-webkit-search-cancel-button { display: none; }
.sidebar-search:focus {
  outline: none;
  border-color: var(--accent);
}
.sidebar-search::placeholder {
  color: var(--text-muted);
}

.sidebar-search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  display: none;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}
.sidebar-search-clear:hover {
  color: var(--text);
  background: var(--bg-active);
}

.sidebar-search-wrap.has-value .sidebar-search-clear {
  display: flex;
}

.badge-web, .badge-installed, .badge-error, .badge-builtin, .badge-custom {
  font-size: 0.7em;
  padding: 2px 6px;
  border-radius: 10px;
  text-transform: uppercase;
  font-weight: bold;
  white-space: nowrap;
}
/* Fonts use installed/web; themes use custom/builtin. Same two color chips,
   named per domain so neither borrows the other's vocabulary. */
.badge-web, .badge-builtin { background: #2a2a4e; color: #a0a0ff; }
.badge-installed, .badge-custom { background: #1e3a2e; color: #80ffb0; }
.badge-error { background: #4a1e1e; color: #ff8080; }

.swatch-strip {
  display: flex;
  gap: 2px;
}
.swatch-strip span {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
}

.lang-tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.lang-tab {
  white-space: nowrap;
  font: inherit;
}

.controls-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
  padding: 12px;
  background: var(--bg-elevated);
  border-radius: 8px;
  border: 1px solid var(--border);
}
.controls-bar label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

input[type="range"] {
  accent-color: var(--accent);
  cursor: pointer;
}

/* Only used as a font pill's loading badge: small enough that a loading pill
   keeps the height of a settled one, and never squeezed by a long name. */
.spinner {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.preview-pane {
  grid-area: preview;
  overflow: auto;
  min-height: 0;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 24px;
  background: var(--theme-bg);
  /* Inherited by the Shiki <pre> and the plain fallback <pre>: tabbed samples
     (go.txt) indent like VS Code, not the browser's 8 columns. */
  tab-size: 4;
}
.shiki {
  padding: 16px;
  border-radius: 8px;
  line-height: 1.5;
  overflow-x: auto;
  background-color: transparent !important;
}

::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: var(--bg);
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--border-hover);
}

header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
}
header h1 {
  font-size: 1.5rem;
  color: var(--accent);
  flex-shrink: 0;
}
.header-quote {
  font-size: 0.85rem;
  font-style: italic;
  color: var(--text-muted);
  white-space: nowrap;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Dialog / Modal ────────────────────────────────────────── */

.upload-dialog {
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  /* Center in the viewport regardless of UA quirks: definite width + auto side
     margins center horizontally; fit-content height + top/bottom 0 + auto
     margins center vertically. Independent of the transform-based open anim. */
  position: fixed;
  inset: 0;
  margin: auto;
  min-width: min(420px, calc(100vw - 24px));
  max-width: 560px;
  width: 90vw;
  height: fit-content;
  max-height: 90vh;
  overflow: hidden;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
}
.upload-dialog[open] {
  display: flex;
  flex-direction: column;
  animation: dialog-in 0.18s ease-out;
}
@keyframes dialog-in {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
  to   { opacity: 1; transform: none; }
}

/* Transient toast for font/theme upload feedback (shown via the hidden
   attribute). Bottom-center and click-through: bottom-right sat exactly on the
   themes sidebar's "Add theme" button and swallowed clicks for six seconds. */
.upload-status {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: none;
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 0.82rem;
  line-height: 1.45;
  max-width: min(360px, calc(100vw - 32px));
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.4);
  z-index: 10000;
  animation: toast-in 0.22s ease-out;
}
.upload-status[hidden] { display: none; }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 12px); }
  to   { opacity: 1; transform: translateX(-50%); }
}

.upload-dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
}

.upload-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.upload-dialog-header h2 {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--accent);
  margin: 0;
}
.upload-dialog-header h2:focus { outline: none; }
.upload-dialog-header h2:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 4px;
}

.upload-dialog-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.25rem;
  padding: 2px 6px;
  border-radius: 4px;
  line-height: 1;
}
.upload-dialog-close:hover {
  color: var(--text);
  background: var(--bg-active);
}
.upload-dialog-close:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.upload-dialog-body {
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

.upload-dialog-body textarea,
.upload-dialog-body input[type="text"] {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 0.85rem;
  resize: vertical;
  transition: border-color 0.2s;
}
.upload-dialog-body textarea:focus,
.upload-dialog-body input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

.upload-dialog-body textarea {
  min-height: 100px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.78rem;
  line-height: 1.5;
}

.drop-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  border: 2px dashed var(--border);
  border-radius: 10px;
  padding: 26px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, color 0.2s;
}
.drop-zone:hover,
.drop-zone.drag-over {
  border-color: var(--accent);
  background: var(--bg-active);
  color: var(--text);
}
.drop-zone:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.drop-zone-icon {
  color: var(--accent);
  opacity: 0.9;
}
.drop-zone-icon svg { display: block; }

.upload-dialog-footer {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.btn-primary,
.btn-secondary {
  padding: 9px 18px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, opacity 0.2s;
}
.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text);
}
.btn-secondary:hover {
  border-color: var(--border-hover);
  background: var(--bg-active);
}
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn-primary:disabled,
.btn-secondary:disabled {
  opacity: 0.55;
  cursor: default;
}
.btn-primary:focus-visible,
.btn-secondary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Segmented control (Add Font dialog) */
.dialog-segments {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.dialog-segment {
  flex: 1;
  padding: 8px 10px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.dialog-segment:hover { color: var(--text); }
.dialog-segment[aria-selected="true"] {
  background: var(--accent);
  color: #fff;
}
.dialog-segment:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.dialog-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.dialog-panel[hidden] { display: none; }

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
}
.field-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.field-head span {
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
}
.field-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.dialog-example-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.dialog-example-btn:hover { color: var(--accent-hover); }
.dialog-example-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

/* Live status region. It stays in the DOM (never display:none) so screen
   readers announce the first message too; while empty it collapses and the
   negative margin swallows the flex gap above it. */
.dialog-status {
  display: block;
  font-size: 0.82rem;
  line-height: 1.45;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid transparent;
}
.dialog-status:empty {
  padding: 0;
  border: 0;
  margin-top: -16px;
}
.dialog-status.is-error {
  background: var(--err-bg);
  border-color: var(--err-border);
  color: var(--err);
}
.dialog-status.is-success {
  background: var(--ok-bg);
  border-color: var(--ok-border);
  color: var(--ok);
}

/* Sidebar "Add font / Add theme" call-to-action */
.sidebar-add-wrap {
  position: sticky;
  bottom: 0;
  margin-top: 12px;
  padding: 10px 0 2px;
  background: var(--theme-bg);
}
.sidebar-add-wrap::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -16px;
  height: 16px;
  background: linear-gradient(to top, var(--theme-bg), transparent);
  pointer-events: none;
}
.sidebar-add-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 11px 16px;
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  transition: background 0.2s, border-color 0.2s, box-shadow 0.2s, transform 0.1s;
}
.sidebar-add-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.28);
  transform: translateY(-1px);
}
.sidebar-add-btn:active { transform: translateY(0); }
.sidebar-add-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.add-btn-icon {
  font-size: 1.15rem;
  font-weight: 400;
  line-height: 1;
}

/* ── Export ("Use this setup in VS Code") ──────────────────── */

.export-btn {
  margin-left: auto;
  padding: 8px 16px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, transform 0.1s;
}
.export-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.export-btn:active { transform: translateY(1px); }
.export-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.export-intro {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.export-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.export-section-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}
.export-theme-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.export-note {
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text-muted);
}
.export-note a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.export-note a:hover { color: var(--accent-hover); }
.export-note.is-ok { color: var(--ok); }

.export-code {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.export-code pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
}
.export-code code {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.78rem;
  line-height: 1.55;
  color: var(--text);
  white-space: pre;
}
.export-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.export-copy-btn:hover {
  color: var(--text);
  border-color: var(--border-hover);
  background: var(--bg-active);
}
.export-copy-btn.is-copied {
  color: var(--ok);
  border-color: var(--ok-border);
}
.export-copy-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.export-download-btn { align-self: flex-start; }

.export-steps {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text-muted);
}
.export-steps li { padding-left: 2px; }

/* ── Responsive: collapse the 3-column grid on narrow viewports ───────────── */
@media (max-width: 820px) {
  body {
    grid-template-columns: 1fr;
    grid-template-areas:
      "header"
      "tabs"
      "ctrls"
      "preview"
      "fonts"
      "themes";
    /* Stacked: let the page scroll again. */
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }
  /* Cap the now-stacked sidebars so the long pill lists don't dominate scroll. */
  .sidebar-fonts,
  .sidebar-themes { max-height: 50vh; }
  .preview-pane { min-height: 40vh; }
  /* A horizontal scroll strip hides most tabs on a phone; wrapping shows all. */
  .lang-tabs { flex-wrap: wrap; overflow-x: visible; }
  header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  .header-quote { white-space: normal; }
}

/* ── Reduced motion ─────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .upload-dialog[open],
  .upload-status { animation: none; }
  .pill, .btn-primary, .btn-secondary, .sidebar-add-btn, .export-btn,
  .export-copy-btn, .dialog-segment, .drop-zone, .sidebar-search,
  .sidebar-search-clear, .pill-remove { transition: none; }
  .sidebar-add-btn:hover, .sidebar-add-btn:active,
  .export-btn:active { transform: none; }
  /* Keep the spinner (it conveys "still loading") but slow it right down. */
  .spinner { animation-duration: 3s; }
}
