:root {
  color-scheme: light;
  --bg: #f5f1e8;
  --ink: #17201d;
  --muted: #64716d;
  --panel: #fffdf7;
  --line: #d9d1c1;
  --brand: #7cc7ee;
  --brand-dark: #2478a8;
  --accent: #b84a3b;
  --shadow: 0 14px 40px rgba(34, 31, 25, 0.12);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111614;
  --ink: #eef4ef;
  --muted: #a4b3ad;
  --panel: #1a211f;
  --line: #33403b;
  --brand: #8fd8ff;
  --brand-dark: #bfeaff;
  --accent: #cf695c;
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(31, 111, 99, 0.08), transparent 280px),
    var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", Roboto, Arial, sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 24px clamp(16px, 4vw, 48px) 12px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 8vw, 4.5rem);
  line-height: 0.96;
  letter-spacing: 0;
}

.brand-word {
  color: var(--brand-dark);
}

.app-header p {
  margin-top: 8px;
  color: var(--muted);
  max-width: 36rem;
}

main {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
  grid-template-areas:
    "library add"
    "library lookup";
  gap: 18px;
  padding: 14px clamp(16px, 4vw, 48px) 40px;
}

.app-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  padding: 0 clamp(16px, 4vw, 48px) 20px;
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
}

section {
  min-width: 0;
}

.scanner-panel,
.lookup-panel,
.library-panel,
.settings-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 16px;
}

.scanner-panel {
  grid-area: add;
  align-self: start;
}

.library-panel {
  grid-area: library;
  grid-row: span 2;
}

.lookup-panel {
  grid-area: lookup;
}

.sync-note {
  color: var(--muted);
  font-size: 0.9rem;
}

.header-actions,
.status-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.settings-panel {
  position: fixed;
  z-index: 20;
  top: 88px;
  right: clamp(16px, 4vw, 48px);
  width: min(92vw, 380px);
}

.settings-group + .settings-group {
  border-top: 1px solid var(--line);
  margin-top: 14px;
  padding-top: 14px;
}

.compact-heading {
  margin-bottom: 8px;
}

.panel-heading,
.record-topline,
.control-row,
.input-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.record-topline {
  justify-content: space-between;
}

.panel-heading {
  justify-content: space-between;
  margin-bottom: 12px;
}

.scanner-panel .panel-heading {
  align-items: center;
}

h2 {
  font-size: 1.1rem;
}

.status-pill,
.catalog-code {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  color: var(--brand-dark);
  background: color-mix(in srgb, var(--brand) 12%, var(--panel));
  font-size: 0.82rem;
  white-space: nowrap;
}

.camera-shell {
  position: relative;
  display: none;
  place-items: center;
  overflow: hidden;
  min-height: 260px;
  border-radius: 8px;
  background: #1b211f;
}

.camera-shell.active {
  display: grid;
}

#camera {
  width: 100%;
  height: 100%;
  min-height: 260px;
  object-fit: cover;
}

.scan-frame {
  position: absolute;
  width: min(78%, 360px);
  aspect-ratio: 1.7;
  border: 3px solid rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.28);
}

.camera-fallback {
  position: absolute;
  max-width: 28rem;
  padding: 18px;
  text-align: center;
  color: #f8f4ec;
}

.camera-shell.active .camera-fallback {
  display: none;
}

.add-toggle {
  display: none;
  width: 40px;
  min-height: 40px;
  padding: 0;
}

.add-toggle-icon {
  font-size: 1.45rem;
  line-height: 1;
}

.status-row {
  margin-bottom: 12px;
}

.control-row,
.barcode-form,
.album-search-form {
  margin-top: 12px;
}

.barcode-form label,
.album-search-form label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.9rem;
}

.album-search-form {
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.album-search-form > input,
.album-search-form select {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 10px;
  background: white;
  color: var(--ink);
}

.album-search-form select {
  margin-bottom: 0;
}

.input-row input {
  min-width: 0;
  flex: 1 1 0;
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: white;
  color: var(--ink);
}

#barcodeInput,
#mediumSelect {
  flex: 0 1 15rem;
}

.form-action-button {
  flex: 0 0 7.5rem;
  white-space: nowrap;
}

[data-theme="dark"] input,
[data-theme="dark"] select,
[data-theme="dark"] textarea,
[data-theme="dark"] .record-card,
[data-theme="dark"] .candidate-card {
  background: #111614;
}

.manual-form {
  display: grid;
  gap: 10px;
}

.manual-form label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.9rem;
}

.manual-form input,
.manual-form textarea {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: white;
  color: var(--ink);
}

[data-theme="dark"] .album-search-form > input,
[data-theme="dark"] .album-search-form select,
[data-theme="dark"] .input-row input,
[data-theme="dark"] .manual-form input,
[data-theme="dark"] .manual-form textarea {
  background: #111614;
  color: var(--ink);
}

.manual-form textarea {
  min-height: 118px;
  resize: vertical;
}

.primary-button,
.secondary-button,
.text-button,
.icon-button,
.delete-button,
.edit-button,
.link-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  text-decoration: none;
}

.primary-button {
  background: var(--brand);
  color: #082130;
  padding: 0 14px;
}

.primary-button:hover {
  background: var(--brand-dark);
  color: white;
}

.secondary-button {
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 88%, var(--ink) 12%);
  color: var(--ink);
  padding: 0 14px;
}

.text-button,
.delete-button,
.edit-button {
  background: transparent;
  color: var(--muted);
}

.record-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}

.delete-button,
.edit-button {
  display: inline-grid;
  place-items: center;
  width: 36px;
  min-height: 36px;
  color: white;
  font-size: 1rem;
  line-height: 1;
}

.edit-button {
  background: var(--brand-dark);
}

.edit-button:hover {
  background: var(--brand);
  color: #082130;
}

.delete-button {
  background: #b3261e;
}

.delete-button:hover {
  background: #8c1d18;
}

.theme-toggle {
  min-height: 28px;
  font-size: 0.78rem;
}

.icon-button {
  width: 44px;
  background: var(--accent);
  color: white;
  font-size: 1.45rem;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button[aria-busy="true"] {
  opacity: 0.78;
}

.lookup-card {
  display: grid;
  grid-template-columns: 116px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.candidate-list {
  display: grid;
  gap: 10px;
}

.result-filter {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 12px;
  background: white;
  color: var(--ink);
}

.pagination-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.88rem;
}

.result-count-pill {
  margin-bottom: 12px;
}

.candidate-card {
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: white;
}

.result-cover-wrap {
  width: 74px;
}

.result-cover {
  display: grid;
  place-items: center;
  width: 74px;
  aspect-ratio: 1;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 82%, var(--ink) 18%);
  color: var(--muted);
  font-size: 0.75rem;
  object-fit: cover;
  text-align: center;
}

.result-cover.missing-cover {
  padding: 8px;
}

.candidate-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.inline-message {
  flex-basis: 100%;
  color: var(--muted);
  font-size: 0.82rem;
  text-align: right;
}

.candidate-card h3 {
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.candidate-card p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.9rem;
}

.source-note {
  color: var(--muted);
  font-size: 0.78rem;
}

.cover {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 82%, var(--ink) 18%);
  object-fit: cover;
}

.lookup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.record-grid {
  display: grid;
  gap: 12px;
  margin-top: 14px;
}

.record-card {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: white;
}

.record-media {
  min-width: 0;
}

.record-body {
  min-width: 0;
}

.record-body h3 {
  margin-top: 8px;
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.artist {
  color: var(--muted);
  margin-top: 4px;
}

.details {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 4px 8px;
  margin: 10px 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.details dt {
  color: var(--ink);
}

summary {
  cursor: pointer;
  color: var(--brand-dark);
  font-weight: 600;
}

.tracks {
  margin: 8px 0 0;
  padding-left: 22px;
  color: var(--muted);
}

.tracks li {
  margin: 4px 0;
  overflow-wrap: anywhere;
}

.empty-state {
  padding: 28px 6px;
  color: var(--muted);
  text-align: center;
}

.compact-state {
  padding: 8px 6px 14px;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (max-width: 850px) {
  main {
    grid-template-columns: 1fr;
    grid-template-areas:
      "library"
      "add"
      "lookup";
  }

  .library-panel {
    grid-row: auto;
  }

  .add-toggle {
    display: inline-flex;
  }

  .scanner-panel.collapsed .add-content {
    display: none;
  }

  .settings-panel {
    top: 76px;
    right: 16px;
    left: 16px;
    width: auto;
  }
}

@media (max-width: 520px) {
  .app-header {
    align-items: flex-start;
  }

  .input-row,
  .control-row {
    align-items: stretch;
  }

  .input-row {
    flex-direction: column;
  }

  #barcodeInput,
  #mediumSelect,
  .form-action-button {
    flex: 1 1 auto;
    width: 100%;
  }

  .lookup-card,
  .record-card {
    grid-template-columns: 78px minmax(0, 1fr);
  }

  .record-actions {
    gap: 6px;
  }

  .record-actions .edit-button,
  .record-actions .delete-button {
    width: 34px;
    min-height: 34px;
  }

  .candidate-card {
    grid-template-columns: 64px minmax(0, 1fr);
  }

  .candidate-actions {
    grid-column: 1 / -1;
    justify-content: stretch;
  }

  .result-cover-wrap,
  .result-cover {
    width: 64px;
  }

  .candidate-actions button {
    flex: 1;
  }

  .status-pill {
    white-space: normal;
  }
}
