:root {
  --fg: #1a1a1a;
  --bg: #fafafa;
  --link: #0a58ca;
  --muted: #777;
  --border: #e0e0e0;
  --card-bg: #fff;
  /* The GUI's content lane: search bar, search results, headers, lists. Tab
   * bodies escape this on wide viewports (see .tab-body media query). */
  --content-max: 720px;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

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

#search-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 1rem;
}
.search-bar-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  max-width: var(--content-max);
  margin: 0 auto;
}
#home-link {
  flex: 0 0 auto;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--card-bg);
  color: var(--fg);
  text-decoration: none;
  font-weight: 500;
  line-height: 1;
}
#home-link:hover { background: #f0f0f0; text-decoration: none; }
#search-input {
  flex: 1;
  min-width: 0;
  padding: 0.5rem 0.75rem;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--card-bg);
  color: var(--fg);
}
#search-btn,
#language-btn {
  flex: 0 0 auto;
  padding: 0.5rem 1rem;
  font: inherit;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--card-bg);
  color: var(--fg);
  cursor: pointer;
}
#search-btn:hover,
#language-btn:hover { background: #f0f0f0; }
#search-input:focus {
  outline: 2px solid var(--link);
  outline-offset: -1px;
}
#search-results {
  max-width: var(--content-max);
  margin: 0.5rem auto 0;
  max-height: 60vh;
  overflow-y: auto;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.25rem 1rem 0.5rem;
}
#search-results section { margin: 0.5rem 0; }
#search-results h3 {
  font-size: 0.75rem;
  margin: 0.5rem 0 0.25rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}
#search-results ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
#search-results li { margin: 0.15rem 0; }
#search-results [data-frame="suggest"] {
  font-style: italic;
  color: var(--muted);
}
#search-results [data-frame="empty"] {
  color: var(--muted);
}
#search-results .fallthrough-link {
  margin: 0.5rem 0 0;
  padding-top: 0.4rem;
  border-top: 1px dashed var(--border);
  font-size: 0.85rem;
  text-align: right;
}
#search-results .fallthrough-link a { color: var(--muted); }
#search-results .fallthrough-link a:hover { color: var(--link); }

#app {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 1rem;
}

h1 { font-size: 1.5rem; margin: 0.5rem 0 1rem; }
p { margin: 0.5rem 0; }

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

ul { padding-left: 1.25rem; }
li { margin: 0.25rem 0; }

/* Background-enrichment status pill. Fixed bottom-right when enqueued work
   is running; click takes the user back to #/import/ug for full detail. */
#enrich-pill {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  background: var(--link);
  color: #fff;
  text-decoration: none;
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  font-size: 0.85em;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  z-index: 100;
  white-space: nowrap;
}
#enrich-pill:hover { filter: brightness(1.1); text-decoration: none; }
#enrich-pill[hidden] { display: none; }

/* UG-import marker: little red "U" right after the entry text (superscript
   feel), nodding to Ultimate Guitar's logo. Inline placement keeps it
   adjacent to the artist/song name regardless of row width. */
li.ug-import::after {
  content: "U";
  color: #d33;
  font-weight: 700;
  font-size: 0.7em;
  font-family: Georgia, "Times New Roman", serif;
  vertical-align: super;
  margin-left: 0.2em;
  pointer-events: none;
}

/* Chord-token highlight inside tab bodies. Set by decorateChordTokens in
   views/tab.js after every wrapTabBody pass. Inline span — no layout
   effect, so chord-over-lyric column alignment in the monospace <pre>
   stays exact. Subtle warm tint + semibold to make chord letters pop
   without screaming. */
.tab-body .chord {
  color: #b3541e;
  font-weight: 600;
}

.letter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(2.75rem, 1fr));
  gap: 0.5rem;
}
.letter-grid a, .letter-grid span {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-weight: 600;
  background: var(--card-bg);
}
.letter-grid .disabled {
  color: var(--muted);
  background: #f0f0f0;
}

.chords {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  color: var(--muted);
}

/* Foldout that shows chord fingering diagrams below the chord-name list.
 * Collapsed by default — the chord-name line keeps the prior compact look. */
.chords-foldout {
  margin: 0.5rem 0;
}
.chords-foldout summary {
  cursor: pointer;
  list-style: none;
  display: inline-flex;
  align-items: baseline;
  gap: 0.35rem;
  padding: 0.1rem 0;
}
.chords-foldout summary::-webkit-details-marker { display: none; }
.chords-foldout summary::before {
  content: '▶';
  color: var(--muted);
  font-size: 0.65em;
  transition: transform 0.12s ease;
  display: inline-block;
}
.chords-foldout[open] summary::before { transform: rotate(90deg); }

.chord-mode-row {
  margin: 0.4rem 0 0.1rem;
}
.chord-mode-toggle {
  font-size: 0.78rem;
  padding: 0.18rem 0.5rem;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--muted);
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
}
.chord-mode-toggle:hover {
  color: var(--fg);
  border-color: var(--fg);
}

.chord-diagrams {
  display: flex;
  flex-wrap: wrap;
  gap: 0.8rem 1rem;
  margin: 0.4rem 0 0.25rem;
  padding: 0.5rem 0;
}
.chord-diagram {
  width: 64px;
  height: 80px;
  flex: 0 0 auto;
}
.chord-diagram__label {
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-weight: 600;
  font-size: 11px;
  fill: var(--fg);
}
.chord-diagram__fret-label {
  font-family: ui-monospace, monospace;
  font-size: 7.5px;
  fill: var(--muted);
}
.chord-diagram__string,
.chord-diagram__fret {
  stroke: var(--border);
  stroke-width: 0.6;
}
.chord-diagram__nut {
  stroke: var(--fg);
  stroke-width: 2;
}
.chord-diagram__open {
  fill: none;
  stroke: var(--fg);
  stroke-width: 0.8;
}
.chord-diagram__mute {
  font-family: ui-sans-serif, sans-serif;
  font-size: 10px;
  fill: var(--fg);
}
.chord-diagram__dot,
.chord-diagram__barre {
  fill: var(--fg);
}
.chord-diagram-fallback {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  color: var(--muted);
  font-size: 0.85rem;
  align-self: center;
  padding: 0.3rem 0.45rem;
  border: 1px dashed var(--border);
  border-radius: 4px;
}

.tab-body {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: calc(1rem * var(--tab-text-scale, 1));
  white-space: pre;
  overflow-x: auto;
  background: var(--card-bg);
  border: 1px solid var(--border);
  padding: 1rem;
  border-radius: 4px;
}

/* `.tab-bleed` is a thin wrapper around `.tab-body` whose job on wide viewports
 * is to escape #app's `--content-max` lane and provide a centered, viewport-wide
 * playground inside which `.tab-body` shrinks to fit its (post-wrap) content.
 *
 * Why a wrapper at all: we want the FRAME (the bordered pre) to be exactly as
 * wide as the longest line of the wrapped text — not always the full lane. The
 * bleed lane gives chord-wrap.js the budget to consider for wrapping; once it's
 * wrapped, `.tab-body { width: max-content }` collapses the frame to the real
 * widest line. Flex-centering on `.tab-bleed` puts that frame in the middle.
 *
 * Bleed math: parent (#app) is centered at `--content-max`, so its left edge
 * sits at `(100vw - --content-max) / 2`. To pull the bleed's left edge to
 * `(100vw - --bleed-max) / 2`, the offset from parent's left is
 * `(--content-max - --bleed-max) / 2` — that's the negative margin-left below. */
.tab-bleed {
  display: flex;
  justify-content: center;
}

@media (min-width: 800px) {
  .tab-bleed {
    --bleed-max: min(calc(100vw - 2rem), 1400px);
    width: var(--bleed-max);
    margin-left: calc((var(--content-max) - var(--bleed-max)) / 2);
  }
  .tab-body {
    /* content-box: `width: max-content` sizes the CONTENT area to the longest
     * line so padding adds on top instead of squeezing content out. With the
     * project-default border-box, max-content would cap the OUTER width and
     * the inner text would overflow by 2 * padding. */
    box-sizing: content-box;
    width: max-content;
    /* In content-box, max-width applies to the content area too. Limit
     * content to the bleed lane minus our padding (1rem each side) and
     * border (1px each side) so the outer frame never exceeds `--bleed-max`. */
    max-width: calc(100% - 2rem - 2px);
  }
}

/* Discrete text-size overlay, right-edge centered. Always visible on a tab
 * page; auto-removed by view swap since it lives inside #app. Sits clear of
 * the playback HUD (bottom-center) and the search bar (top). */
#text-size-ctl {
  position: fixed;
  right: 0.4rem;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  z-index: 15;
}
#text-size-ctl button {
  width: 2.2rem;
  height: 2.2rem;
  padding: 0;
  background: rgba(26, 26, 26, 0.55);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  cursor: pointer;
}
#text-size-ctl button:hover { background: rgba(26, 26, 26, 0.85); }
#text-size-ctl button:disabled { opacity: 0.3; cursor: not-allowed; }

.home-links {
  margin-top: 1.5rem;
}

/* Wordcloud bakgrunn — pynt, ikke informasjon. Fyller hele visningen bak innholdet.
 * Tre presets velges via media queries så aspekt-forholdet passer skjermen. */
.home-wordcloud {
  position: fixed;
  top: 4rem;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('images/home-wordcloud.svg');
  background-repeat: no-repeat;
  background-position: center center;
  /* Rotert SVG må holdes liten nok til at hjørnene ikke krysser
   * viewport-grensen. cos(25°)+sin(25°) ≈ 1.33, så scale ≤ ~1.25
   * holder seg innenfor når background-size er 100%. */
  background-size: contain;
  transform: rotate(-25deg) scale(1.25);
  transform-origin: center center;
  opacity: 0.45;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;                  /* skjul roterte hjørner */
}
/* Skjul horisontal overflow på body slik at rotert SVG ikke gir scrollbar. */
html, body { overflow-x: hidden; }
/* Home content sits on top of the wordcloud. */
.home-content { position: relative; z-index: 1; }
#app { position: relative; }

.tab-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  justify-content: space-between;
}
.tab-header h1 { margin: 0; flex: 1; }

button {
  font: inherit;
  cursor: pointer;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  background: var(--card-bg);
  border-radius: 4px;
  color: var(--fg);
}
button:hover { background: #f0f0f0; }
button.danger { color: #b00020; border-color: #b00020; }
button.danger:hover { background: #fee; }
button.heart {
  font-size: 1.5rem;
  line-height: 1;
  padding: 0.25rem 0.5rem;
  color: #d33;
  border: none;
  background: transparent;
}
button.heart:hover { background: rgba(221, 51, 51, 0.1); }

.picker-row {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
  flex-wrap: wrap;
  margin: 0.5rem 0 1rem;
}
.picker-row .songbook-picker { flex: 1 1 auto; margin: 0; min-width: 14rem; }
.songbook-back-btn {
  flex: 0 0 auto;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--card-bg);
  color: var(--fg);
  text-decoration: none;
  line-height: 1.2;
  white-space: nowrap;
}
.songbook-back-btn:hover { background: #f0f0f0; text-decoration: none; }

.songbook-picker {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.4rem 0.75rem;
  background: var(--card-bg);
}
.songbook-picker summary {
  cursor: pointer;
  user-select: none;
}
.songbook-picker-body {
  margin-top: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.songbook-picker-body label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}
.songbook-picker .new-songbook-btn {
  align-self: flex-start;
  margin-top: 0.25rem;
}

.songbook-list { list-style: none; padding: 0; }
.songbook-list li {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--card-bg);
  margin-bottom: 0.5rem;
}
.songbook-list li a { flex: 1; }

.songbook-actions {
  display: flex;
  gap: 0.5rem;
  margin: 0.5rem 0 1rem;
  flex-wrap: wrap;
}

.songbook-tabs { padding-left: 0; list-style: decimal inside; }
.songbook-tabs li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.25rem 0;
}
.songbook-tabs li a { flex: 1; }
.songbook-tabs li.missing { opacity: 0.6; }
.songbook-tabs .reorder {
  display: inline-flex;
  gap: 0.15rem;
}
.songbook-tabs .reorder button {
  padding: 0.15rem 0.4rem;
  font-size: 0.85rem;
  line-height: 1;
  min-width: 1.75rem;
}
.songbook-tabs .reorder button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.muted { color: var(--muted); }

/* Import-UG view (#/import/ug) */
.card { border: 1px solid var(--muted); border-radius: 8px; padding: .8rem 1rem; margin: .8rem 0; opacity: .92; }
.ug-drop-zone { border: 2px dashed var(--muted); border-radius: 8px; padding: 1.2rem; text-align: center; transition: border-color .15s; }
.ug-drop-zone.over { border-color: var(--accent, #4a9); }
.import-link { display: inline-block; padding: .35rem .7rem; border: 1px solid var(--muted); border-radius: 6px; text-decoration: none; }

#playback-hud {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.75rem;
  align-items: center;
  padding: 0.5rem 1rem;
  background: rgba(26, 26, 26, 0.92);
  color: #fff;
  border-radius: 999px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
  z-index: 20;
  font-variant-numeric: tabular-nums;
}
#playback-hud .hud-time {
  min-width: 7rem;
  text-align: center;
  font-weight: 600;
}
#playback-hud .hud-controls {
  display: flex;
  gap: 0.25rem;
}
#playback-hud button {
  background: transparent;
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
}
#playback-hud button:hover { background: rgba(255, 255, 255, 0.15); }

/* Mobile/narrow-screen polish.
 * Goal: a phone in a music stand at 390-430px width should feel as good
 * as desktop. Trade off: tab bodies shrink to fit chord lines without
 * horizontal scroll on most songs; HUD compacts to leave reading room. */
@media (max-width: 600px) {
  #app { padding: 0.75rem 0.6rem 4rem; }

  /* Search header */
  #search-bar { padding: 0.4rem 0.6rem; }
  .search-bar-row { gap: 0.35rem; }
  #home-link, #search-btn, #language-btn { padding: 0.45rem 0.55rem; }
  #language-btn { font-size: 0.8rem; }
  #search-input { padding: 0.45rem 0.6rem; }

  /* Tab page */
  .tab-header { flex-wrap: wrap; }
  .tab-header h1 { font-size: 1.1rem; line-height: 1.25; flex: 1 1 100%; }
  .tab-body {
    font-size: calc(0.78rem * var(--tab-text-scale, 1));
    padding: 0.6rem;
    line-height: 1.45;
  }
  .chords { font-size: 0.85rem; }

  /* Text-size overlay: smaller & tucked against the edge on phones. */
  #text-size-ctl button {
    width: 1.9rem;
    height: 1.9rem;
    font-size: 0.8rem;
  }

  /* Picker + songbook-back stack vertically on phone */
  .picker-row { flex-direction: column; gap: 0.35rem; }
  .picker-row .songbook-picker,
  .songbook-back-btn { width: 100%; min-width: 0; }
  .songbook-back-btn { text-align: center; }

  /* HUD: shorter padding, smaller text, never wider than the screen */
  #playback-hud {
    bottom: 0.5rem;
    padding: 0.4rem 0.65rem;
    gap: 0.35rem;
    max-width: calc(100vw - 1rem);
  }
  #playback-hud .hud-time {
    min-width: 5.5rem;
    font-size: 0.85rem;
  }
  #playback-hud button { padding: 0.25rem 0.55rem; font-size: 0.9rem; }

  /* Letter grid tighter so all 39 cells fit in a clean grid */
  .letter-grid {
    grid-template-columns: repeat(auto-fill, minmax(2.2rem, 1fr));
    gap: 0.35rem;
  }
  .letter-grid a, .letter-grid span {
    padding: 0.5rem;
    font-size: 0.95rem;
  }

  /* Songbook reorder buttons — bump to touch-friendly size */
  .songbook-tabs .reorder button { min-width: 2.2rem; min-height: 2.2rem; font-size: 1rem; }
  .songbook-tabs li { flex-wrap: wrap; gap: 0.3rem; }
  .songbook-tabs li a { min-width: 55%; }

  /* Heart easier to tap */
  button.heart { font-size: 1.7rem; padding: 0.3rem 0.55rem; }

  /* Songbook actions: stack if cramped */
  .songbook-actions { gap: 0.35rem; }
}
