:root {
  --bg: #f4f5f7; --card: #fff; --text: #111827; --muted: #6b7280; --line: #e5e7eb;
  --primary: #2563eb; --primary-dark: #1d4ed8; --danger: #dc2626; --ok: #16a34a; --warn: #d97706; --live: #7c3aed;
  --radius: 10px;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.hidden { display: none !important; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; }

/* buttons & inputs */
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--text); border-radius: 8px;
  padding: 8px 14px; font: inherit; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #f9fafb; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }
input[type=text], input[type=password], input[type=number], input[type=time], select {
  font: inherit; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 8px; background: #fff; color: var(--text); max-width: 100%;
}
label.field { display: block; margin: 12px 0; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
label.field input[type=text], label.field select { width: 100%; }

/* auth */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card { background: var(--card); padding: 28px; border-radius: 14px; width: 100%; max-width: 360px; box-shadow: 0 10px 30px rgba(0,0,0,.08); }
.auth-card h1 { font-size: 22px; margin-bottom: 6px; }
.auth-card p { color: var(--muted); margin: 0 0 16px; }
.auth-card input { width: 100%; margin-bottom: 10px; }
.auth-card .btn { width: 100%; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 16px;
  background: #111827; color: #fff; padding: 0 16px; height: 54px;
}
.brand { font-weight: 700; }
.tabs { display: flex; gap: 2px; flex: 1; overflow-x: auto; }
.tabs button {
  background: none; border: 0; color: #9ca3af; font: inherit; padding: 16px 12px; cursor: pointer; border-bottom: 3px solid transparent;
}
.tabs button.active { color: #fff; border-bottom-color: #60a5fa; }
.topbar .btn.ghost { color: #d1d5db; }
main { max-width: 1200px; margin: 0 auto; padding: 20px 16px 60px; }

/* cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 18px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.bar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 18px; }

/* screens */
.store-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.tv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.tv {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; background: #fafafa; display: flex; flex-direction: column; gap: 8px;
}
.tv-name { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #9ca3af; flex: none; }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px #dcfce7; }
.dot.off { background: var(--danger); }
.tv select { width: 100%; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: #e5e7eb; color: #374151; }
.badge.live { background: #ede9fe; color: var(--live); }
.badge.offmode { background: #1f2937; color: #e5e7eb; }
.seg { display: flex; border: 1px solid #d1d5db; border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: #fff; font: inherit; font-size: 13px; padding: 6px 4px; cursor: pointer; }
.seg button + button { border-left: 1px solid #d1d5db; }
.seg button.active { background: var(--primary); color: #fff; }
.seg button.active.live { background: var(--live); }
.now { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* playlists */
.pl-layout { display: grid; grid-template-columns: 260px 1fr; gap: 18px; align-items: start; }
.pl-list { padding: 8px; }
.pl-list button {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px; border-radius: 8px; font: inherit; cursor: pointer;
}
.pl-list button.active { background: #eff6ff; color: var(--primary-dark); font-weight: 600; }
.pl-list button:hover:not(.active) { background: #f3f4f6; }
.items { list-style: none; margin: 12px 0; padding: 0; }
.item { display: flex; align-items: center; gap: 10px; padding: 8px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 6px; background: #fff; }
.item.dragging { opacity: 0.4; }
.item .handle { cursor: grab; color: #9ca3af; padding: 0 2px; user-select: none; }
.thumb { width: 72px; height: 41px; object-fit: cover; border-radius: 4px; background: #e5e7eb; flex: none; }
.item .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item input[type=number] { width: 70px; }
.item .ctrls { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

/* media */
.drop {
  display: block; border: 2px dashed #cbd5e1; border-radius: var(--radius); padding: 26px; text-align: center; background: #fff; cursor: pointer;
}
.drop.over { border-color: var(--primary); background: #eff6ff; }
.progress { height: 8px; background: #e5e7eb; border-radius: 4px; overflow: hidden; margin-top: 12px; }
.progress div { height: 100%; width: 0; background: var(--primary); transition: width .2s; }
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-top: 16px; }
.media-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.media-card .preview { width: 100%; aspect-ratio: 16/9; object-fit: cover; background: #111; display: block; }
.media-card .info { padding: 8px 10px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.media-card .title { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media-card .actions { display: flex; gap: 6px; padding: 0 10px 10px; }
.picker .media-card { cursor: pointer; position: relative; }
.picker .media-card.selected { outline: 3px solid var(--primary); outline-offset: -3px; background: #eff6ff; }
.pick-badge {
  position: absolute; top: 8px; right: 8px; width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid #fff; background: rgba(0,0,0,.35); color: #fff; font-weight: 700; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
}
.selected .pick-badge { background: var(--primary); }
.picker .media-card:hover { outline: 2px solid #bfdbfe; }

.folders { margin-bottom: 12px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.chip .count { color: var(--muted); margin-left: 2px; }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip.active .count { color: #dbeafe; }
.media-card .actions { flex-wrap: wrap; }
.media-card .actions select { flex: 1 1 100%; padding: 4px 6px; }

.log { background: #111827; color: #e5e7eb; padding: 10px; border-radius: 8px; font-size: 12px; max-height: 260px; overflow: auto; white-space: pre-wrap; margin: 12px 0 0; }

.media-card { position: relative; }
.media-card.selected { outline: 3px solid var(--primary); outline-offset: -3px; background: #eff6ff; }
#tab-media img.preview { cursor: pointer; }
.sel-check {
  position: absolute; top: 8px; left: 8px; z-index: 1; width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid #fff; background: rgba(0,0,0,.35); color: transparent; font-weight: 700; cursor: pointer; padding: 0;
}
.sel-check[aria-pressed="true"] { background: var(--primary); color: #fff; }

/* schedule */
.sched-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 8px 0; border-bottom: 1px solid var(--line); }
.days { display: flex; gap: 3px; }
.days label { position: relative; }
.days input { position: absolute; opacity: 0; }
.days span {
  display: inline-flex; width: 30px; height: 30px; align-items: center; justify-content: center; border-radius: 50%;
  border: 1px solid #d1d5db; font-size: 12px; cursor: pointer; background: #fff;
}
.days input:checked + span { background: var(--live); border-color: var(--live); color: #fff; }

/* dialog */
dialog { border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 40px); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(17,24,39,.5); }
#dialogBody { padding: 20px; margin: 0; }
.dialog-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 20px; flex-wrap: wrap; }
.dialog-wide { width: min(900px, calc(100vw - 24px)); }
.linkbox { display: flex; gap: 6px; }
.linkbox input { flex: 1; font-family: ui-monospace, monospace; font-size: 12px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #111827; color: #fff;
  padding: 10px 18px; border-radius: 8px; z-index: 100; box-shadow: 0 8px 20px rgba(0,0,0,.2); max-width: calc(100vw - 32px);
}
.toast.err { background: var(--danger); }

@media (max-width: 760px) {
  .pl-layout { grid-template-columns: 1fr; }
  .brand { display: none; }
  .topbar { gap: 4px; padding: 0 8px; }
  .tabs button { padding: 16px 8px; }
  .topbar .btn.small { padding: 5px 4px; }
  .item { flex-wrap: wrap; }
}
