:root {
  --bg: #0f1012;
  --panel: #17181c;
  --panel-2: #1d1f24;
  --line: #2a2d34;
  --text: #e8e9ec;
  --muted: #9aa0ab;
  --accent: #6aa2ff;
  --accent-ink: #0b1220;
  --danger: #ff6b6b;
  --ok: #59d09a;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

/* author `display` rules outrank the UA stylesheet, so `hidden` needs help */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Segoe UI", system-ui, -apple-system, sans-serif;
}

code {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 0.9em;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
}

a {
  color: var(--accent);
}

/* ---------- buttons ---------- */

.btn {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 7px 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, opacity 0.12s;
}

.btn:hover:not(:disabled) {
  background: #262a31;
  border-color: #3a3f49;
}

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

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: #85b4ff;
  border-color: #85b4ff;
}

.btn-ghost {
  background: transparent;
}

.btn-icon {
  padding: 5px 10px;
  font-size: 13px;
}

.btn-danger:hover:not(:disabled) {
  border-color: var(--danger);
  color: var(--danger);
}

/* ---------- app shell ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  letter-spacing: 0.2px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  font-weight: 700;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.pill {
  font-size: 12px;
  color: var(--ok);
  border: 1px solid #2c4a3e;
  background: #14241d;
  border-radius: 999px;
  padding: 3px 10px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

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

.search {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 7px 11px;
  width: 200px;
}

.search:focus,
.code-input:focus,
.modal-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.crumbs {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
  min-height: 30px;
}

.crumb {
  font: inherit;
  color: var(--muted);
  background: none;
  border: 0;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
}

.crumb:hover {
  color: var(--text);
  background: var(--panel-2);
}

.crumb.current {
  color: var(--text);
  font-weight: 600;
  cursor: default;
}

.crumb-sep {
  color: #4b5059;
}

/* ---------- list ---------- */

.workspace {
  display: flex;
  flex: 1;
  min-height: 0;
}

.content {
  position: relative;
  flex: 1;
  min-width: 0;
  overflow: auto;
  padding: 12px 20px 24px;
  /* rows react to the list's own width, which the viewer pane eats into */
  container-type: inline-size;
}

.list {
  display: flex;
  flex-direction: column;
}

.row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 12px;
  padding: 10px 10px;
  border-bottom: 1px solid #202329;
  border-radius: 8px;
}

.row:hover {
  background: var(--panel);
}

.row-icon {
  font-size: 17px;
  text-align: center;
  opacity: 0.9;
}

.row-name {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 9px;
}

.name-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.name-text.is-link {
  cursor: pointer;
}

.name-text.is-link:hover {
  text-decoration: underline;
}

.badge {
  font-size: 10.5px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
}

.badge-pdf {
  color: #ffb27a;
  border-color: #4a3726;
}

.badge-star {
  border: 0;
  color: #f3c969;
}

.row-meta {
  color: var(--muted);
  font-size: 12.5px;
  white-space: nowrap;
}

.row-actions {
  display: flex;
  gap: 6px;
  opacity: 0;
  transition: opacity 0.12s;
}

.row:hover .row-actions,
.row:focus-within .row-actions {
  opacity: 1;
}

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

.drop-hint {
  position: absolute;
  inset: 8px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: rgba(106, 162, 255, 0.08);
  color: var(--accent);
  font-weight: 600;
  pointer-events: none;
}

/* ---------- status bar ---------- */

.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 20px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 12.5px;
  min-height: 36px;
}

.upload-status {
  display: flex;
  align-items: center;
  gap: 10px;
}

.progress {
  display: block;
  width: 160px;
  height: 5px;
  border-radius: 999px;
  background: var(--panel-2);
  overflow: hidden;
}

.progress > span {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s;
}

/* ---------- pdf viewer ---------- */

/* docked beside the list on wide screens, full-screen overlay below 900px */
.viewer {
  display: flex;
  flex-direction: column;
  min-height: 0;
  width: clamp(420px, 46%, 900px);
  flex-shrink: 0;
  border-left: 1px solid var(--line);
  background: var(--bg);
}

.viewer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.viewer-title {
  flex: 1;
  min-width: 0; /* without this the title refuses to shrink and pushes the buttons out */
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewer-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.viewer-body {
  position: relative;
  flex: 1;
  min-height: 0;
  padding: 12px;
}

.viewer-frame {
  width: 100%;
  height: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #2b2d33;
}

.viewer-canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.viewer-pager {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-right: 6px;
}

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

.viewer-loading {
  position: absolute;
  inset: 12px;
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
}

@media (max-width: 900px) {
  .viewer {
    position: fixed;
    inset: 0;
    z-index: 15;
    width: auto;
    border-left: 0;
    background: rgba(8, 9, 11, 0.96);
    backdrop-filter: blur(2px);
  }
}

/* ---------- pairing ---------- */

.pairing {
  display: grid;
  place-items: center;
  min-height: 100%;
  padding: 40px 20px;
}

.card {
  width: min(620px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 32px;
}

.card h1 {
  margin: 0 0 10px;
  font-size: 24px;
}

.lede {
  color: var(--muted);
  margin: 0 0 20px;
}

.steps {
  margin: 0 0 24px;
  padding-left: 20px;
}

.steps li {
  margin-bottom: 8px;
}

.pair-form {
  display: flex;
  gap: 10px;
}

.code-input {
  flex: 1;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 20px;
  letter-spacing: 6px;
  text-transform: lowercase;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
}

.pair-error {
  margin: 14px 0 0;
  color: var(--danger);
}

.fineprint {
  margin: 24px 0 0;
  color: var(--muted);
  font-size: 12.5px;
  border-top: 1px solid var(--line);
  padding-top: 16px;
}

/* ---------- modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.55);
  z-index: 20;
}

.modal {
  width: min(420px, calc(100% - 40px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 22px;
}

.modal h2 {
  margin: 0 0 12px;
  font-size: 17px;
}

.modal-body {
  margin: 0 0 14px;
  color: var(--muted);
}

.modal-input {
  font: inherit;
  width: 100%;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 12px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 30;
}

.toast {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 10px 14px;
  max-width: 380px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  animation: slide-in 0.15s ease-out;
}

.toast-error {
  border-left-color: var(--danger);
}

.toast-ok {
  border-left-color: var(--ok);
}

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
}

@container (max-width: 620px) {
  .row {
    grid-template-columns: 24px minmax(0, 1fr) auto;
  }

  .row-meta {
    display: none;
  }
}

/* nothing to hover with, so the row actions have to stay visible */
@media (hover: none) {
  .row-actions {
    opacity: 1;
  }
}

/* ---------- lyrics ---------- */

.lyrics-modal {
  width: min(880px, calc(100% - 40px));
  display: flex;
  flex-direction: column;
  max-height: calc(100% - 60px);
}

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

.lyrics-head h2 {
  margin: 0;
}

.lyrics-search {
  display: flex;
  gap: 8px;
}

.lyrics-panes {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 14px;
  margin-top: 14px;
  min-height: 0;
}

.lyrics-results {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow: auto;
  max-height: 46vh;
  padding-right: 4px;
}

.lyric-hit {
  font: inherit;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  cursor: pointer;
}

.lyric-hit:hover {
  border-color: #3a3f49;
}

.lyric-hit.is-picked {
  border-color: var(--accent);
}

.lyric-hit-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lyric-hit-meta {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lyric-empty {
  margin: 4px 2px;
  color: var(--muted);
  font-size: 13px;
}

.lyrics-editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

.lyrics-text {
  font: inherit;
  flex: 1;
  min-height: 240px;
  resize: vertical;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  line-height: 1.55;
  white-space: pre-wrap;
}

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

.lyrics-target {
  margin-right: auto;
  color: var(--muted);
  font-size: 13px;
  align-self: center;
}

@media (max-width: 720px) {
  .lyrics-panes {
    grid-template-columns: 1fr;
  }

  .lyrics-results {
    max-height: 22vh;
  }
}

/* ---------- busy states ---------- */

.spinner {
  display: inline-block;
  flex: none;
  width: 13px;
  height: 13px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.65s linear infinite;
}

/* a spinner sitting on a filled button has to be light against the accent */
.btn-primary .spinner {
  border-color: rgba(11, 18, 32, 0.35);
  border-top-color: var(--accent-ink);
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation-duration: 1.6s;
  }
}

.status-line {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* the label keeps its width while the spinner replaces the text, so a busy
   button doesn't jump about */
.btn > .btn-busy {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.lyric-loading {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 6px 2px;
  color: var(--muted);
  font-size: 13px;
}

.lyric-hit.is-loading {
  border-color: var(--accent);
}

/* the name shares its line with a spinner while that track is being fetched */
.lyric-hit-top {
  display: flex;
  align-items: center;
  gap: 8px;
}

.lyric-hit-top .lyric-hit-name {
  flex: 1;
  min-width: 0;
}

/* ---------- phones and touch ---------- */

/* the address bar eats into `100%` while it's on screen, so a phone gets the
   dynamic viewport and the layout stops jumping as that bar slides away */
.app {
  height: 100dvh;
}

.pairing {
  min-height: 100dvh;
}

@media (hover: none) {
  /* iOS Safari zooms the whole page when a field under 16px takes focus */
  .search,
  .modal-input,
  .lyrics-text {
    font-size: 16px;
  }

  /* a fingertip is about 9mm across; mouse-sized targets don't cut it */
  .btn {
    padding: 10px 14px;
  }

  .btn-icon {
    min-height: 38px;
    padding: 8px 12px;
    font-size: 13.5px;
  }

  .crumb {
    padding: 7px 8px;
  }

  .btn,
  .crumb,
  .lyric-hit,
  .name-text.is-link {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }
}

@media (max-width: 720px) {
  .topbar,
  .toolbar {
    padding: 10px 12px;
    gap: 10px;
  }

  .content {
    padding: 10px 12px 24px;
  }

  .statusbar {
    padding: 8px 12px;
  }

  /* the filter takes a line of its own; the buttons share the next one and
     stretch, so none of them ends up a sliver wide */
  .crumbs,
  .toolbar-actions {
    width: 100%;
  }

  .search {
    order: -1;
    flex: 1 0 100%;
    width: 100%;
  }

  .toolbar-actions .btn {
    flex: 1 1 auto;
  }

  .progress {
    width: 90px;
  }

  /* the viewer title gets the first line, its buttons the second */
  .viewer-bar {
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 12px;
  }

  .viewer-title {
    flex: 1 0 100%;
  }

  .viewer-actions {
    width: 100%;
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .viewer-body {
    padding: 8px;
  }

  .toasts {
    left: 12px;
    right: 12px;
    bottom: 12px;
  }

  .toast {
    max-width: none;
  }
}

@media (max-width: 560px) {
  /* the header has no room for a badge that only ever says "connected" */
  .pill {
    display: none;
  }

  .pairing {
    padding: 20px 12px;
  }

  .card {
    padding: 20px 16px;
  }

  .card h1 {
    font-size: 20px;
  }

  /* side by side, the code field and the button squeeze each other until the
     code itself is unreadable; stacked, both get the full width */
  .pair-form {
    flex-direction: column;
  }

  .code-input {
    font-size: 18px;
    letter-spacing: 4px;
    text-align: center;
  }

  .pair-form .btn {
    padding: 12px;
  }

  .steps {
    padding-left: 18px;
  }
}

/* the row's buttons used to squeeze the name down to an ellipsis; below this
   width they drop to a line of their own and the name keeps the first one */
@container (max-width: 560px) {
  .row {
    grid-template-columns: 24px minmax(0, 1fr);
    grid-template-areas:
      "icon name"
      "actions actions";
    row-gap: 8px;
    padding: 10px 6px;
  }

  .row-icon {
    grid-area: icon;
  }

  .row-name {
    grid-area: name;
  }

  .row-meta {
    display: none;
  }

  .row-actions {
    grid-area: actions;
    flex-wrap: wrap;
    gap: 6px;
  }
}

/* ---------- popups on a phone ---------- */

@media (max-width: 720px) {
  /* a centred card taller than the screen hangs off both ends and clips its
     own buttons, so on a phone the popups become sheets anchored to the bottom
     where the thumb already is */
  .modal-backdrop {
    place-items: end stretch;
  }

  .modal {
    width: 100%;
    max-height: 100dvh;
    overflow: auto;
    border-bottom: 0;
    border-radius: 14px 14px 0 0;
    padding: 18px 16px;
  }

  .modal-actions .btn {
    flex: 1 1 0;
  }

  .lyrics-modal {
    height: 100dvh;
    max-height: 100dvh;
    border: 0;
    border-radius: 0;
    padding: 12px 12px 0;
    overflow: hidden; /* the middle scrolls, the chrome stays put */
  }

  /* the blurb costs a third of the screen and says nothing the page doesn't */
  .lyrics-modal > .modal-body {
    display: none;
  }

  .lyrics-search {
    margin-top: 10px;
  }

  .lyrics-panes {
    flex: 1;
    min-height: 0;
    overflow: auto;
    margin-top: 10px;
  }

  .lyrics-results {
    max-height: 30dvh;
  }

  .lyrics-text {
    min-height: 150px;
  }

  /* pinned below the scrolling panes, so "Send" is reachable however long the
     lyrics are */
  .lyrics-modal .modal-actions {
    flex-shrink: 0;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 0;
    padding: 10px 0;
    border-top: 1px solid var(--line);
    background: var(--panel);
  }

  .lyrics-target {
    flex: 1 0 100%;
    margin-right: 0;
  }
}
