:root {
  --accent: #e8762c;
  --accent-dark: #f4923c;
  --border: #34383f;
  --text: #eceef0;
  --text-light: #98a0aa;
  --text-dim: #6b7280;
  --bg: #12141a;
  --surface: #1b1e25;
  --surface-2: #232730;
  --red: #ff8a8a;
  --red-bg: #3a1f22;
  --red-border: #5a2c30;
  --green: #5fb68a;
  --green-bg: #1d3328;
  --green-border: #2f4d3d;
  --radius: 10px;
  --radius-sm: 6px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  min-height: 100vh;
}

/* --- Top navigation bar (full width, separate from page content) --- */

.topnav {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topnav-inner {
  max-width: 1080px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.85rem 1.5rem;
}

.brand {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}

.topnav nav {
  display: flex;
  align-items: center;
  gap: 1.4rem;
  flex: 1;
}

.topnav nav a {
  color: var(--text-light);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.92rem;
  padding: 0.3rem 0;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

.topnav nav a:hover { color: var(--text); }
.topnav nav a.active { color: var(--accent); border-bottom-color: var(--accent); }

.topnav form { margin: 0; }

/* --- Page container --- */

main.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 2rem 1.5rem 3rem;
}

main.container.narrow { max-width: 420px; margin-top: 4rem; padding: 2rem 2.25rem; background: var(--surface); border-radius: var(--radius); box-shadow: 0 4px 24px rgba(0,0,0,0.4); }

.breadcrumbs {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 0.5rem;
}
.breadcrumbs a { color: var(--text-dim); text-decoration: none; }
.breadcrumbs a:hover { color: var(--text-light); text-decoration: underline; }
.breadcrumbs .sep { margin: 0 0.4rem; }

.page-head {
  margin-bottom: 1.5rem;
}

.back-link {
  display: inline-block;
  color: var(--text-dim);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 1.25rem;
}
.back-link:hover { color: var(--text); }

.save-bar {
  position: sticky;
  top: 0.75rem;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  margin-bottom: 1.25rem;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: 360px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
}
.modal p { margin: 0 0 1.1rem; }
.modal-actions { display: flex; flex-direction: column; gap: 0.5rem; }
.modal-actions button { width: 100%; }

h1 { margin: 0; font-size: 1.6rem; font-weight: 700; }
h2 { font-size: 1.05rem; font-weight: 700; margin: 0 0 0.9rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem 1.75rem;
  margin-bottom: 1.5rem;
}

.section { margin-bottom: 2rem; }
.section:last-child { margin-bottom: 0; }

/* --- Forms --- */

label { display: block; margin-bottom: 1.1rem; font-weight: 600; font-size: 0.9rem; color: var(--text-light); }

input, select {
  display: block;
  width: 100%;
  padding: 0.65rem 0.8rem;
  margin-top: 0.45rem;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }
input[readonly] { color: var(--text-light); cursor: pointer; }

button, .button-link {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.6rem 1.3rem;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  transition: background-color 0.15s, transform 0.05s;
  white-space: nowrap;
}
button:hover, .button-link:hover { background: var(--accent-dark); }
button:active, .button-link:active { transform: translateY(1px); }

button.secondary, .button-link.secondary {
  background: transparent;
  color: var(--text-light);
  border: 1px solid var(--border);
}
button.secondary:hover, .button-link.secondary:hover { background: var(--surface-2); color: var(--text); }

button.danger { background: var(--red-bg); color: var(--red); border: 1px solid var(--red-border); }
button.danger:hover { background: #4a262a; }

button.small { padding: 0.4rem 0.75rem; font-size: 0.82rem; }

/* --- Flash / status messages --- */

.error, .success {
  font-weight: 600;
  border-radius: var(--radius-sm);
  padding: 0.65rem 1rem;
  margin-bottom: 1.25rem;
  position: relative;
  padding-right: 2.25rem;
  font-size: 0.9rem;
}
.error { color: var(--red); background: var(--red-bg); border: 1px solid var(--red-border); }
.success { color: var(--green); background: var(--green-bg); border: 1px solid var(--green-border); }
.flash.flash-hide { opacity: 0; transform: translateY(-6px); transition: opacity 0.3s, transform 0.3s; }
.flash-close {
  position: absolute;
  top: 0.45rem;
  right: 0.55rem;
  background: none;
  border: none;
  color: inherit;
  font-size: 1.1rem;
  cursor: pointer;
  line-height: 1;
  padding: 0.1rem 0.3rem;
  opacity: 0.7;
}
.flash-close:hover { opacity: 1; }

.hint {
  font-size: 0.85rem;
  color: var(--text-light);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.9rem;
  margin-bottom: 1.25rem;
  line-height: 1.5;
}
.hint code { background: var(--bg); border: 1px solid var(--border); border-radius: 4px; padding: 0.05rem 0.3rem; font-size: 0.8rem; }

/* --- Stat tiles (dashboard) --- */

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 2rem; }
.stat-tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem 1.4rem; }
.stat-tile .stat-number { font-size: 2rem; font-weight: 700; line-height: 1; margin-bottom: 0.3rem; }
.stat-tile .stat-label { color: var(--text-light); font-size: 0.85rem; margin-bottom: 0.9rem; }

/* --- Media grid --- */

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 1rem; }
.tile { position: relative; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; transition: border-color 0.15s; }
.tile-select {
  position: absolute;
  top: 0.4rem;
  left: 0.4rem;
  z-index: 2;
  background: rgba(0,0,0,0.55);
  border-radius: 4px;
  padding: 0.2rem;
  display: flex;
}
.tile-select input { width: auto; margin: 0; cursor: pointer; }
.tile:hover { border-color: var(--text-dim); }
.tile img { width: 100%; height: 100px; object-fit: cover; display: block; background: var(--surface-2); }
.tile-body { padding: 0.6rem; }
.tile-name { font-size: 0.78rem; color: var(--text-light); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 0.5rem; }
.tile-actions { display: flex; gap: 0.4rem; }
.tile-actions form { flex: 1; margin: 0; }
.tile-actions button { width: 100%; padding: 0.4rem; font-size: 0.78rem; }

/* --- List rows (screens list) --- */

.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 0;
  border-bottom: 1px solid var(--border);
}
.list-row:last-child { border-bottom: none; }
.list-row .name-cell { display: flex; align-items: baseline; gap: 0.6rem; flex: 1; min-width: 0; }
.list-row a.title { color: var(--text); font-weight: 600; text-decoration: none; }
.list-row a.title:hover { color: var(--accent); }
.list-row .meta { color: var(--text-dim); font-size: 0.8rem; background: var(--surface-2); padding: 0.15rem 0.55rem; border-radius: 999px; white-space: nowrap; }

.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.status-online { background: var(--green); box-shadow: 0 0 4px var(--green); }
.status-offline { background: var(--text-dim); }
.list-row .actions { display: flex; align-items: center; gap: 0.5rem; flex-shrink: 0; }
.playlist-select-form { margin: 0; }
.playlist-select-form select { width: auto; margin: 0; display: inline-block; font-size: 0.85rem; padding: 0.4rem 0.6rem; min-width: 140px; }

/* --- Two-column layout (playlist editor) --- */

.two-col { display: grid; grid-template-columns: 1.1fr 1fr; gap: 2rem; align-items: start; }
@media (max-width: 760px) {
  .two-col { grid-template-columns: 1fr; }
}

/* --- Playlist items --- */

#playlist { margin-bottom: 0.5rem; }

.playlist-item {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.55rem 0.7rem;
  margin-bottom: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.playlist-item .drag-handle {
  color: var(--text-dim);
  cursor: grab;
  font-size: 1.1rem;
  line-height: 1;
  padding: 0 0.2rem;
  user-select: none;
}
.playlist-item .move-buttons { display: flex; flex-direction: column; gap: 0.15rem; }
.playlist-item .move-buttons button {
  padding: 0.1rem 0.4rem;
  font-size: 0.7rem;
  line-height: 1.2;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-light);
}
.playlist-item .move-buttons button:hover { background: var(--border); color: var(--text); }
.playlist-item img { width: 48px; height: 48px; object-fit: cover; border-radius: 4px; flex-shrink: 0; background: var(--surface-2); }
.playlist-item .name { flex: 1; min-width: 0; font-size: 0.87rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.playlist-item input.duration-input { width: 64px; margin: 0; padding: 0.4rem 0.5rem; font-size: 0.85rem; flex-shrink: 0; }
.playlist-item form { margin: 0; }
.playlist-item.dragging { opacity: 0.35; }
.playlist-item.drag-over { border-color: var(--accent); }

/* --- QR / copy box --- */

.qr-box { display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap; margin: 1rem 0 0.5rem; }
.qr-box img { width: 150px; height: 150px; border-radius: 8px; background: #fff; padding: 8px; flex-shrink: 0; }
.url-copy { display: flex; gap: 0.5rem; flex: 1; min-width: 240px; }
.url-copy input { flex: 1; margin: 0; font-size: 0.85rem; }
.url-copy button { white-space: nowrap; flex-shrink: 0; }

.filter-bar { display: flex; gap: 0.5rem; margin: 0 0 1rem; }
.filter-bar select { width: auto; margin: 0; }
.filter-search { position: relative; display: flex; }
.filter-search input[type="text"] { width: 200px; margin: 0; padding-right: 1.9rem; }
.filter-clear {
  display: none;
  position: absolute;
  right: 0.3rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.2rem 0.4rem;
  cursor: pointer;
}
.filter-clear:hover { color: var(--text); background: none; }

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-light);
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
  margin-bottom: 1rem;
}
.dropzone p { margin: 0.2rem 0; }
.dropzone:hover { border-color: var(--text-dim); }
.dropzone.dropzone-active { border-color: var(--accent); background: var(--surface-2); color: var(--text); }

.selected-files {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
  font-size: 0.85rem;
  color: var(--text-light);
  max-height: 120px;
  overflow-y: auto;
}
.selected-files li { padding: 0.2rem 0; border-bottom: 1px solid var(--border); }
.selected-files li:last-child { border-bottom: none; }

.empty-state {
  text-align: center;
  color: var(--text-light);
  padding: 2.5rem 1rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* --- Responsive breakpoints --- */

@media (max-width: 640px) {
  .topnav-inner { flex-wrap: wrap; gap: 0.6rem 1rem; padding: 0.75rem 1rem; }
  .topnav nav { flex: 1 1 100%; order: 3; gap: 1rem; overflow-x: auto; -ms-overflow-style: none; scrollbar-width: none; }
  .topnav nav::-webkit-scrollbar { display: none; }
  .topnav nav a { white-space: nowrap; }
  .topnav form { order: 2; margin-left: auto; }

  main.container { padding: 1.25rem 1rem 2.5rem; }
  main.container.narrow { margin-top: 1.5rem; padding: 1.5rem; }

  h1 { font-size: 1.35rem; }
  .card { padding: 1.1rem 1.25rem; }

  .list-row { flex-wrap: wrap; }
  .list-row .name-cell { flex-wrap: wrap; gap: 0.4rem; }
  .list-row .actions { flex-wrap: wrap; width: 100%; }
  .list-row .actions .button-link, .list-row .actions button { flex: 1; text-align: center; }

  .qr-box { gap: 1rem; }
  .qr-box img { width: 120px; height: 120px; }
  .url-copy { min-width: 100%; }

  .stat-grid { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }

  .filter-bar { flex-wrap: wrap; width: 100%; }
  .filter-search { flex: 1 1 auto; }
  .filter-search input[type="text"] { flex: 1 1 auto; width: auto; min-width: 120px; }

  .playlist-item { flex-wrap: wrap; }
  .playlist-item .name { flex: 1 1 100%; order: 1; white-space: normal; }
  .playlist-item input.duration-input { order: 2; flex: 1; }
  .playlist-item form:last-child { order: 3; }
}

@media (max-width: 420px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 0.7rem; }
  .tile img { height: 80px; }
  button, .button-link { padding: 0.55rem 1rem; font-size: 0.85rem; }
  .brand { font-size: 0.95rem; }
}

@media (hover: none) {
  /* Touch devices: dragging is unreliable - lean on the up/down buttons. */
  .playlist-item .drag-handle { display: none; }
}
