:root {
  color-scheme: dark;
  --bg: #0d1117;
  --surface: #161b22;
  --surface-raised: #1c2128;
  --surface-hover: #21262d;
  --border: #30363d;
  --border-subtle: #21262d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #2f81f7;
  --accent-hover: #388bfd;
  --accent-subtle: rgba(47, 129, 247, 0.14);
  --danger: #f85149;
  --danger-bg: rgba(248, 81, 73, 0.12);
  --live: #3fb950;
  --radius: 8px;
  --radius-lg: 10px;
}

*, *::before, *::after {
  box-sizing: border-box;
}

[hidden],
body[data-role="standard"] .admin-only {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

/* Auth Gate */
.auth-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.auth-card {
  width: min(100%, 380px);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

.auth-header {
  margin-bottom: 22px;
}

.auth-header .brand {
  margin-bottom: 18px;
}

.auth-card h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 6px;
  letter-spacing: -0.02em;
}

.auth-card p {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
}

.auth-card form {
  display: grid;
  gap: 14px;
}

.field {
  display: grid;
  gap: 6px;
}

.field label,
.admin-card label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
}

input,
select {
  height: 36px;
  padding: 0 11px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s ease;
}

input:focus,
select:focus {
  border-color: var(--accent);
}

input::placeholder {
  color: #6e7681;
}

.password-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.password-wrap input {
  width: 100%;
  padding-right: 38px;
}

.password-toggle {
  position: absolute;
  right: 4px;
  width: 30px;
  height: 28px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  display: grid;
  place-items: center;
  padding: 0;
  transition: color 0.15s ease, background-color 0.15s ease;
}

.password-toggle:hover {
  color: var(--text);
  background: var(--surface-hover);
}

.password-toggle svg {
  width: 16px;
  height: 16px;
}

.password-toggle .icon-eye-off {
  display: none;
}

.password-toggle.showing .icon-eye {
  display: none;
}

.password-toggle.showing .icon-eye-off {
  display: block;
}

.auth-help {
  margin-top: 14px !important;
  color: var(--danger) !important;
  font-size: 12px !important;
  min-height: 18px;
}

/* Buttons */
button {
  font: inherit;
  cursor: pointer;
}

.btn-primary {
  height: 36px;
  padding: 0 14px;
  border: 1px solid rgba(240, 246, 252, 0.1);
  border-radius: 6px;
  background: var(--accent);
  color: #ffffff;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  transition: background-color 0.15s ease;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-raised);
  color: var(--text);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.btn-secondary:hover {
  background: var(--surface-hover);
  border-color: #484f58;
}

.btn-ghost {
  height: 32px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.btn-ghost:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.btn-sm {
  height: 30px;
  padding: 0 10px;
  font-size: 12px;
}

/* Brand & Header */
.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  text-decoration: none;
  color: var(--text);
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.02em;
}

.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 700;
}

.shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 54px;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-badge {
  font-size: 12px;
  color: var(--text-muted);
  padding: 4px 10px;
  background: var(--bg);
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.logout-button {
  height: 32px;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid rgba(248, 81, 73, 0.35);
  border-radius: 6px;
  background: var(--danger-bg);
  color: #ff7b72;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.logout-button:hover {
  background: rgba(248, 81, 73, 0.22);
  border-color: var(--danger);
}

.logout-button svg {
  width: 14px;
  height: 14px;
}

/* Main Workspace */
.main {
  flex: 1;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 20px 24px 32px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  gap: 18px;
  align-items: start;
}

/* Player Card */
.player-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.player-top {
  min-height: 46px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.playing-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}

.playing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #484f58;
}

.playing-dot.active {
  background: var(--live);
  box-shadow: 0 0 8px rgba(63, 185, 80, 0.5);
}

.player-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.delivery-control {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
}

.delivery-control select {
  height: 30px;
  padding: 0 8px;
  font-size: 12px;
  background: var(--bg);
}

.cast-button {
  height: 30px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-raised);
  color: var(--text);
  font-size: 12px;
  font-weight: 500;
}

.cast-button:hover:not(:disabled),
.cast-button.casting {
  border-color: var(--accent);
  color: #79c0ff;
  background: var(--accent-subtle);
}

.cast-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.cast-button svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

.video-wrap {
  background: #050709;
  aspect-ratio: 16 / 9;
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.video-wrap video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  display: none;
}

.video-wrap.has-video video {
  display: block;
}

.video-wrap.has-video .empty-state {
  display: none;
}

.player-cast-overlay {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 6;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(13, 17, 23, 0.72);
  backdrop-filter: blur(4px);
  color: #fff;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}

.video-wrap.has-video .player-cast-overlay {
  display: inline-flex;
}

.player-cast-overlay:hover:not(:disabled),
.player-cast-overlay.casting {
  border-color: var(--accent);
  color: #79c0ff;
  background: rgba(31, 111, 235, 0.32);
}

.player-cast-overlay svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 24px;
  gap: 6px;
  color: var(--text-muted);
}

.empty-icon {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: grid;
  place-items: center;
  font-size: 15px;
  margin-bottom: 6px;
  padding-left: 2px;
}

.empty-state strong {
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}

.empty-state span {
  font-size: 12px;
}

.video-caption {
  position: absolute;
  left: 14px;
  bottom: 12px;
  color: #fff;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.85);
  font-size: 12px;
  font-weight: 500;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}

.player-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.now-playing {
  min-width: 0;
}

.now-playing b {
  display: block;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.now-playing small {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Right Library Panel (Saved Lists + Channels) */
.library-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-height: calc(100vh - 96px);
  min-height: 480px;
}

.preset-panel {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-subtle);
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.panel-header h2,
.panel-header h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

.panel-header small {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.saved-presets {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 12px;
  max-height: 155px;
  overflow-y: auto;
}

.preset-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 10px;
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  background: var(--bg);
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.preset-row.active {
  border-color: var(--accent);
  background: var(--accent-subtle);
}

.preset-load {
  flex: 1;
  min-width: 0;
  background: none;
  border: 0;
  padding: 0;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preset-row.active .preset-load {
  color: #79c0ff;
  font-weight: 600;
}

.saved-count {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

.preset-refresh {
  background: transparent;
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
  padding: 3px 7px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;
  white-space: nowrap;
}

.preset-refresh:hover:not(:disabled) {
  border-color: var(--accent);
  color: #79c0ff;
  background: var(--accent-subtle);
}

.preset-refresh:disabled,
.preset-refresh.loading {
  opacity: 0.6;
  cursor: not-allowed;
}

.preset-refresh svg {
  width: 12px;
  height: 12px;
  fill: currentColor;
  flex-shrink: 0;
}

.preset-refresh.loading svg {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.preset-remove {
  background: none;
  border: 0;
  padding: 2px 6px;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 11px;
}

.preset-remove:hover {
  background: var(--danger-bg);
  color: var(--danger);
}

/* Channels Section inside Library Panel */
.playlist-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.channel-header {
  padding: 12px 16px 10px;
}

.channel-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.channel-toolbar {
  padding: 0 16px 10px;
  display: grid;
  grid-template-columns: 1fr 135px;
  gap: 8px;
  border-bottom: 1px solid var(--border-subtle);
}

.channel-toolbar input,
.channel-toolbar select {
  height: 32px;
  font-size: 12px;
  min-width: 0;
}

.channel-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px;
}

.channel-row {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 8px 10px;
  transition: background-color 0.12s ease;
}

.channel-row:hover {
  background: var(--surface-hover);
}

.channel-row.active {
  background: var(--accent-subtle);
}

.channel-number {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  width: 24px;
  flex-shrink: 0;
}

.channel-logo {
  width: 34px;
  height: 26px;
  flex: 0 0 34px;
  object-fit: contain;
  border-radius: 4px;
  background: var(--bg);
}

.channel-copy {
  flex: 1;
  min-width: 0;
}

.channel-copy b {
  display: block;
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.channel-row.active .channel-copy b {
  color: #79c0ff;
  font-weight: 600;
}

.channel-copy small {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 1px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.channel-play {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.channel-row:hover .channel-play,
.channel-row.active .channel-play {
  color: var(--accent);
}

.empty-library {
  margin: 0;
  padding: 16px 8px;
  color: var(--text-muted);
  font-size: 12px;
  text-align: center;
}

/* Admin Section */
.admin-section {
  border-top: 1px solid var(--border-subtle);
  padding-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.admin-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.admin-header p {
  margin: 3px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

.admin-tabs {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  gap: 3px;
}

.admin-tab {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
}

.admin-tab:hover {
  color: var(--text);
}

.admin-tab.active {
  background: var(--surface-hover);
  color: var(--text);
}

.admin-tab-content {
  display: none;
}

.admin-tab-content.active {
  display: block;
}

.admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card-head h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

.card-head p {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

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

.input-row {
  display: flex;
  gap: 8px;
}

.input-row input {
  flex: 1;
  min-width: 0;
}

.code-tools {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.code-tools span {
  font-size: 12px;
  color: #79c0ff;
  overflow-wrap: anywhere;
}

.app-token {
  border-top: 1px solid var(--border-subtle);
  padding-top: 12px;
  font-size: 12px;
  color: var(--text-muted);
}

.app-token summary {
  cursor: pointer;
  color: var(--text);
  font-weight: 500;
}

.app-token p {
  margin: 8px 0;
}

.card-divider {
  height: 1px;
  background: var(--border-subtle);
}

.file-import-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.file-import-row strong {
  display: block;
  font-size: 13px;
  font-weight: 500;
}

.file-import-row small {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Users Management */
.user-create-form {
  display: grid;
  grid-template-columns: 1fr 1.2fr 200px auto !important;
  gap: 8px;
  padding: 14px 0;
  border-top: 1px solid var(--border-subtle);
  border-bottom: 1px solid var(--border-subtle);
}

.user-list {
  display: flex;
  flex-direction: column;
}

.user-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) 150px minmax(150px, 1.2fr) auto auto auto;
  gap: 8px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border-subtle);
}

.user-row:last-child {
  border-bottom: 0;
}

.user-name-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.session-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #484f58;
  flex-shrink: 0;
}

.session-dot.online {
  background: var(--live);
  box-shadow: 0 0 6px rgba(63, 185, 80, 0.5);
}

.user-row b {
  font-size: 13px;
  font-weight: 500;
  overflow-wrap: anywhere;
}

.user-row button {
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-raised);
  color: var(--text);
  font-size: 12px;
  font-weight: 500;
}

.user-row button:hover:not(:disabled) {
  background: var(--surface-hover);
}

.user-row .user-delete {
  border-color: rgba(248, 81, 73, 0.3);
  background: var(--danger-bg);
  color: var(--danger);
}

.user-row .user-delete:hover:not(:disabled) {
  border-color: var(--danger);
}

.user-row button:disabled,
.user-row select:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Toast Notification */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  transform: translateY(10px);
  opacity: 0;
  pointer-events: none;
  max-width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 50;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

/* Responsive Breakpoints */
@media (max-width: 1040px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .library-panel {
    max-height: 540px;
    min-height: 360px;
  }

  .admin-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .topbar {
    padding: 0 14px;
  }

  .main {
    padding: 14px 14px 24px;
  }

  .user-create-form {
    grid-template-columns: 1fr !important;
  }

  .user-row {
    grid-template-columns: 1fr 1fr;
  }

  .user-row b,
  .user-row input {
    grid-column: 1 / -1;
  }

  .channel-toolbar {
    grid-template-columns: 1fr;
  }

  .input-row {
    flex-wrap: wrap;
  }

  .input-row input {
    min-width: 100%;
  }

  .input-row button {
    flex: 1;
  }
}
