:root {
  color-scheme: light dark;
  --border: #ccc;
  --accent: #2b6cb0;
  --bg-muted: #f4f4f5;
  --error: #c0392b;
}

* { box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  margin: 0;
  padding: 0 1.5rem 2rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.5rem;
}

.topbar .brand {
  font-weight: 700;
  font-size: 1.2rem;
  text-decoration: none;
  color: inherit;
}

.topbar nav a {
  margin-left: 1rem;
}

.flashes {
  list-style: none;
  padding: 0.75rem 1rem;
  margin: 0 0 1rem;
  background: #fdecea;
  color: var(--error);
  border-radius: 4px;
}

.button, button {
  display: inline-block;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-muted);
  color: inherit;
  text-decoration: none;
  cursor: pointer;
  font-size: 1rem;
}

button:hover, .button:hover {
  border-color: var(--accent);
}

.upload-list, .result-list {
  list-style: none;
  padding: 0;
}

.upload-list li, .result-list li {
  padding: 0.4rem 0;
  border-bottom: 1px solid var(--border);
}

.drop-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 2rem 1rem;
  border: 2px dashed var(--border);
  border-radius: 8px;
  background: var(--bg-muted);
  text-align: center;
  max-width: 480px;
  transition: border-color 0.15s, background 0.15s;
}

.drop-zone.drag-over {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg-muted));
}

.drop-zone-hint {
  margin: 0;
  color: #666;
}

.selected-files {
  list-style: none;
  padding: 0;
  margin: 0;
  width: 100%;
  max-width: 360px;
  text-align: left;
}

.selected-files li {
  padding: 0.2rem 0;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}

.result-table {
  width: 100%;
  border-collapse: collapse;
}

.result-table th, .result-table td {
  text-align: left;
  padding: 0.5rem 0.75rem 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.result-table th {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #666;
}

.muted {
  color: #666;
  font-size: 0.85rem;
}

.result-table .row-actions {
  padding: 0.25rem 0.6rem;
  line-height: 1;
}

.result-count {
  font-size: 1rem;
  font-weight: 400;
  color: #666;
}

/* --- Piece edit --- */

.piece-edit-form {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  max-width: 480px;
  margin-bottom: 1.5rem;
}

.piece-edit-form label {
  font-weight: 600;
  margin-top: 0.5rem;
}

#title-input {
  padding: 0.4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
}

.piece-edit-form button[type="submit"] {
  margin-top: 0.75rem;
  align-self: flex-start;
}

.voice-edit-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.voice-edit-row form {
  display: flex;
  gap: 0.4rem;
}

.voice-edit-row input[type="text"] {
  padding: 0.3rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  width: 140px;
}

.danger {
  color: white;
  background: var(--error);
  border-color: var(--error);
}

.page-checkbox-list {
  list-style: none;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  margin: 0 0 0.75rem;
  max-width: 480px;
}

.assign-voice-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

.assign-voice-form input[type="text"] {
  padding: 0.4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.search-form input {
  padding: 0.4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
}

.search-form .chip-field {
  flex: 1 1 200px;
  min-width: 160px;
}

.search-form button[type="submit"] {
  flex: 0 0 auto;
}

@media (max-width: 480px) {
  .search-form .chip-field {
    flex-basis: 100%;
  }
}

/* --- Workbench --- */

.workbench-layout {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 1.5rem;
  align-items: start;
}

.banner {
  padding: 0.75rem 1rem;
  background: #e6f4ea;
  border-radius: 4px;
  margin-bottom: 1rem;
}

.grid-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.thumb-zoom-controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: auto;
}

.thumb-zoom-controls button {
  padding: 0.3rem 0.7rem;
  line-height: 1;
}

#thumb-zoom-label {
  font-size: 0.85rem;
  color: #666;
  min-width: 3rem;
  text-align: center;
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--thumb-min, 120px), 1fr));
  gap: 0.6rem;
}

.page-thumb {
  border: 2px solid var(--border);
  border-radius: 4px;
  padding: 0.25rem;
  cursor: pointer;
  text-align: center;
  user-select: none;
  background: white;
}

.page-thumb canvas {
  display: block;
  width: 100%;
  height: auto;
}

.page-thumb .page-index-label {
  font-size: 0.75rem;
  color: #666;
}

.page-thumb.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent) inset;
}

.page-grid-divider {
  grid-column: 1 / -1;
  margin-top: 0.5rem;
  padding-bottom: 0.25rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 600;
  color: #666;
}

.page-grid-divider:first-child {
  margin-top: 0;
}

.tag-form-panel form {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  position: sticky;
  top: 1rem;
}

.tag-form-panel label {
  font-weight: 600;
  margin-top: 0.5rem;
}

.form-status {
  min-height: 1.2rem;
  font-size: 0.9rem;
}

.form-status.error {
  color: var(--error);
}

.form-status.success {
  color: #2e7d32;
}

.chip-field {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.3rem;
  background: white;
}

.chip-field input {
  border: none;
  outline: none;
  width: 100%;
  padding: 0.25rem;
  font-size: 1rem;
}

.chips {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  padding: 0;
  margin: 0 0 0.25rem;
}

.chip {
  background: var(--bg-muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.9rem;
}

.chip button {
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
}

.suggestions {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  z-index: 10;
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
  list-style: none;
  margin: 2px 0 0;
  padding: 0.2rem 0;
  max-height: 200px;
  overflow-y: auto;
}

.suggestions li {
  padding: 0.35rem 0.6rem;
  cursor: pointer;
}

.suggestions li.active, .suggestions li:hover {
  background: var(--bg-muted);
}

#save-button {
  margin-top: 0.75rem;
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

/* --- Viewer --- */

.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.viewer-page-count-controls {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.page-count-btn {
  padding: 0.3rem 0.65rem;
  line-height: 1;
}

.page-count-btn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: white;
}

.viewer-zoom-controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: auto;
}

.viewer-zoom-controls button {
  padding: 0.3rem 0.7rem;
  line-height: 1;
}

#zoom-label {
  font-size: 0.85rem;
  color: #666;
  min-width: 3.5rem;
  text-align: center;
}

#viewer-canvas-wrap {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 1rem;
  overflow: auto;
  max-height: 82vh;
  border: 1px solid var(--border);
  touch-action: pan-y;
}

#viewer-canvas-wrap.halves-mode {
  flex-direction: column;
  align-items: center;
  gap: 0;
}

/* Only shown when the top and bottom slots come from different pages — no seam needed
   when both halves belong to the same page. */
#page-canvas-2.half-divider {
  border-top: 1px solid var(--border);
}

#page-canvas,
#page-canvas-2,
#page-canvas-3 {
  display: block;
}

#viewer:fullscreen {
  display: flex;
  flex-direction: column;
  background: #000;
  color: #fff;
  padding: 0.75rem;
}

#viewer:fullscreen .viewer-toolbar {
  flex: none;
  margin-bottom: 0.5rem;
}

#viewer:fullscreen #viewer-canvas-wrap {
  flex: 1;
  max-height: none;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  overflow: hidden;
}

#viewer:fullscreen #viewer-canvas-wrap.halves-mode {
  flex-direction: column;
  gap: 0;
}

/* Touchscreen device (no hover, coarse pointer — i.e. phone/tablet, not mouse/trackpad)
   in fullscreen and rotated to landscape: put the toolbar in a sidebar on the left instead
   of a top bar, since a wide-short screen has little spare height for it up top. */
@media (hover: none) and (pointer: coarse) and (orientation: landscape) {
  #viewer:fullscreen {
    flex-direction: row;
  }

  #viewer:fullscreen .viewer-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    margin-bottom: 0;
    margin-right: 0.75rem;
    max-width: 140px;
    overflow-y: auto;
  }

  #viewer:fullscreen .viewer-zoom-controls,
  #viewer:fullscreen .viewer-page-count-controls {
    margin-left: 0;
    flex-wrap: wrap;
  }
}

/* --- Setlists --- */

.setlist-actions {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

#delete-setlist {
  color: var(--error);
}

.setlist-add {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  margin-bottom: 0.25rem;
}

.setlist-add #piece-field {
  width: 320px;
}

.setlist-add #voice-field {
  width: 280px;
}

.setlist-add #voice-field .suggestions {
  position: static;
  display: block;
}

.setlist-items {
  list-style: none;
  padding: 0;
  counter-reset: setlist-counter;
}

.setlist-items li {
  counter-increment: setlist-counter;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.setlist-items li::before {
  content: counter(setlist-counter) ".";
  font-weight: 600;
  color: #666;
  margin-right: 0.5rem;
}

.setlist-item-title {
  flex: 1;
}

.setlist-item-controls {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.setlist-item-controls button {
  padding: 0.25rem 0.6rem;
  line-height: 1;
}

@media (prefers-color-scheme: dark) {
  :root {
    --border: #444;
    --bg-muted: #2a2a2e;
  }
  body {
    background: #1c1c1f;
    color: #eee;
  }
  .page-thumb, .chip-field, .suggestions {
    background: #26262b;
    color: #eee;
  }
  .flashes {
    background: #3a1f1f;
  }
  .banner {
    background: #1f3a26;
  }
  #zoom-label, #thumb-zoom-label {
    color: #aaa;
  }
  .result-table th, .muted {
    color: #aaa;
  }
  .result-count {
    color: #aaa;
  }
  .page-grid-divider {
    color: #aaa;
  }
  .drop-zone-hint {
    color: #aaa;
  }
}
